koji_habitude.templates

koji_habitude.template

Template loading and Jinja2 expansion system for koji object templates.

author:

Christopher O’Brien <obriencj@gmail.com>

license:

GNU General Public License v3

ai-assistant:

Claude 3.5 Sonnet via Cursor

class koji_habitude.templates.MultiTemplate

Bases: Template

A MultiTemplate is a template that generates multiple YAML documents.

model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(_Template__context: Any)

Override this method to perform additional initialization after __init__ and model_construct. This is useful if you want to do some validation that requires the entire model to be initialized.

render_call(call: TemplateCall) → Iterator[Dict[str, Any]]
typename: ClassVar[str] = 'multi'
class koji_habitude.templates.Template(*, __file__: str | None = None, __line__: int | None = None, __trace__: ~typing.List[~typing.Dict[str, ~typing.Any]] | None = <factory>, name: str, type: str | None = None, defaults: ~typing.Dict[str, ~typing.Any] = <factory>, file: str | None = None, content: str | None = None, model: ~koji_habitude.templates.TemplateModelDefinition | None = None, description: str | None = None)

Bases: BaseModel, IdentifiableMixin, LocalMixin

A Template allows for the expansion of some YAML data into zero or more YAML docs, via Jinja2

property base_path: Path | None

The base path for the template file, used for resolving relative paths

defaults: Dict[str, Any]
description: str | None
get_missing()

Return the set of variable names which are referenced in the Jinja2 template, but which are not defined in the defaults

property jinja2_template: Template

Access the Jinja2 template object

model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(_Template__context: Any)

Override this method to perform additional initialization after __init__ and model_construct. This is useful if you want to do some validation that requires the entire model to be initialized.

render(data: Dict[str, Any]) → str

Render the template with the given data into a str

render_and_load(data: Dict[str, Any]) → Iterator[Dict[str, Any]]

Render the template with the given data and yield the resulting YAML documents

render_call(call: TemplateCall)
template_content: str | None
template_file: str | None
template_model: TemplateModelDefinition | None
typename: ClassVar[str] = 'template'
property undeclared

The list of variable names which are referenced in the Jinja2 template, but which are not defined in the defaults

validate_call(data: Dict[str, Any]) → bool

Validate the call data against the template model if configured.

class koji_habitude.templates.TemplateCall(*, __file__: str | None = None, __line__: int | None = None, __trace__: ~typing.List[~typing.Dict[str, ~typing.Any]] | None = <factory>, type: str)

Bases: DataMixin, BaseModel

Represents a YAML doc that needs to be expanded into zero or more new docs via a Template.

model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(context: Any, /) → None

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that’s what pydantic-core passes when calling it.

Parameters:
  • self – The BaseModel instance.

  • context – The context.

property template_name: str
typename: ClassVar[str] = 'template'
yaml_type: str
class koji_habitude.templates.TemplateFieldDefinition(*, type: str, alias: str | None = None, description: str | None = None, default: Any | None = None, required: bool = True, validation: ValidationRule | None = None, array_item_type: TemplateFieldDefinition | None = None, object_fields: Dict[str, TemplateFieldDefinition] | None = None, object_type_name: str | None = None, reference_type: str | None = None)

Bases: SubModel

Defines a Field in a TemplateModel

alias: str | None
array_item_type: TemplateFieldDefinition | None
default: Any | None
property definition: Tuple[Type, FieldInfo]
description: str | None
model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(_TemplateFieldDefinition__context: Any)

Override this method to perform additional initialization after __init__ and model_construct. This is useful if you want to do some validation that requires the entire model to be initialized.

object_fields: Dict[str, TemplateFieldDefinition] | None
object_type_name: str | None
reference_type: str | None
required: bool
type: str
validation: ValidationRule | None
class koji_habitude.templates.TemplateModelDefinition(*, name: str = 'model', description: str | None = None, fields: ~typing.Dict[str, ~koji_habitude.templates.TemplateFieldDefinition] = <factory>)

Bases: SubModel

YAML definition for creating a Pydantic model for template data validation

description: str | None
fields: Dict[str, TemplateFieldDefinition]
get_model_class() → Type[BaseModel]

Get the model class for the template model.

model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_post_init(_TemplateModelDefinition__context: Any)

Override this method to perform additional initialization after __init__ and model_construct. This is useful if you want to do some validation that requires the entire model to be initialized.

name: str
new(data: Dict[str, Any]) → BaseModel

Create a new instance of the template model from the given data.

class koji_habitude.templates.ValidationRule(*, min_length: int | None = None, max_length: int | None = None, min_value: float | None = None, max_value: float | None = None, regex: Pattern | None = None, enum: List[Any] | None = None)

Bases: SubModel

Validation rules for a DynamicFieldDefinition

as_field_validator(field_name: str) → Callable
enum_values: List[Any] | None
max_length: int | None
max_value: float | None
min_length: int | None
min_value: float | None
model_config: ClassVar[ConfigDict] = {'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

pattern: Pattern | None
classmethod validate_pattern(value: Any) → Pattern