environment.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. # -*- coding: utf-8 -*-
  2. """Classes for managing templates and their runtime and compile time
  3. options.
  4. """
  5. import os
  6. import sys
  7. import weakref
  8. from functools import partial
  9. from functools import reduce
  10. from markupsafe import Markup
  11. from . import nodes
  12. from ._compat import encode_filename
  13. from ._compat import implements_iterator
  14. from ._compat import implements_to_string
  15. from ._compat import iteritems
  16. from ._compat import PY2
  17. from ._compat import PYPY
  18. from ._compat import reraise
  19. from ._compat import string_types
  20. from ._compat import text_type
  21. from .compiler import CodeGenerator
  22. from .compiler import generate
  23. from .defaults import BLOCK_END_STRING
  24. from .defaults import BLOCK_START_STRING
  25. from .defaults import COMMENT_END_STRING
  26. from .defaults import COMMENT_START_STRING
  27. from .defaults import DEFAULT_FILTERS
  28. from .defaults import DEFAULT_NAMESPACE
  29. from .defaults import DEFAULT_POLICIES
  30. from .defaults import DEFAULT_TESTS
  31. from .defaults import KEEP_TRAILING_NEWLINE
  32. from .defaults import LINE_COMMENT_PREFIX
  33. from .defaults import LINE_STATEMENT_PREFIX
  34. from .defaults import LSTRIP_BLOCKS
  35. from .defaults import NEWLINE_SEQUENCE
  36. from .defaults import TRIM_BLOCKS
  37. from .defaults import VARIABLE_END_STRING
  38. from .defaults import VARIABLE_START_STRING
  39. from .exceptions import TemplateNotFound
  40. from .exceptions import TemplateRuntimeError
  41. from .exceptions import TemplatesNotFound
  42. from .exceptions import TemplateSyntaxError
  43. from .exceptions import UndefinedError
  44. from .lexer import get_lexer
  45. from .lexer import TokenStream
  46. from .nodes import EvalContext
  47. from .parser import Parser
  48. from .runtime import Context
  49. from .runtime import new_context
  50. from .runtime import Undefined
  51. from .utils import concat
  52. from .utils import consume
  53. from .utils import have_async_gen
  54. from .utils import import_string
  55. from .utils import internalcode
  56. from .utils import LRUCache
  57. from .utils import missing
  58. # for direct template usage we have up to ten living environments
  59. _spontaneous_environments = LRUCache(10)
  60. def get_spontaneous_environment(cls, *args):
  61. """Return a new spontaneous environment. A spontaneous environment
  62. is used for templates created directly rather than through an
  63. existing environment.
  64. :param cls: Environment class to create.
  65. :param args: Positional arguments passed to environment.
  66. """
  67. key = (cls, args)
  68. try:
  69. return _spontaneous_environments[key]
  70. except KeyError:
  71. _spontaneous_environments[key] = env = cls(*args)
  72. env.shared = True
  73. return env
  74. def create_cache(size):
  75. """Return the cache class for the given size."""
  76. if size == 0:
  77. return None
  78. if size < 0:
  79. return {}
  80. return LRUCache(size)
  81. def copy_cache(cache):
  82. """Create an empty copy of the given cache."""
  83. if cache is None:
  84. return None
  85. elif type(cache) is dict:
  86. return {}
  87. return LRUCache(cache.capacity)
  88. def load_extensions(environment, extensions):
  89. """Load the extensions from the list and bind it to the environment.
  90. Returns a dict of instantiated environments.
  91. """
  92. result = {}
  93. for extension in extensions:
  94. if isinstance(extension, string_types):
  95. extension = import_string(extension)
  96. result[extension.identifier] = extension(environment)
  97. return result
  98. def fail_for_missing_callable(string, name):
  99. msg = string % name
  100. if isinstance(name, Undefined):
  101. try:
  102. name._fail_with_undefined_error()
  103. except Exception as e:
  104. msg = "%s (%s; did you forget to quote the callable name?)" % (msg, e)
  105. raise TemplateRuntimeError(msg)
  106. def _environment_sanity_check(environment):
  107. """Perform a sanity check on the environment."""
  108. assert issubclass(
  109. environment.undefined, Undefined
  110. ), "undefined must be a subclass of undefined because filters depend on it."
  111. assert (
  112. environment.block_start_string
  113. != environment.variable_start_string
  114. != environment.comment_start_string
  115. ), "block, variable and comment start strings must be different"
  116. assert environment.newline_sequence in (
  117. "\r",
  118. "\r\n",
  119. "\n",
  120. ), "newline_sequence set to unknown line ending string."
  121. return environment
  122. class Environment(object):
  123. r"""The core component of Jinja is the `Environment`. It contains
  124. important shared variables like configuration, filters, tests,
  125. globals and others. Instances of this class may be modified if
  126. they are not shared and if no template was loaded so far.
  127. Modifications on environments after the first template was loaded
  128. will lead to surprising effects and undefined behavior.
  129. Here are the possible initialization parameters:
  130. `block_start_string`
  131. The string marking the beginning of a block. Defaults to ``'{%'``.
  132. `block_end_string`
  133. The string marking the end of a block. Defaults to ``'%}'``.
  134. `variable_start_string`
  135. The string marking the beginning of a print statement.
  136. Defaults to ``'{{'``.
  137. `variable_end_string`
  138. The string marking the end of a print statement. Defaults to
  139. ``'}}'``.
  140. `comment_start_string`
  141. The string marking the beginning of a comment. Defaults to ``'{#'``.
  142. `comment_end_string`
  143. The string marking the end of a comment. Defaults to ``'#}'``.
  144. `line_statement_prefix`
  145. If given and a string, this will be used as prefix for line based
  146. statements. See also :ref:`line-statements`.
  147. `line_comment_prefix`
  148. If given and a string, this will be used as prefix for line based
  149. comments. See also :ref:`line-statements`.
  150. .. versionadded:: 2.2
  151. `trim_blocks`
  152. If this is set to ``True`` the first newline after a block is
  153. removed (block, not variable tag!). Defaults to `False`.
  154. `lstrip_blocks`
  155. If this is set to ``True`` leading spaces and tabs are stripped
  156. from the start of a line to a block. Defaults to `False`.
  157. `newline_sequence`
  158. The sequence that starts a newline. Must be one of ``'\r'``,
  159. ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a
  160. useful default for Linux and OS X systems as well as web
  161. applications.
  162. `keep_trailing_newline`
  163. Preserve the trailing newline when rendering templates.
  164. The default is ``False``, which causes a single newline,
  165. if present, to be stripped from the end of the template.
  166. .. versionadded:: 2.7
  167. `extensions`
  168. List of Jinja extensions to use. This can either be import paths
  169. as strings or extension classes. For more information have a
  170. look at :ref:`the extensions documentation <jinja-extensions>`.
  171. `optimized`
  172. should the optimizer be enabled? Default is ``True``.
  173. `undefined`
  174. :class:`Undefined` or a subclass of it that is used to represent
  175. undefined values in the template.
  176. `finalize`
  177. A callable that can be used to process the result of a variable
  178. expression before it is output. For example one can convert
  179. ``None`` implicitly into an empty string here.
  180. `autoescape`
  181. If set to ``True`` the XML/HTML autoescaping feature is enabled by
  182. default. For more details about autoescaping see
  183. :class:`~markupsafe.Markup`. As of Jinja 2.4 this can also
  184. be a callable that is passed the template name and has to
  185. return ``True`` or ``False`` depending on autoescape should be
  186. enabled by default.
  187. .. versionchanged:: 2.4
  188. `autoescape` can now be a function
  189. `loader`
  190. The template loader for this environment.
  191. `cache_size`
  192. The size of the cache. Per default this is ``400`` which means
  193. that if more than 400 templates are loaded the loader will clean
  194. out the least recently used template. If the cache size is set to
  195. ``0`` templates are recompiled all the time, if the cache size is
  196. ``-1`` the cache will not be cleaned.
  197. .. versionchanged:: 2.8
  198. The cache size was increased to 400 from a low 50.
  199. `auto_reload`
  200. Some loaders load templates from locations where the template
  201. sources may change (ie: file system or database). If
  202. ``auto_reload`` is set to ``True`` (default) every time a template is
  203. requested the loader checks if the source changed and if yes, it
  204. will reload the template. For higher performance it's possible to
  205. disable that.
  206. `bytecode_cache`
  207. If set to a bytecode cache object, this object will provide a
  208. cache for the internal Jinja bytecode so that templates don't
  209. have to be parsed if they were not changed.
  210. See :ref:`bytecode-cache` for more information.
  211. `enable_async`
  212. If set to true this enables async template execution which allows
  213. you to take advantage of newer Python features. This requires
  214. Python 3.6 or later.
  215. """
  216. #: if this environment is sandboxed. Modifying this variable won't make
  217. #: the environment sandboxed though. For a real sandboxed environment
  218. #: have a look at jinja2.sandbox. This flag alone controls the code
  219. #: generation by the compiler.
  220. sandboxed = False
  221. #: True if the environment is just an overlay
  222. overlayed = False
  223. #: the environment this environment is linked to if it is an overlay
  224. linked_to = None
  225. #: shared environments have this set to `True`. A shared environment
  226. #: must not be modified
  227. shared = False
  228. #: the class that is used for code generation. See
  229. #: :class:`~jinja2.compiler.CodeGenerator` for more information.
  230. code_generator_class = CodeGenerator
  231. #: the context class thatis used for templates. See
  232. #: :class:`~jinja2.runtime.Context` for more information.
  233. context_class = Context
  234. def __init__(
  235. self,
  236. block_start_string=BLOCK_START_STRING,
  237. block_end_string=BLOCK_END_STRING,
  238. variable_start_string=VARIABLE_START_STRING,
  239. variable_end_string=VARIABLE_END_STRING,
  240. comment_start_string=COMMENT_START_STRING,
  241. comment_end_string=COMMENT_END_STRING,
  242. line_statement_prefix=LINE_STATEMENT_PREFIX,
  243. line_comment_prefix=LINE_COMMENT_PREFIX,
  244. trim_blocks=TRIM_BLOCKS,
  245. lstrip_blocks=LSTRIP_BLOCKS,
  246. newline_sequence=NEWLINE_SEQUENCE,
  247. keep_trailing_newline=KEEP_TRAILING_NEWLINE,
  248. extensions=(),
  249. optimized=True,
  250. undefined=Undefined,
  251. finalize=None,
  252. autoescape=False,
  253. loader=None,
  254. cache_size=400,
  255. auto_reload=True,
  256. bytecode_cache=None,
  257. enable_async=False,
  258. ):
  259. # !!Important notice!!
  260. # The constructor accepts quite a few arguments that should be
  261. # passed by keyword rather than position. However it's important to
  262. # not change the order of arguments because it's used at least
  263. # internally in those cases:
  264. # - spontaneous environments (i18n extension and Template)
  265. # - unittests
  266. # If parameter changes are required only add parameters at the end
  267. # and don't change the arguments (or the defaults!) of the arguments
  268. # existing already.
  269. # lexer / parser information
  270. self.block_start_string = block_start_string
  271. self.block_end_string = block_end_string
  272. self.variable_start_string = variable_start_string
  273. self.variable_end_string = variable_end_string
  274. self.comment_start_string = comment_start_string
  275. self.comment_end_string = comment_end_string
  276. self.line_statement_prefix = line_statement_prefix
  277. self.line_comment_prefix = line_comment_prefix
  278. self.trim_blocks = trim_blocks
  279. self.lstrip_blocks = lstrip_blocks
  280. self.newline_sequence = newline_sequence
  281. self.keep_trailing_newline = keep_trailing_newline
  282. # runtime information
  283. self.undefined = undefined
  284. self.optimized = optimized
  285. self.finalize = finalize
  286. self.autoescape = autoescape
  287. # defaults
  288. self.filters = DEFAULT_FILTERS.copy()
  289. self.tests = DEFAULT_TESTS.copy()
  290. self.globals = DEFAULT_NAMESPACE.copy()
  291. # set the loader provided
  292. self.loader = loader
  293. self.cache = create_cache(cache_size)
  294. self.bytecode_cache = bytecode_cache
  295. self.auto_reload = auto_reload
  296. # configurable policies
  297. self.policies = DEFAULT_POLICIES.copy()
  298. # load extensions
  299. self.extensions = load_extensions(self, extensions)
  300. self.enable_async = enable_async
  301. self.is_async = self.enable_async and have_async_gen
  302. if self.is_async:
  303. # runs patch_all() to enable async support
  304. from . import asyncsupport # noqa: F401
  305. _environment_sanity_check(self)
  306. def add_extension(self, extension):
  307. """Adds an extension after the environment was created.
  308. .. versionadded:: 2.5
  309. """
  310. self.extensions.update(load_extensions(self, [extension]))
  311. def extend(self, **attributes):
  312. """Add the items to the instance of the environment if they do not exist
  313. yet. This is used by :ref:`extensions <writing-extensions>` to register
  314. callbacks and configuration values without breaking inheritance.
  315. """
  316. for key, value in iteritems(attributes):
  317. if not hasattr(self, key):
  318. setattr(self, key, value)
  319. def overlay(
  320. self,
  321. block_start_string=missing,
  322. block_end_string=missing,
  323. variable_start_string=missing,
  324. variable_end_string=missing,
  325. comment_start_string=missing,
  326. comment_end_string=missing,
  327. line_statement_prefix=missing,
  328. line_comment_prefix=missing,
  329. trim_blocks=missing,
  330. lstrip_blocks=missing,
  331. extensions=missing,
  332. optimized=missing,
  333. undefined=missing,
  334. finalize=missing,
  335. autoescape=missing,
  336. loader=missing,
  337. cache_size=missing,
  338. auto_reload=missing,
  339. bytecode_cache=missing,
  340. ):
  341. """Create a new overlay environment that shares all the data with the
  342. current environment except for cache and the overridden attributes.
  343. Extensions cannot be removed for an overlayed environment. An overlayed
  344. environment automatically gets all the extensions of the environment it
  345. is linked to plus optional extra extensions.
  346. Creating overlays should happen after the initial environment was set
  347. up completely. Not all attributes are truly linked, some are just
  348. copied over so modifications on the original environment may not shine
  349. through.
  350. """
  351. args = dict(locals())
  352. del args["self"], args["cache_size"], args["extensions"]
  353. rv = object.__new__(self.__class__)
  354. rv.__dict__.update(self.__dict__)
  355. rv.overlayed = True
  356. rv.linked_to = self
  357. for key, value in iteritems(args):
  358. if value is not missing:
  359. setattr(rv, key, value)
  360. if cache_size is not missing:
  361. rv.cache = create_cache(cache_size)
  362. else:
  363. rv.cache = copy_cache(self.cache)
  364. rv.extensions = {}
  365. for key, value in iteritems(self.extensions):
  366. rv.extensions[key] = value.bind(rv)
  367. if extensions is not missing:
  368. rv.extensions.update(load_extensions(rv, extensions))
  369. return _environment_sanity_check(rv)
  370. lexer = property(get_lexer, doc="The lexer for this environment.")
  371. def iter_extensions(self):
  372. """Iterates over the extensions by priority."""
  373. return iter(sorted(self.extensions.values(), key=lambda x: x.priority))
  374. def getitem(self, obj, argument):
  375. """Get an item or attribute of an object but prefer the item."""
  376. try:
  377. return obj[argument]
  378. except (AttributeError, TypeError, LookupError):
  379. if isinstance(argument, string_types):
  380. try:
  381. attr = str(argument)
  382. except Exception:
  383. pass
  384. else:
  385. try:
  386. return getattr(obj, attr)
  387. except AttributeError:
  388. pass
  389. return self.undefined(obj=obj, name=argument)
  390. def getattr(self, obj, attribute):
  391. """Get an item or attribute of an object but prefer the attribute.
  392. Unlike :meth:`getitem` the attribute *must* be a bytestring.
  393. """
  394. try:
  395. return getattr(obj, attribute)
  396. except AttributeError:
  397. pass
  398. try:
  399. return obj[attribute]
  400. except (TypeError, LookupError, AttributeError):
  401. return self.undefined(obj=obj, name=attribute)
  402. def call_filter(
  403. self, name, value, args=None, kwargs=None, context=None, eval_ctx=None
  404. ):
  405. """Invokes a filter on a value the same way the compiler does it.
  406. Note that on Python 3 this might return a coroutine in case the
  407. filter is running from an environment in async mode and the filter
  408. supports async execution. It's your responsibility to await this
  409. if needed.
  410. .. versionadded:: 2.7
  411. """
  412. func = self.filters.get(name)
  413. if func is None:
  414. fail_for_missing_callable("no filter named %r", name)
  415. args = [value] + list(args or ())
  416. if getattr(func, "contextfilter", False) is True:
  417. if context is None:
  418. raise TemplateRuntimeError(
  419. "Attempted to invoke context filter without context"
  420. )
  421. args.insert(0, context)
  422. elif getattr(func, "evalcontextfilter", False) is True:
  423. if eval_ctx is None:
  424. if context is not None:
  425. eval_ctx = context.eval_ctx
  426. else:
  427. eval_ctx = EvalContext(self)
  428. args.insert(0, eval_ctx)
  429. elif getattr(func, "environmentfilter", False) is True:
  430. args.insert(0, self)
  431. return func(*args, **(kwargs or {}))
  432. def call_test(self, name, value, args=None, kwargs=None):
  433. """Invokes a test on a value the same way the compiler does it.
  434. .. versionadded:: 2.7
  435. """
  436. func = self.tests.get(name)
  437. if func is None:
  438. fail_for_missing_callable("no test named %r", name)
  439. return func(value, *(args or ()), **(kwargs or {}))
  440. @internalcode
  441. def parse(self, source, name=None, filename=None):
  442. """Parse the sourcecode and return the abstract syntax tree. This
  443. tree of nodes is used by the compiler to convert the template into
  444. executable source- or bytecode. This is useful for debugging or to
  445. extract information from templates.
  446. If you are :ref:`developing Jinja extensions <writing-extensions>`
  447. this gives you a good overview of the node tree generated.
  448. """
  449. try:
  450. return self._parse(source, name, filename)
  451. except TemplateSyntaxError:
  452. self.handle_exception(source=source)
  453. def _parse(self, source, name, filename):
  454. """Internal parsing function used by `parse` and `compile`."""
  455. return Parser(self, source, name, encode_filename(filename)).parse()
  456. def lex(self, source, name=None, filename=None):
  457. """Lex the given sourcecode and return a generator that yields
  458. tokens as tuples in the form ``(lineno, token_type, value)``.
  459. This can be useful for :ref:`extension development <writing-extensions>`
  460. and debugging templates.
  461. This does not perform preprocessing. If you want the preprocessing
  462. of the extensions to be applied you have to filter source through
  463. the :meth:`preprocess` method.
  464. """
  465. source = text_type(source)
  466. try:
  467. return self.lexer.tokeniter(source, name, filename)
  468. except TemplateSyntaxError:
  469. self.handle_exception(source=source)
  470. def preprocess(self, source, name=None, filename=None):
  471. """Preprocesses the source with all extensions. This is automatically
  472. called for all parsing and compiling methods but *not* for :meth:`lex`
  473. because there you usually only want the actual source tokenized.
  474. """
  475. return reduce(
  476. lambda s, e: e.preprocess(s, name, filename),
  477. self.iter_extensions(),
  478. text_type(source),
  479. )
  480. def _tokenize(self, source, name, filename=None, state=None):
  481. """Called by the parser to do the preprocessing and filtering
  482. for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`.
  483. """
  484. source = self.preprocess(source, name, filename)
  485. stream = self.lexer.tokenize(source, name, filename, state)
  486. for ext in self.iter_extensions():
  487. stream = ext.filter_stream(stream)
  488. if not isinstance(stream, TokenStream):
  489. stream = TokenStream(stream, name, filename)
  490. return stream
  491. def _generate(self, source, name, filename, defer_init=False):
  492. """Internal hook that can be overridden to hook a different generate
  493. method in.
  494. .. versionadded:: 2.5
  495. """
  496. return generate(
  497. source,
  498. self,
  499. name,
  500. filename,
  501. defer_init=defer_init,
  502. optimized=self.optimized,
  503. )
  504. def _compile(self, source, filename):
  505. """Internal hook that can be overridden to hook a different compile
  506. method in.
  507. .. versionadded:: 2.5
  508. """
  509. return compile(source, filename, "exec")
  510. @internalcode
  511. def compile(self, source, name=None, filename=None, raw=False, defer_init=False):
  512. """Compile a node or template source code. The `name` parameter is
  513. the load name of the template after it was joined using
  514. :meth:`join_path` if necessary, not the filename on the file system.
  515. the `filename` parameter is the estimated filename of the template on
  516. the file system. If the template came from a database or memory this
  517. can be omitted.
  518. The return value of this method is a python code object. If the `raw`
  519. parameter is `True` the return value will be a string with python
  520. code equivalent to the bytecode returned otherwise. This method is
  521. mainly used internally.
  522. `defer_init` is use internally to aid the module code generator. This
  523. causes the generated code to be able to import without the global
  524. environment variable to be set.
  525. .. versionadded:: 2.4
  526. `defer_init` parameter added.
  527. """
  528. source_hint = None
  529. try:
  530. if isinstance(source, string_types):
  531. source_hint = source
  532. source = self._parse(source, name, filename)
  533. source = self._generate(source, name, filename, defer_init=defer_init)
  534. if raw:
  535. return source
  536. if filename is None:
  537. filename = "<template>"
  538. else:
  539. filename = encode_filename(filename)
  540. return self._compile(source, filename)
  541. except TemplateSyntaxError:
  542. self.handle_exception(source=source_hint)
  543. def compile_expression(self, source, undefined_to_none=True):
  544. """A handy helper method that returns a callable that accepts keyword
  545. arguments that appear as variables in the expression. If called it
  546. returns the result of the expression.
  547. This is useful if applications want to use the same rules as Jinja
  548. in template "configuration files" or similar situations.
  549. Example usage:
  550. >>> env = Environment()
  551. >>> expr = env.compile_expression('foo == 42')
  552. >>> expr(foo=23)
  553. False
  554. >>> expr(foo=42)
  555. True
  556. Per default the return value is converted to `None` if the
  557. expression returns an undefined value. This can be changed
  558. by setting `undefined_to_none` to `False`.
  559. >>> env.compile_expression('var')() is None
  560. True
  561. >>> env.compile_expression('var', undefined_to_none=False)()
  562. Undefined
  563. .. versionadded:: 2.1
  564. """
  565. parser = Parser(self, source, state="variable")
  566. try:
  567. expr = parser.parse_expression()
  568. if not parser.stream.eos:
  569. raise TemplateSyntaxError(
  570. "chunk after expression", parser.stream.current.lineno, None, None
  571. )
  572. expr.set_environment(self)
  573. except TemplateSyntaxError:
  574. if sys.exc_info() is not None:
  575. self.handle_exception(source=source)
  576. body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]
  577. template = self.from_string(nodes.Template(body, lineno=1))
  578. return TemplateExpression(template, undefined_to_none)
  579. def compile_templates(
  580. self,
  581. target,
  582. extensions=None,
  583. filter_func=None,
  584. zip="deflated",
  585. log_function=None,
  586. ignore_errors=True,
  587. py_compile=False,
  588. ):
  589. """Finds all the templates the loader can find, compiles them
  590. and stores them in `target`. If `zip` is `None`, instead of in a
  591. zipfile, the templates will be stored in a directory.
  592. By default a deflate zip algorithm is used. To switch to
  593. the stored algorithm, `zip` can be set to ``'stored'``.
  594. `extensions` and `filter_func` are passed to :meth:`list_templates`.
  595. Each template returned will be compiled to the target folder or
  596. zipfile.
  597. By default template compilation errors are ignored. In case a
  598. log function is provided, errors are logged. If you want template
  599. syntax errors to abort the compilation you can set `ignore_errors`
  600. to `False` and you will get an exception on syntax errors.
  601. If `py_compile` is set to `True` .pyc files will be written to the
  602. target instead of standard .py files. This flag does not do anything
  603. on pypy and Python 3 where pyc files are not picked up by itself and
  604. don't give much benefit.
  605. .. versionadded:: 2.4
  606. """
  607. from .loaders import ModuleLoader
  608. if log_function is None:
  609. def log_function(x):
  610. pass
  611. if py_compile:
  612. if not PY2 or PYPY:
  613. import warnings
  614. warnings.warn(
  615. "'py_compile=True' has no effect on PyPy or Python"
  616. " 3 and will be removed in version 3.0",
  617. DeprecationWarning,
  618. stacklevel=2,
  619. )
  620. py_compile = False
  621. else:
  622. import imp
  623. import marshal
  624. py_header = imp.get_magic() + u"\xff\xff\xff\xff".encode("iso-8859-15")
  625. # Python 3.3 added a source filesize to the header
  626. if sys.version_info >= (3, 3):
  627. py_header += u"\x00\x00\x00\x00".encode("iso-8859-15")
  628. def write_file(filename, data):
  629. if zip:
  630. info = ZipInfo(filename)
  631. info.external_attr = 0o755 << 16
  632. zip_file.writestr(info, data)
  633. else:
  634. if isinstance(data, text_type):
  635. data = data.encode("utf8")
  636. with open(os.path.join(target, filename), "wb") as f:
  637. f.write(data)
  638. if zip is not None:
  639. from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED, ZIP_STORED
  640. zip_file = ZipFile(
  641. target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]
  642. )
  643. log_function('Compiling into Zip archive "%s"' % target)
  644. else:
  645. if not os.path.isdir(target):
  646. os.makedirs(target)
  647. log_function('Compiling into folder "%s"' % target)
  648. try:
  649. for name in self.list_templates(extensions, filter_func):
  650. source, filename, _ = self.loader.get_source(self, name)
  651. try:
  652. code = self.compile(source, name, filename, True, True)
  653. except TemplateSyntaxError as e:
  654. if not ignore_errors:
  655. raise
  656. log_function('Could not compile "%s": %s' % (name, e))
  657. continue
  658. filename = ModuleLoader.get_module_filename(name)
  659. if py_compile:
  660. c = self._compile(code, encode_filename(filename))
  661. write_file(filename + "c", py_header + marshal.dumps(c))
  662. log_function('Byte-compiled "%s" as %s' % (name, filename + "c"))
  663. else:
  664. write_file(filename, code)
  665. log_function('Compiled "%s" as %s' % (name, filename))
  666. finally:
  667. if zip:
  668. zip_file.close()
  669. log_function("Finished compiling templates")
  670. def list_templates(self, extensions=None, filter_func=None):
  671. """Returns a list of templates for this environment. This requires
  672. that the loader supports the loader's
  673. :meth:`~BaseLoader.list_templates` method.
  674. If there are other files in the template folder besides the
  675. actual templates, the returned list can be filtered. There are two
  676. ways: either `extensions` is set to a list of file extensions for
  677. templates, or a `filter_func` can be provided which is a callable that
  678. is passed a template name and should return `True` if it should end up
  679. in the result list.
  680. If the loader does not support that, a :exc:`TypeError` is raised.
  681. .. versionadded:: 2.4
  682. """
  683. names = self.loader.list_templates()
  684. if extensions is not None:
  685. if filter_func is not None:
  686. raise TypeError(
  687. "either extensions or filter_func can be passed, but not both"
  688. )
  689. def filter_func(x):
  690. return "." in x and x.rsplit(".", 1)[1] in extensions
  691. if filter_func is not None:
  692. names = [name for name in names if filter_func(name)]
  693. return names
  694. def handle_exception(self, source=None):
  695. """Exception handling helper. This is used internally to either raise
  696. rewritten exceptions or return a rendered traceback for the template.
  697. """
  698. from .debug import rewrite_traceback_stack
  699. reraise(*rewrite_traceback_stack(source=source))
  700. def join_path(self, template, parent):
  701. """Join a template with the parent. By default all the lookups are
  702. relative to the loader root so this method returns the `template`
  703. parameter unchanged, but if the paths should be relative to the
  704. parent template, this function can be used to calculate the real
  705. template name.
  706. Subclasses may override this method and implement template path
  707. joining here.
  708. """
  709. return template
  710. @internalcode
  711. def _load_template(self, name, globals):
  712. if self.loader is None:
  713. raise TypeError("no loader for this environment specified")
  714. cache_key = (weakref.ref(self.loader), name)
  715. if self.cache is not None:
  716. template = self.cache.get(cache_key)
  717. if template is not None and (
  718. not self.auto_reload or template.is_up_to_date
  719. ):
  720. return template
  721. template = self.loader.load(self, name, globals)
  722. if self.cache is not None:
  723. self.cache[cache_key] = template
  724. return template
  725. @internalcode
  726. def get_template(self, name, parent=None, globals=None):
  727. """Load a template from the loader. If a loader is configured this
  728. method asks the loader for the template and returns a :class:`Template`.
  729. If the `parent` parameter is not `None`, :meth:`join_path` is called
  730. to get the real template name before loading.
  731. The `globals` parameter can be used to provide template wide globals.
  732. These variables are available in the context at render time.
  733. If the template does not exist a :exc:`TemplateNotFound` exception is
  734. raised.
  735. .. versionchanged:: 2.4
  736. If `name` is a :class:`Template` object it is returned from the
  737. function unchanged.
  738. """
  739. if isinstance(name, Template):
  740. return name
  741. if parent is not None:
  742. name = self.join_path(name, parent)
  743. return self._load_template(name, self.make_globals(globals))
  744. @internalcode
  745. def select_template(self, names, parent=None, globals=None):
  746. """Works like :meth:`get_template` but tries a number of templates
  747. before it fails. If it cannot find any of the templates, it will
  748. raise a :exc:`TemplatesNotFound` exception.
  749. .. versionchanged:: 2.11
  750. If names is :class:`Undefined`, an :exc:`UndefinedError` is
  751. raised instead. If no templates were found and names
  752. contains :class:`Undefined`, the message is more helpful.
  753. .. versionchanged:: 2.4
  754. If `names` contains a :class:`Template` object it is returned
  755. from the function unchanged.
  756. .. versionadded:: 2.3
  757. """
  758. if isinstance(names, Undefined):
  759. names._fail_with_undefined_error()
  760. if not names:
  761. raise TemplatesNotFound(
  762. message=u"Tried to select from an empty list " u"of templates."
  763. )
  764. globals = self.make_globals(globals)
  765. for name in names:
  766. if isinstance(name, Template):
  767. return name
  768. if parent is not None:
  769. name = self.join_path(name, parent)
  770. try:
  771. return self._load_template(name, globals)
  772. except (TemplateNotFound, UndefinedError):
  773. pass
  774. raise TemplatesNotFound(names)
  775. @internalcode
  776. def get_or_select_template(self, template_name_or_list, parent=None, globals=None):
  777. """Does a typecheck and dispatches to :meth:`select_template`
  778. if an iterable of template names is given, otherwise to
  779. :meth:`get_template`.
  780. .. versionadded:: 2.3
  781. """
  782. if isinstance(template_name_or_list, (string_types, Undefined)):
  783. return self.get_template(template_name_or_list, parent, globals)
  784. elif isinstance(template_name_or_list, Template):
  785. return template_name_or_list
  786. return self.select_template(template_name_or_list, parent, globals)
  787. def from_string(self, source, globals=None, template_class=None):
  788. """Load a template from a string. This parses the source given and
  789. returns a :class:`Template` object.
  790. """
  791. globals = self.make_globals(globals)
  792. cls = template_class or self.template_class
  793. return cls.from_code(self, self.compile(source), globals, None)
  794. def make_globals(self, d):
  795. """Return a dict for the globals."""
  796. if not d:
  797. return self.globals
  798. return dict(self.globals, **d)
  799. class Template(object):
  800. """The central template object. This class represents a compiled template
  801. and is used to evaluate it.
  802. Normally the template object is generated from an :class:`Environment` but
  803. it also has a constructor that makes it possible to create a template
  804. instance directly using the constructor. It takes the same arguments as
  805. the environment constructor but it's not possible to specify a loader.
  806. Every template object has a few methods and members that are guaranteed
  807. to exist. However it's important that a template object should be
  808. considered immutable. Modifications on the object are not supported.
  809. Template objects created from the constructor rather than an environment
  810. do have an `environment` attribute that points to a temporary environment
  811. that is probably shared with other templates created with the constructor
  812. and compatible settings.
  813. >>> template = Template('Hello {{ name }}!')
  814. >>> template.render(name='John Doe') == u'Hello John Doe!'
  815. True
  816. >>> stream = template.stream(name='John Doe')
  817. >>> next(stream) == u'Hello John Doe!'
  818. True
  819. >>> next(stream)
  820. Traceback (most recent call last):
  821. ...
  822. StopIteration
  823. """
  824. #: Type of environment to create when creating a template directly
  825. #: rather than through an existing environment.
  826. environment_class = Environment
  827. def __new__(
  828. cls,
  829. source,
  830. block_start_string=BLOCK_START_STRING,
  831. block_end_string=BLOCK_END_STRING,
  832. variable_start_string=VARIABLE_START_STRING,
  833. variable_end_string=VARIABLE_END_STRING,
  834. comment_start_string=COMMENT_START_STRING,
  835. comment_end_string=COMMENT_END_STRING,
  836. line_statement_prefix=LINE_STATEMENT_PREFIX,
  837. line_comment_prefix=LINE_COMMENT_PREFIX,
  838. trim_blocks=TRIM_BLOCKS,
  839. lstrip_blocks=LSTRIP_BLOCKS,
  840. newline_sequence=NEWLINE_SEQUENCE,
  841. keep_trailing_newline=KEEP_TRAILING_NEWLINE,
  842. extensions=(),
  843. optimized=True,
  844. undefined=Undefined,
  845. finalize=None,
  846. autoescape=False,
  847. enable_async=False,
  848. ):
  849. env = get_spontaneous_environment(
  850. cls.environment_class,
  851. block_start_string,
  852. block_end_string,
  853. variable_start_string,
  854. variable_end_string,
  855. comment_start_string,
  856. comment_end_string,
  857. line_statement_prefix,
  858. line_comment_prefix,
  859. trim_blocks,
  860. lstrip_blocks,
  861. newline_sequence,
  862. keep_trailing_newline,
  863. frozenset(extensions),
  864. optimized,
  865. undefined,
  866. finalize,
  867. autoescape,
  868. None,
  869. 0,
  870. False,
  871. None,
  872. enable_async,
  873. )
  874. return env.from_string(source, template_class=cls)
  875. @classmethod
  876. def from_code(cls, environment, code, globals, uptodate=None):
  877. """Creates a template object from compiled code and the globals. This
  878. is used by the loaders and environment to create a template object.
  879. """
  880. namespace = {"environment": environment, "__file__": code.co_filename}
  881. exec(code, namespace)
  882. rv = cls._from_namespace(environment, namespace, globals)
  883. rv._uptodate = uptodate
  884. return rv
  885. @classmethod
  886. def from_module_dict(cls, environment, module_dict, globals):
  887. """Creates a template object from a module. This is used by the
  888. module loader to create a template object.
  889. .. versionadded:: 2.4
  890. """
  891. return cls._from_namespace(environment, module_dict, globals)
  892. @classmethod
  893. def _from_namespace(cls, environment, namespace, globals):
  894. t = object.__new__(cls)
  895. t.environment = environment
  896. t.globals = globals
  897. t.name = namespace["name"]
  898. t.filename = namespace["__file__"]
  899. t.blocks = namespace["blocks"]
  900. # render function and module
  901. t.root_render_func = namespace["root"]
  902. t._module = None
  903. # debug and loader helpers
  904. t._debug_info = namespace["debug_info"]
  905. t._uptodate = None
  906. # store the reference
  907. namespace["environment"] = environment
  908. namespace["__jinja_template__"] = t
  909. return t
  910. def render(self, *args, **kwargs):
  911. """This method accepts the same arguments as the `dict` constructor:
  912. A dict, a dict subclass or some keyword arguments. If no arguments
  913. are given the context will be empty. These two calls do the same::
  914. template.render(knights='that say nih')
  915. template.render({'knights': 'that say nih'})
  916. This will return the rendered template as unicode string.
  917. """
  918. vars = dict(*args, **kwargs)
  919. try:
  920. return concat(self.root_render_func(self.new_context(vars)))
  921. except Exception:
  922. self.environment.handle_exception()
  923. def render_async(self, *args, **kwargs):
  924. """This works similar to :meth:`render` but returns a coroutine
  925. that when awaited returns the entire rendered template string. This
  926. requires the async feature to be enabled.
  927. Example usage::
  928. await template.render_async(knights='that say nih; asynchronously')
  929. """
  930. # see asyncsupport for the actual implementation
  931. raise NotImplementedError(
  932. "This feature is not available for this version of Python"
  933. )
  934. def stream(self, *args, **kwargs):
  935. """Works exactly like :meth:`generate` but returns a
  936. :class:`TemplateStream`.
  937. """
  938. return TemplateStream(self.generate(*args, **kwargs))
  939. def generate(self, *args, **kwargs):
  940. """For very large templates it can be useful to not render the whole
  941. template at once but evaluate each statement after another and yield
  942. piece for piece. This method basically does exactly that and returns
  943. a generator that yields one item after another as unicode strings.
  944. It accepts the same arguments as :meth:`render`.
  945. """
  946. vars = dict(*args, **kwargs)
  947. try:
  948. for event in self.root_render_func(self.new_context(vars)):
  949. yield event
  950. except Exception:
  951. yield self.environment.handle_exception()
  952. def generate_async(self, *args, **kwargs):
  953. """An async version of :meth:`generate`. Works very similarly but
  954. returns an async iterator instead.
  955. """
  956. # see asyncsupport for the actual implementation
  957. raise NotImplementedError(
  958. "This feature is not available for this version of Python"
  959. )
  960. def new_context(self, vars=None, shared=False, locals=None):
  961. """Create a new :class:`Context` for this template. The vars
  962. provided will be passed to the template. Per default the globals
  963. are added to the context. If shared is set to `True` the data
  964. is passed as is to the context without adding the globals.
  965. `locals` can be a dict of local variables for internal usage.
  966. """
  967. return new_context(
  968. self.environment, self.name, self.blocks, vars, shared, self.globals, locals
  969. )
  970. def make_module(self, vars=None, shared=False, locals=None):
  971. """This method works like the :attr:`module` attribute when called
  972. without arguments but it will evaluate the template on every call
  973. rather than caching it. It's also possible to provide
  974. a dict which is then used as context. The arguments are the same
  975. as for the :meth:`new_context` method.
  976. """
  977. return TemplateModule(self, self.new_context(vars, shared, locals))
  978. def make_module_async(self, vars=None, shared=False, locals=None):
  979. """As template module creation can invoke template code for
  980. asynchronous executions this method must be used instead of the
  981. normal :meth:`make_module` one. Likewise the module attribute
  982. becomes unavailable in async mode.
  983. """
  984. # see asyncsupport for the actual implementation
  985. raise NotImplementedError(
  986. "This feature is not available for this version of Python"
  987. )
  988. @internalcode
  989. def _get_default_module(self):
  990. if self._module is not None:
  991. return self._module
  992. self._module = rv = self.make_module()
  993. return rv
  994. @property
  995. def module(self):
  996. """The template as module. This is used for imports in the
  997. template runtime but is also useful if one wants to access
  998. exported template variables from the Python layer:
  999. >>> t = Template('{% macro foo() %}42{% endmacro %}23')
  1000. >>> str(t.module)
  1001. '23'
  1002. >>> t.module.foo() == u'42'
  1003. True
  1004. This attribute is not available if async mode is enabled.
  1005. """
  1006. return self._get_default_module()
  1007. def get_corresponding_lineno(self, lineno):
  1008. """Return the source line number of a line number in the
  1009. generated bytecode as they are not in sync.
  1010. """
  1011. for template_line, code_line in reversed(self.debug_info):
  1012. if code_line <= lineno:
  1013. return template_line
  1014. return 1
  1015. @property
  1016. def is_up_to_date(self):
  1017. """If this variable is `False` there is a newer version available."""
  1018. if self._uptodate is None:
  1019. return True
  1020. return self._uptodate()
  1021. @property
  1022. def debug_info(self):
  1023. """The debug info mapping."""
  1024. if self._debug_info:
  1025. return [tuple(map(int, x.split("="))) for x in self._debug_info.split("&")]
  1026. return []
  1027. def __repr__(self):
  1028. if self.name is None:
  1029. name = "memory:%x" % id(self)
  1030. else:
  1031. name = repr(self.name)
  1032. return "<%s %s>" % (self.__class__.__name__, name)
  1033. @implements_to_string
  1034. class TemplateModule(object):
  1035. """Represents an imported template. All the exported names of the
  1036. template are available as attributes on this object. Additionally
  1037. converting it into an unicode- or bytestrings renders the contents.
  1038. """
  1039. def __init__(self, template, context, body_stream=None):
  1040. if body_stream is None:
  1041. if context.environment.is_async:
  1042. raise RuntimeError(
  1043. "Async mode requires a body stream "
  1044. "to be passed to a template module. Use "
  1045. "the async methods of the API you are "
  1046. "using."
  1047. )
  1048. body_stream = list(template.root_render_func(context))
  1049. self._body_stream = body_stream
  1050. self.__dict__.update(context.get_exported())
  1051. self.__name__ = template.name
  1052. def __html__(self):
  1053. return Markup(concat(self._body_stream))
  1054. def __str__(self):
  1055. return concat(self._body_stream)
  1056. def __repr__(self):
  1057. if self.__name__ is None:
  1058. name = "memory:%x" % id(self)
  1059. else:
  1060. name = repr(self.__name__)
  1061. return "<%s %s>" % (self.__class__.__name__, name)
  1062. class TemplateExpression(object):
  1063. """The :meth:`jinja2.Environment.compile_expression` method returns an
  1064. instance of this object. It encapsulates the expression-like access
  1065. to the template with an expression it wraps.
  1066. """
  1067. def __init__(self, template, undefined_to_none):
  1068. self._template = template
  1069. self._undefined_to_none = undefined_to_none
  1070. def __call__(self, *args, **kwargs):
  1071. context = self._template.new_context(dict(*args, **kwargs))
  1072. consume(self._template.root_render_func(context))
  1073. rv = context.vars["result"]
  1074. if self._undefined_to_none and isinstance(rv, Undefined):
  1075. rv = None
  1076. return rv
  1077. @implements_iterator
  1078. class TemplateStream(object):
  1079. """A template stream works pretty much like an ordinary python generator
  1080. but it can buffer multiple items to reduce the number of total iterations.
  1081. Per default the output is unbuffered which means that for every unbuffered
  1082. instruction in the template one unicode string is yielded.
  1083. If buffering is enabled with a buffer size of 5, five items are combined
  1084. into a new unicode string. This is mainly useful if you are streaming
  1085. big templates to a client via WSGI which flushes after each iteration.
  1086. """
  1087. def __init__(self, gen):
  1088. self._gen = gen
  1089. self.disable_buffering()
  1090. def dump(self, fp, encoding=None, errors="strict"):
  1091. """Dump the complete stream into a file or file-like object.
  1092. Per default unicode strings are written, if you want to encode
  1093. before writing specify an `encoding`.
  1094. Example usage::
  1095. Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
  1096. """
  1097. close = False
  1098. if isinstance(fp, string_types):
  1099. if encoding is None:
  1100. encoding = "utf-8"
  1101. fp = open(fp, "wb")
  1102. close = True
  1103. try:
  1104. if encoding is not None:
  1105. iterable = (x.encode(encoding, errors) for x in self)
  1106. else:
  1107. iterable = self
  1108. if hasattr(fp, "writelines"):
  1109. fp.writelines(iterable)
  1110. else:
  1111. for item in iterable:
  1112. fp.write(item)
  1113. finally:
  1114. if close:
  1115. fp.close()
  1116. def disable_buffering(self):
  1117. """Disable the output buffering."""
  1118. self._next = partial(next, self._gen)
  1119. self.buffered = False
  1120. def _buffered_generator(self, size):
  1121. buf = []
  1122. c_size = 0
  1123. push = buf.append
  1124. while 1:
  1125. try:
  1126. while c_size < size:
  1127. c = next(self._gen)
  1128. push(c)
  1129. if c:
  1130. c_size += 1
  1131. except StopIteration:
  1132. if not c_size:
  1133. return
  1134. yield concat(buf)
  1135. del buf[:]
  1136. c_size = 0
  1137. def enable_buffering(self, size=5):
  1138. """Enable buffering. Buffer `size` items before yielding them."""
  1139. if size <= 1:
  1140. raise ValueError("buffer size too small")
  1141. self.buffered = True
  1142. self._next = partial(next, self._buffered_generator(size))
  1143. def __iter__(self):
  1144. return self
  1145. def __next__(self):
  1146. return self._next()
  1147. # hook in default template class. if anyone reads this comment: ignore that
  1148. # it's possible to use custom templates ;-)
  1149. Environment.template_class = Template