-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: support Jinja templating for job args
* Closes #36.
- Loading branch information
Showing
6 changed files
with
230 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Jobs is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Utilities.""" | ||
|
||
import ast | ||
|
||
from jinja2.sandbox import SandboxedEnvironment | ||
|
||
jinja_env = SandboxedEnvironment() | ||
|
||
|
||
def eval_tpl_str(val, ctx): | ||
"""Evaluate a Jinja template string.""" | ||
tpl = jinja_env.from_string(val) | ||
res = tpl.render(**ctx) | ||
|
||
try: | ||
res = ast.literal_eval(res) | ||
except Exception: | ||
pass | ||
|
||
return res | ||
|
||
|
||
def walk_values(obj, transform_fn): | ||
"""Recursively apply a function in-place to the value of dictionary or list.""" | ||
if isinstance(obj, dict): | ||
items = obj.items() | ||
elif isinstance(obj, list): | ||
items = enumerate(obj) | ||
else: | ||
return transform_fn(obj) | ||
|
||
for key, val in items: | ||
if isinstance(val, (dict, list)): | ||
walk_values(val, transform_fn) | ||
else: | ||
obj[key] = transform_fn(val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters