# -*- coding: utf-8 -*-"""Classes for managing templates and their runtime and compile timeoptions."""import osimport sysimport weakreffrom functools import partialfrom functools import reducefrom markupsafe import Markupfrom . import nodesfrom ._compat import encode_filenamefrom ._compat import implements_iteratorfrom ._compat import implements_to_stringfrom ._compat import iteritemsfrom ._compat import PY2from ._compat import PYPYfrom ._compat import reraisefrom ._compat import string_typesfrom ._compat import text_typefrom .compiler import CodeGeneratorfrom .compiler import generatefrom .defaults import BLOCK_END_STRINGfrom .defaults import BLOCK_START_STRINGfrom .defaults import COMMENT_END_STRINGfrom .defaults import COMMENT_START_STRINGfrom .defaults import DEFAULT_FILTERSfrom .defaults import DEFAULT_NAMESPACEfrom .defaults import DEFAULT_POLICIESfrom .defaults import DEFAULT_TESTSfrom .defaults import KEEP_TRAILING_NEWLINEfrom .defaults import LINE_COMMENT_PREFIXfrom .defaults import LINE_STATEMENT_PREFIXfrom .defaults import LSTRIP_BLOCKSfrom .defaults import NEWLINE_SEQUENCEfrom .defaults import TRIM_BLOCKSfrom .defaults import VARIABLE_END_STRINGfrom .defaults import VARIABLE_START_STRINGfrom .exceptions import TemplateNotFoundfrom .exceptions import TemplateRuntimeErrorfrom .exceptions import TemplatesNotFoundfrom .exceptions import TemplateSyntaxErrorfrom .exceptions import UndefinedErrorfrom .lexer import get_lexerfrom .lexer import TokenStreamfrom .nodes import EvalContextfrom .parser import Parserfrom .runtime import Contextfrom .runtime import new_contextfrom .runtime import Undefinedfrom .utils import concatfrom .utils import consumefrom .utils import have_async_genfrom .utils import import_stringfrom .utils import internalcodefrom .utils import LRUCachefrom .utils import missing# for direct template usage we have up to ten living environments_spontaneous_environments = LRUCache(10)def get_spontaneous_environment(cls, *args):"""Return a new spontaneous environment. A spontaneous environmentis used for templates created directly rather than through anexisting environment.:param cls: Environment class to create.:param args: Positional arguments passed to environment."""key = (cls, args)try:return _spontaneous_environments[key]except KeyError:_spontaneous_environments[key] = env = cls(*args)env.shared = Truereturn envdef create_cache(size):"""Return the cache class for the given size."""if size == 0:return Noneif size < 0:return {}return LRUCache(size)def copy_cache(cache):"""Create an empty copy of the given cache."""if cache is None:return Noneelif type(cache) is dict:return {}return LRUCache(cache.capacity)def load_extensions(environment, extensions):"""Load the extensions from the list and bind it to the environment.Returns a dict of instantiated environments."""result = {}for extension in extensions:if isinstance(extension, string_types):extension = import_string(extension)result[extension.identifier] = extension(environment)return resultdef fail_for_missing_callable(string, name):msg = string % nameif isinstance(name, Undefined):try:name._fail_with_undefined_error()except Exception as e:msg = "%s (%s; did you forget to quote the callable name?)" % (msg, e)raise TemplateRuntimeError(msg)def _environment_sanity_check(environment):"""Perform a sanity check on the environment."""assert issubclass(environment.undefined, Undefined), "undefined must be a subclass of undefined because filters depend on it."assert (environment.block_start_string!= environment.variable_start_string!= environment.comment_start_string), "block, variable and comment start strings must be different"assert environment.newline_sequence in ("\r","\r\n","\n",), "newline_sequence set to unknown line ending string."return environmentclass Environment(object):r"""The core component of Jinja is the `Environment`. It containsimportant shared variables like configuration, filters, tests,globals and others. Instances of this class may be modified ifthey are not shared and if no template was loaded so far.Modifications on environments after the first template was loadedwill lead to surprising effects and undefined behavior.Here are the possible initialization parameters:`block_start_string`The string marking the beginning of a block. Defaults to ``'{%'``.`block_end_string`The string marking the end of a block. Defaults to ``'%}'``.`variable_start_string`The string marking the beginning of a print statement.Defaults to ``'{{'``.`variable_end_string`The string marking the end of a print statement. Defaults to``'}}'``.`comment_start_string`The string marking the beginning of a comment. Defaults to ``'{#'``.`comment_end_string`The string marking the end of a comment. Defaults to ``'#}'``.`line_statement_prefix`If given and a string, this will be used as prefix for line basedstatements. See also :ref:`line-statements`.`line_comment_prefix`If given and a string, this will be used as prefix for line basedcomments. See also :ref:`line-statements`... versionadded:: 2.2`trim_blocks`If this is set to ``True`` the first newline after a block isremoved (block, not variable tag!). Defaults to `False`.`lstrip_blocks`If this is set to ``True`` leading spaces and tabs are strippedfrom the start of a line to a block. Defaults to `False`.`newline_sequence`The sequence that starts a newline. Must be one of ``'\r'``,``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is auseful default for Linux and OS X systems as well as webapplications.`keep_trailing_newline`Preserve the trailing newline when rendering templates.The default is ``False``, which causes a single newline,if present, to be stripped from the end of the template... versionadded:: 2.7`extensions`List of Jinja extensions to use. This can either be import pathsas strings or extension classes. For more information have alook at :ref:`the extensions documentation <jinja-extensions>`.`optimized`should the optimizer be enabled? Default is ``True``.`undefined`:class:`Undefined` or a subclass of it that is used to representundefined values in the template.`finalize`A callable that can be used to process the result of a variableexpression before it is output. For example one can convert``None`` implicitly into an empty string here.`autoescape`If set to ``True`` the XML/HTML autoescaping feature is enabled bydefault. For more details about autoescaping see:class:`~markupsafe.Markup`. As of Jinja 2.4 this can alsobe a callable that is passed the template name and has toreturn ``True`` or ``False`` depending on autoescape should beenabled by default... versionchanged:: 2.4`autoescape` can now be a function`loader`The template loader for this environment.`cache_size`The size of the cache. Per default this is ``400`` which meansthat if more than 400 templates are loaded the loader will cleanout the least recently used template. If the cache size is set to``0`` templates are recompiled all the time, if the cache size is``-1`` the cache will not be cleaned... versionchanged:: 2.8The cache size was increased to 400 from a low 50.`auto_reload`Some loaders load templates from locations where the templatesources may change (ie: file system or database). If``auto_reload`` is set to ``True`` (default) every time a template isrequested the loader checks if the source changed and if yes, itwill reload the template. For higher performance it's possible todisable that.`bytecode_cache`If set to a bytecode cache object, this object will provide acache for the internal Jinja bytecode so that templates don'thave to be parsed if they were not changed.See :ref:`bytecode-cache` for more information.`enable_async`If set to true this enables async template execution which allowsyou to take advantage of newer Python features. This requiresPython 3.6 or later."""#: if this environment is sandboxed. Modifying this variable won't make#: the environment sandboxed though. For a real sandboxed environment#: have a look at jinja2.sandbox. This flag alone controls the code#: generation by the compiler.sandboxed = False#: True if the environment is just an overlayoverlayed = False#: the environment this environment is linked to if it is an overlaylinked_to = None#: shared environments have this set to `True`. A shared environment#: must not be modifiedshared = False#: the class that is used for code generation. See#: :class:`~jinja2.compiler.CodeGenerator` for more information.code_generator_class = CodeGenerator#: the context class thatis used for templates. See#: :class:`~jinja2.runtime.Context` for more information.context_class = Contextdef __init__(self,block_start_string=BLOCK_START_STRING,block_end_string=BLOCK_END_STRING,variable_start_string=VARIABLE_START_STRING,variable_end_string=VARIABLE_END_STRING,comment_start_string=COMMENT_START_STRING,comment_end_string=COMMENT_END_STRING,line_statement_prefix=LINE_STATEMENT_PREFIX,line_comment_prefix=LINE_COMMENT_PREFIX,trim_blocks=TRIM_BLOCKS,lstrip_blocks=LSTRIP_BLOCKS,newline_sequence=NEWLINE_SEQUENCE,keep_trailing_newline=KEEP_TRAILING_NEWLINE,extensions=(),optimized=True,undefined=Undefined,finalize=None,autoescape=False,loader=None,cache_size=400,auto_reload=True,bytecode_cache=None,enable_async=False,):# !!Important notice!!# The constructor accepts quite a few arguments that should be# passed by keyword rather than position. However it's important to# not change the order of arguments because it's used at least# internally in those cases:# - spontaneous environments (i18n extension and Template)# - unittests# If parameter changes are required only add parameters at the end# and don't change the arguments (or the defaults!) of the arguments# existing already.# lexer / parser informationself.block_start_string = block_start_stringself.block_end_string = block_end_stringself.variable_start_string = variable_start_stringself.variable_end_string = variable_end_stringself.comment_start_string = comment_start_stringself.comment_end_string = comment_end_stringself.line_statement_prefix = line_statement_prefixself.line_comment_prefix = line_comment_prefixself.trim_blocks = trim_blocksself.lstrip_blocks = lstrip_blocksself.newline_sequence = newline_sequenceself.keep_trailing_newline = keep_trailing_newline# runtime informationself.undefined = undefinedself.optimized = optimizedself.finalize = finalizeself.autoescape = autoescape# defaultsself.filters = DEFAULT_FILTERS.copy()self.tests = DEFAULT_TESTS.copy()self.globals = DEFAULT_NAMESPACE.copy()# set the loader providedself.loader = loaderself.cache = create_cache(cache_size)self.bytecode_cache = bytecode_cacheself.auto_reload = auto_reload# configurable policiesself.policies = DEFAULT_POLICIES.copy()# load extensionsself.extensions = load_extensions(self, extensions)self.enable_async = enable_asyncself.is_async = self.enable_async and have_async_genif self.is_async:# runs patch_all() to enable async supportfrom . import asyncsupport # noqa: F401_environment_sanity_check(self)def add_extension(self, extension):"""Adds an extension after the environment was created... versionadded:: 2.5"""self.extensions.update(load_extensions(self, [extension]))def extend(self, **attributes):"""Add the items to the instance of the environment if they do not existyet. This is used by :ref:`extensions <writing-extensions>` to registercallbacks and configuration values without breaking inheritance."""for key, value in iteritems(attributes):if not hasattr(self, key):setattr(self, key, value)def overlay(self,block_start_string=missing,block_end_string=missing,variable_start_string=missing,variable_end_string=missing,comment_start_string=missing,comment_end_string=missing,line_statement_prefix=missing,line_comment_prefix=missing,trim_blocks=missing,lstrip_blocks=missing,extensions=missing,optimized=missing,undefined=missing,finalize=missing,autoescape=missing,loader=missing,cache_size=missing,auto_reload=missing,bytecode_cache=missing,):"""Create a new overlay environment that shares all the data with thecurrent environment except for cache and the overridden attributes.Extensions cannot be removed for an overlayed environment. An overlayedenvironment automatically gets all the extensions of the environment itis linked to plus optional extra extensions.Creating overlays should happen after the initial environment was setup completely. Not all attributes are truly linked, some are justcopied over so modifications on the original environment may not shinethrough."""args = dict(locals())del args["self"], args["cache_size"], args["extensions"]rv = object.__new__(self.__class__)rv.__dict__.update(self.__dict__)rv.overlayed = Truerv.linked_to = selffor key, value in iteritems(args):if value is not missing:setattr(rv, key, value)if cache_size is not missing:rv.cache = create_cache(cache_size)else:rv.cache = copy_cache(self.cache)rv.extensions = {}for key, value in iteritems(self.extensions):rv.extensions[key] = value.bind(rv)if extensions is not missing:rv.extensions.update(load_extensions(rv, extensions))return _environment_sanity_check(rv)lexer = property(get_lexer, doc="The lexer for this environment.")def iter_extensions(self):"""Iterates over the extensions by priority."""return iter(sorted(self.extensions.values(), key=lambda x: x.priority))def getitem(self, obj, argument):"""Get an item or attribute of an object but prefer the item."""try:return obj[argument]except (AttributeError, TypeError, LookupError):if isinstance(argument, string_types):try:attr = str(argument)except Exception:passelse:try:return getattr(obj, attr)except AttributeError:passreturn self.undefined(obj=obj, name=argument)def getattr(self, obj, attribute):"""Get an item or attribute of an object but prefer the attribute.Unlike :meth:`getitem` the attribute *must* be a bytestring."""try:return getattr(obj, attribute)except AttributeError:passtry:return obj[attribute]except (TypeError, LookupError, AttributeError):return self.undefined(obj=obj, name=attribute)def call_filter(self, name, value, args=None, kwargs=None, context=None, eval_ctx=None):"""Invokes a filter on a value the same way the compiler does it.Note that on Python 3 this might return a coroutine in case thefilter is running from an environment in async mode and the filtersupports async execution. It's your responsibility to await thisif needed... versionadded:: 2.7"""func = self.filters.get(name)if func is None:fail_for_missing_callable("no filter named %r", name)args = [value] + list(args or ())if getattr(func, "contextfilter", False) is True:if context is None:raise TemplateRuntimeError("Attempted to invoke context filter without context")args.insert(0, context)elif getattr(func, "evalcontextfilter", False) is True:if eval_ctx is None:if context is not None:eval_ctx = context.eval_ctxelse:eval_ctx = EvalContext(self)args.insert(0, eval_ctx)elif getattr(func, "environmentfilter", False) is True:args.insert(0, self)return func(*args, **(kwargs or {}))def call_test(self, name, value, args=None, kwargs=None):"""Invokes a test on a value the same way the compiler does it... versionadded:: 2.7"""func = self.tests.get(name)if func is None:fail_for_missing_callable("no test named %r", name)return func(value, *(args or ()), **(kwargs or {}))@internalcodedef parse(self, source, name=None, filename=None):"""Parse the sourcecode and return the abstract syntax tree. Thistree of nodes is used by the compiler to convert the template intoexecutable source- or bytecode. This is useful for debugging or toextract information from templates.If you are :ref:`developing Jinja extensions <writing-extensions>`this gives you a good overview of the node tree generated."""try:return self._parse(source, name, filename)except TemplateSyntaxError:self.handle_exception(source=source)def _parse(self, source, name, filename):"""Internal parsing function used by `parse` and `compile`."""return Parser(self, source, name, encode_filename(filename)).parse()def lex(self, source, name=None, filename=None):"""Lex the given sourcecode and return a generator that yieldstokens as tuples in the form ``(lineno, token_type, value)``.This can be useful for :ref:`extension development <writing-extensions>`and debugging templates.This does not perform preprocessing. If you want the preprocessingof the extensions to be applied you have to filter source throughthe :meth:`preprocess` method."""source = text_type(source)try:return self.lexer.tokeniter(source, name, filename)except TemplateSyntaxError:self.handle_exception(source=source)def preprocess(self, source, name=None, filename=None):"""Preprocesses the source with all extensions. This is automaticallycalled for all parsing and compiling methods but *not* for :meth:`lex`because there you usually only want the actual source tokenized."""return reduce(lambda s, e: e.preprocess(s, name, filename),self.iter_extensions(),text_type(source),)def _tokenize(self, source, name, filename=None, state=None):"""Called by the parser to do the preprocessing and filteringfor all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`."""source = self.preprocess(source, name, filename)stream = self.lexer.tokenize(source, name, filename, state)for ext in self.iter_extensions():stream = ext.filter_stream(stream)if not isinstance(stream, TokenStream):stream = TokenStream(stream, name, filename)return streamdef _generate(self, source, name, filename, defer_init=False):"""Internal hook that can be overridden to hook a different generatemethod in... versionadded:: 2.5"""return generate(source,self,name,filename,defer_init=defer_init,optimized=self.optimized,)def _compile(self, source, filename):"""Internal hook that can be overridden to hook a different compilemethod in... versionadded:: 2.5"""return compile(source, filename, "exec")@internalcodedef compile(self, source, name=None, filename=None, raw=False, defer_init=False):"""Compile a node or template source code. The `name` parameter isthe load name of the template after it was joined using:meth:`join_path` if necessary, not the filename on the file system.the `filename` parameter is the estimated filename of the template onthe file system. If the template came from a database or memory thiscan be omitted.The return value of this method is a python code object. If the `raw`parameter is `True` the return value will be a string with pythoncode equivalent to the bytecode returned otherwise. This method ismainly used internally.`defer_init` is use internally to aid the module code generator. Thiscauses the generated code to be able to import without the globalenvironment variable to be set... versionadded:: 2.4`defer_init` parameter added."""source_hint = Nonetry:if isinstance(source, string_types):source_hint = sourcesource = self._parse(source, name, filename)source = self._generate(source, name, filename, defer_init=defer_init)if raw:return sourceif filename is None:filename = "<template>"else:filename = encode_filename(filename)return self._compile(source, filename)except TemplateSyntaxError:self.handle_exception(source=source_hint)def compile_expression(self, source, undefined_to_none=True):"""A handy helper method that returns a callable that accepts keywordarguments that appear as variables in the expression. If called itreturns the result of the expression.This is useful if applications want to use the same rules as Jinjain template "configuration files" or similar situations.Example usage:>>> env = Environment()>>> expr = env.compile_expression('foo == 42')>>> expr(foo=23)False>>> expr(foo=42)TruePer default the return value is converted to `None` if theexpression returns an undefined value. This can be changedby setting `undefined_to_none` to `False`.>>> env.compile_expression('var')() is NoneTrue>>> env.compile_expression('var', undefined_to_none=False)()Undefined.. versionadded:: 2.1"""parser = Parser(self, source, state="variable")try:expr = parser.parse_expression()if not parser.stream.eos:raise TemplateSyntaxError("chunk after expression", parser.stream.current.lineno, None, None)expr.set_environment(self)except TemplateSyntaxError:if sys.exc_info() is not None:self.handle_exception(source=source)body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]template = self.from_string(nodes.Template(body, lineno=1))return TemplateExpression(template, undefined_to_none)def compile_templates(self,target,extensions=None,filter_func=None,zip="deflated",log_function=None,ignore_errors=True,py_compile=False,):"""Finds all the templates the loader can find, compiles themand stores them in `target`. If `zip` is `None`, instead of in azipfile, the templates will be stored in a directory.By default a deflate zip algorithm is used. To switch tothe stored algorithm, `zip` can be set to ``'stored'``.`extensions` and `filter_func` are passed to :meth:`list_templates`.Each template returned will be compiled to the target folder orzipfile.By default template compilation errors are ignored. In case alog function is provided, errors are logged. If you want templatesyntax errors to abort the compilation you can set `ignore_errors`to `False` and you will get an exception on syntax errors.If `py_compile` is set to `True` .pyc files will be written to thetarget instead of standard .py files. This flag does not do anythingon pypy and Python 3 where pyc files are not picked up by itself anddon't give much benefit... versionadded:: 2.4"""from .loaders import ModuleLoaderif log_function is None:def log_function(x):passif py_compile:if not PY2 or PYPY:import warningswarnings.warn("'py_compile=True' has no effect on PyPy or Python"" 3 and will be removed in version 3.0",DeprecationWarning,stacklevel=2,)py_compile = Falseelse:import impimport marshalpy_header = imp.get_magic() + u"\xff\xff\xff\xff".encode("iso-8859-15")# Python 3.3 added a source filesize to the headerif sys.version_info >= (3, 3):py_header += u"\x00\x00\x00\x00".encode("iso-8859-15")def write_file(filename, data):if zip:info = ZipInfo(filename)info.external_attr = 0o755 << 16zip_file.writestr(info, data)else:if isinstance(data, text_type):data = data.encode("utf8")with open(os.path.join(target, filename), "wb") as f:f.write(data)if zip is not None:from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED, ZIP_STOREDzip_file = ZipFile(target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip])log_function('Compiling into Zip archive "%s"' % target)else:if not os.path.isdir(target):os.makedirs(target)log_function('Compiling into folder "%s"' % target)try:for name in self.list_templates(extensions, filter_func):source, filename, _ = self.loader.get_source(self, name)try:code = self.compile(source, name, filename, True, True)except TemplateSyntaxError as e:if not ignore_errors:raiselog_function('Could not compile "%s": %s' % (name, e))continuefilename = ModuleLoader.get_module_filename(name)if py_compile:c = self._compile(code, encode_filename(filename))write_file(filename + "c", py_header + marshal.dumps(c))log_function('Byte-compiled "%s" as %s' % (name, filename + "c"))else:write_file(filename, code)log_function('Compiled "%s" as %s' % (name, filename))finally:if zip:zip_file.close()log_function("Finished compiling templates")def list_templates(self, extensions=None, filter_func=None):"""Returns a list of templates for this environment. This requiresthat the loader supports the loader's:meth:`~BaseLoader.list_templates` method.If there are other files in the template folder besides theactual templates, the returned list can be filtered. There are twoways: either `extensions` is set to a list of file extensions fortemplates, or a `filter_func` can be provided which is a callable thatis passed a template name and should return `True` if it should end upin the result list.If the loader does not support that, a :exc:`TypeError` is raised... versionadded:: 2.4"""names = self.loader.list_templates()if extensions is not None:if filter_func is not None:raise TypeError("either extensions or filter_func can be passed, but not both")def filter_func(x):return "." in x and x.rsplit(".", 1)[1] in extensionsif filter_func is not None:names = [name for name in names if filter_func(name)]return namesdef handle_exception(self, source=None):"""Exception handling helper. This is used internally to either raiserewritten exceptions or return a rendered traceback for the template."""from .debug import rewrite_traceback_stackreraise(*rewrite_traceback_stack(source=source))def join_path(self, template, parent):"""Join a template with the parent. By default all the lookups arerelative to the loader root so this method returns the `template`parameter unchanged, but if the paths should be relative to theparent template, this function can be used to calculate the realtemplate name.Subclasses may override this method and implement template pathjoining here."""return template@internalcodedef _load_template(self, name, globals):if self.loader is None:raise TypeError("no loader for this environment specified")cache_key = (weakref.ref(self.loader), name)if self.cache is not None:template = self.cache.get(cache_key)if template is not None and (not self.auto_reload or template.is_up_to_date):return templatetemplate = self.loader.load(self, name, globals)if self.cache is not None:self.cache[cache_key] = templatereturn template@internalcodedef get_template(self, name, parent=None, globals=None):"""Load a template from the loader. If a loader is configured thismethod asks the loader for the template and returns a :class:`Template`.If the `parent` parameter is not `None`, :meth:`join_path` is calledto get the real template name before loading.The `globals` parameter can be used to provide template wide globals.These variables are available in the context at render time.If the template does not exist a :exc:`TemplateNotFound` exception israised... versionchanged:: 2.4If `name` is a :class:`Template` object it is returned from thefunction unchanged."""if isinstance(name, Template):return nameif parent is not None:name = self.join_path(name, parent)return self._load_template(name, self.make_globals(globals))@internalcodedef select_template(self, names, parent=None, globals=None):"""Works like :meth:`get_template` but tries a number of templatesbefore it fails. If it cannot find any of the templates, it willraise a :exc:`TemplatesNotFound` exception... versionchanged:: 2.11If names is :class:`Undefined`, an :exc:`UndefinedError` israised instead. If no templates were found and namescontains :class:`Undefined`, the message is more helpful... versionchanged:: 2.4If `names` contains a :class:`Template` object it is returnedfrom the function unchanged... versionadded:: 2.3"""if isinstance(names, Undefined):names._fail_with_undefined_error()if not names:raise TemplatesNotFound(message=u"Tried to select from an empty list " u"of templates.")globals = self.make_globals(globals)for name in names:if isinstance(name, Template):return nameif parent is not None:name = self.join_path(name, parent)try:return self._load_template(name, globals)except (TemplateNotFound, UndefinedError):passraise TemplatesNotFound(names)@internalcodedef get_or_select_template(self, template_name_or_list, parent=None, globals=None):"""Does a typecheck and dispatches to :meth:`select_template`if an iterable of template names is given, otherwise to:meth:`get_template`... versionadded:: 2.3"""if isinstance(template_name_or_list, (string_types, Undefined)):return self.get_template(template_name_or_list, parent, globals)elif isinstance(template_name_or_list, Template):return template_name_or_listreturn self.select_template(template_name_or_list, parent, globals)def from_string(self, source, globals=None, template_class=None):"""Load a template from a string. This parses the source given andreturns a :class:`Template` object."""globals = self.make_globals(globals)cls = template_class or self.template_classreturn cls.from_code(self, self.compile(source), globals, None)def make_globals(self, d):"""Return a dict for the globals."""if not d:return self.globalsreturn dict(self.globals, **d)class Template(object):"""The central template object. This class represents a compiled templateand is used to evaluate it.Normally the template object is generated from an :class:`Environment` butit also has a constructor that makes it possible to create a templateinstance directly using the constructor. It takes the same arguments asthe environment constructor but it's not possible to specify a loader.Every template object has a few methods and members that are guaranteedto exist. However it's important that a template object should beconsidered immutable. Modifications on the object are not supported.Template objects created from the constructor rather than an environmentdo have an `environment` attribute that points to a temporary environmentthat is probably shared with other templates created with the constructorand compatible settings.>>> template = Template('Hello {{ name }}!')>>> template.render(name='John Doe') == u'Hello John Doe!'True>>> stream = template.stream(name='John Doe')>>> next(stream) == u'Hello John Doe!'True>>> next(stream)Traceback (most recent call last):...StopIteration"""#: Type of environment to create when creating a template directly#: rather than through an existing environment.environment_class = Environmentdef __new__(cls,source,block_start_string=BLOCK_START_STRING,block_end_string=BLOCK_END_STRING,variable_start_string=VARIABLE_START_STRING,variable_end_string=VARIABLE_END_STRING,comment_start_string=COMMENT_START_STRING,comment_end_string=COMMENT_END_STRING,line_statement_prefix=LINE_STATEMENT_PREFIX,line_comment_prefix=LINE_COMMENT_PREFIX,trim_blocks=TRIM_BLOCKS,lstrip_blocks=LSTRIP_BLOCKS,newline_sequence=NEWLINE_SEQUENCE,keep_trailing_newline=KEEP_TRAILING_NEWLINE,extensions=(),optimized=True,undefined=Undefined,finalize=None,autoescape=False,enable_async=False,):env = get_spontaneous_environment(cls.environment_class,block_start_string,block_end_string,variable_start_string,variable_end_string,comment_start_string,comment_end_string,line_statement_prefix,line_comment_prefix,trim_blocks,lstrip_blocks,newline_sequence,keep_trailing_newline,frozenset(extensions),optimized,undefined,finalize,autoescape,None,0,False,None,enable_async,)return env.from_string(source, template_class=cls)@classmethoddef from_code(cls, environment, code, globals, uptodate=None):"""Creates a template object from compiled code and the globals. Thisis used by the loaders and environment to create a template object."""namespace = {"environment": environment, "__file__": code.co_filename}exec(code, namespace)rv = cls._from_namespace(environment, namespace, globals)rv._uptodate = uptodatereturn rv@classmethoddef from_module_dict(cls, environment, module_dict, globals):"""Creates a template object from a module. This is used by themodule loader to create a template object... versionadded:: 2.4"""return cls._from_namespace(environment, module_dict, globals)@classmethoddef _from_namespace(cls, environment, namespace, globals):t = object.__new__(cls)t.environment = environmentt.globals = globalst.name = namespace["name"]t.filename = namespace["__file__"]t.blocks = namespace["blocks"]# render function and modulet.root_render_func = namespace["root"]t._module = None# debug and loader helperst._debug_info = namespace["debug_info"]t._uptodate = None# store the referencenamespace["environment"] = environmentnamespace["__jinja_template__"] = treturn tdef render(self, *args, **kwargs):"""This method accepts the same arguments as the `dict` constructor:A dict, a dict subclass or some keyword arguments. If no argumentsare given the context will be empty. These two calls do the same::template.render(knights='that say nih')template.render({'knights': 'that say nih'})This will return the rendered template as unicode string."""vars = dict(*args, **kwargs)try:return concat(self.root_render_func(self.new_context(vars)))except Exception:self.environment.handle_exception()def render_async(self, *args, **kwargs):"""This works similar to :meth:`render` but returns a coroutinethat when awaited returns the entire rendered template string. Thisrequires the async feature to be enabled.Example usage::await template.render_async(knights='that say nih; asynchronously')"""# see asyncsupport for the actual implementationraise NotImplementedError("This feature is not available for this version of Python")def stream(self, *args, **kwargs):"""Works exactly like :meth:`generate` but returns a:class:`TemplateStream`."""return TemplateStream(self.generate(*args, **kwargs))def generate(self, *args, **kwargs):"""For very large templates it can be useful to not render the wholetemplate at once but evaluate each statement after another and yieldpiece for piece. This method basically does exactly that and returnsa generator that yields one item after another as unicode strings.It accepts the same arguments as :meth:`render`."""vars = dict(*args, **kwargs)try:for event in self.root_render_func(self.new_context(vars)):yield eventexcept Exception:yield self.environment.handle_exception()def generate_async(self, *args, **kwargs):"""An async version of :meth:`generate`. Works very similarly butreturns an async iterator instead."""# see asyncsupport for the actual implementationraise NotImplementedError("This feature is not available for this version of Python")def new_context(self, vars=None, shared=False, locals=None):"""Create a new :class:`Context` for this template. The varsprovided will be passed to the template. Per default the globalsare added to the context. If shared is set to `True` the datais passed as is to the context without adding the globals.`locals` can be a dict of local variables for internal usage."""return new_context(self.environment, self.name, self.blocks, vars, shared, self.globals, locals)def make_module(self, vars=None, shared=False, locals=None):"""This method works like the :attr:`module` attribute when calledwithout arguments but it will evaluate the template on every callrather than caching it. It's also possible to providea dict which is then used as context. The arguments are the sameas for the :meth:`new_context` method."""return TemplateModule(self, self.new_context(vars, shared, locals))def make_module_async(self, vars=None, shared=False, locals=None):"""As template module creation can invoke template code forasynchronous executions this method must be used instead of thenormal :meth:`make_module` one. Likewise the module attributebecomes unavailable in async mode."""# see asyncsupport for the actual implementationraise NotImplementedError("This feature is not available for this version of Python")@internalcodedef _get_default_module(self):if self._module is not None:return self._moduleself._module = rv = self.make_module()return rv@propertydef module(self):"""The template as module. This is used for imports in thetemplate runtime but is also useful if one wants to accessexported template variables from the Python layer:>>> t = Template('{% macro foo() %}42{% endmacro %}23')>>> str(t.module)'23'>>> t.module.foo() == u'42'TrueThis attribute is not available if async mode is enabled."""return self._get_default_module()def get_corresponding_lineno(self, lineno):"""Return the source line number of a line number in thegenerated bytecode as they are not in sync."""for template_line, code_line in reversed(self.debug_info):if code_line <= lineno:return template_linereturn 1@propertydef is_up_to_date(self):"""If this variable is `False` there is a newer version available."""if self._uptodate is None:return Truereturn self._uptodate()@propertydef debug_info(self):"""The debug info mapping."""if self._debug_info:return [tuple(map(int, x.split("="))) for x in self._debug_info.split("&")]return []def __repr__(self):if self.name is None:name = "memory:%x" % id(self)else:name = repr(self.name)return "<%s %s>" % (self.__class__.__name__, name)@implements_to_stringclass TemplateModule(object):"""Represents an imported template. All the exported names of thetemplate are available as attributes on this object. Additionallyconverting it into an unicode- or bytestrings renders the contents."""def __init__(self, template, context, body_stream=None):if body_stream is None:if context.environment.is_async:raise RuntimeError("Async mode requires a body stream ""to be passed to a template module. Use ""the async methods of the API you are ""using.")body_stream = list(template.root_render_func(context))self._body_stream = body_streamself.__dict__.update(context.get_exported())self.__name__ = template.namedef __html__(self):return Markup(concat(self._body_stream))def __str__(self):return concat(self._body_stream)def __repr__(self):if self.__name__ is None:name = "memory:%x" % id(self)else:name = repr(self.__name__)return "<%s %s>" % (self.__class__.__name__, name)class TemplateExpression(object):"""The :meth:`jinja2.Environment.compile_expression` method returns aninstance of this object. It encapsulates the expression-like accessto the template with an expression it wraps."""def __init__(self, template, undefined_to_none):self._template = templateself._undefined_to_none = undefined_to_nonedef __call__(self, *args, **kwargs):context = self._template.new_context(dict(*args, **kwargs))consume(self._template.root_render_func(context))rv = context.vars["result"]if self._undefined_to_none and isinstance(rv, Undefined):rv = Nonereturn rv@implements_iteratorclass TemplateStream(object):"""A template stream works pretty much like an ordinary python generatorbut it can buffer multiple items to reduce the number of total iterations.Per default the output is unbuffered which means that for every unbufferedinstruction in the template one unicode string is yielded.If buffering is enabled with a buffer size of 5, five items are combinedinto a new unicode string. This is mainly useful if you are streamingbig templates to a client via WSGI which flushes after each iteration."""def __init__(self, gen):self._gen = genself.disable_buffering()def dump(self, fp, encoding=None, errors="strict"):"""Dump the complete stream into a file or file-like object.Per default unicode strings are written, if you want to encodebefore writing specify an `encoding`.Example usage::Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')"""close = Falseif isinstance(fp, string_types):if encoding is None:encoding = "utf-8"fp = open(fp, "wb")close = Truetry:if encoding is not None:iterable = (x.encode(encoding, errors) for x in self)else:iterable = selfif hasattr(fp, "writelines"):fp.writelines(iterable)else:for item in iterable:fp.write(item)finally:if close:fp.close()def disable_buffering(self):"""Disable the output buffering."""self._next = partial(next, self._gen)self.buffered = Falsedef _buffered_generator(self, size):buf = []c_size = 0push = buf.appendwhile 1:try:while c_size < size:c = next(self._gen)push(c)if c:c_size += 1except StopIteration:if not c_size:returnyield concat(buf)del buf[:]c_size = 0def enable_buffering(self, size=5):"""Enable buffering. Buffer `size` items before yielding them."""if size <= 1:raise ValueError("buffer size too small")self.buffered = Trueself._next = partial(next, self._buffered_generator(size))def __iter__(self):return selfdef __next__(self):return self._next()# hook in default template class. if anyone reads this comment: ignore that# it's possible to use custom templates ;-)Environment.template_class = Template
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。