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.jinja2glue
This commit is contained in:
parent
1124052f92
commit
1734844e7c
@ -10,42 +10,37 @@
|
|||||||
|
|
||||||
from os import path
|
from os import path
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from typing import Any, Callable, Iterator, Tuple # NOQA
|
from typing import Any, Callable, Dict, Iterator, List, Tuple, Union
|
||||||
|
|
||||||
from jinja2 import FileSystemLoader, BaseLoader, TemplateNotFound, \
|
from jinja2 import FileSystemLoader, BaseLoader, TemplateNotFound, contextfunction
|
||||||
contextfunction
|
from jinja2.environment import Environment
|
||||||
from jinja2.sandbox import SandboxedEnvironment
|
from jinja2.sandbox import SandboxedEnvironment
|
||||||
from jinja2.utils import open_if_exists
|
from jinja2.utils import open_if_exists
|
||||||
|
|
||||||
from sphinx.application import TemplateBridge
|
from sphinx.application import TemplateBridge
|
||||||
|
from sphinx.theming import Theme
|
||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
from sphinx.util.osutil import mtimes_of_files
|
from sphinx.util.osutil import mtimes_of_files
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
# For type annotation
|
# For type annotation
|
||||||
from typing import Dict, List, Union # NOQA
|
from sphinx.builders import Builder
|
||||||
from jinja2.environment import Environment # NOQA
|
|
||||||
from sphinx.builders import Builder # NOQA
|
|
||||||
from sphinx.theming import Theme # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
def _tobool(val):
|
def _tobool(val: str) -> bool:
|
||||||
# type: (str) -> bool
|
|
||||||
if isinstance(val, str):
|
if isinstance(val, str):
|
||||||
return val.lower() in ('true', '1', 'yes', 'on')
|
return val.lower() in ('true', '1', 'yes', 'on')
|
||||||
return bool(val)
|
return bool(val)
|
||||||
|
|
||||||
|
|
||||||
def _toint(val):
|
def _toint(val: str) -> int:
|
||||||
# type: (str) -> int
|
|
||||||
try:
|
try:
|
||||||
return int(val)
|
return int(val)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _todim(val):
|
def _todim(val: Union[int, str]) -> str:
|
||||||
# type: (Union[int, str]) -> str
|
|
||||||
"""
|
"""
|
||||||
Make val a css dimension. In particular the following transformations
|
Make val a css dimension. In particular the following transformations
|
||||||
are performed:
|
are performed:
|
||||||
@ -63,8 +58,7 @@ def _todim(val):
|
|||||||
return val # type: ignore
|
return val # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def _slice_index(values, slices):
|
def _slice_index(values: List, slices: int) -> Iterator[List]:
|
||||||
# type: (List, int) -> Iterator[List]
|
|
||||||
seq = list(values)
|
seq = list(values)
|
||||||
length = 0
|
length = 0
|
||||||
for value in values:
|
for value in values:
|
||||||
@ -85,8 +79,7 @@ def _slice_index(values, slices):
|
|||||||
yield seq[start:offset]
|
yield seq[start:offset]
|
||||||
|
|
||||||
|
|
||||||
def accesskey(context, key):
|
def accesskey(context: Any, key: str) -> str:
|
||||||
# type: (Any, str) -> str
|
|
||||||
"""Helper to output each access key only once."""
|
"""Helper to output each access key only once."""
|
||||||
if '_accesskeys' not in context:
|
if '_accesskeys' not in context:
|
||||||
context.vars['_accesskeys'] = {}
|
context.vars['_accesskeys'] = {}
|
||||||
@ -97,24 +90,20 @@ def accesskey(context, key):
|
|||||||
|
|
||||||
|
|
||||||
class idgen:
|
class idgen:
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
self.id = 0
|
self.id = 0
|
||||||
|
|
||||||
def current(self):
|
def current(self) -> int:
|
||||||
# type: () -> int
|
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self) -> int:
|
||||||
# type: () -> int
|
|
||||||
self.id += 1
|
self.id += 1
|
||||||
return self.id
|
return self.id
|
||||||
next = __next__ # Python 2/Jinja compatibility
|
next = __next__ # Python 2/Jinja compatibility
|
||||||
|
|
||||||
|
|
||||||
@contextfunction
|
@contextfunction
|
||||||
def warning(context, message, *args, **kwargs):
|
def warning(context: Dict, message: str, *args, **kwargs) -> str:
|
||||||
# type: (Dict, str, Any, Any) -> str
|
|
||||||
if 'pagename' in context:
|
if 'pagename' in context:
|
||||||
filename = context.get('pagename') + context.get('file_suffix', '')
|
filename = context.get('pagename') + context.get('file_suffix', '')
|
||||||
message = 'in rendering %s: %s' % (filename, message)
|
message = 'in rendering %s: %s' % (filename, message)
|
||||||
@ -129,8 +118,7 @@ class SphinxFileSystemLoader(FileSystemLoader):
|
|||||||
template names.
|
template names.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get_source(self, environment, template):
|
def get_source(self, environment: Environment, template: str) -> Tuple[str, str, Callable]:
|
||||||
# type: (Environment, str) -> Tuple[str, str, Callable]
|
|
||||||
for searchpath in self.searchpath:
|
for searchpath in self.searchpath:
|
||||||
filename = path.join(searchpath, template)
|
filename = path.join(searchpath, template)
|
||||||
f = open_if_exists(filename)
|
f = open_if_exists(filename)
|
||||||
@ -141,8 +129,7 @@ class SphinxFileSystemLoader(FileSystemLoader):
|
|||||||
|
|
||||||
mtime = path.getmtime(filename)
|
mtime = path.getmtime(filename)
|
||||||
|
|
||||||
def uptodate():
|
def uptodate() -> bool:
|
||||||
# type: () -> bool
|
|
||||||
try:
|
try:
|
||||||
return path.getmtime(filename) == mtime
|
return path.getmtime(filename) == mtime
|
||||||
except OSError:
|
except OSError:
|
||||||
@ -158,8 +145,7 @@ class BuiltinTemplateLoader(TemplateBridge, BaseLoader):
|
|||||||
|
|
||||||
# TemplateBridge interface
|
# TemplateBridge interface
|
||||||
|
|
||||||
def init(self, builder, theme=None, dirs=None):
|
def init(self, builder: "Builder", theme: Theme = None, dirs: List[str] = None) -> None:
|
||||||
# type: (Builder, Theme, List[str]) -> None
|
|
||||||
# create a chain of paths to search
|
# create a chain of paths to search
|
||||||
if theme:
|
if theme:
|
||||||
# the theme's own dir and its bases' dirs
|
# the theme's own dir and its bases' dirs
|
||||||
@ -202,22 +188,18 @@ class BuiltinTemplateLoader(TemplateBridge, BaseLoader):
|
|||||||
if use_i18n:
|
if use_i18n:
|
||||||
self.environment.install_gettext_translations(builder.app.translator) # type: ignore # NOQA
|
self.environment.install_gettext_translations(builder.app.translator) # type: ignore # NOQA
|
||||||
|
|
||||||
def render(self, template, context): # type: ignore
|
def render(self, template: str, context: Dict) -> str: # type: ignore
|
||||||
# type: (str, Dict) -> str
|
|
||||||
return self.environment.get_template(template).render(context)
|
return self.environment.get_template(template).render(context)
|
||||||
|
|
||||||
def render_string(self, source, context):
|
def render_string(self, source: str, context: Dict) -> str:
|
||||||
# type: (str, Dict) -> str
|
|
||||||
return self.environment.from_string(source).render(context)
|
return self.environment.from_string(source).render(context)
|
||||||
|
|
||||||
def newest_template_mtime(self):
|
def newest_template_mtime(self) -> float:
|
||||||
# type: () -> float
|
|
||||||
return max(mtimes_of_files(self.pathchain, '.html'))
|
return max(mtimes_of_files(self.pathchain, '.html'))
|
||||||
|
|
||||||
# Loader interface
|
# Loader interface
|
||||||
|
|
||||||
def get_source(self, environment, template):
|
def get_source(self, environment: Environment, template: str) -> Tuple[str, str, Callable]:
|
||||||
# type: (Environment, str) -> Tuple[str, str, Callable]
|
|
||||||
loaders = self.loaders
|
loaders = self.loaders
|
||||||
# exclamation mark starts search from theme
|
# exclamation mark starts search from theme
|
||||||
if template.startswith('!'):
|
if template.startswith('!'):
|
||||||
|
Loading…
Reference in New Issue
Block a user