mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Migrate to py3 style type annotation: sphinx.theming
This commit is contained in:
@@ -13,6 +13,7 @@ import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from os import path
|
||||
from typing import Any, Dict, List
|
||||
from zipfile import ZipFile
|
||||
|
||||
import pkg_resources
|
||||
@@ -23,19 +24,18 @@ from sphinx.locale import __
|
||||
from sphinx.util import logging
|
||||
from sphinx.util.osutil import ensuredir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if False:
|
||||
# For type annotation
|
||||
from typing import Any, Dict, List # NOQA
|
||||
from sphinx.application import Sphinx # NOQA
|
||||
from sphinx.application import Sphinx
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
NODEFAULT = object()
|
||||
THEMECONF = 'theme.conf'
|
||||
|
||||
|
||||
def extract_zip(filename, targetdir):
|
||||
# type: (str, str) -> None
|
||||
def extract_zip(filename: str, targetdir: str) -> None:
|
||||
"""Extract zip file to target directory."""
|
||||
ensuredir(targetdir)
|
||||
|
||||
@@ -54,8 +54,7 @@ class Theme:
|
||||
|
||||
This class supports both theme directory and theme archive (zipped theme)."""
|
||||
|
||||
def __init__(self, name, theme_path, factory):
|
||||
# type: (str, str, HTMLThemeFactory) -> None
|
||||
def __init__(self, name: str, theme_path: str, factory: "HTMLThemeFactory") -> None:
|
||||
self.name = name
|
||||
self.base = None
|
||||
self.rootdir = None
|
||||
@@ -87,8 +86,7 @@ class Theme:
|
||||
raise ThemeError(__('no theme named %r found, inherited by %r') %
|
||||
(inherit, name))
|
||||
|
||||
def get_theme_dirs(self):
|
||||
# type: () -> List[str]
|
||||
def get_theme_dirs(self) -> List[str]:
|
||||
"""Return a list of theme directories, beginning with this theme's,
|
||||
then the base theme's, then that one's base theme's, etc.
|
||||
"""
|
||||
@@ -97,8 +95,7 @@ class Theme:
|
||||
else:
|
||||
return [self.themedir] + self.base.get_theme_dirs()
|
||||
|
||||
def get_config(self, section, name, default=NODEFAULT):
|
||||
# type: (str, str, Any) -> Any
|
||||
def get_config(self, section: str, name: str, default: Any = NODEFAULT) -> Any:
|
||||
"""Return the value for a theme configuration setting, searching the
|
||||
base theme chain.
|
||||
"""
|
||||
@@ -114,8 +111,7 @@ class Theme:
|
||||
else:
|
||||
return default
|
||||
|
||||
def get_options(self, overrides={}):
|
||||
# type: (Dict[str, Any]) -> Dict[str, Any]
|
||||
def get_options(self, overrides: Dict[str, Any] = {}) -> Dict[str, Any]:
|
||||
"""Return a dictionary of theme options and their values."""
|
||||
if self.base:
|
||||
options = self.base.get_options()
|
||||
@@ -135,8 +131,7 @@ class Theme:
|
||||
|
||||
return options
|
||||
|
||||
def cleanup(self):
|
||||
# type: () -> None
|
||||
def cleanup(self) -> None:
|
||||
"""Remove temporary directories."""
|
||||
if self.rootdir:
|
||||
try:
|
||||
@@ -147,8 +142,7 @@ class Theme:
|
||||
self.base.cleanup()
|
||||
|
||||
|
||||
def is_archived_theme(filename):
|
||||
# type: (str) -> bool
|
||||
def is_archived_theme(filename: str) -> bool:
|
||||
"""Check the specified file is an archived theme file or not."""
|
||||
try:
|
||||
with ZipFile(filename) as f:
|
||||
@@ -160,23 +154,20 @@ def is_archived_theme(filename):
|
||||
class HTMLThemeFactory:
|
||||
"""A factory class for HTML Themes."""
|
||||
|
||||
def __init__(self, app):
|
||||
# type: (Sphinx) -> None
|
||||
def __init__(self, app: "Sphinx") -> None:
|
||||
self.app = app
|
||||
self.themes = app.html_themes
|
||||
self.load_builtin_themes()
|
||||
if getattr(app.config, 'html_theme_path', None):
|
||||
self.load_additional_themes(app.config.html_theme_path)
|
||||
|
||||
def load_builtin_themes(self):
|
||||
# type: () -> None
|
||||
def load_builtin_themes(self) -> None:
|
||||
"""Load built-in themes."""
|
||||
themes = self.find_themes(path.join(package_dir, 'themes'))
|
||||
for name, theme in themes.items():
|
||||
self.themes[name] = theme
|
||||
|
||||
def load_additional_themes(self, theme_paths):
|
||||
# type: (str) -> None
|
||||
def load_additional_themes(self, theme_paths: str) -> None:
|
||||
"""Load additional themes placed at specified directories."""
|
||||
for theme_path in theme_paths:
|
||||
abs_theme_path = path.abspath(path.join(self.app.confdir, theme_path))
|
||||
@@ -184,8 +175,7 @@ class HTMLThemeFactory:
|
||||
for name, theme in themes.items():
|
||||
self.themes[name] = theme
|
||||
|
||||
def load_extra_theme(self, name):
|
||||
# type: (str) -> None
|
||||
def load_extra_theme(self, name: str) -> None:
|
||||
"""Try to load a theme having specifed name."""
|
||||
if name == 'alabaster':
|
||||
self.load_alabaster_theme()
|
||||
@@ -194,14 +184,12 @@ class HTMLThemeFactory:
|
||||
else:
|
||||
self.load_external_theme(name)
|
||||
|
||||
def load_alabaster_theme(self):
|
||||
# type: () -> None
|
||||
def load_alabaster_theme(self) -> None:
|
||||
"""Load alabaster theme."""
|
||||
import alabaster
|
||||
self.themes['alabaster'] = path.join(alabaster.get_path(), 'alabaster')
|
||||
|
||||
def load_sphinx_rtd_theme(self):
|
||||
# type: () -> None
|
||||
def load_sphinx_rtd_theme(self) -> None:
|
||||
"""Load sphinx_rtd_theme theme (if exists)."""
|
||||
try:
|
||||
import sphinx_rtd_theme
|
||||
@@ -210,8 +198,7 @@ class HTMLThemeFactory:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
def load_external_theme(self, name):
|
||||
# type: (str) -> None
|
||||
def load_external_theme(self, name: str) -> None:
|
||||
"""Try to load a theme using entry_points.
|
||||
|
||||
Sphinx refers to ``sphinx_themes`` entry_points.
|
||||
@@ -225,8 +212,7 @@ class HTMLThemeFactory:
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def find_themes(self, theme_path):
|
||||
# type: (str) -> Dict[str, str]
|
||||
def find_themes(self, theme_path: str) -> Dict[str, str]:
|
||||
"""Search themes from specified directory."""
|
||||
themes = {} # type: Dict[str, str]
|
||||
if not path.isdir(theme_path):
|
||||
@@ -247,8 +233,7 @@ class HTMLThemeFactory:
|
||||
|
||||
return themes
|
||||
|
||||
def create(self, name):
|
||||
# type: (str) -> Theme
|
||||
def create(self, name: str) -> Theme:
|
||||
"""Create an instance of theme."""
|
||||
if name not in self.themes:
|
||||
self.load_extra_theme(name)
|
||||
|
||||
Reference in New Issue
Block a user