本文整理汇总了Python中babel.messages.extract.extract函数的典型用法代码示例。如果您正苦于以下问题:Python extract函数的具体用法?Python extract怎么用?Python extract使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了extract函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract
def extract(self):
catalog_obj = catalog.Catalog()
path = os.path.join(self.root, 'messages.pot')
template = self.pod.open_file(path, mode='w')
extracted = []
# Extracts messages from views.
pod_files = self.pod.list_dir('/')
for pod_path in pod_files:
if os.path.splitext(pod_path)[-1] in _TRANSLATABLE_EXTENSIONS:
content = self.pod.read_file(pod_path)
import cStringIO
fp = cStringIO.StringIO()
fp.write(content)
fp.seek(0)
import tokenize
try:
messages = extract.extract('python', fp)
for message in messages:
lineno, string, comments, context = message
catalog_obj.add(string, None, [(pod_path, lineno)], auto_comments=comments, context=context)
except tokenize.TokenError:
print 'Problem extracting: {}'.format(pod_path)
raise
# TODO(jeremydw): Extract messages from content.
# Writes to PO template.
pofile.write_po(template, catalog_obj, width=80, no_location=True, omit_header=True, sort_output=True, sort_by_file=True)
logging.info('Extracted {} messages from {} files to: {}'.format(len(extracted), len(pod_files), template))
template.close()
return catalog_obj
示例2: test_template_string_standard_usage
def test_template_string_standard_usage():
buf = BytesIO(b"msg1 = gettext(`Very template, wow`)")
messages = list(
extract.extract('javascript', buf, {"gettext": None}, [], {})
)
assert messages == [(1, 'Very template, wow', [], None, ())]
示例3: _extract
def _extract(self, app):
catalog = Catalog(domain="django", charset="utf8")
files = {}
for dirpath, dirnames, filenames in filtered_walk(app.path):
for filename in filenames:
filename = os.path.join(dirpath, filename)
if ACCEPTABLE_FILENAMES_RE.match(filename):
rel_filename = filename[len(os.path.commonprefix((app.path, filename))) + 1:].replace(os.sep, "/")
files[rel_filename] = filename
self.log.info("%s: %d translatable files found", app.label, len(files))
extractors = self.get_extractors()
for rel_filename, filename in sorted(files.items()):
extractor_tup = extractors.get(os.path.splitext(filename)[1])
if not extractor_tup:
self.log.warning("Not sure how to extract messages from %s", filename)
continue
extractor, options = extractor_tup
with open(filename, "rb") as fp:
for (lineno, message, comments, context) in extract(extractor, fp, options=options):
catalog.add(message, locations=[(rel_filename, 0)], auto_comments=comments)
if len(catalog):
pot_path = self._get_pot_path(app)
with open(pot_path, "w") as outf:
pofile.write_po(outf, catalog, width=1000, omit_header=True, sort_output=True)
self.log.info("%s: %d messages in %s", app.label, len(catalog), pot_path)
return catalog
示例4: test_dotted_keyword_extract
def test_dotted_keyword_extract():
buf = BytesIO(b"msg1 = com.corporate.i18n.formatMessage('Insert coin to continue')")
messages = list(
extract.extract('javascript', buf, {"com.corporate.i18n.formatMessage": None}, [], {})
)
assert messages == [(1, 'Insert coin to continue', [], None, ())]
示例5: test_template_string_tag_usage
def test_template_string_tag_usage():
buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }")
messages = list(
extract.extract('javascript', buf, {"i18n": None}, [], {})
)
assert messages == [(1, 'Tag template, wow', [], None, ())]
示例6: fake_extract_from_dir
def fake_extract_from_dir(filename, fileobj, method, options, keywords, comment_tags):
"""We use Babel's exctract_from_dir() to pull out our gettext
strings. In the tests, I don't have a directory of files, I have StringIO
objects. So, we fake the original function with this one."""
for lineno, message, comments, context in extract(method, fileobj, keywords,
comment_tags, options):
yield filename, lineno, message, comments, context
示例7: extract_from_file
def extract_from_file(filename):
for pattern, method in MAPPING:
if filename.endswith(pattern):
with open(filename, "rb") as in_file:
for lineno, message, comments, context in extract(method, in_file, keywords=KEYWORDS):
lineno = 0 # Avoid messy diffs
yield filename, lineno, message, comments
break
示例8: test_future
def test_future(self):
buf = BytesIO(br"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
nbsp = _('\xa0')
""")
messages = list(extract.extract('python', buf,
extract.DEFAULT_KEYWORDS, [], {}))
assert messages[0][1] == u'\xa0'
示例9: test_warn_if_empty_string_msgid_found_in_context_aware_extraction_method
def test_warn_if_empty_string_msgid_found_in_context_aware_extraction_method(self):
buf = BytesIO(b"\nmsg = pgettext('ctxt', '')\n")
stderr = sys.stderr
sys.stderr = StringIO()
try:
messages = extract.extract('python', buf)
self.assertEqual([], list(messages))
assert 'warning: Empty msgid.' in sys.stderr.getvalue()
finally:
sys.stderr = stderr
示例10: test_simple_extract
def test_simple_extract(self):
buf = BytesIO(
b"""\
msg1 = _('simple')
msg2 = gettext('simple')
msg3 = ngettext('s', 'p', 42)
"""
)
messages = list(extract.extract("javascript", buf, extract.DEFAULT_KEYWORDS, [], {}))
self.assertEqual([(1, "simple", [], None), (2, "simple", [], None), (3, ("s", "p"), [], None)], messages)
示例11: test_simple_extract
def test_simple_extract(self):
buf = StringIO("""\
msg1 = _('simple')
msg2 = gettext('simple')
msg3 = ngettext('s', 'p', 42)
""")
messages = \
list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS,
[], {}))
self.assertEqual([(1, 'simple', []),
(2, 'simple', []),
(3, ('s', 'p'), [])], messages)
示例12: test_simple_extract
def test_simple_extract():
buf = BytesIO(b"""\
msg1 = _('simple')
msg2 = gettext('simple')
msg3 = ngettext('s', 'p', 42)
""")
messages = \
list(extract.extract('javascript', buf, extract.DEFAULT_KEYWORDS,
[], {}))
assert messages == [(1, 'simple', [], None, ()),
(2, 'simple', [], None, ()),
(3, ('s', 'p'), [], None, ())]
示例13: test_empty_string_msgid
def test_empty_string_msgid(self):
buf = BytesIO(b"""\
msg = _('')
""")
stderr = sys.stderr
sys.stderr = StringIO()
try:
messages = \
list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS,
[], {}))
self.assertEqual([], messages)
assert 'warning: Empty msgid.' in sys.stderr.getvalue()
finally:
sys.stderr = stderr
示例14: test_different_signatures
def test_different_signatures(self):
buf = BytesIO(b"""
foo = _('foo', 'bar')
n = ngettext('hello', 'there', n=3)
n = ngettext(n=3, 'hello', 'there')
n = ngettext(n=3, *messages)
n = ngettext()
n = ngettext('foo')
""")
messages = \
list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [],
{}))
self.assertEqual(len(messages), 2)
self.assertEqual(u'foo', messages[0][1])
self.assertEqual((u'hello', u'there'), messages[1][1])
示例15: babel_extract_terms
def babel_extract_terms(fname, path, root, extract_method="python", trans_type='code',
extra_comments=None, extract_keywords={'_': None}):
module, fabsolutepath, _, display_path = verified_module_filepaths(fname, path, root)
extra_comments = extra_comments or []
if module:
src_file = open(fabsolutepath, 'r')
try:
for lineno, message, comments in extract.extract(extract_method, src_file,
keywords=extract_keywords):
push_translation(module, trans_type, display_path, lineno,
encode(message), comments + extra_comments)
except Exception:
_logger.exception("Failed to extract terms from %s", fabsolutepath)
finally:
src_file.close()