Organizing.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2021-09-29 21:35:40 -04:00
parent 45062c31a1
commit 1104d1a4e1
3 changed files with 38 additions and 1 deletions

View File

@@ -5,6 +5,8 @@ import dataclasses
import datetime
import functools
from plsylist.data import yaml_base
from playlist.data import base
from playlist.data import const
@@ -76,7 +78,7 @@ class PlaylistSettings(base.BaseData):
@dataclasses.dataclass
class Settings(base.YAMLData):
class Settings(yaml_base.YAMLBase):
"""Settings object, loaded from settings.yaml file."""
creds: CredentialSettings = dataclasses.field(

View File

@@ -0,0 +1,35 @@
"""Base class for YAML data."""
from __future__ import annotations
import dataclasses
import pathlib
import typing
import yaml
from playlist.data import base
@dataclasses.dataclass
class YAMLBase(base.BaseData):
"""Data class base that contains functionality to read/write as YAML."""
@classmethod
def yaml_read(cls: type[YAMLBase], filepath: pathlib.Path) -> base.DataSubtype:
"""Read the given YAML file and convert it into an object."""
with filepath.open() as fp:
data = yaml.safe_load(fp)
return cls.load(data)
def yaml_write(self: YAMLBase, 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], filepath: pathlib.Path) -> base.DataSubtype:
"""Reload the YAML file with this object."""
data = cls()
data.yaml_write(filepath)
return typing.cast(base.DataSubtype, data)