Reconfigured settings to use desert.
Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
@@ -2,8 +2,7 @@
|
||||
import dataclasses
|
||||
import typing
|
||||
|
||||
import marshmallow
|
||||
import marshmallow_dataclass
|
||||
import desert
|
||||
import yaml
|
||||
|
||||
from playlist import const
|
||||
@@ -13,38 +12,34 @@ __all__ = ["get"]
|
||||
SETTINGS_PATH = const.PATHS.CONFIG / "settings.yaml"
|
||||
|
||||
|
||||
def _read_settings() -> dict:
|
||||
def _read_settings() -> dict[str, typing.Any]:
|
||||
with SETTINGS_PATH.open() as fp:
|
||||
return yaml.safe_load(fp)
|
||||
return typing.cast(dict[str, typing.Any], yaml.safe_load(fp))
|
||||
|
||||
|
||||
def _write_settings(data: dict) -> None:
|
||||
def _write_settings(data: dict[str, typing.Any]) -> None:
|
||||
with SETTINGS_PATH.open(mode="w") as fp:
|
||||
fp.write(yaml.dump(data))
|
||||
|
||||
|
||||
def _refresh_settings(data: dict) -> dict:
|
||||
def _refresh_settings(data: dict[str, typing.Any]) -> dict[str, typing.Any]:
|
||||
_write_settings(data)
|
||||
return _read_settings()
|
||||
|
||||
|
||||
@marshmallow_dataclass.dataclass
|
||||
@dataclasses.dataclass
|
||||
class Creds:
|
||||
"""Credentials component for Settings object."""
|
||||
|
||||
baseurl: str
|
||||
token: str
|
||||
|
||||
Schema: typing.ClassVar[
|
||||
typing.Type[marshmallow.Schema] # noqa: S101
|
||||
] = marshmallow.Schema # For the type checker
|
||||
|
||||
def to_dict(self: "Creds") -> dict:
|
||||
return self.Schema().dump(self)
|
||||
def to_dict(self: "Creds") -> dict[str, typing.Any]:
|
||||
return typing.cast(dict[str, typing.Any], desert.schema(Creds).dump(self))
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls: typing.Type["Creds"], data: dict) -> "Creds":
|
||||
return cls.Schema().load(data)
|
||||
def from_dict(cls: typing.Type["Creds"], data: dict[str, typing.Any]) -> "Creds":
|
||||
return typing.cast(Creds, desert.schema(Creds).load(data))
|
||||
|
||||
|
||||
def _make_creds() -> Creds:
|
||||
@@ -62,16 +57,12 @@ def _make_creds() -> Creds:
|
||||
return Creds.from_dict(data)
|
||||
|
||||
|
||||
@marshmallow_dataclass.dataclass
|
||||
@dataclasses.dataclass
|
||||
class Settings:
|
||||
"""Settings object, loaded from settings.yaml file."""
|
||||
|
||||
creds: Creds = dataclasses.field(default_factory=_make_creds)
|
||||
|
||||
Schema: typing.ClassVar[
|
||||
typing.Type[marshmallow.Schema] # noqa: S101
|
||||
] = marshmallow.Schema # For the type checker
|
||||
|
||||
_instance: typing.ClassVar["Settings"] = dataclasses.field(
|
||||
metadata={"required": False},
|
||||
)
|
||||
@@ -88,8 +79,8 @@ def get() -> Settings:
|
||||
const.PATHS.CONFIG.mkdir(parents=True, exist_ok=True)
|
||||
data = _refresh_settings({})
|
||||
|
||||
Settings._instance = Settings.Schema().load(data)
|
||||
new_data = Settings.Schema().dump(Settings._instance)
|
||||
Settings._instance = desert.schema(Settings).load(data)
|
||||
new_data = desert.schema(Settings).dump(Settings._instance)
|
||||
if new_data != data:
|
||||
_write_settings(new_data)
|
||||
return Settings._instance
|
||||
|
||||
Reference in New Issue
Block a user