diff --git a/src/caoscrawler/macros/macro_yaml_object.py b/src/caoscrawler/macros/macro_yaml_object.py index 5c776f039615032e3ccf3e79027cce56797ba53e..056152c9278e0b5e97749bf91523f192a53fc45f 100644 --- a/src/caoscrawler/macros/macro_yaml_object.py +++ b/src/caoscrawler/macros/macro_yaml_object.py @@ -134,4 +134,9 @@ def multimacro_constructor(loader, node): It can be registered in pyaml using: yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor) """ - pass + + res = dict() + for val in node.value: + res.update(macro_constructor(loader, val)) + + return res diff --git a/unittests/test_macros.py b/unittests/test_macros.py index b6dbc16b67db6201ab5bcda9b803e4aac7a34f8e..dfb4bd0a7854e65847d5224641589b5e1a8e04bb 100644 --- a/unittests/test_macros.py +++ b/unittests/test_macros.py @@ -22,7 +22,10 @@ # ** end header # -from caoscrawler.macros import defmacro_constructor, macro_constructor, multimacro_constructor +from caoscrawler.macros import (defmacro_constructor, + macro_constructor, + multimacro_constructor) +from caoscrawler.macros.macro_yaml_object import macro_store import yaml import pytest @@ -32,7 +35,11 @@ def register_macros(): yaml.SafeLoader.add_constructor("!macro", macro_constructor) yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor) -def test_macros(register_macros): +@pytest.fixture +def macro_store_reset(): + macro_store.clear() + +def test_macros(register_macros, macro_store_reset): dat = yaml.load(""" defs: - !defmacro @@ -58,7 +65,7 @@ testnode: assert "expanded_bla" not in dat["testnode"]["obl"] assert "bla" not in dat["testnode"]["obl"]["expanded_yea"] -def test_macro_list_replacment(register_macros): +def test_macro_list_replacment(register_macros, macro_store_reset): dat = yaml.load(""" defs: - !defmacro @@ -84,3 +91,89 @@ testnode: assert len(dat["testnode"]["obl"]["expanded_yea"]["blubb"]) == 2 assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][0] == "ok4" assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][1]["yea"] == "$variable" + +def test_multi_macros(register_macros, macro_store_reset): + dat = yaml.load(""" +defs: +- !defmacro + name: test_one + params: {} + definition: + replaced1: ok +- !defmacro + name: test_two + params: {} + definition: + replaced2: ok + replaced3: ok + +testnode: + obl: !multimacro + - name: test_one + - name: test_two +""", Loader=yaml.SafeLoader) + print(yaml.dump(dat)) + assert dat["testnode"]["obl"]["replaced1"] == "ok" + assert dat["testnode"]["obl"]["replaced2"] == "ok" + assert dat["testnode"]["obl"]["replaced3"] == "ok" + +@pytest.mark.xfail +def test_multi_macros_toplevel(register_macros, macro_store_reset): + """ + See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/23 + """ + dat = yaml.load(""" +defs: +- !defmacro + name: test_one + params: {} + definition: + replaced1: ok +- !defmacro + name: test_two + params: {} + definition: + replaced2: ok + replaced3: ok + +testnode: !multimacro + - name: test_one + - name: test_two +""", Loader=yaml.SafeLoader) + assert dat["testnode"]["replaced1"] == "ok" + assert dat["testnode"]["replaced2"] == "ok" + assert dat["testnode"]["replaced3"] == "ok" + + +@pytest.mark.xfail +def test_replace_arbitrary_objects(register_macros, macro_store_reset): + """ + See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/24 + """ + dat = yaml.load(""" +defs: +- !defmacro + name: test + params: + b: 25 + testvar_list: + - a + - $b + testvar_dict: + t1: a + t2: $b + definition: + replaced1: + $b: ok + c: $testvar_dict + d: $testvar_list + +testnode: + obl: !macro + name: test +""", Loader=yaml.SafeLoader) + print(yaml.dump(dat)) + assert dat["testnode"]["obl"]["replaced1"]["c"]["t1"] == "a" + assert dat["testnode"]["obl"]["replaced1"]["c"]["t2"] == "25" + assert dat["testnode"]["obl"]["replaced1"]["d"][0] == "a" + assert dat["testnode"]["obl"]["replaced1"]["d"][1] == "25"