Skip to content

Commit

Permalink
Allow loading of plugins via entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
domna committed Nov 28, 2023
1 parent 76f305e commit 4f7cd0a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
from pynxtools.dataconverter.template import Template
from pynxtools.nexus import nexus

if sys.version_info.major >= 3 and sys.version_info.minor > 8:
from importlib.metadata import entry_points
else:
from importlib_metadata import entry_points


logger = logging.getLogger(__name__) # pylint: disable=C0103
UNDOCUMENTED = 9
Expand All @@ -46,8 +51,15 @@ def get_reader(reader_name) -> BaseReader:
path_prefix = f"{os.path.dirname(__file__)}{os.sep}" if os.path.dirname(__file__) else ""
path = os.path.join(path_prefix, "readers", reader_name, "reader.py")
spec = importlib.util.spec_from_file_location("reader.py", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore[attr-defined]
try:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore[attr-defined]
except FileNotFoundError as exc:
importlib_module = entry_points(group=f'pynxtools.reader.{reader_name}')
if importlib_module:
return importlib_module['reader'].load()
else:
raise ValueError(f"The reader, {reader_name}, was not found.") from exc
return module.READER # type: ignore[attr-defined]


Expand Down

0 comments on commit 4f7cd0a

Please sign in to comment.