Skip to content

Commit 5916be8

Browse files
authored
Simplify merge_config (#999)
Makes config_merge implementation easier to maintain and avoid C901 when new options will be added. Fixes: #998
1 parent acb59c4 commit 5916be8

1 file changed

Lines changed: 19 additions & 27 deletions

File tree

lib/ansiblelint/cli.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,42 +161,34 @@ def get_cli_parser() -> argparse.ArgumentParser:
161161

162162

163163
def merge_config(file_config, cli_config) -> NamedTuple:
164+
bools = (
165+
'display_relative_path',
166+
'parseable',
167+
'parseable_severity',
168+
'quiet',
169+
'use_default_rules',
170+
)
171+
lists = (
172+
'exclude_paths',
173+
'rulesdir',
174+
'skip_list',
175+
'tags',
176+
)
177+
164178
if not file_config:
165179
return cli_config
166180

167-
if 'quiet' in file_config:
168-
cli_config.quiet = cli_config.quiet or file_config['quiet']
169-
170-
if 'parseable' in file_config:
171-
cli_config.parseable = (cli_config.parseable or
172-
file_config['parseable'])
173-
174-
if 'parseable_severity' in file_config:
175-
cli_config.parseable_severity = (cli_config.parseable_severity or
176-
file_config['parseable_severity'])
177-
178-
if 'display_relative_path' in file_config:
179-
cli_config.display_relative_path = (cli_config.display_relative_path or
180-
file_config['display_relative_path'])
181+
for entry in bools:
182+
x = getattr(cli_config, entry) or file_config.get(entry, False)
183+
setattr(cli_config, entry, x)
181184

182-
if 'use_default_rules' in file_config:
183-
cli_config.use_default_rules = (cli_config.use_default_rules or
184-
file_config['use_default_rules'])
185+
for entry in lists:
186+
getattr(cli_config, entry).extend(file_config.get(entry, []))
185187

186188
if 'verbosity' in file_config:
187189
cli_config.verbosity = (cli_config.verbosity +
188190
file_config['verbosity'])
189191

190-
cli_config.exclude_paths.extend(file_config.get('exclude_paths', []))
191-
192-
cli_config.rulesdir.extend(file_config.get('rulesdir', []))
193-
194-
if 'skip_list' in file_config:
195-
cli_config.skip_list = cli_config.skip_list + file_config['skip_list']
196-
197-
if 'tags' in file_config:
198-
cli_config.tags = cli_config.tags + file_config['tags']
199-
200192
return cli_config
201193

202194

0 commit comments

Comments
 (0)