Skip to content

Commit

Permalink
feat: add file default value
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Mar 29, 2021
1 parent 83e9e59 commit 205c9ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 18 additions & 2 deletions beet/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class File(Generic[ValueType, SerializeType]):
content: Union[ValueType, SerializeType, None] = None
source_path: Optional[FileSystemPath] = None

def __post_init__(self):
if self.content is self.source_path is None:
self.content = self.default()

def merge(self: FileType, other: FileType) -> bool:
"""Merge the given file or return False to indicate no special handling."""
return False
Expand All @@ -65,8 +69,8 @@ def ensure_source_path(self) -> FileSystemPath:
if self.source_path:
return self.source_path
raise ValueError(
f"{self.__class__.__name__} object must be initialized with "
"either a value, raw bytes or a source path."
f"Expected {self.__class__.__name__} object to be initialized with "
"a source path."
)

def ensure_serialized(self) -> SerializeType:
Expand All @@ -91,6 +95,14 @@ def __eq__(self, other: Any) -> bool:
or self.ensure_deserialized() == other.ensure_deserialized()
)

@classmethod
def default(cls) -> ValueType:
"""Return the file's default value."""
raise ValueError(
f"{cls.__name__} object must be initialized with "
"either a value, serialized data, or a source path."
)

@classmethod
def serialize(cls, content: Union[ValueType, SerializeType]) -> SerializeType:
"""Serialize file content."""
Expand Down Expand Up @@ -264,6 +276,10 @@ class JsonFileBase(TextFileBase[ValueType]):

data: FileDeserialize[ValueType] = FileDeserialize()

@classmethod
def default(cls) -> ValueType:
return {} # type: ignore

@classmethod
def to_str(cls, content: ValueType) -> str:
return dump_json(content)
Expand Down
10 changes: 9 additions & 1 deletion beet/library/data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
TextFileBase,
TextFileContent,
)
from beet.core.utils import extra_field
from beet.core.utils import JsonDict, extra_field

from .base import Namespace, NamespaceFile, NamespacePin, NamespaceProxyDescriptor, Pack

Expand All @@ -69,6 +69,10 @@ class Function(TextFileBase[List[str]], NamespaceFile):

lines = FileDeserialize() # type: FileDeserialize[List[str]]

@classmethod
def default(cls) -> List[str]:
return []

@classmethod
def to_str(cls, content: List[str]) -> str:
return "\n".join(content) + "\n"
Expand Down Expand Up @@ -147,6 +151,10 @@ def merge(self: TagFileType, other: TagFileType) -> bool: # type: ignore
values.append(deepcopy(value))
return True

@classmethod
def default(cls) -> JsonDict:
return {"values": []}


class BlockTag(TagFile):
"""Class representing a block tag."""
Expand Down

0 comments on commit 205c9ff

Please sign in to comment.