diff --git a/pynxtools/dataconverter/readers/rii_database/reader.py b/pynxtools/dataconverter/readers/rii_database/reader.py index ae36b3884..32fb7c5fa 100644 --- a/pynxtools/dataconverter/readers/rii_database/reader.py +++ b/pynxtools/dataconverter/readers/rii_database/reader.py @@ -17,13 +17,12 @@ # """Convert refractiveindex.info yaml files to nexus""" from typing import Tuple, Any, Dict -import logging from pynxtools.dataconverter.readers.json_yml.reader import YamlJsonReader from pynxtools.dataconverter.readers.rii_database.dispersion_reader import ( DispersionReader, ) -from pynxtools.dataconverter.readers.utils import parse_json +from pynxtools.dataconverter.readers.utils import parse_json, handle_objects class RiiReader(YamlJsonReader): @@ -40,7 +39,7 @@ def __init__(self, *args, **kwargs): ".yaml": self.read_dispersion, ".json": self.parse_json_w_fileinfo, "default": lambda _: self.appdef_defaults(), - "objects": self.handle_objects, + "objects": self.handle_rii_objects, } def read_dispersion(self, filename: str): @@ -86,20 +85,9 @@ def parse_json_w_fileinfo(self, filename: str) -> Dict[str, Any]: return template - def handle_objects(self, objects: Tuple[Any]) -> Dict[str, Any]: + def handle_rii_objects(self, objects: Tuple[Any]) -> Dict[str, Any]: """Handle objects and generate template entries from them""" - if objects is None: - return {} - - template = {} - - for obj in objects: - if not isinstance(obj, dict): - logging.warning("Ignoring unknown object of type %s", type(obj)) - continue - - template.update(obj) - + template = handle_objects(objects) self.fill_dispersion_in(template) return template diff --git a/pynxtools/dataconverter/readers/transmission/reader.py b/pynxtools/dataconverter/readers/transmission/reader.py index 3d4f0e152..ccc94374e 100644 --- a/pynxtools/dataconverter/readers/transmission/reader.py +++ b/pynxtools/dataconverter/readers/transmission/reader.py @@ -22,7 +22,7 @@ from pynxtools.dataconverter.readers.json_yml.reader import YamlJsonReader import pynxtools.dataconverter.readers.transmission.metadata_parsers as mpars -from pynxtools.dataconverter.readers.utils import parse_json, parse_yml +from pynxtools.dataconverter.readers.utils import parse_json, parse_yml, handle_objects # Dictionary mapping metadata in the asc file to the paths in the NeXus file. @@ -254,6 +254,7 @@ class TransmissionReader(YamlJsonReader): ".yml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED), ".yaml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED), "default": lambda _: add_def_info(), + "objects": handle_objects, } diff --git a/pynxtools/dataconverter/readers/utils.py b/pynxtools/dataconverter/readers/utils.py index 23fbfbdd9..c1826d744 100644 --- a/pynxtools/dataconverter/readers/utils.py +++ b/pynxtools/dataconverter/readers/utils.py @@ -16,12 +16,15 @@ # limitations under the License. # """Utility functions for the NeXus reader classes.""" +import logging from dataclasses import dataclass, replace -from typing import List, Any, Dict, Optional +from typing import List, Any, Dict, Optional, Tuple from collections.abc import Mapping import json import yaml +logger = logging.getLogger(__name__) + @dataclass class FlattenSettings(): @@ -201,3 +204,20 @@ def parse_json(file_path: str) -> Dict[str, Any]: """ with open(file_path, "r", encoding="utf-8") as file: return json.load(file) + + +def handle_objects(objects: Tuple[Any]) -> Dict[str, Any]: + """Handle objects and generate template entries from them""" + if objects is None: + return {} + + template = {} + + for obj in objects: + if not isinstance(obj, dict): + logger.warning("Ignoring unknown object of type %s", type(obj)) + continue + + template.update(obj) + + return template