Skip to content

Commit

Permalink
feature/import-any-reader-from-readers (#326)
Browse files Browse the repository at this point in the history
* allow reader import

* use forward ref

* fix docs
  • Loading branch information
tlambert03 authored Oct 1, 2021
1 parent 3ff8d7b commit 5ea864b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions aicsimageio/readers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .array_like_reader import ArrayLikeReader # noqa: F401
from .ome_tiff_reader import OmeTiffReader # noqa: F401
from .tiff_reader import TiffReader # noqa: F401
from typing import TYPE_CHECKING, Type

# To add a new reader add it both to TYPE_CHECKING and _READERS

if TYPE_CHECKING:
from .array_like_reader import ArrayLikeReader # noqa: F401
from .bioformats_reader import BioformatsReader # noqa: F401
from .czi_reader import CziReader # noqa: F401
from .lif_reader import LifReader # noqa: F401
from .ome_tiff_reader import OmeTiffReader # noqa: F401
from .reader import Reader
from .tiff_reader import TiffReader # noqa: F401


# add ".relativepath.ClassName"
_READERS = (
".array_like_reader.ArrayLikeReader",
".bioformats_reader.BioformatsReader",
".czi_reader.CziReader",
".lif_reader.LifReader",
".ome_tiff_reader.OmeTiffReader",
".tiff_reader.TiffReader",
)
_LOOKUP = {k.rsplit(".", 1)[-1]: k for k in _READERS}
__all__ = list(_LOOKUP)


def __getattr__(name: str) -> Type["Reader"]:
if name in _LOOKUP:
from importlib import import_module

path, clsname = _LOOKUP[name].rsplit(".", 1)
mod = import_module(path, __name__)
return getattr(mod, clsname)
raise AttributeError(f"module {__name__!r} has no attribute import name {name!r}")

0 comments on commit 5ea864b

Please sign in to comment.