Skip to content

liblaf.melon.io.abc ¤

Classes:

  • ConverterDispatcher –
  • Reader –
  • ReaderDispatcher –
  • UnsupportedConverterError –
  • UnsupportedReaderError –
  • UnsupportedWriterError –
  • Writer –
  • WriterDispatcher –

ConverterDispatcher ¤

ConverterDispatcher(to_type: type[T])

Parameters:

  • to_type ¤

    (type[T]) –
  • dispatch ¤

    (T) –

Methods:

  • __call__ –
  • register –

Attributes:

  • dispatch (SingleDispatchCallable[T]) –
  • to_type (type[T]) –
Source code in src/liblaf/melon/io/abc/_converter.py
30
31
32
33
34
35
def __init__(self, to_type: type[T]) -> None:
    @functools.singledispatch
    def dispatch(obj: Any, /, **kwargs) -> T:  # noqa: ARG001
        raise UnsupportedConverterError(obj, to_type)

    self.__attrs_init__(to_type=to_type, dispatch=dispatch)  # pyright: ignore[reportAttributeAccessIssue]

dispatch instance-attribute ¤

dispatch: SingleDispatchCallable[T]

to_type instance-attribute ¤

to_type: type[T]

__call__ ¤

__call__(obj: Any, /, **kwargs) -> T
Source code in src/liblaf/melon/io/abc/_converter.py
37
38
39
40
41
42
def __call__(self, obj: Any, /, **kwargs) -> T:
    if isinstance(obj, self.to_type):
        return obj
    result: T = self.dispatch(obj, **kwargs)
    # logger.trace(f"Converted {type(obj)} to {type(result)}.")
    return result

register ¤

register(
    cls: RegType,
) -> Callable[[Callable[..., T]], Callable[..., T]]
Source code in src/liblaf/melon/io/abc/_converter.py
44
45
def register(self, cls: RegType) -> Callable[[Callable[..., T]], Callable[..., T]]:
    return self.dispatch.register(cls)

Reader ¤

Bases: Protocol


              flowchart TD
              liblaf.melon.io.abc.Reader[Reader]

              

              click liblaf.melon.io.abc.Reader href "" "liblaf.melon.io.abc.Reader"
            

Methods:

  • __call__ –

__call__ ¤

__call__(path: Path, /, **kwargs) -> T
Source code in src/liblaf/melon/io/abc/_reader.py
26
def __call__(self, path: Path, /, **kwargs) -> T: ...

ReaderDispatcher ¤

Parameters:

  • to_type ¤

    (type[T]) –
  • registry ¤

    (dict[str, Reader[T]], default: <class 'dict'> ) –

    dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

Methods:

  • __call__ –
  • register –

Attributes:

  • registry (dict[str, Reader[T]]) –
  • to_type (type[T]) –

registry class-attribute instance-attribute ¤

registry: dict[str, Reader[T]] = field(factory=dict)

to_type instance-attribute ¤

to_type: type[T]

__call__ ¤

__call__(path: StrPath, /, **kwargs) -> T
Source code in src/liblaf/melon/io/abc/_reader.py
34
35
36
37
38
39
40
41
42
def __call__(self, path: StrPath, /, **kwargs) -> T:
    __tracebackhide__ = True
    path = Path(path)
    reader: Reader[T] | None = self.registry.get(path.suffix)
    if reader is None:
        raise UnsupportedReaderError(path, self.to_type)
    obj: T = reader(path, **kwargs)
    autolog.debug("Loaded '%s' as <%s>.", path, utils.abbr_type_name(obj))
    return obj

register ¤

register(
    *suffixes: str,
) -> Callable[[Reader[T]], Reader[T]]
Source code in src/liblaf/melon/io/abc/_reader.py
44
45
46
47
48
49
50
def register(self, *suffixes: str) -> Callable[[Reader[T]], Reader[T]]:
    def wrapper(reader: Reader[T]) -> Reader[T]:
        for s in suffixes:
            self.registry[s] = reader
        return reader

    return wrapper

UnsupportedConverterError ¤

UnsupportedConverterError(obj_or_cls: Any, to_type: type)

Bases: ValueError


              flowchart TD
              liblaf.melon.io.abc.UnsupportedConverterError[UnsupportedConverterError]

              

              click liblaf.melon.io.abc.UnsupportedConverterError href "" "liblaf.melon.io.abc.UnsupportedConverterError"
            

Parameters:

Methods:

  • __str__ –

Attributes:

  • from_type (type) –
  • to_type (type) –
Source code in src/liblaf/melon/io/abc/_converter.py
15
16
17
18
19
def __init__(self, obj_or_cls: Any, to_type: type, /) -> None:
    from_type: type = (
        obj_or_cls if isinstance(obj_or_cls, type) else type(obj_or_cls)
    )
    self.__attrs_init__(from_type=from_type, to_type=to_type)  # pyright: ignore[reportAttributeAccessIssue]

from_type instance-attribute ¤

from_type: type

to_type instance-attribute ¤

to_type: type

__str__ ¤

__str__() -> str
Source code in src/liblaf/melon/io/abc/_converter.py
21
22
def __str__(self) -> str:
    return f"Cannot convert {self.from_type} to {self.to_type}."

UnsupportedReaderError ¤

Bases: ValueError


              flowchart TD
              liblaf.melon.io.abc.UnsupportedReaderError[UnsupportedReaderError]

              

              click liblaf.melon.io.abc.UnsupportedReaderError href "" "liblaf.melon.io.abc.UnsupportedReaderError"
            

Parameters:

Methods:

  • __str__ –

Attributes:

  • path (Path) –
  • to_type (type) –

path class-attribute instance-attribute ¤

path: Path = field(converter=Path)

to_type instance-attribute ¤

to_type: type

__str__ ¤

__str__() -> str
Source code in src/liblaf/melon/io/abc/_reader.py
21
22
def __str__(self) -> str:
    return f"Cannot load '{self.path}' as {self.to_type}."

UnsupportedWriterError ¤

Bases: ValueError


              flowchart TD
              liblaf.melon.io.abc.UnsupportedWriterError[UnsupportedWriterError]

              

              click liblaf.melon.io.abc.UnsupportedWriterError href "" "liblaf.melon.io.abc.UnsupportedWriterError"
            

Parameters:

Methods:

  • __str__ –

Attributes:

  • from_type (type) –
  • path (Path) –

from_type instance-attribute ¤

from_type: type

path class-attribute instance-attribute ¤

path: Path = field(converter=Path)

__str__ ¤

__str__() -> str
Source code in src/liblaf/melon/io/abc/_writer.py
26
27
def __str__(self) -> str:
    return f"Cannot save {self.from_type} to '{self.path}'."

Writer ¤

Bases: Protocol


              flowchart TD
              liblaf.melon.io.abc.Writer[Writer]

              

              click liblaf.melon.io.abc.Writer href "" "liblaf.melon.io.abc.Writer"
            

Methods:

  • __call__ –

__call__ ¤

__call__(path: Path, obj: Any, /, **kwargs) -> None
Source code in src/liblaf/melon/io/abc/_writer.py
31
def __call__(self, path: Path, obj: Any, /, **kwargs) -> None: ...

WriterDispatcher ¤

Parameters:

  • writers ¤

    (dict[str, SingleDispatchCallable[None]], default: defaultdict(<function WriterDispatcher.<lambda>.<locals>.<lambda> at 0x713e0e171f80>, {}) ) –

Methods:

  • __call__ –
  • register –

Attributes:

  • writers (dict[str, SingleDispatchCallable[None]]) –

writers class-attribute instance-attribute ¤

writers: dict[str, SingleDispatchCallable[None]] = field(
    factory=lambda: defaultdict(
        lambda: singledispatch(_dummy)
    )
)

__call__ ¤

__call__(path: StrPath, obj: Any, /, **kwargs) -> None
Source code in src/liblaf/melon/io/abc/_writer.py
46
47
48
49
50
51
52
53
54
55
56
57
def __call__(self, path: StrPath, obj: Any, /, **kwargs) -> None:
    __tracebackhide__ = True
    path = Path(path)
    writer: SingleDispatchCallable[None] | None = self.writers.get(path.suffix)
    if writer is None:
        raise UnsupportedWriterError(type(obj), path)
    path.parent.mkdir(parents=True, exist_ok=True)
    impl: Writer = writer.dispatch(type(obj))
    if impl is _dummy:
        raise UnsupportedWriterError(type(obj), path)
    impl(path, obj, **kwargs)
    autolog.debug("Saved <%s> to '%s'.", utils.abbr_type_name(obj), path)

register ¤

register(
    cls: RegType, suffix: str | Iterable[str]
) -> Callable[[Writer], Writer]
Source code in src/liblaf/melon/io/abc/_writer.py
59
60
61
62
63
64
65
66
67
def register(
    self, cls: RegType, suffix: str | Iterable[str]
) -> Callable[[Writer], Writer]:
    def wrapper(func: Writer) -> Writer:
        for s in mit.always_iterable(suffix):
            self.writers[s].register(cls)(func)
        return func

    return wrapper