mirror of
https://github.com/enpaul/keyosk.git
synced 2024-11-05 06:07:06 +00:00
Add config.load function for loading config container
Document/lint config module Add constants module
This commit is contained in:
parent
2e3c10209b
commit
ee99c1b9ab
@ -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)
|
||||
|
@ -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)
|
||||
|
3
keyosk/constants.py
Normal file
3
keyosk/constants.py
Normal file
@ -0,0 +1,3 @@
|
||||
DEFAULT_CONFIG_PATH = "/etc/keyosk/conf.toml"
|
||||
|
||||
ENV_CONFIG_PATH = "KYSK_CONF_PATH"
|
Loading…
Reference in New Issue
Block a user