1
0
mirror of https://github.com/enpaul/keyosk.git synced 2024-11-05 06:07:06 +00:00

Refactor datatypes into submodules

Gonna do some composition up in here
Add py38 tox env
This commit is contained in:
Ethan Paul 2020-02-25 22:28:45 -05:00
parent 227f1e7c18
commit 07828f0063
7 changed files with 24 additions and 27 deletions

View File

@ -13,6 +13,7 @@ import toml
from keyosk import constants
from keyosk.config.storage import KeyoskStorageConfig
from keyosk.config.storage import StorageBackend # pylint: disable=unused-import
from keyosk.config.storage import StorageConfigSerializer

View File

@ -1,4 +1,5 @@
"""Data containers and utilities related to the storage configuration"""
import enum
from dataclasses import asdict
from dataclasses import dataclass
from dataclasses import field
@ -11,10 +12,17 @@ from typing import Union
import marshmallow as msh
from keyosk import datatypes
from keyosk import fields as custom_fields
@enum.unique
class StorageBackend(enum.Enum):
"""Supported storage backends"""
SQLITE = "sqlite"
MARIA = "maria"
def _default_sqlite_pragmas() -> Dict[str, Any]:
"""Generate the default pragmas for the sqlite connection
@ -131,7 +139,7 @@ class KeyoskStorageConfig:
time, depending on the value of the ``backend`` setting.
"""
backend: datatypes.StorageBackend = datatypes.StorageBackend.SQLITE
backend: StorageBackend = StorageBackend.SQLITE
sqlite: KeyoskSQLiteStorageConfig = KeyoskSQLiteStorageConfig()
maria: KeyoskMariaStorageConfig = KeyoskMariaStorageConfig()
@ -143,7 +151,7 @@ class StorageConfigSerializer(msh.Schema):
:class:`KeyoskStorageConfig` class.
"""
backend = custom_fields.EnumItem(datatypes.StorageBackend, pretty_names=True)
backend = custom_fields.EnumItem(StorageBackend, pretty_names=True)
sqlite = msh.fields.Nested(SQLiteStorageConfigSerializer)
maria = msh.fields.Nested(MariaStorageConfigSerializer)

View File

@ -24,7 +24,6 @@ from typing import Type
import peewee
from keyosk import config
from keyosk import datatypes
from keyosk.database._shared import INTERFACE as interface
from keyosk.database._shared import KeyoskBaseModel
from keyosk.database.account import Account
@ -57,7 +56,7 @@ def initialize(conf: config.KeyoskConfig):
logger = logging.getLogger(__name__)
if conf.storage.backend == datatypes.StorageBackend.SQLITE:
if conf.storage.backend == config.StorageBackend.SQLITE:
logger.debug("Using SQLite database backend")
logger.debug(f"Applying SQLite pragmas: {conf.storage.sqlite.pragmas}")
# Explicit cast-to-string on the path is to support py3.6: sqlite driver
@ -66,7 +65,7 @@ def initialize(conf: config.KeyoskConfig):
str(conf.storage.sqlite.path), pragmas=conf.storage.sqlite.pragmas
)
elif conf.storage.backend == datatypes.StorageBackend.MARIA:
elif conf.storage.backend == config.StorageBackend.MARIA:
logger.debug("Using MariaDB database backend")
logger.debug(
f"Configuring MariaDB: {conf.storage.maria.username}@{conf.storage.maria.host}:{conf.storage.maria.port}, with database '{conf.storage.maria.schema}'"

View File

@ -1,18 +0,0 @@
"""Shared types, enums, and data containers"""
import enum
from typing import Dict
from typing import Union
Extras = Dict[str, Union[int, float, bool, str, None]]
TokenClaims = Dict[str, Union[str, int, bool, Dict[str, int]]]
@enum.unique
class StorageBackend(enum.Enum):
"""Supported storage backends"""
SQLITE = "sqlite"
MARIA = "maria"

View File

@ -0,0 +1,8 @@
from typing import Dict
from typing import Union
Extras = Dict[str, Union[int, float, bool, str, None]]
TokenClaims = Dict[str, Union[str, int, bool, Dict[str, int]]]

View File

@ -5,7 +5,6 @@ import toml
from keyosk import config
from keyosk import constants
from keyosk import datatypes
DEMO_CONFIG = {
@ -39,7 +38,7 @@ def test_roundtrip():
def test_settings():
loaded = config.ConfigSerializer().load(DEMO_CONFIG)
assert loaded.storage.backend == datatypes.StorageBackend.MARIA
assert loaded.storage.backend == config.StorageBackend.MARIA
assert loaded.storage.sqlite.path == Path(DEMO_CONFIG["storage"]["sqlite"]["path"])
assert loaded.storage.sqlite.pragmas == DEMO_CONFIG["storage"]["sqlite"]["pragmas"]
for key, value in DEMO_CONFIG["storage"]["maria"].items():

View File

@ -1,5 +1,5 @@
[tox]
envlist = py37, py36, lint, typing, security, docs
envlist = py36, py37, py38, lint, typing, security, docs
isolated_build = true
[testenv]