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
(node, gettext_functions=GETTEXT_FUNCTIONS,
babel_style=True)
| 435 | |
| 436 | |
| 437 | def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS, |
| 438 | babel_style=True): |
| 439 | """Extract localizable strings from the given template node. Per |
| 440 | default this function returns matches in babel style that means non string |
| 441 | parameters as well as keyword arguments are returned as `None`. This |
| 442 | allows Babel to figure out what you really meant if you are using |
| 443 | gettext functions that allow keyword arguments for placeholder expansion. |
| 444 | If you don't want that behavior set the `babel_style` parameter to `False` |
| 445 | which causes only strings to be returned and parameters are always stored |
| 446 | in tuples. As a consequence invalid gettext calls (calls without a single |
| 447 | string parameter or string parameters after non-string parameters) are |
| 448 | skipped. |
| 449 | |
| 450 | This example explains the behavior: |
| 451 | |
| 452 | >>> from jinja2 import Environment |
| 453 | >>> env = Environment() |
| 454 | >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}') |
| 455 | >>> list(extract_from_ast(node)) |
| 456 | [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))] |
| 457 | >>> list(extract_from_ast(node, babel_style=False)) |
| 458 | [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))] |
| 459 | |
| 460 | For every string found this function yields a ``(lineno, function, |
| 461 | message)`` tuple, where: |
| 462 | |
| 463 | * ``lineno`` is the number of the line on which the string was found, |
| 464 | * ``function`` is the name of the ``gettext`` function used (if the |
| 465 | string was extracted from embedded Python code), and |
| 466 | * ``message`` is the string itself (a ``unicode`` object, or a tuple |
| 467 | of ``unicode`` objects for functions with multiple string arguments). |
| 468 | |
| 469 | This extraction function operates on the AST and is because of that unable |
| 470 | to extract any comments. For comment support you have to use the babel |
| 471 | extraction interface or extract comments yourself. |
| 472 | """ |
| 473 | for node in node.find_all(nodes.Call): |
| 474 | if not isinstance(node.node, nodes.Name) or \ |
| 475 | node.node.name not in gettext_functions: |
| 476 | continue |
| 477 | |
| 478 | strings = [] |
| 479 | for arg in node.args: |
| 480 | if isinstance(arg, nodes.Const) and \ |
| 481 | isinstance(arg.value, string_types): |
| 482 | strings.append(arg.value) |
| 483 | else: |
| 484 | strings.append(None) |
| 485 | |
| 486 | for arg in node.kwargs: |
| 487 | strings.append(None) |
| 488 | if node.dyn_args is not None: |
| 489 | strings.append(None) |
| 490 | if node.dyn_kwargs is not None: |
| 491 | strings.append(None) |
| 492 | |
| 493 | if not babel_style: |
| 494 | strings = tuple(x for x in strings if x is not None) |
no test coverage detected
searching dependent graphs…