From d9f6ec8e8804b157447e29406966133a951b6784 Mon Sep 17 00:00:00 2001 From: Sebastian Lobinger <sebastian.lobinger@zib.de> Date: Thu, 17 Aug 2017 16:07:38 +0200 Subject: [PATCH] add basic structure, write test and implement class, fix issue #2 --- README.md | 27 ++++++++++++++++++--------- __init__.py | 0 config_provider.py | 8 ++++++++ test/__init__.py | 0 test/test_config_provider.py | 9 +++++++++ 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 __init__.py create mode 100644 config_provider.py create mode 100644 test/__init__.py create mode 100644 test/test_config_provider.py diff --git a/README.md b/README.md index 0378b68..9120057 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 0000000..e69de29 diff --git a/config_provider.py b/config_provider.py new file mode 100644 index 0000000..d45df3e --- /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 0000000..e69de29 diff --git a/test/test_config_provider.py b/test/test_config_provider.py new file mode 100644 index 0000000..3664c88 --- /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 -- GitLab