-
Notifications
You must be signed in to change notification settings - Fork 6
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
EVA-3414 add Writable config script #49
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env python | ||
import os | ||
|
||
import yaml | ||
from ebi_eva_common_pyutils.config import Configuration, cfg | ||
|
||
|
||
class WritableConfig(Configuration): | ||
"""Configuration object that allows writes to the config file""" | ||
|
||
def __init__(self, *search_path, version=None): | ||
super().__init__(search_path) | ||
self.version = version | ||
|
||
def load_config_file(self, *search_path): | ||
try: | ||
super().load_config_file(*search_path) | ||
except FileNotFoundError: | ||
# expected if it's the first time we are creating the config file | ||
# In that case the first search path is set to be the config files | ||
self.config_file = search_path[0] | ||
pass | ||
|
||
def backup(self): | ||
""" | ||
Rename the config file by adding a '.1' at the end. If the '.1' file exists it move it to a '.2' and so on. | ||
""" | ||
if os.path.isfile(self.config_file): | ||
file_name = self.config_file | ||
suffix = 1 | ||
backup_name = f'{file_name}.{suffix}' | ||
while os.path.exists(backup_name): | ||
suffix += 1 | ||
backup_name = f'{file_name}.{suffix}' | ||
|
||
for i in range(suffix, 1, -1): | ||
os.rename(f'{file_name}.{i - 1}', f'{file_name}.{i}') | ||
os.rename(file_name, file_name + '.1') | ||
|
||
def write(self): | ||
if self.config_file and self.content and os.path.isdir(os.path.dirname(self.config_file)): | ||
with open(self.config_file, 'w') as open_config: | ||
yaml.safe_dump(self.content, open_config) | ||
|
||
def set(self, *path, value): | ||
self._set_version() | ||
top_level = self.content | ||
for p in path[:-1]: | ||
if p not in top_level: | ||
top_level[p] = {} | ||
top_level = top_level[p] | ||
top_level[path[-1]] = value | ||
|
||
def pop(self, *path, default=None): | ||
"""Recursive dictionary pop with default""" | ||
top_level = self.content | ||
for p in path[:-1]: | ||
if p not in top_level: | ||
return default | ||
top_level = top_level[p] | ||
return top_level.pop(path[-1], default) | ||
|
||
def is_empty(self): | ||
return not self.content | ||
|
||
def clear(self): | ||
self.content = {} | ||
|
||
def _set_version(self): | ||
# If we're starting to fill in an empty config, set the version if available | ||
if self.is_empty() and self.version: | ||
self.content['version'] = self.version | ||
|
||
def __contains__(self, item): | ||
return item in self.content | ||
|
||
def __setitem__(self, item, value): | ||
"""Allow dict-style write access, e.g. config['this']='that'.""" | ||
self._set_version() | ||
self.content[item] = value | ||
|
||
|
||
def load_config(*args): | ||
"""Load a config file from any path provided. | ||
If none are provided then read from a file path provided in the environment variable CONFIG.""" | ||
cfg.load_config_file( | ||
*args, | ||
os.getenv('CONFIG') | ||
) | ||
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. I don't think this part of submission_config should be lifted to pyutils. 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. removed |
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,5 +1,4 @@ | ||
import os | ||
from os import environ | ||
from os.path import join | ||
|
||
import pytest | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
With the way pyutils is organised at the moment, I would prefer to have this class in
config.py
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.
moved to config.py