-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For this, we refactored the CIVConfig class and changed the behavior to allow overriding config items with cli params. Also, if the config file does not exist, it will always be created with default values and then updated with the values provided via cli params.
- Loading branch information
Showing
3 changed files
with
113 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,90 @@ | ||
import os.path | ||
|
||
import yaml | ||
|
||
|
||
class CIVConfig(): | ||
def __init__(self, config_path='/tmp/civ_config.yml'): | ||
self.config_path = config_path | ||
class CIVConfig: | ||
config_path = '/tmp/civ_config.yaml' | ||
command_line_args = {} | ||
|
||
config_file_arg_name = 'config_file' | ||
|
||
def __init__(self, args=None): | ||
if args.config_file is not None: | ||
self.config_path = args.config_file | ||
|
||
def validate(self): | ||
self.command_line_args = args.__dict__ | ||
|
||
def validate_config(self): | ||
with open(self.config_path) as config_file: | ||
try: | ||
config = yaml.safe_load(config_file) | ||
except Exception as e: | ||
print(f'ERROR: loading the config yaml failed, please check the sintax.\n{e}') | ||
exit() | ||
print(f'ERROR: Failed to load the config yaml, please check the syntax.') | ||
print(e) | ||
exit(1) | ||
|
||
assert 'resources_file' in config.keys(), 'ERROR: Please provide a resources file' | ||
assert 'output_file' in config.keys(), 'ERROR: Please provide an output path' | ||
|
||
self.set_defaults(config) | ||
|
||
def write_config(self, args): | ||
args = args.__dict__ | ||
def write_config(self, config_to_write): | ||
with open(self.config_path, 'w+') as config_file: | ||
yaml.dump(args, config_file) | ||
yaml.dump(config_to_write, config_file) | ||
|
||
def update_config(self): | ||
if os.path.exists(self.config_path): | ||
config = self.get_config() | ||
else: | ||
config = self.get_default_config() | ||
|
||
if len(self.command_line_args) == 1 and self.config_file_arg_name in self.command_line_args: | ||
return | ||
|
||
self.command_line_args.pop(self.config_file_arg_name) | ||
|
||
for arg_name, arg_value in self.command_line_args.items(): | ||
if arg_value == config[arg_name] or arg_value is None: | ||
continue | ||
|
||
print(f'DEBUG: Overriding "{arg_name}" config item...') | ||
|
||
if arg_name is 'tags': | ||
config[arg_name] = self.get_tags_dict_from_command_line_arg_value(arg_value) | ||
continue | ||
|
||
config[arg_name] = arg_value | ||
|
||
self.write_config(config) | ||
|
||
def get_tags_dict_from_command_line_arg_value(self, tags_arg_value): | ||
tags_dict = {} | ||
|
||
tags_list = tags_arg_value.split(',') | ||
|
||
for t in tags_list: | ||
tag_data = t.split(':') | ||
tags_dict[tag_data[0].strip()] = tag_data[1].strip() | ||
|
||
return tags_dict | ||
|
||
def get_config(self): | ||
with open(self.config_path) as config_file: | ||
config = yaml.safe_load(config_file) | ||
|
||
return config | ||
|
||
def set_defaults(self, config): | ||
config_defaults = {'environment': 'local', | ||
'tags': None, | ||
'debug': False, | ||
'include_markers': None, | ||
'parallel': False, | ||
'stop_cleanup': False, | ||
'test_filter': None} | ||
def get_default_config(self): | ||
config_defaults = { | ||
'resources_file': None, | ||
'output_file': None, | ||
'environment': 'local', | ||
'tags': None, | ||
'debug': False, | ||
'include_markers': None, | ||
'parallel': False, | ||
'stop_cleanup': None, | ||
'test_filter': None, | ||
'strict_mode': True | ||
} | ||
|
||
for default in config_defaults: | ||
if default not in config: | ||
config[default] = config_defaults[default] | ||
|
||
with open(self.config_path, 'w+') as config_file: | ||
yaml.dump(config, config_file) | ||
return config_defaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters