• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python extract.extract函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中babel.messages.extract.extract函数的典型用法代码示例。如果您正苦于以下问题:Python extract函数的具体用法?Python extract怎么用?Python extract使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了extract函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:statusz,项目名称:pygrow,代码行数:32,代码来源:translations.py


示例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, ())]
开发者ID:JonathanRRogers,项目名称:babel,代码行数:7,代码来源:test_js_extract.py


示例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
开发者ID:akx,项目名称:kompassi,代码行数:27,代码来源:kompassi_i18n.py


示例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, ())]
开发者ID:JonathanRRogers,项目名称:babel,代码行数:7,代码来源:test_js_extract.py


示例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, ())]
开发者ID:JonathanRRogers,项目名称:babel,代码行数:7,代码来源:test_js_extract.py


示例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
开发者ID:alexgibson,项目名称:bedrock,代码行数:8,代码来源:test_extract.py


示例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
开发者ID:cuberskulk,项目名称:shoop,代码行数:8,代码来源:gather_i18n.py


示例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'
开发者ID:JonathanRRogers,项目名称:babel,代码行数:9,代码来源:test_extract.py


示例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
开发者ID:FlowSea,项目名称:babel,代码行数:10,代码来源:test_extract.py


示例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)
开发者ID:ENuge,项目名称:babel,代码行数:11,代码来源:test_extract.py


示例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)
开发者ID:10sr,项目名称:hue,代码行数:13,代码来源:extract.py


示例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, ())]
开发者ID:JonathanRRogers,项目名称:babel,代码行数:13,代码来源:test_js_extract.py


示例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
开发者ID:FlowSea,项目名称:babel,代码行数:14,代码来源:test_extract.py


示例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])
开发者ID:FlowSea,项目名称:babel,代码行数:15,代码来源:test_extract.py


示例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()
开发者ID:AbdAllah-Ahmed,项目名称:openerp-env,代码行数:15,代码来源:translate.py


示例16: test_extract_strip_comment_tags

    def test_extract_strip_comment_tags(self):
        buf = BytesIO(
            b"""\
#: This is a comment with a very simple
#: prefix specified
_('Servus')

# NOTE: This is a multiline comment with
# a prefix too
_('Babatschi')"""
        )
        messages = list(extract.extract("python", buf, comment_tags=["NOTE:", ":"], strip_comment_tags=True))
        self.assertEqual(u"Servus", messages[0][1])
        self.assertEqual([u"This is a comment with a very simple", u"prefix specified"], messages[0][2])
        self.assertEqual(u"Babatschi", messages[1][1])
        self.assertEqual([u"This is a multiline comment with", u"a prefix too"], messages[1][2])
开发者ID:ENuge,项目名称:babel,代码行数:16,代码来源:test_extract.py


示例17: 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 extracted in extract.extract(extract_method, src_file, keywords=extract_keywords):
                 # Babel 0.9.6 yields lineno, message, comments
                 # Babel 1.3 yields lineno, message, comments, context
                 lineno, message, comments = extracted[:3]
                 push_translation(module, trans_type, display_path, lineno, encode(message))
         except Exception:
             _logger.exception('Failed to extract terms from {absolutepath}'.format(absolutepath=fabsolutepath))
         finally:
             src_file.close()
开发者ID:iw3hxn,项目名称:server,代码行数:16,代码来源:translate.py


示例18: test_extract_strip_comment_tags

    def test_extract_strip_comment_tags(self):
        buf = StringIO("""\
// NOTE: hello
// NOTE: goodbye
// multiline
msg = _('Bonjour à tous')

//: Simple comment tag
msg = _('Simple')
""")
        messages = list(extract.extract('javascript', buf,
                                        comment_tags=['NOTE:', ':'],
                                        strip_comment_tags=True))
        self.assertEqual(u'Bonjour à tous', messages[0][1])
        self.assertEqual([u'hello', u'goodbye multiline'], messages[0][2])
        self.assertEqual(u'Simple', messages[1][1])
        self.assertEqual([u'Simple comment tag'], messages[1][2])
开发者ID:Khan,项目名称:babel,代码行数:17,代码来源:test_extract.py


示例19: test_extract_strip_comment_tags

    def test_extract_strip_comment_tags(self):
        buf = BytesIO(b"""\
#: This is a comment with a very simple
#: prefix specified
_('Servus')

# NOTE: This is a multiline comment with
# a prefix too
_('Babatschi')""")
        messages = list(extract.extract('python', buf, comment_tags=['NOTE:', ':'],
                                        strip_comment_tags=True))
        self.assertEqual(u'Servus', messages[0][1])
        self.assertEqual([u'This is a comment with a very simple',
                          u'prefix specified'], messages[0][2])
        self.assertEqual(u'Babatschi', messages[1][1])
        self.assertEqual([u'This is a multiline comment with',
                          u'a prefix too'], messages[1][2])
开发者ID:FlowSea,项目名称:babel,代码行数:17,代码来源:test_extract.py


示例20: 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 not module:
         return
     src_file = open(fabsolutepath, "r")
     try:
         for extracted in extract.extract(extract_method, src_file, keywords=extract_keywords):
             # Babel 0.9.6 yields lineno, message, comments
             # Babel 1.3 yields lineno, message, comments, context
             lineno, message, comments = extracted[:3]
             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()
开发者ID:odoousers2014,项目名称:odoo,代码行数:18,代码来源:translate.py



注:本文中的babel.messages.extract.extract函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python extract.extract_from_dir函数代码示例发布时间:2022-05-24
下一篇:
Python catalog.Catalog类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap