MCPcopy
hub / github.com/pallets/jinja / babel_extract

Function babel_extract

src/jinja2/ext.py:774–863  ·  view source on GitHub ↗

Babel extraction method for Jinja templates. .. versionchanged:: 2.3 Basic support for translation comments was added. If `comment_tags` is now set to a list of keywords for extraction, the extractor will try to find the best preceding comment that begins with one of the

(
    fileobj: t.BinaryIO,
    keywords: t.Sequence[str],
    comment_tags: t.Sequence[str],
    options: t.Dict[str, t.Any],
)

Source from the content-addressed store, hash-verified

772
773
774def babel_extract(
775 fileobj: t.BinaryIO,
776 keywords: t.Sequence[str],
777 comment_tags: t.Sequence[str],
778 options: t.Dict[str, t.Any],
779) -> t.Iterator[
780 t.Tuple[
781 int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]], t.List[str]
782 ]
783]:
784 """Babel extraction method for Jinja templates.
785
786 .. versionchanged:: 2.3
787 Basic support for translation comments was added. If `comment_tags`
788 is now set to a list of keywords for extraction, the extractor will
789 try to find the best preceding comment that begins with one of the
790 keywords. For best results, make sure to not have more than one
791 gettext call in one line of code and the matching comment in the
792 same line or the line before.
793
794 .. versionchanged:: 2.5.1
795 The `newstyle_gettext` flag can be set to `True` to enable newstyle
796 gettext calls.
797
798 .. versionchanged:: 2.7
799 A `silent` option can now be provided. If set to `False` template
800 syntax errors are propagated instead of being ignored.
801
802 :param fileobj: the file-like object the messages should be extracted from
803 :param keywords: a list of keywords (i.e. function names) that should be
804 recognized as translation functions
805 :param comment_tags: a list of translator tags to search for and include
806 in the results.
807 :param options: a dictionary of additional options (optional)
808 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
809 (comments will be empty currently)
810 """
811 extensions: t.Dict[t.Type[Extension], None] = {}
812
813 for extension_name in options.get("extensions", "").split(","):
814 extension_name = extension_name.strip()
815
816 if not extension_name:
817 continue
818
819 extensions[import_string(extension_name)] = None
820
821 if InternationalizationExtension not in extensions:
822 extensions[InternationalizationExtension] = None
823
824 def getbool(options: t.Mapping[str, str], key: str, default: bool = False) -> bool:
825 return options.get(key, str(default)).lower() in {"1", "on", "yes", "true"}
826
827 silent = getbool(options, "silent", True)
828 environment = Environment(
829 options.get("block_start_string", defaults.BLOCK_START_STRING),
830 options.get("block_end_string", defaults.BLOCK_END_STRING),
831 options.get("variable_start_string", defaults.VARIABLE_START_STRING),

Callers 5

test_extractMethod · 0.90
test_extract_trimmedMethod · 0.90
test_comment_extractMethod · 0.90
test_extract_contextMethod · 0.90

Calls 10

parseMethod · 0.95
lexMethod · 0.95
preprocessMethod · 0.95
import_stringFunction · 0.85
getboolFunction · 0.85
EnvironmentClass · 0.85
_CommentFinderClass · 0.85
extract_from_astFunction · 0.85
find_commentsMethod · 0.80
getMethod · 0.45

Tested by 5

test_extractMethod · 0.72
test_extract_trimmedMethod · 0.72
test_comment_extractMethod · 0.72
test_extract_contextMethod · 0.72