Format a style dictionary. Args: style_dict: The style dictionary to format. Returns: The formatted style dictionary.
(
style_dict: dict[str, Var | dict | list | str],
)
| 163 | |
| 164 | |
| 165 | def convert( |
| 166 | style_dict: dict[str, Var | dict | list | str], |
| 167 | ) -> tuple[dict[str, str | list | dict], VarData | None]: |
| 168 | """Format a style dictionary. |
| 169 | |
| 170 | Args: |
| 171 | style_dict: The style dictionary to format. |
| 172 | |
| 173 | Returns: |
| 174 | The formatted style dictionary. |
| 175 | """ |
| 176 | var_data = None # Track import/hook data from any Vars in the style dict. |
| 177 | out = {} |
| 178 | |
| 179 | def update_out_dict( |
| 180 | return_value: Var | dict | list | str, keys_to_update: tuple[str, ...] |
| 181 | ): |
| 182 | for k in keys_to_update: |
| 183 | out[k] = return_value |
| 184 | |
| 185 | for key, value in style_dict.items(): |
| 186 | keys = ( |
| 187 | format_style_key(key) |
| 188 | if not isinstance(value, (dict, ObjectVar, list)) |
| 189 | or ( |
| 190 | isinstance(value, Breakpoints) |
| 191 | and all(not isinstance(v, dict) for v in value.values()) |
| 192 | ) |
| 193 | or (isinstance(value, list) and all(not isinstance(v, dict) for v in value)) |
| 194 | or ( |
| 195 | isinstance(value, ObjectVar) |
| 196 | and not typehint_issubclass(value._var_type, Mapping) |
| 197 | ) |
| 198 | else (key,) |
| 199 | ) |
| 200 | |
| 201 | if isinstance(value, Var): |
| 202 | return_val = value |
| 203 | new_var_data = value._get_all_var_data() |
| 204 | update_out_dict(return_val, keys) |
| 205 | elif isinstance(value, dict): |
| 206 | # Recursively format nested style dictionaries. |
| 207 | return_val, new_var_data = convert(value) |
| 208 | update_out_dict(return_val, keys) |
| 209 | elif isinstance(value, list): |
| 210 | # Responsive value is a list of dict or value |
| 211 | return_val, new_var_data = convert_list(value) |
| 212 | update_out_dict(return_val, keys) |
| 213 | else: |
| 214 | return_val, new_var_data = convert_item(value) |
| 215 | update_out_dict(return_val, keys) |
| 216 | # Combine all the collected VarData instances. |
| 217 | var_data = VarData.merge(var_data, new_var_data) |
| 218 | |
| 219 | if isinstance(style_dict, Breakpoints): |
| 220 | out = Breakpoints(out).factorize() |
| 221 | |
| 222 | return out, var_data |
no test coverage detected