From 8099e2c3ca02be000db8d18e4643af21747cbd06 Mon Sep 17 00:00:00 2001 From: Alexander Werner Date: Mon, 5 Nov 2018 16:50:06 +0100 Subject: [PATCH] Added config parsing and validation with voluptuous --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- carom/client.ini | 2 ++ carom/config.py | 21 +++++++++++++++++++++ carom/const.py | 6 +++--- setup.py | 3 +++ 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 carom/client.ini create mode 100644 carom/config.py diff --git a/Pipfile b/Pipfile index bcea968..4c2d247 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,7 @@ gpiozero = "*" colorama = "*" click-log = "*" click-didyoumean = "*" +voluptuous = "*" [dev-packages] "flake8" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index f5d6bd2..4842bb9 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "bd0fbeefa6073d17fbdcf3879ed282ebcb875445ae4ea4ef2eef2c261c13094c" + "sha256": "07aac5a19e64863bc015b63d301912dc79586e7be06dd8056b60dd0e6fb26629" }, "pipfile-spec": 6, "requires": { @@ -105,6 +105,14 @@ "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22" ], "version": "==1.24.1" + }, + "voluptuous": { + "hashes": [ + "sha256:303542b3fc07fb52ec3d7a1c614b329cdbee13a9d681935353d8ea56a7bfa9f1", + "sha256:567a56286ef82a9d7ae0628c5842f65f516abcb496e74f3f59f1d7b28df314ef" + ], + "index": "pypi", + "version": "==0.11.5" } }, "develop": { diff --git a/carom/client.ini b/carom/client.ini new file mode 100644 index 0000000..2c6062d --- /dev/null +++ b/carom/client.ini @@ -0,0 +1,2 @@ +[test] +foo = bar \ No newline at end of file diff --git a/carom/config.py b/carom/config.py new file mode 100644 index 0000000..d4cd248 --- /dev/null +++ b/carom/config.py @@ -0,0 +1,21 @@ +import configparser + +from voluptuous import Schema +from voluptuous.humanize import validate_with_humanized_errors + +from carom.const import CONFIG_FILES, DEFAULT_CONFIG + +_parser = configparser.ConfigParser() +_parser.read_string(DEFAULT_CONFIG) +_parser.read(CONFIG_FILES) +# Convert the ConfigParser object to a real dict now: +_parser_dict = {s: dict(_parser.items(s)) for s in _parser.sections()} +_schema = Schema({ + 'test': { + 'foo': str, + }, +}) + +config = validate_with_humanized_errors(_parser_dict, _schema) + +__all__ = ('config', ) diff --git a/carom/const.py b/carom/const.py index 65f27f6..ba1804f 100644 --- a/carom/const.py +++ b/carom/const.py @@ -1,8 +1,8 @@ -import os +from pkg_resources import resource_string AVAILABLE_PINS = range(0, 28) """Pins available for Configuration""" -DEFAULT_CONFIGFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), "client.ini")) +DEFAULT_CONFIG = resource_string(__name__, 'client.ini').decode() """Default configfile, that is used as a Fallback if no other configuration exists.""" -CONFIG_FILES = ('~/.config/carom/client.ini', '/etc/carom/client.ini', DEFAULT_CONFIGFILE) +CONFIG_FILES = ('~/.config/carom/client.ini', '/etc/carom/client.ini') """Order of resolution of Config Files. Files are tried in order, first one found is used.""" diff --git a/setup.py b/setup.py index d579d5f..268f7db 100644 --- a/setup.py +++ b/setup.py @@ -15,4 +15,7 @@ setup(name='carom-client', data_files=[ ('share/man/man1', ('doc/man/carom-client.1', )), ], + package_data={ + 'carom': ['client.ini'], + }, )