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

add values support (completed) #68

Merged
merged 16 commits into from
May 11, 2023
Merged
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions src/ibek/ioc.py
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
)
from apischema.conversions import Conversion, reset_deserializers
from apischema.metadata import conversion
from jinja2 import Template
from typing_extensions import Annotated as A
from typing_extensions import Literal

@@ -72,9 +73,14 @@ def __post_init__(self: "Entity"):
# add in the global __utils__ object for state sharing
context["__utils__"] = self.__utils__

# todo jinja expand all values with context
# Do Jinja expansion of any string args/values in the context
for arg, value in context.items():
if isinstance(value, str):
jinja_template = Template(value)
rendered = jinja_template.render(context)
context[arg] = rendered

self.__context__ = context
# then pass __context__ to jinja template in render.py


id_to_entity: Dict[str, Entity] = {}
14 changes: 7 additions & 7 deletions src/ibek/render.py
Original file line number Diff line number Diff line change
@@ -22,9 +22,12 @@ def __init__(self: "Render"):

def render_text(self, instance: Entity, text: str, once=False, suffix="") -> str:
"""
render a line of jinja template in ``text`` using the values supplied
in the ``instance`` object. Supports the ``once`` flag to only render
the line once per definitions file.
Add in the next line of text, honouring the ``once`` flag which will
only add the line once per IOC.

Jinja rendering of values/args has already been done in Entity.__post_init__
but we pass all strings though jinja again to render any other jinja
in the IOC (e.g. database and function entries)

``once`` uses the name of the definition + suffix to track which lines
have been rendered already. The suffix can be used where a given
@@ -38,9 +41,6 @@ def render_text(self, instance: Entity, text: str, once=False, suffix="") -> str
else:
return ""

jinja_template = Template(text)
result = jinja_template.render(instance.__context__) # type: ignore

# run the result through jinja again so we can refer to args for arg defaults
# e.g.
#
@@ -49,7 +49,7 @@ def render_text(self, instance: Entity, text: str, once=False, suffix="") -> str
# description: IPAC identifier
# default: "IPAC{{ slot }}"

jinja_template = Template(result)
jinja_template = Template(text)
result = jinja_template.render(instance.__context__) # type: ignore

if result == "":
4 changes: 3 additions & 1 deletion tests/samples/values_test/st.cmd
Original file line number Diff line number Diff line change
@@ -11,7 +11,9 @@ ioc_registerRecordDeviceDriver pdbbase
# Create a new Hy8002 carrier.
# The resulting carrier handle (card id) is saved in an env variable.
ipacAddHy8002 "4, 2"
epicsEnvSet IPAC4 0 1 2
epicsEnvSet IPAC4 0 0 0
ipacAddHy8002 "5, 2"
epicsEnvSet IPAC5 1 1 1

dbLoadRecords /tmp/ioc.db
iocInit
4 changes: 4 additions & 0 deletions tests/samples/values_test/values.ibek.ioc.yaml
Original file line number Diff line number Diff line change
@@ -11,3 +11,7 @@ entities:
- type: ipac.Hy8002
name: IPAC4
slot: 4

- type: ipac.Hy8002
name: IPAC5
slot: 5