Significantly overhaul configuration management
Everything now uses bog-standard Python dataclasses, with Pydantic providing validation and type conversion through separate classes using its type adapter feature. It's also possible to define your classes using Pydantic's own model type directly, making the type adapter unnecessary, but I didn't want to do things that way because no actual validation is needed when constructing a Song instance for example. Having Pydantic do its thing only on-demand was preferable. I tried a number of validation libraries before settling on Pydantic for this. It's not the fastest option out there (msgspec is I think), but it makes adding support for third-party types like yarl.URL really easy, it generates a nice clean JSON Schema which is easy enough to adjust to my requirements through its GenerateJsonSchema hooks, and raw speed isn't all that important anyway since this is a single-user desktop program that reads its configuration file once on startup. Also, MessagePack is now mandatory if you're caching to an external service. It just didn't make a whole lot sense to explicitly install mpd-now-playable's Redis or Memcached support and then use pickling with them. With all this fussing around done, I'm probably finally ready to actually use that configuration file to configure new features! Yay!
This commit is contained in:
parent
3b7ddfa718
commit
27d8c37139
18 changed files with 355 additions and 169 deletions
|
@ -2,7 +2,6 @@ from collections.abc import Mapping
|
|||
from os import environ
|
||||
from typing import TypeVar
|
||||
|
||||
from apischema import deserialize
|
||||
from boltons.iterutils import remap
|
||||
from pytomlpp import load
|
||||
from xdg_base_dirs import xdg_config_home
|
||||
|
@ -33,7 +32,7 @@ def loadConfigFromFile() -> Config:
|
|||
path = xdg_config_home() / "mpd-now-playable" / "config.toml"
|
||||
data = load(path)
|
||||
print(f"Loaded your configuration from {path}")
|
||||
return deserialize(Config, data)
|
||||
return Config.schema.validate_python(data)
|
||||
|
||||
|
||||
def loadConfigFromEnv() -> Config:
|
||||
|
@ -44,7 +43,7 @@ def loadConfigFromEnv() -> Config:
|
|||
if password is None and host is not None and "@" in host:
|
||||
password, host = host.split("@", maxsplit=1)
|
||||
data = {"cache": cache, "mpd": {"port": port, "host": host, "password": password}}
|
||||
return deserialize(Config, withoutNones(data))
|
||||
return Config.schema.validate_python(withoutNones(data))
|
||||
|
||||
|
||||
def loadConfig() -> Config:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue