From ead1b4bc285e77bce9dfcdcccac5a164c3407225 Mon Sep 17 00:00:00 2001 From: Rubel Date: Fri, 1 Dec 2023 09:25:27 +0100 Subject: [PATCH] ci/cd --- pynxtools/dataconverter/convert.py | 34 ++++++++++++++++++++++-------- pynxtools/dataconverter/helpers.py | 12 +++++++---- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/pynxtools/dataconverter/convert.py b/pynxtools/dataconverter/convert.py index 508c0c761..29dc3d427 100644 --- a/pynxtools/dataconverter/convert.py +++ b/pynxtools/dataconverter/convert.py @@ -36,14 +36,29 @@ from pynxtools.dataconverter.logger import logger as pynx_logger +if sys.version_info >= (3, 10): + from importlib.metadata import entry_points +else: + from importlib_metadata import entry_points + def get_reader(reader_name) -> BaseReader: """Helper function to get the reader object from it's given name""" 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: + # pylint: disable=unexpected-keyword-arg + importlib_module = entry_points(group='pynxtools.reader') + if ( + importlib_module + and reader_name in map(lambda ep: ep.name, entry_points(group='pynxtools.reader')) + ): + return importlib_module[reader_name].load() + raise ValueError(f"The reader, {reader_name}, was not found.") from exc return module.READER # type: ignore[attr-defined] @@ -158,7 +173,7 @@ def transfer_data_into_template(input_file, return data -# pylint: disable=too-many-arguments,too-many-locals +# pylint: disable=too-many-arguments, W1203 def convert(input_file: Tuple[str, ...], reader: str, nxdl: str, @@ -211,12 +226,13 @@ def convert(input_file: Tuple[str, ...], if fair and data.undocumented.keys(): logger.warning("There are undocumented paths in the template. This is not acceptable!") return - for path in data.undocumented.keys(): - if "/@default" in path: - continue - logger.info( - f"NO DOCUMENTATION: The path, {path} is being written but has no documentation." - ) + if undocumented: + for path in data.undocumented.keys(): + if "/@default" in path: + continue + logger.info( + f"NO DOCUMENTATION: The path, {path} is being written but has no documentation." + ) helpers.add_default_root_attributes(data=data, filename=os.path.basename(output)) Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write() diff --git a/pynxtools/dataconverter/helpers.py b/pynxtools/dataconverter/helpers.py index f5621aff1..8c3c3e45d 100644 --- a/pynxtools/dataconverter/helpers.py +++ b/pynxtools/dataconverter/helpers.py @@ -33,7 +33,7 @@ from pynxtools.nexus import nexus from pynxtools.nexus.nexus import NxdlAttributeError -logger = logging.getLogger(__name__) +pynx_logger = logging.getLogger(__name__) def is_a_lone_group(xml_element) -> bool: @@ -447,7 +447,9 @@ def does_group_exist(path_to_group, data): return False -def ensure_all_required_fields_exist(template, data, nxdl_root, logger): +# pylint: disable=W1203 +def ensure_all_required_fields_exist(template, data, + nxdl_root, logger=pynx_logger): """Checks whether all the required fields are in the returned data object.""" for path in template["required"]: entry_name = get_name_from_data_dict_entry(path[path.rindex('/') + 1:]) @@ -498,7 +500,9 @@ def try_undocumented(data, nxdl_root: ET.Element): pass -def validate_data_dict(template, data, nxdl_root: ET.Element, logger): +def validate_data_dict(template, data, + nxdl_root: ET.Element, + logger=pynx_logger): """Checks whether all the required paths from the template are returned in data dict.""" assert nxdl_root is not None, "The NXDL file hasn't been loaded." @@ -590,7 +594,7 @@ def convert_to_hill(atoms_typ): return atom_list + list(atoms_typ) -def add_default_root_attributes(data, filename): +def add_default_root_attributes(data, filename, logger=pynx_logger): """ Takes a dict/Template and adds NXroot fields/attributes that are inherently available """