-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cli options for config file management (#33)
* add cli options for config file management Add cli options: --testing-config-action: Read config file if present ('read'), require config file ('require') or generate new config file ('generate'). --regenerate-test-data: Regenerate test data * add tests for new cli options * add documentation of new cli options * fix: check that the data directory path is absolute and exists * fix: add quotation marks to exported environment variables Paths may contain spaces etc.
- Loading branch information
Showing
10 changed files
with
296 additions
and
67 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,4 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
"""A pytest plugin for testing AiiDA plugins.""" | ||
|
||
__version__ = '0.1.0.dev0' | ||
__version__ = '0.1.0.dev1' |
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,30 +1,87 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Defines a helper for loading the ``.aiida-testing-config.yml`` | ||
configuration file. | ||
Helpers for managing the ``.aiida-testing-config.yml`` configuration file. | ||
""" | ||
|
||
import os | ||
import pathlib | ||
import typing as ty | ||
import collections | ||
from voluptuous import Schema | ||
from enum import Enum | ||
|
||
import yaml | ||
|
||
CONFIG_FILE_NAME = '.aiida-testing-config.yml' | ||
|
||
def get_config() -> ty.Dict[str, str]: | ||
|
||
class ConfigActions(Enum): | ||
""" | ||
Reads the configuration file ``.aiida-testing-config.yml``. The | ||
file is searched in the current working directory and all its parent | ||
directories. | ||
An enum containing the actions to perform on the config file. | ||
""" | ||
cwd = pathlib.Path(os.getcwd()) | ||
config: ty.Dict[str, str] | ||
for dir_path in [cwd, *cwd.parents]: | ||
config_file_path = (dir_path / '.aiida-testing-config.yml') | ||
if config_file_path.exists(): | ||
with open(config_file_path) as config_file: | ||
config = yaml.load(config_file, Loader=yaml.SafeLoader) | ||
break | ||
else: | ||
config = {} | ||
return config | ||
READ = 'read' | ||
GENERATE = 'generate' | ||
REQUIRE = 'require' | ||
|
||
|
||
class Config(collections.abc.MutableMapping): | ||
"""Configuration of aiida-testing package.""" | ||
|
||
schema = Schema({'mock_code': Schema({str: str})}) | ||
|
||
def __init__(self, config=None): | ||
self._dict = config or {} | ||
self.validate() | ||
|
||
def validate(self): | ||
"""Validate configuration dictionary.""" | ||
return self.schema(self._dict) | ||
|
||
@classmethod | ||
def from_file(cls): | ||
""" | ||
Parses the configuration file ``.aiida-testing-config.yml``. | ||
The file is searched in the current working directory and all its parent | ||
directories. | ||
""" | ||
cwd = pathlib.Path(os.getcwd()) | ||
config: ty.Dict[str, str] | ||
for dir_path in [cwd, *cwd.parents]: | ||
config_file_path = (dir_path / CONFIG_FILE_NAME) | ||
if config_file_path.exists(): | ||
with open(config_file_path) as config_file: | ||
config = yaml.load(config_file, Loader=yaml.SafeLoader) | ||
break | ||
else: | ||
config = {} | ||
|
||
return cls(config) | ||
|
||
def to_file(self): | ||
"""Write configuration to file in yaml format. | ||
Writes to current working directory. | ||
:param handle: File handle to write config file to. | ||
""" | ||
cwd = pathlib.Path(os.getcwd()) | ||
config_file_path = (cwd / CONFIG_FILE_NAME) | ||
|
||
with open(config_file_path, 'w') as handle: | ||
yaml.dump(self._dict, handle, Dumper=yaml.SafeDumper) | ||
|
||
def __getitem__(self, item): | ||
return self._dict.__getitem__(item) | ||
|
||
def __setitem__(self, key, value): | ||
return self._dict.__setitem__(key, value) | ||
|
||
def __delitem__(self, key): | ||
return self._dict.__delitem__(key) | ||
|
||
def __iter__(self): | ||
return self._dict.__iter__() | ||
|
||
def __len__(self): | ||
return self._dict.__len__() |
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
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
Oops, something went wrong.