Skip to content

Commit

Permalink
Improvements to Jinja context helpers
Browse files Browse the repository at this point in the history
add _ctx_ as a short alias for __utils__
add get and set functions as aliases of get_var set_var
make get have a default for non pre-existing variables
make set return the value it is setting for jinja render purposes
make all variables created with get/set directly available in the context
  • Loading branch information
gilesknap committed May 29, 2024
1 parent 67e4ddf commit b71d51c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/ibek/gen_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def create_boot_script(entities: Sequence[Entity]) -> str:
renderer = Render()

return template.render(
# old name for global context for backward compatibility
__utils__=UTILS,
# new short name for global context
_ctx_=UTILS,
# put variables created with set/get directly in the context
**UTILS.variables,
env_var_elements=renderer.render_environment_variable_elements(entities),
script_elements=renderer.render_pre_ioc_init_elements(entities),
post_ioc_init_elements=renderer.render_post_ioc_init_elements(entities),
Expand Down
2 changes: 1 addition & 1 deletion src/ibek/render_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def add_database(self, database: Database, entity: Entity) -> None:

for arg, value in database.args.items():
if value is None:
if arg not in entity.__dict__:
if arg not in entity.__dict__ and arg not in UTILS.variables:
raise ValueError(
f"database arg '{arg}' in database template "
f"'{database.file}' not found in context"
Expand Down
23 changes: 20 additions & 3 deletions src/ibek/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def increment(self, count: int):
f"Counter {self.current} exceeded stop value of {self.stop}"
)

def __repr__(self) -> str:
return str(self.current)

def __str__(self) -> str:
return str(self.current)


class Utils:
"""
Expand All @@ -43,6 +49,10 @@ def __init__(self: "Utils"):
self.ioc_name: str = ""
self.__reset__()

# old names for backward compatibility
self.set_var = self.set
self.get_var = self.get

def __reset__(self: "Utils"):
"""
Reset all saved state. For use in testing where more than one
Expand All @@ -69,13 +79,15 @@ def get_env(self, key: str) -> str:
"""
return os.environ.get(key, "")

def set_var(self, key: str, value: Any):
def set(self, key: str, value: Any) -> Any:
"""create a global variable for our jinja context"""
self.variables[key] = value
return value

def get_var(self, key: str) -> Any:
def get(self, key: str, default="") -> Any:
"""get the value a global variable for our jinja context"""
return self.variables.get(key, "")
# default is used to set an initial value if the variable is not set
return self.variables.get(key, default)

def counter(
self, name: str, start: int = 0, stop: int = 65535, inc: int = 1
Expand Down Expand Up @@ -113,7 +125,12 @@ def render(self, context: Any, template_text: Any) -> str:
jinja_template = Template(template_text, undefined=StrictUndefined)
return jinja_template.render(
context,
# old name for global context for backward compatibility
__utils__=self,
# new short name for global context
_ctx_=self,
# put variables created with set/get directly in the context
**self.variables,
ioc_yaml_file_name=self.file_name,
ioc_name=self.ioc_name,
)
Expand Down

0 comments on commit b71d51c

Please sign in to comment.