Skip to content

hkb_editor.templates.context

TemplateContext

Bases: CommonActionsMixin

Stores information and provides helper functions for temapltes.

Templates are python scripts with a run function that takes a TemplateContext as their first argument. This object should be the main way of modifying the behavior from templates, primarily to give proper support for undo (or rollback in case of errors).

Note that this class inherits from CommonActionsMixin and thus provides many convenience functions for common tasks like creating CMSGs and the likes.

Raises:

Type Description
SyntaxError

If the template does not contain valid python code.

ValueError

If the template is not a valid template file.

array_add

array_add(record: HkbRecord | str, path: str, item: Any) -> int

Append a value to an array field of the specified record.

Note that when appending to pointer arrays you need to pass an object ID, not an actual object.

Parameters:

Name Type Description Default
record HkbRecord | str

The record holding the array.

required
path str

Path to the array within the record, with deeper levels separated by /.

required
item Any

The item to append to the array.

required

Returns:

Type Description
The index of the item in the array.

array_pop

array_pop(record: HkbRecord | str, path: str, index: int = -1) -> Any

Remove a value from an array inside a record.

Parameters:

Name Type Description Default
record HkbRecord | str

The record holding the array.

required
path str

Path to the array within the record, with deeper levels separated by /.

required
index int

The index of the item to pop.

-1

Returns:

Type Description
Any

The value that was removed from the array.

delete

delete(record: HkbRecord | str) -> HkbRecord

Delete the specified HkbRecord from the behavior.

Parameters:

Name Type Description Default
record HkbRecord | str

The record to delete.

required

Returns:

Type Description
HkbRecord

The record that was deleted.

find

find(*query: str, default: Any = _undefined, start_from: HkbRecord | str = None) -> HkbRecord

Returns the first object matching the specified query.

Parameters:

Name Type Description Default
query str

The query string. See hkb_editor.hkb.tagfile.Tagfile.query for details.

()
default Any

The value to return if no match is found.

_undefined
start_from HkbRecord | str

Only search part of the hierarchy starting at this node.

None

Raises:

Type Description
KeyError

If no match was found and no default was provided.

Returns:

Type Description
HkbRecord

A matching HkbRecord object.

find_all

find_all(*query: str, start_from: HkbRecord | str = None) -> list[HkbRecord]

Returns all objects matching the specified query.

Parameters:

Name Type Description Default
query str

The query string. See hkb_editor.hkb.tagfile.Tagfile.query for details.

()
start_from HkbRecord | str

Only search part of the hierarchy starting at this node.

None

Returns:

Type Description
list[HkbRecord]

A list of matching HkbRecord objects.

get

get(record: HkbRecord | str, path: str, default: Any = None) -> Any

Retrieve a value from the specified HkbRecord.

Parameters:

Name Type Description Default
record HkbRecord | str

The record to retrieve a value from.

required
path str

Member path to the value of interest with deeper levels separated by /.

required
default Any

A default to return if the path doesn't exist.

None

Raises:

Type Description
KeyError

If the specified path does not exist.

Returns:

Type Description
Any

The value resolved to a regular type (non-recursive).

set

set(record: HkbRecord | str, **attributes) -> None

Update a one or more fields of the specified HkbRecord.

Parameters:

Name Type Description Default
record HkbRecord | str

The record to update.

required
attributes dict[str, Any]

Keyword arguments of fields and values to set.

{}