diff --git a/src/newcrawler/converters.py b/src/newcrawler/converters.py index c8172a1575a6309b6919f24bce04699efd63a273..79ee17dcc6232d214144a19b9a79d6948b45e3ab 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 9bb5f1639791579099694025005a557cd74084bb..1b4e8784a69d1ad1b80fa757ad77cd137c8cc7b5 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 5b3194432468b7119a2e98da13a03147ece57ff4..0df761d400524b1883b3bde31882b205940888e7 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"