MCPcopy
hub / github.com/Textualize/rich / Theme

Class Theme

rich/theme.py:7–74  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

5
6
7class 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

Callers 12

test_inheritFunction · 0.90
test_configFunction · 0.90
test_from_fileFunction · 0.90
test_readFunction · 0.90
test_theme_stackFunction · 0.90
log.pyFile · 0.90
highlighter.pyFile · 0.90
__rich_console__Method · 0.85
from_fileMethod · 0.85
theme.pyFile · 0.85
themes.pyFile · 0.85

Calls

no outgoing calls

Tested by 6

test_inheritFunction · 0.72
test_configFunction · 0.72
test_from_fileFunction · 0.72
test_readFunction · 0.72
test_theme_stackFunction · 0.72