A style dictionary.
| 242 | |
| 243 | |
| 244 | class Style(dict[str, Any]): |
| 245 | """A style dictionary.""" |
| 246 | |
| 247 | def __init__(self, style_dict: dict[str, Any] | None = None, **kwargs): |
| 248 | """Initialize the style. |
| 249 | |
| 250 | Args: |
| 251 | style_dict: The style dictionary. |
| 252 | kwargs: Other key value pairs to apply to the dict update. |
| 253 | """ |
| 254 | if style_dict: |
| 255 | style_dict.update(kwargs) |
| 256 | else: |
| 257 | style_dict = kwargs |
| 258 | if style_dict: |
| 259 | style_dict, self._var_data = convert(style_dict) |
| 260 | else: |
| 261 | self._var_data = EMPTY_VAR_DATA |
| 262 | super().__init__(style_dict) |
| 263 | |
| 264 | def update(self, style_dict: dict | None, **kwargs): |
| 265 | """Update the style. |
| 266 | |
| 267 | Args: |
| 268 | style_dict: The style dictionary. |
| 269 | kwargs: Other key value pairs to apply to the dict update. |
| 270 | """ |
| 271 | if not isinstance(style_dict, Style): |
| 272 | converted_dict = type(self)(style_dict) |
| 273 | else: |
| 274 | converted_dict = style_dict |
| 275 | if kwargs: |
| 276 | if converted_dict is None: |
| 277 | converted_dict = type(self)(kwargs) |
| 278 | else: |
| 279 | converted_dict.update(kwargs) |
| 280 | # Combine our VarData with that of any Vars in the style_dict that was passed. |
| 281 | self._var_data = VarData.merge(self._var_data, converted_dict._var_data) |
| 282 | super().update(converted_dict) |
| 283 | |
| 284 | def __setitem__(self, key: str, value: Any): |
| 285 | """Set an item in the style. |
| 286 | |
| 287 | Args: |
| 288 | key: The key to set. |
| 289 | value: The value to set. |
| 290 | """ |
| 291 | # Create a Var to collapse VarData encoded in f-string. |
| 292 | var = LiteralVar.create(value) |
| 293 | if var is not None: |
| 294 | # Carry the imports/hooks when setting a Var as a value. |
| 295 | self._var_data = VarData.merge( |
| 296 | getattr(self, "_var_data", None), var._get_all_var_data() |
| 297 | ) |
| 298 | super().__setitem__(key, value) |
| 299 | |
| 300 | def __or__(self, other: Style | dict) -> Style: |
| 301 | """Combine two styles. |
no outgoing calls