Skip to content

template_helper

Helper functions for templating strings.

apply_template(template_strings)

Apply template to single or multiple strings at once.

Returns:

Type Description
list[str] | str

Templated strings as list or single string.

Source code in client/ayon_comfyui/settings_util/template_helper.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def apply_template(template_strings: str | list[str]) -> list[str] | str:
    """Apply template to single or multiple strings at once.

    Returns:
        Templated strings as list or single string.
    """
    template_data = construct_template_data()
    if isinstance(template_strings, str):
        return str(StringTemplate(template_strings).format(template_data))

    return [
        str(StringTemplate(template).format(template_data))
        for template in template_strings
    ]

construct_template_data()

Return this contexts' current template data.

Source code in client/ayon_comfyui/settings_util/template_helper.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def construct_template_data() -> dict[str, Any]:
    """Return this contexts' current template data."""
    project_name = get_current_project_name()
    project_entity = get_project(project_name)

    folder_path = get_current_folder_path()
    folder_entity = get_folder_by_path(project_name, folder_path)

    task_name = get_current_task_name()
    task_entity = get_task_by_name(
        project_name, folder_entity["id"], task_name
    )

    settings = get_project_settings(project_name=project_name)

    username = get_ayon_username()

    roots = Anatomy(
        project_name=project_name, project_entity=project_entity
    ).roots

    top_roots_dict = {"roots": roots}

    return (
        get_template_data(
            project_entity=project_entity,
            folder_entity=folder_entity,
            task_entity=task_entity,
            host_name=get_current_host_name(),
            settings=settings,
            username=username,
        )
        | roots
        | top_roots_dict
    )

template_wrap(func)

Wrapper that automatically formats function output.

Returns:

Type Description
Callable

Callable with applied templated output.

Source code in client/ayon_comfyui/settings_util/template_helper.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def template_wrap(func: Callable) -> Callable:
    """Wrapper that automatically formats function output.

    Returns:
        Callable with applied templated output.
    """

    @wraps(func)
    def inner(*args: list, **kwargs: dict) -> list[str] | str:
        """Return templated result if appliccable."""
        result = func(*args, **kwargs)
        if isinstance(result, str) or (
            isinstance(result, list)
            and all(isinstance(el, str) for el in result)
        ):
            return apply_template(result)
        return result

    return inner