Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create replace_tmpl filter for Jinja2 #33

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/wxflow/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jinja2
from markupsafe import Markup

from .template import replace_tmpl
from .timetools import (add_to_datetime, strftime, to_fv3time, to_isotime,
to_julian, to_timedelta, to_YMD, to_YMDH)

Expand Down Expand Up @@ -146,6 +147,7 @@ def get_set_env(self, loader: jinja2.BaseLoader, filters: Dict[str, callable] =
if not (isinstance(dt, SilentUndefined) or isinstance(delta, SilentUndefined))
else dt if isinstance(dt, SilentUndefined) else delta)
env.filters["to_timedelta"] = lambda delta_str: to_timedelta(delta_str) if not isinstance(delta_str, SilentUndefined) else delta_str
env.filters["replace_tmpl"] = lambda string, tmpl_dict: replace_tmpl(string, tmpl_dict)

# Add any additional filters
if filters is not None:
Expand Down
2 changes: 1 addition & 1 deletion src/wxflow/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# Create task_config with everything that is inside _config and whatever the user chooses to
# extend it with when initializing a child subclass of Task. Only task_config should be referenced
# in any application, not _config.
self.task_config = self._config.copy()
self.task_config = self._config.deepcopy()

Check warning on line 44 in src/wxflow/task.py

View check run for this annotation

Codecov / codecov/patch

src/wxflow/task.py#L44

Added line #L44 was not covered by tests

# Any other composite runtime variables that may be needed for the duration of the task
# can be constructed here
Expand Down
10 changes: 10 additions & 0 deletions src/wxflow/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@
return True
else:
return False


def replace_tmpl(string, tmpl_dict):
"""
Replace substrings of input string using input dictionary.
"""

for key in tmpl_dict:
string = string.replace(key, tmpl_dict[key])
return string

Check warning on line 201 in src/wxflow/template.py

View check run for this annotation

Codecov / codecov/patch

src/wxflow/template.py#L200-L201

Added lines #L200 - L201 were not covered by tests