Added config parsing and validation with voluptuous

This commit is contained in:
Alexander Werner 2018-11-05 16:50:06 +01:00
parent 228043c0e0
commit 8099e2c3ca
6 changed files with 39 additions and 4 deletions

View File

@ -12,6 +12,7 @@ gpiozero = "*"
colorama = "*" colorama = "*"
click-log = "*" click-log = "*"
click-didyoumean = "*" click-didyoumean = "*"
voluptuous = "*"
[dev-packages] [dev-packages]
"flake8" = "*" "flake8" = "*"

10
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "bd0fbeefa6073d17fbdcf3879ed282ebcb875445ae4ea4ef2eef2c261c13094c" "sha256": "07aac5a19e64863bc015b63d301912dc79586e7be06dd8056b60dd0e6fb26629"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -105,6 +105,14 @@
"sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22" "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
], ],
"version": "==1.24.1" "version": "==1.24.1"
},
"voluptuous": {
"hashes": [
"sha256:303542b3fc07fb52ec3d7a1c614b329cdbee13a9d681935353d8ea56a7bfa9f1",
"sha256:567a56286ef82a9d7ae0628c5842f65f516abcb496e74f3f59f1d7b28df314ef"
],
"index": "pypi",
"version": "==0.11.5"
} }
}, },
"develop": { "develop": {

2
carom/client.ini Normal file
View File

@ -0,0 +1,2 @@
[test]
foo = bar

21
carom/config.py Normal file
View File

@ -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', )

View File

@ -1,8 +1,8 @@
import os from pkg_resources import resource_string
AVAILABLE_PINS = range(0, 28) AVAILABLE_PINS = range(0, 28)
"""Pins available for Configuration""" """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.""" """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.""" """Order of resolution of Config Files. Files are tried in order, first one found is used."""

View File

@ -15,4 +15,7 @@ setup(name='carom-client',
data_files=[ data_files=[
('share/man/man1', ('doc/man/carom-client.1', )), ('share/man/man1', ('doc/man/carom-client.1', )),
], ],
package_data={
'carom': ['client.ini'],
},
) )