MCPcopy Create free account
hub / github.com/nodejs/node / extract_from_ast

Function extract_from_ast

deps/v8/third_party/jinja2/ext.py:644–720  ·  view source on GitHub ↗

Extract localizable strings from the given template node. Per default this function returns matches in babel style that means non string parameters as well as keyword arguments are returned as `None`. This allows Babel to figure out what you really meant if you are using gettext fu

(
    ast: nodes.Template,
    gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,
    babel_style: bool = True,
)

Source from the content-addressed store, hash-verified

642
643
644def extract_from_ast(
645 ast: nodes.Template,
646 gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,
647 babel_style: bool = True,
648) -> t.Iterator[
649 t.Tuple[int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]]
650]:
651 """Extract localizable strings from the given template node. Per
652 default this function returns matches in babel style that means non string
653 parameters as well as keyword arguments are returned as `None`. This
654 allows Babel to figure out what you really meant if you are using
655 gettext functions that allow keyword arguments for placeholder expansion.
656 If you don't want that behavior set the `babel_style` parameter to `False`
657 which causes only strings to be returned and parameters are always stored
658 in tuples. As a consequence invalid gettext calls (calls without a single
659 string parameter or string parameters after non-string parameters) are
660 skipped.
661
662 This example explains the behavior:
663
664 >>> from jinja2 import Environment
665 >>> env = Environment()
666 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
667 >>> list(extract_from_ast(node))
668 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
669 >>> list(extract_from_ast(node, babel_style=False))
670 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]
671
672 For every string found this function yields a ``(lineno, function,
673 message)`` tuple, where:
674
675 * ``lineno`` is the number of the line on which the string was found,
676 * ``function`` is the name of the ``gettext`` function used (if the
677 string was extracted from embedded Python code), and
678 * ``message`` is the string, or a tuple of strings for functions
679 with multiple string arguments.
680
681 This extraction function operates on the AST and is because of that unable
682 to extract any comments. For comment support you have to use the babel
683 extraction interface or extract comments yourself.
684 """
685 out: t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]
686
687 for node in ast.find_all(nodes.Call):
688 if (
689 not isinstance(node.node, nodes.Name)
690 or node.node.name not in gettext_functions
691 ):
692 continue
693
694 strings: t.List[t.Optional[str]] = []
695
696 for arg in node.args:
697 if isinstance(arg, nodes.Const) and isinstance(arg.value, str):
698 strings.append(arg.value)
699 else:
700 strings.append(None)
701

Callers 2

_extractMethod · 0.70
babel_extractFunction · 0.70

Calls 2

find_allMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected