Skip to content

plugin_tools

AYON plugin tools.

prepare_template_data(fill_pairs)

Prepares formatted data for filling template.

It produces multiple variants of keys (key, Key, KEY) to control format of filled template.

Example

src_data = { ... "host": "maya", ... } output = prepare_template_data(src_data) sorted(list(output.items())) # sort & list conversion for tests [('HOST', 'MAYA'), ('Host', 'Maya'), ('host', 'maya')]

Parameters:

Name Type Description Default
fill_pairs Union[dict[str, Any], Iterable[Tuple[str, Any]]]

The value that are prepared for template.

required

Returns:

Type Description

dict[str, str]: Prepared values for template.

Source code in client/ayon_core/lib/plugin_tools.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def prepare_template_data(fill_pairs):
    """Prepares formatted data for filling template.

    It produces multiple variants of keys (key, Key, KEY) to control
    format of filled template.

    Example:
        >>> src_data = {
        ...    "host": "maya",
        ... }
        >>> output = prepare_template_data(src_data)
        >>> sorted(list(output.items())) # sort & list conversion for tests
        [('HOST', 'MAYA'), ('Host', 'Maya'), ('host', 'maya')]

    Args:
        fill_pairs (Union[dict[str, Any], Iterable[Tuple[str, Any]]]): The
            value that are prepared for template.

    Returns:
        dict[str, str]: Prepared values for template.
    """

    valid_items = _separate_keys_and_value(fill_pairs)
    output = {}
    for item in valid_items:
        keys, value = item
        # Convert only string values
        if isinstance(value, str):
            upper_value = value.upper()
            capitalized_value = _capitalize_value(value)
        else:
            upper_value = capitalized_value = value

        first_key = keys.pop(0)
        if not keys:
            output[first_key] = value
            output[first_key.upper()] = upper_value
            output[first_key.capitalize()] = capitalized_value
            continue

        # Prepare 'normal', 'upper' and 'capitalized' variables
        normal = output.setdefault(first_key, {})
        capitalized = output.setdefault(first_key.capitalize(), {})
        upper = output.setdefault(first_key.upper(), {})

        keys_deque = collections.deque(keys)
        while keys_deque:
            key = keys_deque.popleft()
            upper_key = key
            if isinstance(key, str):
                upper_key = key.upper()

            if not keys_deque:
                # Fill value on last key
                upper[upper_key] = upper_value
                capitalized[key] = capitalized_value
                normal[key] = value
            else:
                normal = normal.setdefault(key, {})
                capitalized = capitalized.setdefault(key, {})
                upper = upper.setdefault(upper_key, {})
    return output

source_hash(filepath, *args)

Generate simple identifier for a source file. This is used to identify whether a source file has previously been processe into the pipeline, e.g. a texture. The hash is based on source filepath, modification time and file size. This is only used to identify whether a specific source file was already published before from the same location with the same modification date. We opt to do it this way as opposed to Avalanch C4 hash as this is much faster and predictable enough for all our production use cases. Args: filepath (str): The source file path. You can specify additional arguments in the function to allow for specific 'processing' values to be included.

Source code in client/ayon_core/lib/plugin_tools.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def source_hash(filepath, *args):
    """Generate simple identifier for a source file.
    This is used to identify whether a source file has previously been
    processe into the pipeline, e.g. a texture.
    The hash is based on source filepath, modification time and file size.
    This is only used to identify whether a specific source file was already
    published before from the same location with the same modification date.
    We opt to do it this way as opposed to Avalanch C4 hash as this is much
    faster and predictable enough for all our production use cases.
    Args:
        filepath (str): The source file path.
    You can specify additional arguments in the function
    to allow for specific 'processing' values to be included.
    """
    # We replace dots with comma because . cannot be a key in a pymongo dict.
    file_name = os.path.basename(filepath)
    time = str(os.path.getmtime(filepath))
    size = str(os.path.getsize(filepath))
    return "|".join([file_name, time, size] + list(args)).replace(".", ",")