Skip to content

variants

check_variants_sync()

Check if the variants.py file is in sync with the style data.

Source code in client/ayon_ui_qt/variants.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def check_variants_sync():
    """Check if the variants.py file is in sync with the style data."""
    from .style import get_ayon_style

    # Runtime sync assertion at import
    style_data = get_ayon_style().model
    enum_desync = False
    report = ""

    for widget in style_data.widget_list():
        json_variants = style_data.widget_variants(widget)
        if not json_variants:
            continue

        enum_name = f"{widget}Variants"

        # check if the enum exists for that widget class
        if enum_name not in globals():
            report += f"    Missing enum for {widget}\n"
            enum_desync = True
            continue

        # check if the enum values match the style data
        enum = globals()[enum_name]
        if set(json_variants) != {v.value for v in enum}:
            report += (
                f"    Desync for {widget}: {json_variants} != {enum}\n"
            )
            enum_desync = True

    # print the report
    print("-" * 80)
    if enum_desync:
        update_variants_file(style_data)
        print("Variants are not in sync with the style data.\n")
        print(f"VARIANTS ANALYSIS {'_' * 62}\n" + report)
    else:
        print("Variants are in sync with the style data.")
    print("-" * 80)

update_variants_file(style_data)

Update the variants.py file with the current style data.

Source code in client/ayon_ui_qt/variants.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def update_variants_file(style_data):
    """Update the variants.py file with the current style data."""
    import os
    import inspect
    import re

    # get the path of the current file
    current_file = inspect.getfile(inspect.currentframe())
    current_dir = os.path.dirname(current_file)

    # load the current variants.py file
    with open(os.path.join(current_dir, "variants.py"), "r") as f:
        current = f.read()

    # get the start and end of the variants definitions
    start = current.find("# VARIANTS DEFINITIONS")
    end = current.find("# END OF VARIANTs DEFINITIONS")
    if start == -1 or end == -1:
        raise RuntimeError(
            "Could not find variants definitions in variants.py"
        )
    # adjust start to the end of the line
    start = current.find("\n", start) + 1

    # get the part before and after the variants definitions
    before = current[:start]
    after = current[end:]

    # generate all variants enums from style data
    defs = []
    for widget in style_data.widget_list():
        json_variants = style_data.widget_variants(widget)
        if not json_variants:
            continue

        defs.append(f"class {widget}Variants(Enum):\n")
        for variant in json_variants:
            # remove spaces and special characters
            field_name = re.sub(r"[^a-zA-Z0-9]", "_", variant).title()
            # avoid empty strings
            if variant == "":
                field_name = "Default"
            defs.append(f'    {field_name} = "{variant}"\n')
        defs.append("\n\n")

    new = before + "\n\n" + "".join(defs) + after
    # print("-" * 80 + "\n" + new + "\n" + "-" * 80 + "\n")

    # write the new variants.py file
    with open(os.path.join(current_dir, "variants.py"), "w") as f:
        f.write(new)