-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small tweaks to simplify the code #325
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,11 @@ | |
import os | ||
import functools | ||
import yaml | ||
from os.path import expandvars | ||
|
||
ZAZA_SETUP_FILE_LOCATIONS = [ | ||
'{home}/.zaza.yaml'] | ||
expandvars('$HOME/.zaza.yaml'), | ||
] | ||
|
||
SECRETS = 'secrets' | ||
RUNTIME_CONFIG = 'runtime_config' | ||
|
@@ -32,14 +34,14 @@ | |
MODEL_SETTINGS_SECTION = 'model_settings' | ||
MODEL_CONSTRAINTS_SECTION = 'model_constraints' | ||
|
||
VALID_ENVIRONMENT_KEY_PREFIXES = [ | ||
VALID_ENVIRONMENT_KEY_PREFIXES = ( | ||
'OS_', | ||
'TEST_', | ||
'MOJO_', | ||
'JUJU_', | ||
'CHARM_', | ||
'MODEL_', | ||
] | ||
) | ||
|
||
MODEL_DEFAULTS = { | ||
# Model defaults from charm-test-infra | ||
|
@@ -116,12 +118,7 @@ def is_valid_env_key(key): | |
:returns: Whether key is a valid environment variable name | ||
:rtype: bool | ||
""" | ||
valid = False | ||
for _k in VALID_ENVIRONMENT_KEY_PREFIXES: | ||
if key.startswith(_k): | ||
valid = True | ||
break | ||
return valid | ||
return key.startswith(VALID_ENVIRONMENT_KEY_PREFIXES) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice optimisation; might be useful to add a comment indicating that |
||
|
||
|
||
def find_setup_file(): | ||
|
@@ -130,11 +127,9 @@ def find_setup_file(): | |
:returns: Location of zaza config file or None if not found. | ||
:rtype: str or None | ||
""" | ||
ctxt = { | ||
'home': os.environ.get('HOME')} | ||
for setup_file in ZAZA_SETUP_FILE_LOCATIONS: | ||
if os.path.isfile(setup_file.format(**ctxt)): | ||
return setup_file.format(**ctxt) | ||
if os.path.isfile(setup_file): | ||
return setup_file | ||
Comment on lines
+131
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do the |
||
|
||
|
||
def get_setup_file_contents(): | ||
|
@@ -145,16 +140,13 @@ def get_setup_file_contents(): | |
:rtype: dict | ||
""" | ||
setup_file = find_setup_file() | ||
setup_file_data = {} | ||
if setup_file: | ||
with open(setup_file, 'r') as stream: | ||
try: | ||
_file_data = yaml.safe_load(stream) | ||
if _file_data: | ||
setup_file_data.update(_file_data) | ||
return yaml.safe_load(stream) or {} | ||
except yaml.YAMLError: | ||
logging.warn("Unable to load data from {}".format(setup_file)) | ||
return setup_file_data | ||
return {} | ||
|
||
|
||
def get_setup_file_section(section_name): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the same way as line 35 change, this could also be a tuple.
However, I would leave out the
expandvars()
call here, and delay it to where the string is used. This is because it's just about possible that theHOME
env will change between load time of the module and use time of the function.