Reorganization.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2021-09-30 10:21:03 -04:00
parent 30fa665351
commit 79bf390e00
5 changed files with 14 additions and 80 deletions

View File

@@ -11,7 +11,7 @@ import marshmallow.schema # type: ignore [import]
DataSub = typing.TypeVar("DataSub")
DataDict = typing.TypeVar("DataDict")
DataDict = typing.NewType("DataDict", dict[str, typing.Any])
class DataMeta(type):
@@ -26,23 +26,24 @@ class DataMeta(type):
@property # type: ignore [no-redef,misc]
@functools.cache
def Schema(cls: DataMeta) -> marshmallow.schema.Schema: # noqa: N802
"""Return the schema object for this class."""
"""The marshmallow Schema object for this data class."""
return desert.schema(cls)
@property # type: ignore [no-redef,misc]
@functools.cache
def Dict(cls: DataMeta) -> type[DataDict]: # noqa: N802
"""Return the TypedDict definition for this class."""
def Dict(cls: DataMeta) -> DataDict: # noqa: N802
"""The TypedDict definition for this class.
This provides a type definition for data to be converted to/from the
data class in dictionary form.
"""
fields = {
name: type_ if type_ is not datetime.datetime else str
for name, type_ in cls.__annotations__.items()
}
return typing.cast(
type[DataDict],
typing.TypedDict( # type: ignore [operator]
f"{cls.__name__}.Dict",
**fields,
),
return typing.TypedDict( # type: ignore [no-any-return,operator]
f"{cls.__name__}Dict",
fields,
)

View File

@@ -1,66 +0,0 @@
"""Base class for YAML data."""
from __future__ import annotations
import dataclasses
import functools
import pathlib
import typing
import yaml
from playlist.data import base
from playlist.data import const
YAMLSub = typing.TypeVar("YAMLSub")
@dataclasses.dataclass
class YAMLBase(base.BaseData[YAMLSub]):
"""Data class base that contains functionality to read/write as YAML."""
@classmethod
def yaml_read(
cls: type[YAMLBase[YAMLSub]],
filepath: pathlib.Path,
) -> YAMLSub:
"""Read the given YAML file and convert it into an object."""
with filepath.open() as fp:
data = yaml.safe_load(fp)
return typing.cast(YAMLSub, cls.load(data))
def yaml_write(self: YAMLBase[YAMLSub], filepath: pathlib.Path) -> None:
"""Write this object as the given YAML file."""
data: type(self).Dict = self.dump() # type: ignore [valid-type]
with filepath.open(mode="w") as fp:
fp.write(yaml.dump(data))
@classmethod
def yaml_create(
cls: type[YAMLBase[YAMLSub]],
filepath: pathlib.Path,
) -> YAMLSub:
"""Create the YAML file with this object."""
data = cls()
data.yaml_write(filepath)
return typing.cast(YAMLSub, data)
@functools.cache
@classmethod
def get(cls: type[YAMLBase[YAMLSub]], filename: str) -> YAMLSub:
"""Get the data from the given YAML file."""
instance: YAMLSub
filepath = const.PATHS.CONFIG / filename
try:
instance = cls.yaml_read(filepath)
except FileNotFoundError:
const.PATHS.CONFIG.mkdir(parents=True, exist_ok=True)
instance = cls.yaml_create(filepath)
return instance
@classmethod
def write(cls: type[YAMLBase[YAMLSub]], filename: str) -> None:
"""Store the current data to the YAML file."""
filepath = const.PATHS.CONFIG / filename
cls.get(filepath).yaml_write(filepath) # type: ignore [attr-defined]

View File

@@ -46,10 +46,7 @@ async def gen_tracks(
times.append(process_time * weight)
weights.append(weight)
for track_data in batch:
track: models.Track = typing.cast(
models.Track,
models.Track.load(track_data),
)
track: models.Track = models.Track.load(track_data)
durations.append(track.duration)
yield track

1
tests/data/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for the playlist.data package."""

1
tests/plex/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for the playlist.plex package."""