From 07828f006337c8b232ef4ecc5d02cd5a0afc5c01 Mon Sep 17 00:00:00 2001 From: Ethan Paul Date: Tue, 25 Feb 2020 22:28:45 -0500 Subject: [PATCH] Refactor datatypes into submodules Gonna do some composition up in here Add py38 tox env --- keyosk/config/__init__.py | 1 + keyosk/config/storage.py | 14 +++++++++++--- keyosk/database/__init__.py | 5 ++--- keyosk/datatypes.py | 18 ------------------ keyosk/datatypes/__init__.py | 8 ++++++++ tests/test_config.py | 3 +-- tox.ini | 2 +- 7 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 keyosk/datatypes.py create mode 100644 keyosk/datatypes/__init__.py diff --git a/keyosk/config/__init__.py b/keyosk/config/__init__.py index 9b5f92f..03a3aa2 100644 --- a/keyosk/config/__init__.py +++ b/keyosk/config/__init__.py @@ -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 diff --git a/keyosk/config/storage.py b/keyosk/config/storage.py index 8fdb118..d26a7b9 100644 --- a/keyosk/config/storage.py +++ b/keyosk/config/storage.py @@ -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) diff --git a/keyosk/database/__init__.py b/keyosk/database/__init__.py index c648750..fb75f82 100644 --- a/keyosk/database/__init__.py +++ b/keyosk/database/__init__.py @@ -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}'" diff --git a/keyosk/datatypes.py b/keyosk/datatypes.py deleted file mode 100644 index 088b4e6..0000000 --- a/keyosk/datatypes.py +++ /dev/null @@ -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" diff --git a/keyosk/datatypes/__init__.py b/keyosk/datatypes/__init__.py new file mode 100644 index 0000000..86dcbe0 --- /dev/null +++ b/keyosk/datatypes/__init__.py @@ -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]]] diff --git a/tests/test_config.py b/tests/test_config.py index c4fb69b..52ba773 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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(): diff --git a/tox.ini b/tox.ini index 53669fd..1070af0 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py37, py36, lint, typing, security, docs +envlist = py36, py37, py38, lint, typing, security, docs isolated_build = true [testenv]