diff --git a/README.md b/README.md index 0378b6840f1e5a4798fd45f56216530f09f3d191..9120057bbc82998f191b1ed42adc803d19a0d9cf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# YamlConfig -Pyhton Konfigurationsklasse für YAML Dateien. +# ConfigProvider +Pyhton Konfigurationsklasse. ## Einbindung in andere Projekte @@ -7,24 +7,33 @@ Dieses Projekt ist selbst als python package Strukturiert und kann daher einfach als git submodule direkt in andere Projekte eingebunden werden. ```bash -git submodule add git@git.imp.fu-berlin.de:coding-at-fu/yaml_config.git dict_filter +git submodule add git@git.imp.fu-berlin.de:coding-at-fu/yaml_config.git config_provider ``` ## Verwendung Wurde das Projekt wie oben stehend eingebunden kann das Modul wie folgt verwendet werden: ```python -from yaml_config import yaml_config +import config_provider +import yaml -config = yaml_config.YamlConfig(path = '/path/to/conf/file.yml') +def load_config_from_yaml_file(path): + with open(path) as f: + yaml_string = f.read() + return_dict = yaml.load(yaml_string) + return return_dict + +config = config_provider.ConfigProvider( + load_function = load_config_from_yaml_file, + path = '/path/to.yml') def do_whatever(): - config = yaml_config.YamlConfig.getInstance() - print (config.toDict().get('config_key')) + config = config_provider.ConfigProvider.getInstance() + print (config.dict().get('config_key')) # wird foo ausgeben wenn config_key den Wert foo hat. def reconfigure(): - config = yaml_config.YamlConfig.getInstance() + config = config_provider.ConfigProvider.getInstance() config.reload() - # Die Konfiguration wurde neu eingelesen. + # Die Konfiguration wurde neu eingelesen. ``` diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/config_provider.py b/config_provider.py new file mode 100644 index 0000000000000000000000000000000000000000..d45df3ea465d2d61dcd9dd32aa959779268b04bc --- /dev/null +++ b/config_provider.py @@ -0,0 +1,8 @@ +class ConfigProvider: + + def __init__(self, load_function, *args, **kwargs): + self._conf_dict = load_function(*args, **kwargs) + + @property + def dict(self): + return self._conf_dict \ No newline at end of file diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/test_config_provider.py b/test/test_config_provider.py new file mode 100644 index 0000000000000000000000000000000000000000..3664c88a801bb5fd93d06ee43543d3b56ead6aa6 --- /dev/null +++ b/test/test_config_provider.py @@ -0,0 +1,9 @@ +import unittest +import config_provider + +class TestConfigProvider(unittest.TestCase): + + def test_init(self): + config = config_provider.ConfigProvider( + load_function = dict, foo = 'bar') + self.assertEqual({'foo': 'bar'}, config.dict) \ No newline at end of file