|
| 1 | +""" |
| 2 | +Unified IO framework. Program interfaces can plug in custom |
| 3 | +load/write routines for pairs of component class and format. |
| 4 | +
|
| 5 | +Most of this module is stolen/simplified from astropy, at: |
| 6 | +- https://github.com/astropy/astropy/tree/main/astropy/io. |
| 7 | +""" |
| 8 | + |
| 9 | +__all__ = ["IO", "Loader", "Writer", "DEFAULT_REGISTRY"] |
| 10 | + |
| 11 | + |
| 12 | +from typing import Literal |
| 13 | + |
| 14 | + |
| 15 | +class Registry: |
| 16 | + """ |
| 17 | + Registry for IO operations. Loaders and writers |
| 18 | + are registered by format and the class they are |
| 19 | + associated with. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + self._loaders = {} |
| 24 | + self._writers = {} |
| 25 | + |
| 26 | + def get_loader(self, cls, format=None): |
| 27 | + return next( |
| 28 | + iter( |
| 29 | + [ |
| 30 | + fn |
| 31 | + for ((fmt, cls_), fn) in self._loaders.items() |
| 32 | + if fmt == format and issubclass(cls, cls_) |
| 33 | + ] |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + def get_writer(self, cls, format=None): |
| 38 | + return next( |
| 39 | + iter( |
| 40 | + [ |
| 41 | + fn |
| 42 | + for ((fmt, cls_), fn) in self._writers.items() |
| 43 | + if fmt == format and issubclass(cls, cls_) |
| 44 | + ] |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + def register_loader(self, cls, format, function): |
| 49 | + if format in self._loaders: |
| 50 | + raise ValueError(f"Loader for format {format} already registered.") |
| 51 | + self._loaders[cls, format] = (cls, function) |
| 52 | + |
| 53 | + def register_writer(self, cls, format, function): |
| 54 | + if format in self._writers: |
| 55 | + raise ValueError(f"Writer for format {format} already registered.") |
| 56 | + self._writers[cls, format] = (cls, function) |
| 57 | + |
| 58 | + def load(self, cls, *args, format=None, **kwargs): |
| 59 | + return self.get_loader(cls, format)(*args, **kwargs) |
| 60 | + |
| 61 | + def write(self, cls, *args, format=None, **kwargs): |
| 62 | + return self.get_writer(cls, format)(*args, **kwargs) |
| 63 | + |
| 64 | + |
| 65 | +DEFAULT_REGISTRY = Registry() |
| 66 | + |
| 67 | + |
| 68 | +Op = Literal["load", "write"] |
| 69 | + |
| 70 | + |
| 71 | +class IO(property): |
| 72 | + """Wrap a file IO descriptor as a property.""" |
| 73 | + |
| 74 | + def __get__(self, instance, owner_cls): |
| 75 | + return self.fget(instance, owner_cls) |
| 76 | + |
| 77 | + |
| 78 | +class IODescriptor: |
| 79 | + """Base class for file IO operations, implemented as descriptors.""" |
| 80 | + |
| 81 | + def __init__(self, instance, cls, op: Op, registry: Registry | None = None): |
| 82 | + self._registry = registry or DEFAULT_REGISTRY |
| 83 | + self._instance = instance |
| 84 | + self._cls = cls |
| 85 | + self._op: Op = op |
| 86 | + |
| 87 | + @property |
| 88 | + def registry(self): |
| 89 | + return self._registry |
| 90 | + |
| 91 | + def list_formats(self, out=None): |
| 92 | + formats = self._registry.get_formats(self._cls, self._op) |
| 93 | + |
| 94 | + if out is None: |
| 95 | + formats.pprint(max_lines=-1, max_width=-1) |
| 96 | + else: |
| 97 | + out.write("\n".join(formats.pformat(max_lines=-1, max_width=-1))) |
| 98 | + |
| 99 | + return out |
| 100 | + |
| 101 | + |
| 102 | +class Loader(IODescriptor): |
| 103 | + """Descriptor for loading data from file.""" |
| 104 | + |
| 105 | + def __init__(self, instance, cls): |
| 106 | + super().__init__(instance, cls, "load", registry=None) |
| 107 | + |
| 108 | + def __call__(self, *args, **kwargs) -> None: |
| 109 | + return self.registry.load(self._cls, *args, **kwargs) |
| 110 | + |
| 111 | + |
| 112 | +class Writer(IODescriptor): |
| 113 | + """Descriptor for writing data to file.""" |
| 114 | + |
| 115 | + def __init__(self, instance, cls): |
| 116 | + super().__init__(instance, cls, "write", registry=None) |
| 117 | + |
| 118 | + def __call__(self, *args, **kwargs) -> None: |
| 119 | + return self.registry.write(self._cls, *args, **kwargs) |
0 commit comments