From 188ca288188cfebf72405890d3988ed66602c29f Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <alexander.schlemmer@ds.mpg.de> Date: Thu, 12 May 2022 13:59:36 +0200 Subject: [PATCH] FIX: variable substitution with referenced properties was not possible --- src/newcrawler/converters.py | 10 +++++++++- .../example_substitutions/substitutions.yml | 10 ++++++++++ unittests/test_variable_substitutions.py | 15 ++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/newcrawler/converters.py b/src/newcrawler/converters.py index c8172a15..79ee17dc 100644 --- a/src/newcrawler/converters.py +++ b/src/newcrawler/converters.py @@ -91,9 +91,17 @@ def handle_value(value: Union[dict, str], values: GeneralStore): propvalue = value return (propvalue, collection_mode) + # Check if the replacement is a single variable containing a record: + match = re.match(r"^\$(\{)?(?P<varname>[0-9a-zA-Z_]+)(\})?$", propvalue) + if match is not None: + varname = match.group("varname") + if varname in values: + if isinstance(values[varname], db.Entity): + propvalue = values[varname] + return (propvalue, collection_mode) + propvalue_template = Template(propvalue) propvalue = propvalue_template.safe_substitute(**values.get_storage()) - return (propvalue, collection_mode) diff --git a/unittests/test_directories/example_substitutions/substitutions.yml b/unittests/test_directories/example_substitutions/substitutions.yml index 9bb5f163..1b4e8784 100644 --- a/unittests/test_directories/example_substitutions/substitutions.yml +++ b/unittests/test_directories/example_substitutions/substitutions.yml @@ -2,6 +2,9 @@ ExperimentalData: # name of the converter type: Directory match: ExperimentalData + records: + Project: + name: project subtree: File: # name of the converter type: SimpleFile @@ -9,4 +12,11 @@ ExperimentalData: # name of the converter records: Experiment: date: 20$year-$month-$day + + ExperimentSeries: + Experiment: $Experiment + + Project: + Experiments: +$Experiment + dates: +20$year-$month-$day diff --git a/unittests/test_variable_substitutions.py b/unittests/test_variable_substitutions.py index 5b319443..0df761d4 100644 --- a/unittests/test_variable_substitutions.py +++ b/unittests/test_variable_substitutions.py @@ -40,6 +40,15 @@ def crawler(): def test_substitutions(crawler): - subd = crawler.debug_tree[dircheckstr("File", "ExperimentalData", "220512_data.dat")] - subd[1]["Experiment"].get_property("date") == "2022-05-12" - subd[1]["Experiment"].get_property("date") == "2022-05-12" + for i in range(2): + subd = crawler.debug_tree[dircheckstr("File", "ExperimentalData", "220512_data.dat")] + assert subd[i]["Experiment"].get_property("date").value == "2022-05-12" + assert isinstance(subd[i]["ExperimentSeries"].get_property("Experiment").value, db.Record) + + subd = crawler.debug_tree[dircheckstr("Directory", "ExperimentalData")] + assert subd[i]["Project"].name == "project" + assert isinstance(subd[i]["Project"].get_property("Experiments").value, list) + assert isinstance(subd[i]["Project"].get_property("Experiments").value[0], db.Record) + + assert isinstance(subd[i]["Project"].get_property("dates").value, list) + assert subd[i]["Project"].get_property("dates").value[0] == "2022-05-12" -- GitLab