From 963d2f538d66bab0ef147f52c03b0697e151fcbf Mon Sep 17 00:00:00 2001 From: Florian Spreckelsen <f.spreckelsen@indiscale.com> Date: Mon, 25 Mar 2024 12:50:41 +0100 Subject: [PATCH] WIP: Move to separate converter --- src/caoscrawler/converters.py | 26 ++++++++++++++++++++++++-- src/caoscrawler/default_converters.yml | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/caoscrawler/converters.py b/src/caoscrawler/converters.py index bc30c7d4..7d994377 100644 --- a/src/caoscrawler/converters.py +++ b/src/caoscrawler/converters.py @@ -810,7 +810,29 @@ class DictElementConverter(Converter): raise RuntimeError("Element must be a DictElement.") return match_name_and_value(self.definition, element.name, element.value) - def create_records(self, values: GeneralStore, records: RecordStore, element: StructureElement): + +class HeuristicDictConverter(DictElementConverter): + """Extend the :py:class:`DictElementConverter` by a heuristic to set + property values from the dictionary keys. + + """ + def _validate_definition(definition: dict, name: str): + + if "record_from_dict" not in definition or definition["record_from_dict"] is None: + + raise ValueError( + "You need to specify the (root) record, the properties of " + f"which will be set from the dict in converter {name}." + ) + + def __init__(self, definition: dict, name: str, converter_registry: dict): + + _validate_definition(definition) + super().__init__(definition, name, converter_registry) + + def create_records(self, values: GeneralStore, records: RecordStore, + element: StructureElement, referenced_record_callback: + Optional[callable] = None): keys_modified = [] if "record_from_dict" in self.definition and self.definition["record_from_dict"] is not None: @@ -826,7 +848,7 @@ class DictElementConverter(Converter): class DictConverter(DictElementConverter): def __init__(self, *args, **kwargs): warnings.warn(DeprecationWarning( - "This class is depricated. Please use DictConverter.")) + "This class is depricated. Please use DictElementConverter.")) super().__init__(*args, **kwargs) diff --git a/src/caoscrawler/default_converters.yml b/src/caoscrawler/default_converters.yml index e192ab1b..98c7345d 100644 --- a/src/caoscrawler/default_converters.yml +++ b/src/caoscrawler/default_converters.yml @@ -11,6 +11,9 @@ Date: Dict: converter: DictElementConverter package: caoscrawler.converters +DictHeuristic: + converter: HeuristicDictConverter + package: caoscrawler.converters FloatElement: converter: FloatElementConverter package: caoscrawler.converters -- GitLab