From fda9a313a6ac14ab1fbb27b10931afa8159984a3 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 9 Jan 2024 00:31:04 +0000 Subject: [PATCH] Extract sub-functions from ``Theme.__init__()`` --- sphinx/theming.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/sphinx/theming.py b/sphinx/theming.py index 4172f17965f..9526ca41b14 100644 --- a/sphinx/theming.py +++ b/sphinx/theming.py @@ -49,6 +49,15 @@ def _extract_zip(filename: str, target_dir: str, /) -> None: fp.write(archive.read(name)) +def _load_theme_conf(theme_dir: os.PathLike[str] | str) -> configparser.RawConfigParser: + c = configparser.RawConfigParser() + config_file_path = path.join(theme_dir, _THEME_CONF) + if not os.path.isfile(config_file_path): + raise ThemeError(__('theme configuration file %r not found') % config_file_path) + c.read(config_file_path, encoding='utf-8') + return c + + class Theme: """A Theme is a set of HTML templates and configurations. @@ -69,11 +78,7 @@ def __init__(self, name: str, theme_path: str, theme_factory: HTMLThemeFactory) self._theme_dir = path.join(self._root_dir, name) _extract_zip(theme_path, self._theme_dir) - self.config = configparser.RawConfigParser() - config_file_path = path.join(self._theme_dir, _THEME_CONF) - if not os.path.isfile(config_file_path): - raise ThemeError(__('theme configuration file %r not found') % config_file_path) - self.config.read(config_file_path, encoding='utf-8') + self.config = _load_theme_conf(self._theme_dir) try: inherit = self.config.get('theme', 'inherit') @@ -81,7 +86,14 @@ def __init__(self, name: str, theme_path: str, theme_factory: HTMLThemeFactory) raise ThemeError(__('theme %r doesn\'t have "theme" setting') % name) from exc except configparser.NoOptionError as exc: raise ThemeError(__('theme %r doesn\'t have "inherit" setting') % name) from exc - + self._load_ancestor_theme(inherit, theme_factory, name) + + def _load_ancestor_theme( + self, + inherit: str, + theme_factory: HTMLThemeFactory, + name: str, + ) -> None: if inherit != 'none': try: self._base = theme_factory.create(inherit)