Skip to content

Commit

Permalink
Add support for a simpler extra_vars option for register_template
Browse files Browse the repository at this point in the history
  • Loading branch information
linsword13 committed Jan 9, 2025
1 parent ef0296c commit a82f624
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/ramble/ramble/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,9 @@ def _render_object_templates(self, extra_vars):
src_path = tpl_config["src_path"]
with open(src_path) as f_in:
content = f_in.read()
extra_vars_wm = tpl_config.get("extra_vars")
if extra_vars_wm is not None:
extra_vars.update(extra_vars_wm)
extra_vars_func_name = tpl_config.get("extra_vars_func_name")
if extra_vars_func_name is not None:
extra_vars_func = getattr(obj, extra_vars_func_name)
Expand Down
6 changes: 6 additions & 0 deletions lib/ramble/ramble/language/shared_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def register_template(
src_name: str,
dest_name: str,
define_var: bool = True,
extra_vars: Optional[dict] = None,
extra_vars_func: Optional[str] = None,
output_perm=None,
):
Expand All @@ -505,8 +506,12 @@ def register_template(
dest_name: The leaf name of the rendered output under the experiment
run directory.
define_var: Controls if a variable named `name` should be defined.
extra_vars: If present, the variable dict is used as extra variables to
render the template.
extra_vars_func: If present, the name of the function to call to return
a dict of extra variables used to render the template.
This option is combined together with and takes precedence
over `extra_vars`, if both are present.
output_perm: The chmod mask for the rendered output file.
"""

Expand All @@ -517,6 +522,7 @@ def _define_template(obj):
"src_name": src_name,
"dest_name": dest_name,
"var_name": var_name,
"extra_vars": extra_vars,
"extra_vars_func_name": extra_vars_func_name,
"output_perm": output_perm,
}
Expand Down
2 changes: 2 additions & 0 deletions lib/ramble/ramble/test/end_to_end/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def test_template():
assert os.path.isfile(script_path)
with open(script_path) as f:
content = f.read()
assert "echo foobar" in content
assert "echo hello santa" in content
assert "echo not_exist" not in content
execute_path = os.path.join(run_dir, "execute_experiment")
with open(execute_path) as f:
content = f.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class Template(ExecutableApplication):
name="bar",
src_name="bar.tpl",
dest_name="bar.sh",
# The `dynamic_hello_world` will be overridden by `_bar_vars`
extra_vars={
"dynamic_var1": "foobar",
"dynamic_hello_world": "not_exist",
},
extra_vars_func="bar_vars",
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/bash
echo {dynamic_var1}
echo {dynamic_hello_world}

0 comments on commit a82f624

Please sign in to comment.