Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.1.2 #14

Merged
merged 7 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/iokit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = [
"Dat",
"Gzip",
"Json",
"Jsonl",
Expand All @@ -11,8 +12,8 @@
"save_file",
"save_temp",
]
__version__ = "0.1.1"
__version__ = "0.1.2"

from .extensions import Gzip, Json, Jsonl, Tar, Txt
from .extensions import Dat, Gzip, Json, Jsonl, Tar, Txt
from .state import State, filter_states, find_state
from .storage import load_file, save_file, save_temp
2 changes: 2 additions & 0 deletions src/iokit/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
__all__ = [
"Dat",
"Gzip",
"Json",
"Jsonl",
"Tar",
"Txt",
]

from .dat import Dat
from .gz import Gzip
from .json import Json
from .jsonl import Jsonl
Expand Down
15 changes: 15 additions & 0 deletions src/iokit/extensions/dat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
__all__ = [
"Dat",
]

from typing import Any

from iokit.state import Payload, State


class Dat(State, suffix="dat"):
def __init__(self, data: Payload, **kwargs: Any):
super().__init__(data=data, **kwargs)

def load(self) -> bytes:
return self._data.getvalue()
19 changes: 12 additions & 7 deletions src/iokit/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"find_state",
]

from contextlib import suppress
from datetime import datetime
from fnmatch import fnmatch
from io import BytesIO
from typing import Any, Generator, Iterable
from typing import Any, Generator, Iterable, Type

from humanize import naturalsize
from typing_extensions import Self
Expand Down Expand Up @@ -126,13 +127,17 @@ def __repr__(self) -> str:
size = naturalsize(self.size, gnu=True)
return f"{self.name} ({size})"

@classmethod
def by_suffix(cls, suffix: str) -> Type[Self]:
if suffix in cls._suffixes:
return cls
for klass in cls.__subclasses__():
with suppress(ValueError):
return klass.by_suffix(suffix)
raise ValueError(f"Unknown state suffix {suffix}")

def cast(self) -> "State":
suffix = self.name.suffix
for klass in State.__subclasses__():
if suffix in getattr(klass, "_suffixes"):
break
else:
raise ValueError(f"Unknown state suffix {suffix}")
klass = self.by_suffix(self.name.suffix)
state = klass.__new__(klass)
setattr(state, "_data", self.data)
setattr(state, "_name", self.name)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_dat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from iokit import Dat, State


def test_dat() -> None:
state = Dat(b"test", name="test")
assert state.name == "test.dat"
assert state.load() == b"test"


def load_dat() -> None:
state = State(b"test", name="test.dat")
assert state.load() == b"test"
assert isinstance(state.cast(), Dat)
14 changes: 14 additions & 0 deletions tests/test_state_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from iokit import Json, State


class MyJson(Json, suffix="myjson"):
pass


def test_state_inheritance_json() -> None:
assert getattr(MyJson, "_suffix") == "myjson"
assert getattr(MyJson, "_suffixes") == ("myjson",)
myjson = MyJson({"a": 1}, name="test")
assert myjson.name == "test.myjson"
loaded = State(data=myjson.data, name="test.myjson")
assert loaded.load() == myjson.load() == {"a": 1}