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