@@ -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(
|
||||
|
||||
35
src/playlist/data/yaml_base.py
Normal file
35
src/playlist/data/yaml_base.py
Normal 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)
|
||||
Reference in New Issue
Block a user