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 pre_values and Jinja strict mode #223

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion src/ibek/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ class EntityDefinition(BaseSettings):
description="The arguments IOC instance should supply", default=()
)
values: Sequence[Value] = Field(
description="Calculated values to use as additional arguments", default=()
description="Calculated values to use as additional arguments "
"With Jinja evaluation after all Args",
default=(),
)
pre_values: Sequence[Value] = Field(
description="Calculated values to use as additional arguments "
"With Jinja evaluation before all Args",
default=(),
)
databases: Sequence[Database] = Field(
description="Databases to instantiate", default=[]
Expand Down
22 changes: 16 additions & 6 deletions src/ibek/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from pydantic import create_model, field_validator
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined
from pydantic_core import PydanticUndefined, ValidationError
from ruamel.yaml.main import YAML

from .args import EnumArg, IdArg, ObjectArg
Expand Down Expand Up @@ -41,12 +41,16 @@ def make_entity_models(self, definition_yaml: List[Path]) -> List[Type[Entity]]:
for definition in definition_yaml:
support_dict = YAML(typ="safe").load(definition)

Support.model_validate(support_dict)
try:
Support.model_validate(support_dict)

# deserialize the support module definition file
support = Support(**support_dict)
# make Entity classes described in the support module definition file
self._make_entity_models(support)
# deserialize the support module definition file
support = Support(**support_dict)
# make Entity classes described in the support module definition file
self._make_entity_models(support)
except ValidationError:
print(f"PYDANTIC VALIDATION ERROR IN {definition}")
raise

return list(self._entity_models.values())

Expand All @@ -71,6 +75,12 @@ def add_arg(name, typ, description, default):
# fully qualified name of the Entity class including support module
full_name = f"{support.module}.{definition.name}"

# add in the calculated values Jinja Templates as Fields in the Entity
# these are the pre_values that should be Jinja rendered before any
# Args (or post values)
for value in definition.pre_values:
add_arg(value.name, str, value.description, value.value)

# add in each of the arguments as a Field in the Entity
for arg in definition.args:
full_arg_name = f"{full_name}.{arg.name}"
Expand Down
15 changes: 13 additions & 2 deletions src/ibek/gen_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from typing import List, Sequence, Tuple

from jinja2 import Template
from jinja2 import StrictUndefined, Template

from ibek.utils import UTILS

Expand All @@ -31,7 +31,13 @@ def create_db_script(

templates = renderer.render_database(extra_databases)

return Template(jinja_txt).render(templates=templates)
try:
return Template(jinja_txt).render(
templates=templates, undefined=StrictUndefined
)
except Exception:
print(f"ERROR RENDERING DATABASE TEMPLATE:\n{templates}")
raise


def create_boot_script(entities: Sequence[Entity]) -> str:
Expand All @@ -44,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
13 changes: 2 additions & 11 deletions src/ibek/ioc_cmds/docker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import shutil
import subprocess
from pathlib import Path
from typing import List
Expand Down Expand Up @@ -35,16 +34,8 @@ def handle_command(context: Path, tokens: List[str], step, start):
os.chdir(tokens[1])
elif docker_action == "COPY":
print("COPY", tokens)
src = context / (Path(tokens[1]))
dest = (Path.cwd() / Path(tokens[2])).absolute()
if src == dest:
print("SKIPPING copy of same path")
else:
print(f"copying {src} to {dest}")
if src.is_file():
shutil.copy(src, dest)
else:
shutil.copytree(src, dest, dirs_exist_ok=True)
# skipping because in devcontainer the project folder is already mounted
# where the destination copy is supposed to go inside the container
elif docker_action == "FROM":
if "runtime_prep" in tokens:
print("\n== Aborting before destructive runtime prep stage. ==\n")
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
27 changes: 22 additions & 5 deletions src/ibek/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pathlib import Path
from typing import Any, Dict, Mapping

from jinja2 import Template
from jinja2 import StrictUndefined, Template


@dataclass
Expand All @@ -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 @@ -110,10 +122,15 @@ def render(self, context: Any, template_text: Any) -> str:
return template_text

try:
jinja_template = Template(template_text)
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
Loading
Loading