ext.py
This file
Path
src/jinja2/ext.py
704 lines · 25.8 KiB · Python
Last changed by
import Markup from markupsafe, fix flake8 import warningsDavid Lordbefore adoption9849979
History of this fileTake it
1# -*- coding: utf-8 -*-2"""Extension API for adding custom tags and behavior."""3import pprint4import re5from sys import version_info67from markupsafe import Markup89from . import nodes10from ._compat import iteritems11from ._compat import string_types12from ._compat import with_metaclass13from .defaults import BLOCK_END_STRING14from .defaults import BLOCK_START_STRING15from .defaults import COMMENT_END_STRING16from .defaults import COMMENT_START_STRING17from .defaults import KEEP_TRAILING_NEWLINE18from .defaults import LINE_COMMENT_PREFIX19from .defaults import LINE_STATEMENT_PREFIX20from .defaults import LSTRIP_BLOCKS21from .defaults import NEWLINE_SEQUENCE22from .defaults import TRIM_BLOCKS23from .defaults import VARIABLE_END_STRING24from .defaults import VARIABLE_START_STRING25from .environment import Environment26from .exceptions import TemplateAssertionError27from .exceptions import TemplateSyntaxError28from .nodes import ContextReference29from .runtime import concat30from .utils import contextfunction31from .utils import import_string3233# the only real useful gettext functions for a Jinja template. Note34# that ugettext must be assigned to gettext as Jinja doesn't support35# non unicode strings.36GETTEXT_FUNCTIONS = ("_", "gettext", "ngettext")3738_ws_re = re.compile(r"\s*\n\s*")394041class ExtensionRegistry(type):42 """Gives the extension an unique identifier."""4344 def __new__(mcs, name, bases, d):45 rv = type.__new__(mcs, name, bases, d)46 rv.identifier = rv.__module__ + "." + rv.__name__47 return rv484950class Extension(with_metaclass(ExtensionRegistry, object)):51 """Extensions can be used to add extra functionality to the Jinja template52 system at the parser level. Custom extensions are bound to an environment53 but may not store environment specific data on `self`. The reason for54 this is that an extension can be bound to another environment (for55 overlays) by creating a copy and reassigning the `environment` attribute.5657 As extensions are created by the environment they cannot accept any58 arguments for configuration. One may want to work around that by using59 a factory function, but that is not possible as extensions are identified60 by their import name. The correct way to configure the extension is61 storing the configuration values on the environment. Because this way the62 environment ends up acting as central configuration storage the63 attributes may clash which is why extensions have to ensure that the names64 they choose for configuration are not too generic. ``prefix`` for example65 is a terrible name, ``fragment_cache_prefix`` on the other hand is a good66 name as includes the name of the extension (fragment cache).67 """6869 #: if this extension parses this is the list of tags it's listening to.70 tags = set()7172 #: the priority of that extension. This is especially useful for73 #: extensions that preprocess values. A lower value means higher74 #: priority.75 #:76 #: .. versionadded:: 2.477 priority = 1007879 def __init__(self, environment):80 self.environment = environment8182 def bind(self, environment):83 """Create a copy of this extension bound to another environment."""84 rv = object.__new__(self.__class__)85 rv.__dict__.update(self.__dict__)86 rv.environment = environment87 return rv8889 def preprocess(self, source, name, filename=None):90 """This method is called before the actual lexing and can be used to91 preprocess the source. The `filename` is optional. The return value92 must be the preprocessed source.93 """94 return source9596 def filter_stream(self, stream):97 """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used98 to filter tokens returned. This method has to return an iterable of99 :class:`~jinja2.lexer.Token`\\s, but it doesn't have to return a100 :class:`~jinja2.lexer.TokenStream`.101 """102 return stream103104 def parse(self, parser):105 """If any of the :attr:`tags` matched this method is called with the106 parser as first argument. The token the parser stream is pointing at107 is the name token that matched. This method has to return one or a108 list of multiple nodes.109 """110 raise NotImplementedError()111112 def attr(self, name, lineno=None):113 """Return an attribute node for the current extension. This is useful114 to pass constants on extensions to generated template code.115116 ::117118 self.attr('_my_attribute', lineno=lineno)119 """120 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)121122 def call_method(123 self, name, args=None, kwargs=None, dyn_args=None, dyn_kwargs=None, lineno=None124 ):125 """Call a method of the extension. This is a shortcut for126 :meth:`attr` + :class:`jinja2.nodes.Call`.127 """128 if args is None:129 args = []130 if kwargs is None:131 kwargs = []132 return nodes.Call(133 self.attr(name, lineno=lineno),134 args,135 kwargs,136 dyn_args,137 dyn_kwargs,138 lineno=lineno,139 )140141142@contextfunction143def _gettext_alias(__context, *args, **kwargs):144 return __context.call(__context.resolve("gettext"), *args, **kwargs)145146147def _make_new_gettext(func):148 @contextfunction149 def gettext(__context, __string, **variables):150 rv = __context.call(func, __string)151 if __context.eval_ctx.autoescape:152 rv = Markup(rv)153 # Always treat as a format string, even if there are no154 # variables. This makes translation strings more consistent155 # and predictable. This requires escaping156 return rv % variables157158 return gettext159160161def _make_new_ngettext(func):162 @contextfunction163 def ngettext(__context, __singular, __plural, __num, **variables):164 variables.setdefault("num", __num)165 rv = __context.call(func, __singular, __plural, __num)166 if __context.eval_ctx.autoescape:167 rv = Markup(rv)168 # Always treat as a format string, see gettext comment above.169 return rv % variables170171 return ngettext172173174class InternationalizationExtension(Extension):175 """This extension adds gettext support to Jinja."""176177 tags = {"trans"}178179 # TODO: the i18n extension is currently reevaluating values in a few180 # situations. Take this example:181 # {% trans count=something() %}{{ count }} foo{% pluralize182 # %}{{ count }} fooss{% endtrans %}183 # something is called twice here. One time for the gettext value and184 # the other time for the n-parameter of the ngettext function.185186 def __init__(self, environment):187 Extension.__init__(self, environment)188 environment.globals["_"] = _gettext_alias189 environment.extend(190 install_gettext_translations=self._install,191 install_null_translations=self._install_null,192 install_gettext_callables=self._install_callables,193 uninstall_gettext_translations=self._uninstall,194 extract_translations=self._extract,195 newstyle_gettext=False,196 )197198 def _install(self, translations, newstyle=None):199 gettext = getattr(translations, "ugettext", None)200 if gettext is None:201 gettext = translations.gettext202 ngettext = getattr(translations, "ungettext", None)203 if ngettext is None:204 ngettext = translations.ngettext205 self._install_callables(gettext, ngettext, newstyle)206207 def _install_null(self, newstyle=None):208 self._install_callables(209 lambda x: x, lambda s, p, n: (n != 1 and (p,) or (s,))[0], newstyle210 )211212 def _install_callables(self, gettext, ngettext, newstyle=None):213 if newstyle is not None:214 self.environment.newstyle_gettext = newstyle215 if self.environment.newstyle_gettext:216 gettext = _make_new_gettext(gettext)217 ngettext = _make_new_ngettext(ngettext)218 self.environment.globals.update(gettext=gettext, ngettext=ngettext)219220 def _uninstall(self, translations):221 for key in "gettext", "ngettext":222 self.environment.globals.pop(key, None)223224 def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):225 if isinstance(source, string_types):226 source = self.environment.parse(source)227 return extract_from_ast(source, gettext_functions)228229 def parse(self, parser):230 """Parse a translatable tag."""231 lineno = next(parser.stream).lineno232 num_called_num = False233234 # find all the variables referenced. Additionally a variable can be235 # defined in the body of the trans block too, but this is checked at236 # a later state.237 plural_expr = None238 plural_expr_assignment = None239 variables = {}240 trimmed = None241 while parser.stream.current.type != "block_end":242 if variables:243 parser.stream.expect("comma")244245 # skip colon for python compatibility246 if parser.stream.skip_if("colon"):247 break248249 name = parser.stream.expect("name")250 if name.value in variables:251 parser.fail(252 "translatable variable %r defined twice." % name.value,253 name.lineno,254 exc=TemplateAssertionError,255 )256257 # expressions258 if parser.stream.current.type == "assign":259 next(parser.stream)260 variables[name.value] = var = parser.parse_expression()261 elif trimmed is None and name.value in ("trimmed", "notrimmed"):262 trimmed = name.value == "trimmed"263 continue264 else:265 variables[name.value] = var = nodes.Name(name.value, "load")266267 if plural_expr is None:268 if isinstance(var, nodes.Call):269 plural_expr = nodes.Name("_trans", "load")270 variables[name.value] = plural_expr271 plural_expr_assignment = nodes.Assign(272 nodes.Name("_trans", "store"), var273 )274 else:275 plural_expr = var276 num_called_num = name.value == "num"277278 parser.stream.expect("block_end")279280 plural = None281 have_plural = False282 referenced = set()283284 # now parse until endtrans or pluralize285 singular_names, singular = self._parse_block(parser, True)286 if singular_names:287 referenced.update(singular_names)288 if plural_expr is None:289 plural_expr = nodes.Name(singular_names[0], "load")290 num_called_num = singular_names[0] == "num"291292 # if we have a pluralize block, we parse that too293 if parser.stream.current.test("name:pluralize"):294 have_plural = True295 next(parser.stream)296 if parser.stream.current.type != "block_end":297 name = parser.stream.expect("name")298 if name.value not in variables:299 parser.fail(300 "unknown variable %r for pluralization" % name.value,301 name.lineno,302 exc=TemplateAssertionError,303 )304 plural_expr = variables[name.value]305 num_called_num = name.value == "num"306 parser.stream.expect("block_end")307 plural_names, plural = self._parse_block(parser, False)308 next(parser.stream)309 referenced.update(plural_names)310 else:311 next(parser.stream)312313 # register free names as simple name expressions314 for var in referenced:315 if var not in variables:316 variables[var] = nodes.Name(var, "load")317318 if not have_plural:319 plural_expr = None320 elif plural_expr is None:321 parser.fail("pluralize without variables", lineno)322323 if trimmed is None:324 trimmed = self.environment.policies["ext.i18n.trimmed"]325 if trimmed:326 singular = self._trim_whitespace(singular)327 if plural:328 plural = self._trim_whitespace(plural)329330 node = self._make_node(331 singular,332 plural,333 variables,334 plural_expr,335 bool(referenced),336 num_called_num and have_plural,337 )338 node.set_lineno(lineno)339 if plural_expr_assignment is not None:340 return [plural_expr_assignment, node]341 else:342 return node343344 def _trim_whitespace(self, string, _ws_re=_ws_re):345 return _ws_re.sub(" ", string.strip())346347 def _parse_block(self, parser, allow_pluralize):348 """Parse until the next block tag with a given name."""349 referenced = []350 buf = []351 while 1:352 if parser.stream.current.type == "data":353 buf.append(parser.stream.current.value.replace("%", "%%"))354 next(parser.stream)355 elif parser.stream.current.type == "variable_begin":356 next(parser.stream)357 name = parser.stream.expect("name").value358 referenced.append(name)359 buf.append("%%(%s)s" % name)360 parser.stream.expect("variable_end")361 elif parser.stream.current.type == "block_begin":362 next(parser.stream)363 if parser.stream.current.test("name:endtrans"):364 break365 elif parser.stream.current.test("name:pluralize"):366 if allow_pluralize:367 break368 parser.fail(369 "a translatable section can have only one pluralize section"370 )371 parser.fail(372 "control structures in translatable sections are not allowed"373 )374 elif parser.stream.eos:375 parser.fail("unclosed translation block")376 else:377 raise RuntimeError("internal parser error")378379 return referenced, concat(buf)380381 def _make_node(382 self, singular, plural, variables, plural_expr, vars_referenced, num_called_num383 ):384 """Generates a useful node from the data provided."""385 # no variables referenced? no need to escape for old style386 # gettext invocations only if there are vars.387 if not vars_referenced and not self.environment.newstyle_gettext:388 singular = singular.replace("%%", "%")389 if plural:390 plural = plural.replace("%%", "%")391392 # singular only:393 if plural_expr is None:394 gettext = nodes.Name("gettext", "load")395 node = nodes.Call(gettext, [nodes.Const(singular)], [], None, None)396397 # singular and plural398 else:399 ngettext = nodes.Name("ngettext", "load")400 node = nodes.Call(401 ngettext,402 [nodes.Const(singular), nodes.Const(plural), plural_expr],403 [],404 None,405 None,406 )407408 # in case newstyle gettext is used, the method is powerful409 # enough to handle the variable expansion and autoescape410 # handling itself411 if self.environment.newstyle_gettext:412 for key, value in iteritems(variables):413 # the function adds that later anyways in case num was414 # called num, so just skip it.415 if num_called_num and key == "num":416 continue417 node.kwargs.append(nodes.Keyword(key, value))418419 # otherwise do that here420 else:421 # mark the return value as safe if we are in an422 # environment with autoescaping turned on423 node = nodes.MarkSafeIfAutoescape(node)424 if variables:425 node = nodes.Mod(426 node,427 nodes.Dict(428 [429 nodes.Pair(nodes.Const(key), value)430 for key, value in variables.items()431 ]432 ),433 )434 return nodes.Output([node])435436437class ExprStmtExtension(Extension):438 """Adds a `do` tag to Jinja that works like the print statement just439 that it doesn't print the return value.440 """441442 tags = set(["do"])443444 def parse(self, parser):445 node = nodes.ExprStmt(lineno=next(parser.stream).lineno)446 node.node = parser.parse_tuple()447 return node448449450class LoopControlExtension(Extension):451 """Adds break and continue to the template engine."""452453 tags = set(["break", "continue"])454455 def parse(self, parser):456 token = next(parser.stream)457 if token.value == "break":458 return nodes.Break(lineno=token.lineno)459 return nodes.Continue(lineno=token.lineno)460461462class WithExtension(Extension):463 pass464465466class AutoEscapeExtension(Extension):467 pass468469470class DebugExtension(Extension):471 """A ``{% debug %}`` tag that dumps the available variables,472 filters, and tests.473474 .. code-block:: html+jinja475476 <pre>{% debug %}</pre>477478 .. code-block:: text479480 {'context': {'cycler': <class 'jinja2.utils.Cycler'>,481 ...,482 'namespace': <class 'jinja2.utils.Namespace'>},483 'filters': ['abs', 'attr', 'batch', 'capitalize', 'center', 'count', 'd',484 ..., 'urlencode', 'urlize', 'wordcount', 'wordwrap', 'xmlattr'],485 'tests': ['!=', '<', '<=', '==', '>', '>=', 'callable', 'defined',486 ..., 'odd', 'sameas', 'sequence', 'string', 'undefined', 'upper']}487488 .. versionadded:: 2.11.0489 """490491 tags = {"debug"}492493 def parse(self, parser):494 lineno = parser.stream.expect("name:debug").lineno495 context = ContextReference()496 result = self.call_method("_render", [context], lineno=lineno)497 return nodes.Output([result], lineno=lineno)498499 def _render(self, context):500 result = {501 "context": context.get_all(),502 "filters": sorted(self.environment.filters.keys()),503 "tests": sorted(self.environment.tests.keys()),504 }505506 # Set the depth since the intent is to show the top few names.507 if version_info[:2] >= (3, 4):508 return pprint.pformat(result, depth=3, compact=True)509 else:510 return pprint.pformat(result, depth=3)511512513def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS, babel_style=True):514 """Extract localizable strings from the given template node. Per515 default this function returns matches in babel style that means non string516 parameters as well as keyword arguments are returned as `None`. This517 allows Babel to figure out what you really meant if you are using518 gettext functions that allow keyword arguments for placeholder expansion.519 If you don't want that behavior set the `babel_style` parameter to `False`520 which causes only strings to be returned and parameters are always stored521 in tuples. As a consequence invalid gettext calls (calls without a single522 string parameter or string parameters after non-string parameters) are523 skipped.524525 This example explains the behavior:526527 >>> from jinja2 import Environment528 >>> env = Environment()529 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')530 >>> list(extract_from_ast(node))531 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]532 >>> list(extract_from_ast(node, babel_style=False))533 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]534535 For every string found this function yields a ``(lineno, function,536 message)`` tuple, where:537538 * ``lineno`` is the number of the line on which the string was found,539 * ``function`` is the name of the ``gettext`` function used (if the540 string was extracted from embedded Python code), and541 * ``message`` is the string itself (a ``unicode`` object, or a tuple542 of ``unicode`` objects for functions with multiple string arguments).543544 This extraction function operates on the AST and is because of that unable545 to extract any comments. For comment support you have to use the babel546 extraction interface or extract comments yourself.547 """548 for node in node.find_all(nodes.Call):549 if (550 not isinstance(node.node, nodes.Name)551 or node.node.name not in gettext_functions552 ):553 continue554555 strings = []556 for arg in node.args:557 if isinstance(arg, nodes.Const) and isinstance(arg.value, string_types):558 strings.append(arg.value)559 else:560 strings.append(None)561562 for _ in node.kwargs:563 strings.append(None)564 if node.dyn_args is not None:565 strings.append(None)566 if node.dyn_kwargs is not None:567 strings.append(None)568569 if not babel_style:570 strings = tuple(x for x in strings if x is not None)571 if not strings:572 continue573 else:574 if len(strings) == 1:575 strings = strings[0]576 else:577 strings = tuple(strings)578 yield node.lineno, node.node.name, strings579580581class _CommentFinder(object):582 """Helper class to find comments in a token stream. Can only583 find comments for gettext calls forwards. Once the comment584 from line 4 is found, a comment for line 1 will not return a585 usable value.586 """587588 def __init__(self, tokens, comment_tags):589 self.tokens = tokens590 self.comment_tags = comment_tags591 self.offset = 0592 self.last_lineno = 0593594 def find_backwards(self, offset):595 try:596 for _, token_type, token_value in reversed(597 self.tokens[self.offset : offset]598 ):599 if token_type in ("comment", "linecomment"):600 try:601 prefix, comment = token_value.split(None, 1)602 except ValueError:603 continue604 if prefix in self.comment_tags:605 return [comment.rstrip()]606 return []607 finally:608 self.offset = offset609610 def find_comments(self, lineno):611 if not self.comment_tags or self.last_lineno > lineno:612 return []613 for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset :]):614 if token_lineno > lineno:615 return self.find_backwards(self.offset + idx)616 return self.find_backwards(len(self.tokens))617618619def babel_extract(fileobj, keywords, comment_tags, options):620 """Babel extraction method for Jinja templates.621622 .. versionchanged:: 2.3623 Basic support for translation comments was added. If `comment_tags`624 is now set to a list of keywords for extraction, the extractor will625 try to find the best preceding comment that begins with one of the626 keywords. For best results, make sure to not have more than one627 gettext call in one line of code and the matching comment in the628 same line or the line before.629630 .. versionchanged:: 2.5.1631 The `newstyle_gettext` flag can be set to `True` to enable newstyle632 gettext calls.633634 .. versionchanged:: 2.7635 A `silent` option can now be provided. If set to `False` template636 syntax errors are propagated instead of being ignored.637638 :param fileobj: the file-like object the messages should be extracted from639 :param keywords: a list of keywords (i.e. function names) that should be640 recognized as translation functions641 :param comment_tags: a list of translator tags to search for and include642 in the results.643 :param options: a dictionary of additional options (optional)644 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.645 (comments will be empty currently)646 """647 extensions = set()648 for extension in options.get("extensions", "").split(","):649 extension = extension.strip()650 if not extension:651 continue652 extensions.add(import_string(extension))653 if InternationalizationExtension not in extensions:654 extensions.add(InternationalizationExtension)655656 def getbool(options, key, default=False):657 return options.get(key, str(default)).lower() in ("1", "on", "yes", "true")658659 silent = getbool(options, "silent", True)660 environment = Environment(661 options.get("block_start_string", BLOCK_START_STRING),662 options.get("block_end_string", BLOCK_END_STRING),663 options.get("variable_start_string", VARIABLE_START_STRING),664 options.get("variable_end_string", VARIABLE_END_STRING),665 options.get("comment_start_string", COMMENT_START_STRING),666 options.get("comment_end_string", COMMENT_END_STRING),667 options.get("line_statement_prefix") or LINE_STATEMENT_PREFIX,668 options.get("line_comment_prefix") or LINE_COMMENT_PREFIX,669 getbool(options, "trim_blocks", TRIM_BLOCKS),670 getbool(options, "lstrip_blocks", LSTRIP_BLOCKS),671 NEWLINE_SEQUENCE,672 getbool(options, "keep_trailing_newline", KEEP_TRAILING_NEWLINE),673 frozenset(extensions),674 cache_size=0,675 auto_reload=False,676 )677678 if getbool(options, "trimmed"):679 environment.policies["ext.i18n.trimmed"] = True680 if getbool(options, "newstyle_gettext"):681 environment.newstyle_gettext = True682683 source = fileobj.read().decode(options.get("encoding", "utf-8"))684 try:685 node = environment.parse(source)686 tokens = list(environment.lex(environment.preprocess(source)))687 except TemplateSyntaxError:688 if not silent:689 raise690 # skip templates with syntax errors691 return692693 finder = _CommentFinder(tokens, comment_tags)694 for lineno, func, message in extract_from_ast(node, keywords):695 yield lineno, func, message, finder.find_comments(lineno)696697698#: nicer import names699i18n = InternationalizationExtension700do = ExprStmtExtension701loopcontrols = LoopControlExtension702with_ = WithExtension703autoescape = AutoEscapeExtension704debug = DebugExtension