<s> """<STR_LIT>""" <EOL> from os import path <EOL> from pprint import pformat <EOL> from six import string_types <EOL> from jinja2 import FileSystemLoader , BaseLoader , TemplateNotFound , contextfunction <EOL> from jinja2 . utils import open_if_exists <EOL> from jinja2 . sandbox import SandboxedEnvironment <EOL> from sphinx . application import TemplateBridge <EOL> from sphinx . util . osutil import mtimes_of_files <EOL> def _tobool ( val ) : <EOL> if isinstance ( val , string_types ) : <EOL> return val . lower ( ) in ( '<STR_LIT:true>' , '<STR_LIT:1>' , '<STR_LIT:yes>' , '<STR_LIT>' ) <EOL> return bool ( val ) <EOL> def _toint ( val ) : <EOL> try : <EOL> return int ( val ) <EOL> except ValueError : <EOL> return <NUM_LIT:0> <EOL> def accesskey ( context , key ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in context : <EOL> context . vars [ '<STR_LIT>' ] = { } <EOL> if key and key not in context . vars [ '<STR_LIT>' ] : <EOL> context . vars [ '<STR_LIT>' ] [ key ] = <NUM_LIT:1> <EOL> return '<STR_LIT>' % key <EOL> return '<STR_LIT>' <EOL> class idgen ( object ) : <EOL> def __init__ ( self ) : <EOL> self . id = <NUM_LIT:0> <EOL> def current ( self ) : <EOL> return self . id <EOL> def __next__ ( self ) : <EOL> self . id += <NUM_LIT:1> <EOL> return self . id <EOL> next = __next__ <EOL> class SphinxFileSystemLoader ( FileSystemLoader ) : <EOL> """<STR_LIT>""" <EOL> def get_source ( self , environment , template ) : <EOL> for searchpath in self . searchpath : <EOL> filename = path . join ( searchpath , template ) <EOL> f = open_if_exists ( filename ) <EOL> if f is None : <EOL> continue <EOL> try : <EOL> contents = f . read ( ) . decode ( self . encoding ) <EOL> finally : <EOL> f . close ( ) <EOL> mtime = path . getmtime ( filename ) <EOL> def uptodate ( ) : <EOL> try : <EOL> return path . getmtime ( filename ) == mtime <EOL> except OSError : <EOL> return False <EOL> return contents , filename , uptodate <EOL> raise TemplateNotFound ( template ) <EOL> class BuiltinTemplateLoader ( TemplateBridge , BaseLoader ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , builder , theme = None , dirs = None ) : <EOL> if theme : <EOL> pathchain = theme . get_dirchain ( ) <EOL> loaderchain = pathchain + theme . themepath <EOL> elif dirs : <EOL> pathchain = list ( dirs ) <EOL> loaderchain = list ( dirs ) <EOL> else : <EOL> pathchain = [ ] <EOL> loaderchain = [ ] <EOL> self . templatepathlen = len ( builder . config . templates_path ) <EOL> if builder . config . templates_path : <EOL> cfg_templates_path = [ path . join ( builder . confdir , tp ) <EOL> for tp in builder . config . templates_path ] <EOL> pathchain [ <NUM_LIT:0> : <NUM_LIT:0> ] = cfg_templates_path <EOL> loaderchain [ <NUM_LIT:0> : <NUM_LIT:0> ] = cfg_templates_path <EOL> self . pathchain = pathchain <EOL> self . loaders = [ SphinxFileSystemLoader ( x ) for x in loaderchain ] <EOL> use_i18n = builder . app . translator is not None <EOL> extensions = use_i18n and [ '<STR_LIT>' ] or [ ] <EOL> self . environment = SandboxedEnvironment ( loader = self , <EOL> extensions = extensions ) <EOL> self . environment . filters [ '<STR_LIT>' ] = _tobool <EOL> self . environment . filters [ '<STR_LIT>' ] = _toint <EOL> self . environment . globals [ '<STR_LIT>' ] = contextfunction ( pformat ) <EOL> self . environment . globals [ '<STR_LIT>' ] = contextfunction ( accesskey ) <EOL> self . environment . globals [ '<STR_LIT>' ] = idgen <EOL> if use_i18n : <EOL> self . environment . install_gettext_translations ( <EOL> builder . app . translator ) <EOL> def render ( self , template , context ) : <EOL> return self . environment . get_template ( template ) . render ( context ) <EOL> def render_string ( self , source , context ) : <EOL> return self . environment . from_string ( source ) . render ( context ) <EOL> def newest_template_mtime ( self ) : <EOL> return max ( mtimes_of_files ( self . pathchain , '<STR_LIT>' ) ) <EOL> def get_source ( self , environment , template ) : <EOL> loaders = self . loaders <EOL> if template . startswith ( '<STR_LIT:!>' ) : <EOL> loaders = loaders [ self . templatepathlen : ] <EOL> template = template [ <NUM_LIT:1> : ] <EOL> for loader in loaders : <EOL> try : <EOL> return loader . get_source ( environment , template ) <EOL> except TemplateNotFound : <EOL> pass <EOL> raise TemplateNotFound ( template ) </s>
