template.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. # mako/template.py
  2. # Copyright 2006-2025 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """Provides the Template class, a facade for parsing, generating and executing
  7. template strings, as well as template runtime operations."""
  8. import json
  9. import os
  10. import re
  11. import shutil
  12. import stat
  13. import tempfile
  14. import types
  15. import weakref
  16. from mako import cache
  17. from mako import codegen
  18. from mako import compat
  19. from mako import exceptions
  20. from mako import runtime
  21. from mako import util
  22. from mako.lexer import Lexer
  23. class Template:
  24. r"""Represents a compiled template.
  25. :class:`.Template` includes a reference to the original
  26. template source (via the :attr:`.source` attribute)
  27. as well as the source code of the
  28. generated Python module (i.e. the :attr:`.code` attribute),
  29. as well as a reference to an actual Python module.
  30. :class:`.Template` is constructed using either a literal string
  31. representing the template text, or a filename representing a filesystem
  32. path to a source file.
  33. :param text: textual template source. This argument is mutually
  34. exclusive versus the ``filename`` parameter.
  35. :param filename: filename of the source template. This argument is
  36. mutually exclusive versus the ``text`` parameter.
  37. :param buffer_filters: string list of filters to be applied
  38. to the output of ``%def``\ s which are buffered, cached, or otherwise
  39. filtered, after all filters
  40. defined with the ``%def`` itself have been applied. Allows the
  41. creation of default expression filters that let the output
  42. of return-valued ``%def``\ s "opt out" of that filtering via
  43. passing special attributes or objects.
  44. :param cache_args: Dictionary of cache configuration arguments that
  45. will be passed to the :class:`.CacheImpl`. See :ref:`caching_toplevel`.
  46. :param cache_dir:
  47. .. deprecated:: 0.6
  48. Use the ``'dir'`` argument in the ``cache_args`` dictionary.
  49. See :ref:`caching_toplevel`.
  50. :param cache_enabled: Boolean flag which enables caching of this
  51. template. See :ref:`caching_toplevel`.
  52. :param cache_impl: String name of a :class:`.CacheImpl` caching
  53. implementation to use. Defaults to ``'beaker'``.
  54. :param cache_type:
  55. .. deprecated:: 0.6
  56. Use the ``'type'`` argument in the ``cache_args`` dictionary.
  57. See :ref:`caching_toplevel`.
  58. :param cache_url:
  59. .. deprecated:: 0.6
  60. Use the ``'url'`` argument in the ``cache_args`` dictionary.
  61. See :ref:`caching_toplevel`.
  62. :param default_filters: List of string filter names that will
  63. be applied to all expressions. See :ref:`filtering_default_filters`.
  64. :param enable_loop: When ``True``, enable the ``loop`` context variable.
  65. This can be set to ``False`` to support templates that may
  66. be making usage of the name "``loop``". Individual templates can
  67. re-enable the "loop" context by placing the directive
  68. ``enable_loop="True"`` inside the ``<%page>`` tag -- see
  69. :ref:`migrating_loop`.
  70. :param encoding_errors: Error parameter passed to ``encode()`` when
  71. string encoding is performed. See :ref:`usage_unicode`.
  72. :param error_handler: Python callable which is called whenever
  73. compile or runtime exceptions occur. The callable is passed
  74. the current context as well as the exception. If the
  75. callable returns ``True``, the exception is considered to
  76. be handled, else it is re-raised after the function
  77. completes. Is used to provide custom error-rendering
  78. functions.
  79. .. seealso::
  80. :paramref:`.Template.include_error_handler` - include-specific
  81. error handler function
  82. :param format_exceptions: if ``True``, exceptions which occur during
  83. the render phase of this template will be caught and
  84. formatted into an HTML error page, which then becomes the
  85. rendered result of the :meth:`.render` call. Otherwise,
  86. runtime exceptions are propagated outwards.
  87. :param imports: String list of Python statements, typically individual
  88. "import" lines, which will be placed into the module level
  89. preamble of all generated Python modules. See the example
  90. in :ref:`filtering_default_filters`.
  91. :param future_imports: String list of names to import from `__future__`.
  92. These will be concatenated into a comma-separated string and inserted
  93. into the beginning of the template, e.g. ``futures_imports=['FOO',
  94. 'BAR']`` results in ``from __future__ import FOO, BAR``.
  95. :param include_error_handler: An error handler that runs when this template
  96. is included within another one via the ``<%include>`` tag, and raises an
  97. error. Compare to the :paramref:`.Template.error_handler` option.
  98. .. versionadded:: 1.0.6
  99. .. seealso::
  100. :paramref:`.Template.error_handler` - top-level error handler function
  101. :param input_encoding: Encoding of the template's source code. Can
  102. be used in lieu of the coding comment. See
  103. :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for
  104. details on source encoding.
  105. :param lookup: a :class:`.TemplateLookup` instance that will be used
  106. for all file lookups via the ``<%namespace>``,
  107. ``<%include>``, and ``<%inherit>`` tags. See
  108. :ref:`usage_templatelookup`.
  109. :param module_directory: Filesystem location where generated
  110. Python module files will be placed.
  111. :param module_filename: Overrides the filename of the generated
  112. Python module file. For advanced usage only.
  113. :param module_writer: A callable which overrides how the Python
  114. module is written entirely. The callable is passed the
  115. encoded source content of the module and the destination
  116. path to be written to. The default behavior of module writing
  117. uses a tempfile in conjunction with a file move in order
  118. to make the operation atomic. So a user-defined module
  119. writing function that mimics the default behavior would be:
  120. .. sourcecode:: python
  121. import tempfile
  122. import os
  123. import shutil
  124. def module_writer(source, outputpath):
  125. (dest, name) = \\
  126. tempfile.mkstemp(
  127. dir=os.path.dirname(outputpath)
  128. )
  129. os.write(dest, source)
  130. os.close(dest)
  131. shutil.move(name, outputpath)
  132. from mako.template import Template
  133. mytemplate = Template(
  134. filename="index.html",
  135. module_directory="/path/to/modules",
  136. module_writer=module_writer
  137. )
  138. The function is provided for unusual configurations where
  139. certain platform-specific permissions or other special
  140. steps are needed.
  141. :param output_encoding: The encoding to use when :meth:`.render`
  142. is called.
  143. See :ref:`usage_unicode` as well as :ref:`unicode_toplevel`.
  144. :param preprocessor: Python callable which will be passed
  145. the full template source before it is parsed. The return
  146. result of the callable will be used as the template source
  147. code.
  148. :param lexer_cls: A :class:`.Lexer` class used to parse
  149. the template. The :class:`.Lexer` class is used by
  150. default.
  151. .. versionadded:: 0.7.4
  152. :param strict_undefined: Replaces the automatic usage of
  153. ``UNDEFINED`` for any undeclared variables not located in
  154. the :class:`.Context` with an immediate raise of
  155. ``NameError``. The advantage is immediate reporting of
  156. missing variables which include the name.
  157. .. versionadded:: 0.3.6
  158. :param uri: string URI or other identifier for this template.
  159. If not provided, the ``uri`` is generated from the filesystem
  160. path, or from the in-memory identity of a non-file-based
  161. template. The primary usage of the ``uri`` is to provide a key
  162. within :class:`.TemplateLookup`, as well as to generate the
  163. file path of the generated Python module file, if
  164. ``module_directory`` is specified.
  165. """
  166. lexer_cls = Lexer
  167. def __init__(
  168. self,
  169. text=None,
  170. filename=None,
  171. uri=None,
  172. format_exceptions=False,
  173. error_handler=None,
  174. lookup=None,
  175. output_encoding=None,
  176. encoding_errors="strict",
  177. module_directory=None,
  178. cache_args=None,
  179. cache_impl="beaker",
  180. cache_enabled=True,
  181. cache_type=None,
  182. cache_dir=None,
  183. cache_url=None,
  184. module_filename=None,
  185. input_encoding=None,
  186. module_writer=None,
  187. default_filters=None,
  188. buffer_filters=(),
  189. strict_undefined=False,
  190. imports=None,
  191. future_imports=None,
  192. enable_loop=True,
  193. preprocessor=None,
  194. lexer_cls=None,
  195. include_error_handler=None,
  196. ):
  197. if uri:
  198. self.module_id = re.sub(r"\W", "_", uri)
  199. self.uri = uri
  200. elif filename:
  201. self.module_id = re.sub(r"\W", "_", filename)
  202. drive, path = os.path.splitdrive(filename)
  203. path = os.path.normpath(path).replace(os.path.sep, "/")
  204. self.uri = path
  205. else:
  206. self.module_id = "memory:" + hex(id(self))
  207. self.uri = self.module_id
  208. u_norm = self.uri.replace("\\", "/").lstrip("/")
  209. u_norm = os.path.normpath(u_norm)
  210. if u_norm.startswith(".."):
  211. raise exceptions.TemplateLookupException(
  212. 'Template uri "%s" is invalid - '
  213. "it cannot be relative outside "
  214. "of the root path." % self.uri
  215. )
  216. self.input_encoding = input_encoding
  217. self.output_encoding = output_encoding
  218. self.encoding_errors = encoding_errors
  219. self.enable_loop = enable_loop
  220. self.strict_undefined = strict_undefined
  221. self.module_writer = module_writer
  222. if default_filters is None:
  223. self.default_filters = ["str"]
  224. else:
  225. self.default_filters = default_filters
  226. self.buffer_filters = buffer_filters
  227. self.imports = imports
  228. self.future_imports = future_imports
  229. self.preprocessor = preprocessor
  230. if lexer_cls is not None:
  231. self.lexer_cls = lexer_cls
  232. # if plain text, compile code in memory only
  233. if text is not None:
  234. (code, module) = _compile_text(self, text, filename)
  235. self._code = code
  236. self._source = text
  237. ModuleInfo(module, None, self, filename, code, text, uri)
  238. elif filename is not None:
  239. # if template filename and a module directory, load
  240. # a filesystem-based module file, generating if needed
  241. if module_filename is not None:
  242. path = module_filename
  243. elif module_directory is not None:
  244. path = os.path.abspath(
  245. os.path.join(
  246. os.path.normpath(module_directory), u_norm + ".py"
  247. )
  248. )
  249. else:
  250. path = None
  251. module = self._compile_from_file(path, filename)
  252. else:
  253. raise exceptions.RuntimeException(
  254. "Template requires text or filename"
  255. )
  256. self.module = module
  257. self.filename = filename
  258. self.callable_ = self.module.render_body
  259. self.format_exceptions = format_exceptions
  260. self.error_handler = error_handler
  261. self.include_error_handler = include_error_handler
  262. self.lookup = lookup
  263. self.module_directory = module_directory
  264. self._setup_cache_args(
  265. cache_impl,
  266. cache_enabled,
  267. cache_args,
  268. cache_type,
  269. cache_dir,
  270. cache_url,
  271. )
  272. @util.memoized_property
  273. def reserved_names(self):
  274. if self.enable_loop:
  275. return codegen.RESERVED_NAMES
  276. else:
  277. return codegen.RESERVED_NAMES.difference(["loop"])
  278. def _setup_cache_args(
  279. self,
  280. cache_impl,
  281. cache_enabled,
  282. cache_args,
  283. cache_type,
  284. cache_dir,
  285. cache_url,
  286. ):
  287. self.cache_impl = cache_impl
  288. self.cache_enabled = cache_enabled
  289. self.cache_args = cache_args or {}
  290. # transfer deprecated cache_* args
  291. if cache_type:
  292. self.cache_args["type"] = cache_type
  293. if cache_dir:
  294. self.cache_args["dir"] = cache_dir
  295. if cache_url:
  296. self.cache_args["url"] = cache_url
  297. def _compile_from_file(self, path, filename):
  298. if path is not None:
  299. util.verify_directory(os.path.dirname(path))
  300. filemtime = os.stat(filename)[stat.ST_MTIME]
  301. if (
  302. not os.path.exists(path)
  303. or os.stat(path)[stat.ST_MTIME] < filemtime
  304. ):
  305. data = util.read_file(filename)
  306. _compile_module_file(
  307. self, data, filename, path, self.module_writer
  308. )
  309. module = compat.load_module(self.module_id, path)
  310. if module._magic_number != codegen.MAGIC_NUMBER:
  311. data = util.read_file(filename)
  312. _compile_module_file(
  313. self, data, filename, path, self.module_writer
  314. )
  315. module = compat.load_module(self.module_id, path)
  316. ModuleInfo(module, path, self, filename, None, None, None)
  317. else:
  318. # template filename and no module directory, compile code
  319. # in memory
  320. data = util.read_file(filename)
  321. code, module = _compile_text(self, data, filename)
  322. self._source = None
  323. self._code = code
  324. ModuleInfo(module, None, self, filename, code, None, None)
  325. return module
  326. @property
  327. def source(self):
  328. """Return the template source code for this :class:`.Template`."""
  329. return _get_module_info_from_callable(self.callable_).source
  330. @property
  331. def code(self):
  332. """Return the module source code for this :class:`.Template`."""
  333. return _get_module_info_from_callable(self.callable_).code
  334. @util.memoized_property
  335. def cache(self):
  336. return cache.Cache(self)
  337. @property
  338. def cache_dir(self):
  339. return self.cache_args["dir"]
  340. @property
  341. def cache_url(self):
  342. return self.cache_args["url"]
  343. @property
  344. def cache_type(self):
  345. return self.cache_args["type"]
  346. def render(self, *args, **data):
  347. """Render the output of this template as a string.
  348. If the template specifies an output encoding, the string
  349. will be encoded accordingly, else the output is raw (raw
  350. output uses `StringIO` and can't handle multibyte
  351. characters). A :class:`.Context` object is created corresponding
  352. to the given data. Arguments that are explicitly declared
  353. by this template's internal rendering method are also
  354. pulled from the given ``*args``, ``**data`` members.
  355. """
  356. return runtime._render(self, self.callable_, args, data)
  357. def render_unicode(self, *args, **data):
  358. """Render the output of this template as a unicode object."""
  359. return runtime._render(
  360. self, self.callable_, args, data, as_unicode=True
  361. )
  362. def render_context(self, context, *args, **kwargs):
  363. """Render this :class:`.Template` with the given context.
  364. The data is written to the context's buffer.
  365. """
  366. if getattr(context, "_with_template", None) is None:
  367. context._set_with_template(self)
  368. runtime._render_context(self, self.callable_, context, *args, **kwargs)
  369. def has_def(self, name):
  370. return hasattr(self.module, "render_%s" % name)
  371. def get_def(self, name):
  372. """Return a def of this template as a :class:`.DefTemplate`."""
  373. return DefTemplate(self, getattr(self.module, "render_%s" % name))
  374. def list_defs(self):
  375. """return a list of defs in the template.
  376. .. versionadded:: 1.0.4
  377. """
  378. return [i[7:] for i in dir(self.module) if i[:7] == "render_"]
  379. def _get_def_callable(self, name):
  380. return getattr(self.module, "render_%s" % name)
  381. @property
  382. def last_modified(self):
  383. return self.module._modified_time
  384. class ModuleTemplate(Template):
  385. """A Template which is constructed given an existing Python module.
  386. e.g.::
  387. t = Template("this is a template")
  388. f = file("mymodule.py", "w")
  389. f.write(t.code)
  390. f.close()
  391. import mymodule
  392. t = ModuleTemplate(mymodule)
  393. print(t.render())
  394. """
  395. def __init__(
  396. self,
  397. module,
  398. module_filename=None,
  399. template=None,
  400. template_filename=None,
  401. module_source=None,
  402. template_source=None,
  403. output_encoding=None,
  404. encoding_errors="strict",
  405. format_exceptions=False,
  406. error_handler=None,
  407. lookup=None,
  408. cache_args=None,
  409. cache_impl="beaker",
  410. cache_enabled=True,
  411. cache_type=None,
  412. cache_dir=None,
  413. cache_url=None,
  414. include_error_handler=None,
  415. ):
  416. self.module_id = re.sub(r"\W", "_", module._template_uri)
  417. self.uri = module._template_uri
  418. self.input_encoding = module._source_encoding
  419. self.output_encoding = output_encoding
  420. self.encoding_errors = encoding_errors
  421. self.enable_loop = module._enable_loop
  422. self.module = module
  423. self.filename = template_filename
  424. ModuleInfo(
  425. module,
  426. module_filename,
  427. self,
  428. template_filename,
  429. module_source,
  430. template_source,
  431. module._template_uri,
  432. )
  433. self.callable_ = self.module.render_body
  434. self.format_exceptions = format_exceptions
  435. self.error_handler = error_handler
  436. self.include_error_handler = include_error_handler
  437. self.lookup = lookup
  438. self._setup_cache_args(
  439. cache_impl,
  440. cache_enabled,
  441. cache_args,
  442. cache_type,
  443. cache_dir,
  444. cache_url,
  445. )
  446. class DefTemplate(Template):
  447. """A :class:`.Template` which represents a callable def in a parent
  448. template."""
  449. def __init__(self, parent, callable_):
  450. self.parent = parent
  451. self.callable_ = callable_
  452. self.output_encoding = parent.output_encoding
  453. self.module = parent.module
  454. self.encoding_errors = parent.encoding_errors
  455. self.format_exceptions = parent.format_exceptions
  456. self.error_handler = parent.error_handler
  457. self.include_error_handler = parent.include_error_handler
  458. self.enable_loop = parent.enable_loop
  459. self.lookup = parent.lookup
  460. def get_def(self, name):
  461. return self.parent.get_def(name)
  462. class ModuleInfo:
  463. """Stores information about a module currently loaded into
  464. memory, provides reverse lookups of template source, module
  465. source code based on a module's identifier.
  466. """
  467. _modules = weakref.WeakValueDictionary()
  468. def __init__(
  469. self,
  470. module,
  471. module_filename,
  472. template,
  473. template_filename,
  474. module_source,
  475. template_source,
  476. template_uri,
  477. ):
  478. self.module = module
  479. self.module_filename = module_filename
  480. self.template_filename = template_filename
  481. self.module_source = module_source
  482. self.template_source = template_source
  483. self.template_uri = template_uri
  484. self._modules[module.__name__] = template._mmarker = self
  485. if module_filename:
  486. self._modules[module_filename] = self
  487. @classmethod
  488. def get_module_source_metadata(cls, module_source, full_line_map=False):
  489. source_map = re.search(
  490. r"__M_BEGIN_METADATA(.+?)__M_END_METADATA", module_source, re.S
  491. ).group(1)
  492. source_map = json.loads(source_map)
  493. source_map["line_map"] = {
  494. int(k): int(v) for k, v in source_map["line_map"].items()
  495. }
  496. if full_line_map:
  497. f_line_map = source_map["full_line_map"] = []
  498. line_map = source_map["line_map"]
  499. curr_templ_line = 1
  500. for mod_line in range(1, max(line_map)):
  501. if mod_line in line_map:
  502. curr_templ_line = line_map[mod_line]
  503. f_line_map.append(curr_templ_line)
  504. return source_map
  505. @property
  506. def code(self):
  507. if self.module_source is not None:
  508. return self.module_source
  509. else:
  510. return util.read_python_file(self.module_filename)
  511. @property
  512. def source(self):
  513. if self.template_source is None:
  514. data = util.read_file(self.template_filename)
  515. if self.module._source_encoding:
  516. return data.decode(self.module._source_encoding)
  517. else:
  518. return data
  519. elif self.module._source_encoding and not isinstance(
  520. self.template_source, str
  521. ):
  522. return self.template_source.decode(self.module._source_encoding)
  523. else:
  524. return self.template_source
  525. def _compile(template, text, filename, generate_magic_comment):
  526. lexer = template.lexer_cls(
  527. text,
  528. filename,
  529. input_encoding=template.input_encoding,
  530. preprocessor=template.preprocessor,
  531. )
  532. node = lexer.parse()
  533. source = codegen.compile(
  534. node,
  535. template.uri,
  536. filename,
  537. default_filters=template.default_filters,
  538. buffer_filters=template.buffer_filters,
  539. imports=template.imports,
  540. future_imports=template.future_imports,
  541. source_encoding=lexer.encoding,
  542. generate_magic_comment=generate_magic_comment,
  543. strict_undefined=template.strict_undefined,
  544. enable_loop=template.enable_loop,
  545. reserved_names=template.reserved_names,
  546. )
  547. return source, lexer
  548. def _compile_text(template, text, filename):
  549. identifier = template.module_id
  550. source, lexer = _compile(
  551. template, text, filename, generate_magic_comment=False
  552. )
  553. cid = identifier
  554. module = types.ModuleType(cid)
  555. code = compile(source, cid, "exec")
  556. # this exec() works for 2.4->3.3.
  557. exec(code, module.__dict__, module.__dict__)
  558. return (source, module)
  559. def _compile_module_file(template, text, filename, outputpath, module_writer):
  560. source, lexer = _compile(
  561. template, text, filename, generate_magic_comment=True
  562. )
  563. if isinstance(source, str):
  564. source = source.encode(lexer.encoding or "ascii")
  565. if module_writer:
  566. module_writer(source, outputpath)
  567. else:
  568. # make tempfiles in the same location as the ultimate
  569. # location. this ensures they're on the same filesystem,
  570. # avoiding synchronization issues.
  571. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath))
  572. os.write(dest, source)
  573. os.close(dest)
  574. shutil.move(name, outputpath)
  575. def _get_module_info_from_callable(callable_):
  576. return _get_module_info(callable_.__globals__["__name__"])
  577. def _get_module_info(filename):
  578. return ModuleInfo._modules[filename]