theme.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import configparser
  2. from typing import Dict, List, IO, Mapping, Optional
  3. from .default_styles import DEFAULT_STYLES
  4. from .style import Style, StyleType
  5. class Theme:
  6. """A container for style information, used by :class:`~rich.console.Console`.
  7. Args:
  8. styles (Dict[str, Style], optional): A mapping of style names on to styles. Defaults to None for a theme with no styles.
  9. inherit (bool, optional): Inherit default styles. Defaults to True.
  10. """
  11. styles: Dict[str, Style]
  12. def __init__(
  13. self, styles: Optional[Mapping[str, StyleType]] = None, inherit: bool = True
  14. ):
  15. self.styles = DEFAULT_STYLES.copy() if inherit else {}
  16. if styles is not None:
  17. self.styles.update(
  18. {
  19. name: style if isinstance(style, Style) else Style.parse(style)
  20. for name, style in styles.items()
  21. }
  22. )
  23. @property
  24. def config(self) -> str:
  25. """Get contents of a config file for this theme."""
  26. config = "[styles]\n" + "\n".join(
  27. f"{name} = {style}" for name, style in sorted(self.styles.items())
  28. )
  29. return config
  30. @classmethod
  31. def from_file(
  32. cls, config_file: IO[str], source: Optional[str] = None, inherit: bool = True
  33. ) -> "Theme":
  34. """Load a theme from a text mode file.
  35. Args:
  36. config_file (IO[str]): An open conf file.
  37. source (str, optional): The filename of the open file. Defaults to None.
  38. inherit (bool, optional): Inherit default styles. Defaults to True.
  39. Returns:
  40. Theme: A New theme instance.
  41. """
  42. config = configparser.ConfigParser()
  43. config.read_file(config_file, source=source)
  44. styles = {name: Style.parse(value) for name, value in config.items("styles")}
  45. theme = Theme(styles, inherit=inherit)
  46. return theme
  47. @classmethod
  48. def read(cls, path: str, inherit: bool = True) -> "Theme":
  49. """Read a theme from a path.
  50. Args:
  51. path (str): Path to a config file readable by Python configparser module.
  52. inherit (bool, optional): Inherit default styles. Defaults to True.
  53. Returns:
  54. Theme: A new theme instance.
  55. """
  56. with open(path, "rt") as config_file:
  57. return cls.from_file(config_file, source=path, inherit=inherit)
  58. class ThemeStackError(Exception):
  59. """Base exception for errors related to the theme stack."""
  60. class ThemeStack:
  61. """A stack of themes.
  62. Args:
  63. theme (Theme): A theme instance
  64. """
  65. def __init__(self, theme: Theme) -> None:
  66. self._entries: List[Dict[str, Style]] = [theme.styles]
  67. self.get = self._entries[-1].get
  68. def push_theme(self, theme: Theme, inherit: bool = True) -> None:
  69. """Push a theme on the top of the stack.
  70. Args:
  71. theme (Theme): A Theme instance.
  72. inherit (boolean, optional): Inherit styles from current top of stack.
  73. """
  74. styles: Dict[str, Style]
  75. styles = (
  76. {**self._entries[-1], **theme.styles} if inherit else theme.styles.copy()
  77. )
  78. self._entries.append(styles)
  79. self.get = self._entries[-1].get
  80. def pop_theme(self) -> None:
  81. """Pop (and discard) the top-most theme."""
  82. if len(self._entries) == 1:
  83. raise ThemeStackError("Unable to pop base theme")
  84. self._entries.pop()
  85. self.get = self._entries[-1].get
  86. if __name__ == "__main__": # pragma: no cover
  87. theme = Theme()
  88. print(theme.config)