From ee99c1b9abbad540c5cf57f79bd86f478b6cc4a5 Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Fri, 21 Feb 2020 20:30:44 -0500 Subject: [PATCH] Add config.load function for loading config container Document/lint config module Add constants module --- keyosk/config/__init__.py | 33 +++++++++++++++++++++++++++++++++ keyosk/config/storage.py | 7 +++++++ keyosk/constants.py | 3 +++ 3 files changed, 43 insertions(+) create mode 100644 keyosk/constants.py diff --git a/keyosk/config/__init__.py b/keyosk/config/__init__.py index cbb80da..c4db20f 100644 --- a/keyosk/config/__init__.py +++ b/keyosk/config/__init__.py @@ -1,7 +1,14 @@ +"""Application configuration settings containers and loading functions""" +import os from dataclasses import dataclass +from pathlib import Path +from typing import Optional +from typing import Union import marshmallow as msh +import toml +from keyosk import constants from keyosk.config.storage import KeyoskStorageConfig from keyosk.config.storage import StorageConfigSerializer @@ -21,3 +28,29 @@ class ConfigSerializer(msh.Schema): """ storage = msh.fields.Nested(StorageConfigSerializer) + + +def load(source: Optional[Union[str, Path]] = None) -> KeyoskConfig: + """Load the configuration data + + :param source: Optional path to the configuration file to load + + The configuration file path can be found from one of the three follow places, listed + in the order of priority: + + 1. Parameter passed directly to this function + 2. Environment variable specified by ``constants.ENV_CONFIG_PATH`` + 3. The default location, specified by ``constants.DEFAULT_CONFIG_PATH`` + """ + + source = Path( + source or os.getenv(constants.ENV_CONFIG_PATH, constants.DEFAULT_CONFIG_PATH) + ) + + if source.exists(): + with source.open() as infile: + data = toml.load(infile) + else: + data = {} + + return ConfigSerializer().load(data) diff --git a/keyosk/config/storage.py b/keyosk/config/storage.py index 7d429d9..a589638 100644 --- a/keyosk/config/storage.py +++ b/keyosk/config/storage.py @@ -1,3 +1,4 @@ +"""Data containers and utilities related to the storage configuration""" from dataclasses import asdict from dataclasses import dataclass from dataclasses import field @@ -51,6 +52,8 @@ class SQLiteStorageConfigSerializer(msh.Schema): path = custom_fields.Path() pragmas = msh.fields.Dict(keys=msh.fields.String(), values=msh.fields.Raw()) + # pylint: disable=unused-argument,no-self-use + @msh.post_load def _make_dataclass(self, data: Mapping[str, Any], *args, **kwargs): return KeyoskSQLiteStorageConfig(**data) @@ -101,6 +104,8 @@ class MariaStorageConfigSerializer(msh.Schema): username = msh.fields.String() password = msh.fields.String(allow_none=True) + # pylint: disable=unused-argument,no-self-use + @msh.post_load def _make_dataclass(self, data: Mapping[str, Any], *args, **kwargs): return KeyoskMariaStorageConfig(**data) @@ -142,6 +147,8 @@ class StorageConfigSerializer(msh.Schema): sqlite = msh.fields.Nested(SQLiteStorageConfigSerializer) maria = msh.fields.Nested(MariaStorageConfigSerializer) + # pylint: disable=unused-argument,no-self-use + @msh.post_load def _make_dataclass(self, data: Mapping[str, Any], *args, **kwargs): return KeyoskStorageConfig(**data) diff --git a/keyosk/constants.py b/keyosk/constants.py new file mode 100644 index 0000000..95f45d9 --- /dev/null +++ b/keyosk/constants.py @@ -0,0 +1,3 @@ +DEFAULT_CONFIG_PATH = "/etc/keyosk/conf.toml" + +ENV_CONFIG_PATH = "KYSK_CONF_PATH"