本文整理汇总了Python中babel.messages.extract.extract_python函数的典型用法代码示例。如果您正苦于以下问题:Python extract_python函数的具体用法?Python extract_python怎么用?Python extract_python使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了extract_python函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extract_default_encoding_ascii
def test_extract_default_encoding_ascii(self):
buf = BytesIO(b'_("a")')
messages = list(extract.extract_python(
buf, list(extract.DEFAULT_KEYWORDS), [], {},
))
# Should work great in both py2 and py3
self.assertEqual([(1, '_', 'a', [])], messages)
示例2: extract_tower_python
def extract_tower_python(fileobj, keywords, comment_tags, options):
for lineno, funcname, message, comments in \
list(extract_python(fileobj, keywords, comment_tags, options)):
message = tweak_message(message)
yield lineno, funcname, message, comments
示例3: extract
def extract(fileobj, keywords, comment_tags, options):
"""Babel entry point that extracts translation strings from XML templates."""
from .template import KajikiSyntaxError
from .xml_template import _Parser, _Compiler, _DomTransformer
try:
from babel.messages.extract import extract_python
extract_expr = options.get('extract_python', False)
except ImportError:
extract_python = None
extract_expr = False
source = fileobj.read()
if isinstance(source, bytes):
source = source.decode('utf-8')
doc = _Parser(filename='<string>', source=source).parse()
doc = _DomTransformer(doc, strip_text=options.get('strip_text', False)).transform()
compiler = _Compiler(filename='<string>', doc=doc,
mode=options.get('mode', 'xml'),
is_fragment=options.get('is_fragment', False))
ir = compiler.compile()
for node in ir:
if isinstance(node, TranslatableTextNode):
if node.text.strip():
yield (node.lineno, '_', node.text, [])
elif extract_expr and isinstance(node, ExprNode):
try:
for e in extract_python(BytesIO(node.text.encode('utf-8')),
keywords, comment_tags, options):
yield (node.lineno, e[1], e[2], e[3])
except (TokenError, SyntaxError) as e:
raise KajikiSyntaxError(e, source, '<string>', node.lineno, 0)
示例4: test_utf8_bom_with_latin_magic_comment_fails
def test_utf8_bom_with_latin_magic_comment_fails(self):
buf = BytesIO(codecs.BOM_UTF8 + u"""# -*- coding: latin-1 -*-
# NOTE: hello
msg = _('Bonjour à tous')
""".encode('utf-8'))
self.assertRaises(SyntaxError, list,
extract.extract_python(buf, ('_',), ['NOTE:'], {}))
示例5: test_nested_calls
def test_nested_calls(self):
buf = BytesIO(b"""\
msg1 = _(i18n_arg.replace(r'\"', '"'))
msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2)
msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2)
msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2)
msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2))
msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2))
msg7 = _(hello.there)
msg8 = gettext('Rabbit')
msg9 = dgettext('wiki', model.addPage())
msg10 = dngettext(getDomain(), 'Page', 'Pages', 3)
""")
messages = list(extract.extract_python(buf,
extract.DEFAULT_KEYWORDS.keys(),
[], {}))
self.assertEqual([
(1, '_', None, []),
(2, 'ungettext', (None, None, None), []),
(3, 'ungettext', (u'Babel', None, None), []),
(4, 'ungettext', (None, u'Babels', None), []),
(5, 'ungettext', (u'bunny', u'bunnies', None), []),
(6, 'ungettext', (None, u'bunnies', None), []),
(7, '_', None, []),
(8, 'gettext', u'Rabbit', []),
(9, 'dgettext', (u'wiki', None), []),
(10, 'dngettext', (None, u'Page', u'Pages', None), [])],
messages)
示例6: test_nested_messages
def test_nested_messages(self):
buf = BytesIO(b"""
# NOTE: First
_(u'Hello, {name}!', name=_(u'Foo Bar'))
# NOTE: Second
_(u'Hello, {name1} and {name2}!', name1=_(u'Heungsub'),
name2=_(u'Armin'))
# NOTE: Third
_(u'Hello, {0} and {1}!', _(u'Heungsub'),
_(u'Armin'))
""")
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual((u'Hello, {name}!', None), messages[0][2])
self.assertEqual([u'NOTE: First'], messages[0][3])
self.assertEqual(u'Foo Bar', messages[1][2])
self.assertEqual([], messages[1][3])
self.assertEqual((u'Hello, {name1} and {name2}!', None), messages[2][2])
self.assertEqual([u'NOTE: Second'], messages[2][3])
self.assertEqual(u'Heungsub', messages[3][2])
self.assertEqual([], messages[3][3])
self.assertEqual(u'Armin', messages[4][2])
self.assertEqual([], messages[4][3])
self.assertEqual((u'Hello, {0} and {1}!', None), messages[5][2])
self.assertEqual([u'NOTE: Third'], messages[5][3])
self.assertEqual(u'Heungsub', messages[6][2])
self.assertEqual([], messages[6][3])
self.assertEqual(u'Armin', messages[7][2])
self.assertEqual([], messages[7][3])
示例7: process_python
def process_python(self, code, code_lineno, translator_strings):
comment_tags = self.config['comment-tags']
for lineno, funcname, messages, python_translator_comments \
in extract_python(code,
self.keywords, comment_tags, self.options):
yield (code_lineno + (lineno - 1), funcname, messages,
translator_strings + python_translator_comments)
示例8: test_comment_tag
def test_comment_tag(self):
buf = BytesIO(b"""
# NOTE: A translation comment
msg = _(u'Foo Bar')
""")
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Foo Bar', messages[0][2])
self.assertEqual([u'NOTE: A translation comment'], messages[0][3])
示例9: test_concatenated_strings
def test_concatenated_strings(self):
buf = BytesIO(b"""\
foobar = _('foo' 'bar')
""")
messages = list(extract.extract_python(buf,
extract.DEFAULT_KEYWORDS.keys(),
[], {}))
self.assertEqual(u'foobar', messages[0][2])
示例10: test_utf8_message_with_utf8_bom_and_magic_comment
def test_utf8_message_with_utf8_bom_and_magic_comment(self):
buf = BytesIO(codecs.BOM_UTF8 + u"""# -*- coding: utf-8 -*-
# NOTE: hello
msg = _('Bonjour à tous')
""".encode('utf-8'))
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Bonjour à tous', messages[0][2])
self.assertEqual([u'NOTE: hello'], messages[0][3])
示例11: extract_template
def extract_template(fileobj, keywords, comment_tags, options):
src = force_text(fileobj.read(), settings.FILE_CHARSET)
if fileobj.name.endswith(".jade"):
src = process(src, compiler=Compiler)
src = templatize(src, "")
if "gettext" in src:
return extract_python(StringIO.StringIO(src.encode("utf8")), keywords, comment_tags, options)
return ()
示例12: test_utf8_message_with_utf8_bom
def test_utf8_message_with_utf8_bom(self):
buf = StringIO(codecs.BOM_UTF8 + """
# NOTE: hello
msg = _('Bonjour à tous')
""")
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Bonjour à tous', messages[0][2])
self.assertEqual([u'NOTE: hello'], messages[0][3])
示例13: test_utf8_message_with_magic_comment
def test_utf8_message_with_magic_comment(self):
buf = StringIO("""# -*- coding: utf-8 -*-
# NOTE: hello
msg = _('Bonjour à tous')
""")
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Bonjour à tous', messages[0][2])
self.assertEqual([u'NOTE: hello'], messages[0][3])
示例14: test_utf8_message_with_utf8_bom
def test_utf8_message_with_utf8_bom(self):
buf = BytesIO(codecs.BOM_UTF8 + """
# NOTE: hello
msg = _('Bonjour \xe0 tous')
""".encode('utf-8'))
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual('Bonjour \xe0 tous', messages[0][2])
self.assertEqual(['NOTE: hello'], messages[0][3])
示例15: test_utf8_raw_strings_match_unicode_strings
def test_utf8_raw_strings_match_unicode_strings(self):
buf = BytesIO(codecs.BOM_UTF8 + u"""
msg = _('Bonjour à tous')
msgu = _(u'Bonjour à tous')
""".encode('utf-8'))
messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
self.assertEqual(u'Bonjour à tous', messages[0][2])
self.assertEqual(messages[0][2], messages[1][2])