|
35 | 35 | _THEME_CONF = 'theme.conf' |
36 | 36 |
|
37 | 37 |
|
38 | | -def _extract_zip(filename: str, target_dir: str, /) -> None: |
39 | | - """Extract zip file to target directory.""" |
40 | | - ensuredir(target_dir) |
41 | | - |
42 | | - with ZipFile(filename) as archive: |
43 | | - for name in archive.namelist(): |
44 | | - if name.endswith('/'): |
45 | | - continue |
46 | | - entry = path.join(target_dir, name) |
47 | | - ensuredir(path.dirname(entry)) |
48 | | - with open(path.join(entry), 'wb') as fp: |
49 | | - fp.write(archive.read(name)) |
50 | | - |
51 | | - |
52 | | -def _load_theme_conf(theme_dir: os.PathLike[str] | str) -> configparser.RawConfigParser: |
53 | | - c = configparser.RawConfigParser() |
54 | | - config_file_path = path.join(theme_dir, _THEME_CONF) |
55 | | - if not os.path.isfile(config_file_path): |
56 | | - raise ThemeError(__('theme configuration file %r not found') % config_file_path) |
57 | | - c.read(config_file_path, encoding='utf-8') |
58 | | - return c |
59 | | - |
60 | | - |
61 | 38 | class Theme: |
62 | 39 | """A Theme is a set of HTML templates and configurations. |
63 | 40 |
|
@@ -169,15 +146,6 @@ def _cleanup(self) -> None: |
169 | 146 | self._base._cleanup() |
170 | 147 |
|
171 | 148 |
|
172 | | -def _is_archived_theme(filename: str, /) -> bool: |
173 | | - """Check whether the specified file is an archived theme file or not.""" |
174 | | - try: |
175 | | - with ZipFile(filename) as f: |
176 | | - return _THEME_CONF in f.namelist() |
177 | | - except Exception: |
178 | | - return False |
179 | | - |
180 | | - |
181 | 149 | class HTMLThemeFactory: |
182 | 150 | """A factory class for HTML Themes.""" |
183 | 151 |
|
@@ -214,9 +182,10 @@ def _load_extra_theme(self, name: str) -> None: |
214 | 182 | pass |
215 | 183 | else: |
216 | 184 | self._app.registry.load_extension(self._app, entry_point.module) |
217 | | - _config_post_init(None, self._app.config) |
| 185 | + _config_post_init(self._app, self._app.config) |
218 | 186 |
|
219 | | - def _find_themes(self, theme_path: str) -> dict[str, str]: |
| 187 | + @staticmethod |
| 188 | + def _find_themes(theme_path: str) -> dict[str, str]: |
220 | 189 | """Search themes from specified directory.""" |
221 | 190 | themes: dict[str, str] = {} |
222 | 191 | if not path.isdir(theme_path): |
@@ -246,3 +215,35 @@ def create(self, name: str) -> Theme: |
246 | 215 | raise ThemeError(__('no theme named %r found (missing theme.conf?)') % name) |
247 | 216 |
|
248 | 217 | return Theme(name, self._themes[name], self) |
| 218 | + |
| 219 | + |
| 220 | +def _is_archived_theme(filename: str, /) -> bool: |
| 221 | + """Check whether the specified file is an archived theme file or not.""" |
| 222 | + try: |
| 223 | + with ZipFile(filename) as f: |
| 224 | + return _THEME_CONF in f.namelist() |
| 225 | + except Exception: |
| 226 | + return False |
| 227 | + |
| 228 | + |
| 229 | +def _extract_zip(filename: str, target_dir: str, /) -> None: |
| 230 | + """Extract zip file to target directory.""" |
| 231 | + ensuredir(target_dir) |
| 232 | + |
| 233 | + with ZipFile(filename) as archive: |
| 234 | + for name in archive.namelist(): |
| 235 | + if name.endswith('/'): |
| 236 | + continue |
| 237 | + entry = path.join(target_dir, name) |
| 238 | + ensuredir(path.dirname(entry)) |
| 239 | + with open(path.join(entry), 'wb') as fp: |
| 240 | + fp.write(archive.read(name)) |
| 241 | + |
| 242 | + |
| 243 | +def _load_theme_conf(theme_dir: os.PathLike[str] | str, /) -> configparser.RawConfigParser: |
| 244 | + c = configparser.RawConfigParser() |
| 245 | + config_file_path = path.join(theme_dir, _THEME_CONF) |
| 246 | + if not os.path.isfile(config_file_path): |
| 247 | + raise ThemeError(__('theme configuration file %r not found') % config_file_path) |
| 248 | + c.read(config_file_path, encoding='utf-8') |
| 249 | + return c |
0 commit comments