A container for style information, used by :class:`~rich.console.Console`. Args: styles (Dict[str, Style], optional): A mapping of style names on to styles. Defaults to None for a theme with no styles. inherit (bool, optional): Inherit default styles. Defaults to True.
| 5 | |
| 6 | |
| 7 | class Theme: |
| 8 | """A container for style information, used by :class:`~rich.console.Console`. |
| 9 | |
| 10 | Args: |
| 11 | styles (Dict[str, Style], optional): A mapping of style names on to styles. Defaults to None for a theme with no styles. |
| 12 | inherit (bool, optional): Inherit default styles. Defaults to True. |
| 13 | """ |
| 14 | |
| 15 | styles: Dict[str, Style] |
| 16 | |
| 17 | def __init__( |
| 18 | self, styles: Optional[Mapping[str, StyleType]] = None, inherit: bool = True |
| 19 | ): |
| 20 | self.styles = DEFAULT_STYLES.copy() if inherit else {} |
| 21 | if styles is not None: |
| 22 | self.styles.update( |
| 23 | { |
| 24 | name: style if isinstance(style, Style) else Style.parse(style) |
| 25 | for name, style in styles.items() |
| 26 | } |
| 27 | ) |
| 28 | |
| 29 | @property |
| 30 | def config(self) -> str: |
| 31 | """Get contents of a config file for this theme.""" |
| 32 | config = "[styles]\n" + "\n".join( |
| 33 | f"{name} = {style}" for name, style in sorted(self.styles.items()) |
| 34 | ) |
| 35 | return config |
| 36 | |
| 37 | @classmethod |
| 38 | def from_file( |
| 39 | cls, config_file: IO[str], source: Optional[str] = None, inherit: bool = True |
| 40 | ) -> "Theme": |
| 41 | """Load a theme from a text mode file. |
| 42 | |
| 43 | Args: |
| 44 | config_file (IO[str]): An open conf file. |
| 45 | source (str, optional): The filename of the open file. Defaults to None. |
| 46 | inherit (bool, optional): Inherit default styles. Defaults to True. |
| 47 | |
| 48 | Returns: |
| 49 | Theme: A New theme instance. |
| 50 | """ |
| 51 | import configparser |
| 52 | |
| 53 | config = configparser.ConfigParser() |
| 54 | config.read_file(config_file, source=source) |
| 55 | styles = {name: Style.parse(value) for name, value in config.items("styles")} |
| 56 | theme = Theme(styles, inherit=inherit) |
| 57 | return theme |
| 58 | |
| 59 | @classmethod |
| 60 | def read( |
| 61 | cls, path: str, inherit: bool = True, encoding: Optional[str] = None |
| 62 | ) -> "Theme": |
| 63 | """Read a theme from a path. |
| 64 |
no outgoing calls