From 266c2c9a072bf3289fd7f2d75278b7d59528378c Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Sat, 24 Dec 2022 10:27:09 +0100 Subject: package restructuring: base * Reader and Extractor to respective reader/extractor modules * ReaderBuilder to reader module * ExtractorBuilder to extractor module * Loading module in utils (safe_load, unpack_name) * Pipeline and PipelineBuilder to lib module * errors to utils * documentation: "standard import" and "external import" --- bsie/utils/loading.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 bsie/utils/loading.py (limited to 'bsie/utils/loading.py') diff --git a/bsie/utils/loading.py b/bsie/utils/loading.py new file mode 100644 index 0000000..eb05c35 --- /dev/null +++ b/bsie/utils/loading.py @@ -0,0 +1,54 @@ +""" + +Part of the bsie module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# standard imports +import importlib +import typing + +# inner-module imports +from . import errors + +# exports +__all__: typing.Sequence[str] = ( + 'safe_load', + 'unpack_qualified_name', + ) + + +## code ## + +def safe_load(module_name: str, class_name: str): + """Get a class from a module. Raise BuilderError if anything goes wrong.""" + try: + # load the module + module = importlib.import_module(module_name) + except Exception as err: + # cannot import module + raise errors.LoaderError(f'cannot load module {module_name}') from err + + try: + # get the class from the module + cls = getattr(module, class_name) + except Exception as err: + # cannot find the class + raise errors.LoaderError(f'cannot load class {class_name} from module {module_name}') from err + + return cls + + +def unpack_qualified_name(name): + """Split a name into its module and class component (dot-separated).""" + if not isinstance(name, str): + raise TypeError(name) + if '.' not in name: + raise ValueError('name must be a qualified class name.') + module_name, class_name = name[:name.rfind('.')], name[name.rfind('.')+1:] + if module_name == '': + raise ValueError('name must be a qualified class name.') + return module_name, class_name + + +## EOF ## -- cgit v1.2.3 From 4b5c4d486bb4f0f4da2e25ad464e8336a781cdcb Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 22:31:03 +0100 Subject: removed module header stubs --- bsie/utils/loading.py | 5 ----- 1 file changed, 5 deletions(-) (limited to 'bsie/utils/loading.py') diff --git a/bsie/utils/loading.py b/bsie/utils/loading.py index eb05c35..3c5c7c1 100644 --- a/bsie/utils/loading.py +++ b/bsie/utils/loading.py @@ -1,9 +1,4 @@ -""" -Part of the bsie module. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import importlib import typing -- cgit v1.2.3 From 8b460aa0232cd841af7b7734c91982bc83486e03 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Sun, 5 Mar 2023 19:14:11 +0100 Subject: build fixes --- bsie/utils/loading.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bsie/utils/loading.py') diff --git a/bsie/utils/loading.py b/bsie/utils/loading.py index 3c5c7c1..58202d1 100644 --- a/bsie/utils/loading.py +++ b/bsie/utils/loading.py @@ -22,14 +22,14 @@ def safe_load(module_name: str, class_name: str): module = importlib.import_module(module_name) except Exception as err: # cannot import module - raise errors.LoaderError(f'cannot load module {module_name}') from err + raise errors.LoaderError(f'cannot load module {module_name} ({err})') from err try: # get the class from the module cls = getattr(module, class_name) except Exception as err: # cannot find the class - raise errors.LoaderError(f'cannot load class {class_name} from module {module_name}') from err + raise errors.LoaderError(f'cannot load class {class_name} from module {module_name} ({err})') from err return cls -- cgit v1.2.3