|
| 1 | +from typing import Any, Optional, Union |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +import tmt.utils |
| 6 | +from tmt._compat.pydantic import ValidationError |
| 7 | +from tmt._compat.typing import TypeAlias |
| 8 | +from tmt.container import MetadataContainer |
| 9 | + |
| 10 | +Color: TypeAlias = Union[int, tuple[int, int, int], str, None] |
| 11 | + |
| 12 | + |
| 13 | +class Style(MetadataContainer): |
| 14 | + """ |
| 15 | + A collection of parameters accepted by :py:func:`click.style`. |
| 16 | + """ |
| 17 | + |
| 18 | + fg: Optional[Color] = None |
| 19 | + bg: Optional[Color] = None |
| 20 | + bold: Optional[bool] = None |
| 21 | + dim: Optional[bool] = None |
| 22 | + underline: Optional[bool] = None |
| 23 | + italic: Optional[bool] = None |
| 24 | + blink: Optional[bool] = None |
| 25 | + strikethrough: Optional[bool] = None |
| 26 | + |
| 27 | + def apply(self, text: str) -> str: |
| 28 | + """ |
| 29 | + Apply this style to a given string. |
| 30 | + """ |
| 31 | + |
| 32 | + return click.style(text, **self.dict()) |
| 33 | + |
| 34 | + |
| 35 | +_DEFAULT_STYLE = Style() |
| 36 | + |
| 37 | + |
| 38 | +class Theme(MetadataContainer): |
| 39 | + """ |
| 40 | + A collection of items tmt uses to colorize various tokens of its CLI. |
| 41 | + """ |
| 42 | + |
| 43 | + restructuredtext_text: Style = _DEFAULT_STYLE |
| 44 | + |
| 45 | + restructuredtext_literal: Style = _DEFAULT_STYLE |
| 46 | + restructuredtext_emphasis: Style = _DEFAULT_STYLE |
| 47 | + restructuredtext_strong: Style = _DEFAULT_STYLE |
| 48 | + |
| 49 | + restructuredtext_literalblock: Style = _DEFAULT_STYLE |
| 50 | + restructuredtext_literalblock_yaml: Style = _DEFAULT_STYLE |
| 51 | + restructuredtext_literalblock_shell: Style = _DEFAULT_STYLE |
| 52 | + |
| 53 | + restructuredtext_admonition_note: Style = _DEFAULT_STYLE |
| 54 | + restructuredtext_admonition_warning: Style = _DEFAULT_STYLE |
| 55 | + |
| 56 | + def to_spec(self) -> dict[str, Any]: |
| 57 | + return {key.replace('_', '-'): value for key, value in self.dict().items()} |
| 58 | + |
| 59 | + def to_minimal_spec(self) -> dict[str, Any]: |
| 60 | + spec: dict[str, Any] = {} |
| 61 | + |
| 62 | + for theme_key, style in self.to_spec().items(): |
| 63 | + style_spec = { |
| 64 | + style_key: value for style_key, value in style.items() if value is not None |
| 65 | + } |
| 66 | + |
| 67 | + if style_spec: |
| 68 | + spec[theme_key.replace('_', '-')] = style_spec |
| 69 | + |
| 70 | + return spec |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def from_spec(cls: type['Theme'], data: Any) -> 'Theme': |
| 74 | + try: |
| 75 | + return Theme.parse_obj(data) |
| 76 | + |
| 77 | + except ValidationError as error: |
| 78 | + raise tmt.utils.SpecificationError("Invalid theme configuration.") from error |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def from_file(cls: type['Theme'], path: tmt.utils.Path) -> 'Theme': |
| 82 | + return Theme.from_spec(tmt.utils.yaml_to_dict(path.read_text())) |
| 83 | + |
| 84 | + |
| 85 | +class ThemeConfig(MetadataContainer): |
| 86 | + active_theme: str = 'default' |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def load_theme(cls, theme_name: str) -> Theme: |
| 90 | + try: |
| 91 | + return Theme.from_file( |
| 92 | + tmt.utils.resource_files(tmt.utils.Path('config/themes') / f'{theme_name}.yaml') |
| 93 | + ) |
| 94 | + |
| 95 | + except FileNotFoundError as exc: |
| 96 | + raise tmt.utils.GeneralError(f"No such theme '{theme_name}'.") from exc |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def get_default_theme(cls) -> Theme: |
| 100 | + return cls.load_theme('default') |
| 101 | + |
| 102 | + def get_active_theme(self) -> Theme: |
| 103 | + return self.load_theme(self.active_theme) |
0 commit comments