Skip to content

plugins

Type helpers for plugin schemas.

build_type_metadata(value, _visited=frozenset())

Builds a type metadata dictionary from a python type value.

Source code in client/ayon_workflow/web_editor/plugins.py
 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
133
def build_type_metadata(
    value: Any,  # python type or typing value e.g. Union[Any, dataclass]
    _visited: frozenset = frozenset(),
) -> Union[dict[str, Any], None]:
    """ Builds a type metadata dictionary from a python type value.
    """
    if value in _SIMPLE_KINDS:
        return {"kind": _SIMPLE_KINDS[value]}

    origin = get_origin(value)
    if origin is Union or (_UNION_TYPE is not None and origin is _UNION_TYPE):
        options = []
        nullable = False
        for arg in get_args(value):
            if arg is type(None):
                nullable = True
                continue
            option_metadata = build_type_metadata(arg, _visited)
            if option_metadata is not None:
                options.append(option_metadata)
        return {"kind": "union", "nullable": nullable, "options": options}

    if origin is list:
        item_type = get_args(value)[0] if get_args(value) else Any
        return {
            "kind": "array",
            "item_type": build_type_metadata(item_type, _visited),
        }

    if isinstance(value, type) and issubclass(value, enum.Enum):
        return {
            "kind": "enum",
            "__type__": f"{value.__module__}.{value.__qualname__}",
            "enum_options": [
                {
                    "name": member.name,
                    "value": serialization.serialize_to_primitive(
                        member.value
                    ),
                }
                for member in value
            ],
        }

    if isinstance(value, type) and is_dataclass(value):
        if value in _visited:
            return {"kind": "unknown"}
        return build_dataclass_metadata(value, _visited | {value})

    return {"kind": "unknown"}

to_public_mapping(value)

Converts a python "field mapping" to a JSON serializable mapping.

Source code in client/ayon_workflow/web_editor/plugins.py
136
137
138
139
140
141
142
143
144
145
146
def to_public_mapping(value: dict[str, Any]) -> dict[str, Any]:
    """ Converts a python "field mapping" to a JSON serializable mapping.
    """
    # TODO give an example here.
    result = {}
    for key, item in value.items():
        if key == "type":
            result["type_metadata"] = build_type_metadata(item)
        else:
            result[key] = serialization.serialize_to_primitive(item)
    return result