Skip to content

Commit

Permalink
fix: add defaults for everything
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 5, 2021
1 parent 5906fb2 commit f229b3c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions beet/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ def to_str(cls, content: str) -> str:
def from_str(cls, content: str) -> str:
return content

@classmethod
def default(cls) -> str:
return ""


class BinaryFileBase(File[ValueType, bytes]):
"""Base class for files that get serialized to bytes."""
Expand Down Expand Up @@ -270,16 +274,16 @@ def to_bytes(cls, content: bytes) -> bytes:
def from_bytes(cls, content: bytes) -> bytes:
return content

@classmethod
def default(cls) -> bytes:
return b""


class JsonFileBase(TextFileBase[ValueType]):
"""Base class for json files."""

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 All @@ -294,6 +298,10 @@ class JsonFile(JsonFileBase[JsonDict]):

data: FileDeserialize[JsonDict] = FileDeserialize()

@classmethod
def default(cls) -> JsonDict:
return {}


class PngFile(BinaryFileBase[img.Image]):
"""Class representing a png file."""
Expand All @@ -309,3 +317,7 @@ def to_bytes(cls, content: img.Image) -> bytes:
@classmethod
def from_bytes(cls, content: bytes) -> img.Image:
return img.open(io.BytesIO(content))

@classmethod
def default(cls) -> img.Image:
return img.new("RGB", (16, 16), "black")

0 comments on commit f229b3c

Please sign in to comment.