|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Iterable, Sequence |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import NamedTuple |
| 6 | + |
| 7 | + |
| 8 | +class Alias(NamedTuple): |
| 9 | + name: str |
| 10 | + modifier: str |
| 11 | + separator: str | None = None |
| 12 | + |
| 13 | + |
| 14 | +class BaseParams: |
| 15 | + def __str__(self): |
| 16 | + values = [] |
| 17 | + for alias in self.aliases: |
| 18 | + value = getattr(self, alias.name) |
| 19 | + if value in (None, False): |
| 20 | + continue |
| 21 | + if value is True: |
| 22 | + value = "" |
| 23 | + if isinstance(value, Iterable) and not isinstance(value, str): |
| 24 | + value = alias.separator.join(map(str, value)) |
| 25 | + values.append(f"{alias.modifier}{value}") |
| 26 | + return "".join(values) |
| 27 | + |
| 28 | + def __repr__(self): |
| 29 | + string = [] |
| 30 | + for alias in self.aliases: |
| 31 | + value = getattr(self, alias.name) |
| 32 | + if value is None or value is False: |
| 33 | + continue |
| 34 | + string.append(f"{alias.name}={value!r}") |
| 35 | + return f"{self.__class__.__name__}({', '.join(string)})" |
| 36 | + |
| 37 | + |
| 38 | +@dataclass(repr=False) |
| 39 | +class Box(BaseParams): |
| 40 | + clearance: float | str | Sequence[float | str] | None = None |
| 41 | + fill: str | None = None |
| 42 | + innerborder: str | Sequence | None = None |
| 43 | + pen: str | None = None |
| 44 | + radius: float | bool | None = False |
| 45 | + shading: str | Sequence | None = None |
| 46 | + |
| 47 | + aliases = [ |
| 48 | + Alias("clearance", "+c", "/"), |
| 49 | + Alias("fill", "+g"), |
| 50 | + Alias("innerborder", "+i", "/"), |
| 51 | + Alias("pen", "+p"), |
| 52 | + Alias("radius", "+r"), |
| 53 | + Alias("shading", "+s", "/"), |
| 54 | + ] |
0 commit comments