示例#1
0
    def test_sorted_po(self):
        catalog = Catalog()
        catalog.add(
            u('bar'),
            locations=[('utils.py', 3)],
            user_comments=['Comment About `bar` with', 'multiple lines.'])
        catalog.add((u('foo'), u('foos')), (u('Voh'), u('Voeh')),
                    locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, sort_output=True)
        value = buf.getvalue().strip()
        assert b('''\
# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""

#: main.py:1
msgid "foo"
msgid_plural "foos"
msgstr[0] "Voh"
msgstr[1] "Voeh"''') in value
        assert value.find(b('msgid ""')) < value.find(
            b('msgid "bar"')) < value.find(b('msgid "foo"'))
示例#2
0
    def test_wrap_long_lines(self):
        text = """Here's some text where       
white space and line breaks matter, and should

not be removed

"""
        catalog = Catalog()
        catalog.add(text, locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf,
                        catalog,
                        no_location=True,
                        omit_header=True,
                        width=42)
        self.assertEqual(
            b(r'''msgid ""
"Here's some text where       \n"
"white space and line breaks matter, and"
" should\n"
"\n"
"not be removed\n"
"\n"
msgstr ""'''),
            buf.getvalue().strip())
示例#3
0
    def test_sorting(self):
        # Ensure the header is sorted to the first entry so that its charset
        # can be applied to all subsequent messages by GNUTranslations
        # (ensuring all messages are safely converted to unicode)
        catalog = Catalog(locale='en_US')
        catalog.add(
            u(''), '''\
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n''')
        catalog.add(u('foo'), 'Voh')
        catalog.add((u('There is'), u('There are')),
                    (u('Es gibt'), u('Es gibt')))
        catalog.add(u('Fizz'), '')
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
        buf = BytesIO()
        mofile.write_mo(buf, catalog)
        buf.seek(0)
        translations = gettext.GNUTranslations(fp=buf)
        self.assertEqual(u('Voh'), translations.ugettext('foo'))
        assert isinstance(translations.ugettext('foo'), text_type)
        self.assertEqual(u('Es gibt'),
                         translations.ungettext('There is', 'There are', 1))
        assert isinstance(translations.ungettext('There is', 'There are', 1),
                          text_type)
        self.assertEqual(u('Fizz'), translations.ugettext('Fizz'))
        assert isinstance(translations.ugettext('Fizz'), text_type)
        self.assertEqual(u('Fuzz'), translations.ugettext('Fuzz'))
        assert isinstance(translations.ugettext('Fuzz'), text_type)
        self.assertEqual(u('Fuzzes'), translations.ugettext('Fuzzes'))
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
示例#4
0
    def test_with_context(self):
        buf = StringIO(r'''# Some string in the menu
#: main.py:1
msgctxt "Menu"
msgid "foo"
msgstr "Voh"

# Another string in the menu
#: main.py:2
msgctxt "Menu"
msgid "bar"
msgstr "Bahr"
''')
        catalog = pofile.read_po(buf, ignore_obsolete=True)
        self.assertEqual(2, len(catalog))
        message = catalog.get('foo', context='Menu')
        self.assertEqual('Menu', message.context)
        message = catalog.get('bar', context='Menu')
        self.assertEqual('Menu', message.context)

        # And verify it pass through write_po
        out_buf = BytesIO()
        pofile.write_po(out_buf, catalog, omit_header=True)
        assert out_buf.getvalue().strip() == b(buf.getvalue().strip()), \
                                                            out_buf.getvalue()
示例#5
0
文件: pofile.py 项目: vsajip/babel3
    def test_with_context(self):
        buf = StringIO(
            r"""# Some string in the menu
#: main.py:1
msgctxt "Menu"
msgid "foo"
msgstr "Voh"

# Another string in the menu
#: main.py:2
msgctxt "Menu"
msgid "bar"
msgstr "Bahr"
"""
        )
        catalog = pofile.read_po(buf, ignore_obsolete=True)
        self.assertEqual(2, len(catalog))
        message = catalog.get("foo", context="Menu")
        self.assertEqual("Menu", message.context)
        message = catalog.get("bar", context="Menu")
        self.assertEqual("Menu", message.context)

        # And verify it pass through write_po
        out_buf = BytesIO()
        pofile.write_po(out_buf, catalog, omit_header=True)
        assert out_buf.getvalue().strip() == buf.getvalue().strip().encode("latin-1"), out_buf.getvalue()
示例#6
0
    def test_no_wrap_and_width_behaviour_on_comments(self):
        catalog = Catalog()
        catalog.add("Pretty dam long message id, which must really be big "
                    "to test this wrap behaviour, if not it won't work.",
                    locations=[("fake.py", n) for n in range(1, 30)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, width=None, omit_header=True)
        self.assertEqual(b("""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7
#: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14
#: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21
#: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work."
msgstr ""

"""), buf.getvalue().lower())
        buf = BytesIO()
        pofile.write_po(buf, catalog, width=100, omit_header=True)
        self.assertEqual(b("""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10
#: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19
#: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid ""
"pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't"
" work."
msgstr ""

"""), buf.getvalue().lower())
示例#7
0
    def test_with_context(self):
        buf = StringIO(r'''# Some string in the menu
#: main.py:1
msgctxt "Menu"
msgid "foo"
msgstr "Voh"

# Another string in the menu
#: main.py:2
msgctxt "Menu"
msgid "bar"
msgstr "Bahr"
''')
        catalog = pofile.read_po(buf, ignore_obsolete=True)
        self.assertEqual(2, len(catalog))
        message = catalog.get('foo', context='Menu')
        self.assertEqual('Menu', message.context)
        message = catalog.get('bar', context='Menu')
        self.assertEqual('Menu', message.context)

        # And verify it pass through write_po
        out_buf = BytesIO()
        pofile.write_po(out_buf, catalog, omit_header=True)
        assert out_buf.getvalue().strip() == b(buf.getvalue().strip()), \
                                                            out_buf.getvalue()
示例#8
0
    def obtainFilter(self, processing, uri):
        """
        Checks the filter URI.
        
        @param processing: Processing
            The processing used for delivering the request.
        @param uri: string
            The URI to call, parameters are allowed.
        @return: tuple(boolean|None, integer, string)
            A tuple containing as the first True if the filter URI provided a True value, None if the filter cannot be fetched,
            on the second position the response status and on the last position the response text.
        """
        assert isinstance(processing, Processing), "Invalid processing %s" % processing
        assert isinstance(uri, str), "Invalid URI %s" % uri

        request = processing.ctx.request()
        assert isinstance(request, RequestFilter), "Invalid request %s" % request

        url = urlparse(uri)
        request.scheme, request.method = self.scheme, HTTP_GET
        request.headers = {}
        request.uri = url.path.lstrip("/")
        request.parameters = parse_qsl(url.query, True, False)
        request.accTypes = [self.mimeTypeJson]
        request.accCharSets = [self.encodingJson]

        chain = Chain(processing)
        chain.process(
            request=request,
            requestCnt=processing.ctx.requestCnt(),
            response=processing.ctx.response(),
            responseCnt=processing.ctx.responseCnt(),
        ).doAll()

        response, responseCnt = chain.arg.response, chain.arg.responseCnt
        assert isinstance(response, ResponseHTTP), "Invalid response %s" % response
        assert isinstance(responseCnt, ResponseContentHTTP), "Invalid response content %s" % responseCnt

        if ResponseHTTP.text in response and response.text:
            text = response.text
        elif ResponseHTTP.code in response and response.code:
            text = response.code
        else:
            text = None
        if (
            ResponseContentHTTP.source not in responseCnt
            or responseCnt.source is None
            or not isSuccess(response.status)
        ):
            return None, response.status, text

        if isinstance(responseCnt.source, IInputStream):
            source = responseCnt.source
        else:
            source = BytesIO()
            for bytes in responseCnt.source:
                source.write(bytes)
            source.seek(0)
        allowed = json.load(codecs.getreader(self.encodingJson)(source))
        return allowed["HasAccess"] == "True", response.status, text
示例#9
0
文件: mofile.py 项目: vsajip/babel3
    def test_sorting(self):
        # Ensure the header is sorted to the first entry so that its charset
        # can be applied to all subsequent messages by GNUTranslations
        # (ensuring all messages are safely converted to unicode)
        catalog = Catalog(locale='en_US')
        catalog.add('', '''\
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n''')
        catalog.add('foo', 'Voh')
        catalog.add(('There is', 'There are'), ('Es gibt', 'Es gibt'))
        catalog.add('Fizz', '')
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
        buf = BytesIO()
        mofile.write_mo(buf, catalog)
        buf.seek(0)
        translations = gettext.GNUTranslations(fp=buf)
        self.assertEqual('Voh', translations.ugettext('foo'))
        assert isinstance(translations.ugettext('foo'), text_type)
        self.assertEqual('Es gibt', translations.ungettext('There is', 'There are', 1))
        assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
        self.assertEqual('Fizz', translations.ugettext('Fizz'))
        assert isinstance(translations.ugettext('Fizz'), text_type)
        self.assertEqual('Fuzz', translations.ugettext('Fuzz'))
        assert isinstance(translations.ugettext('Fuzz'), text_type)
        self.assertEqual('Fuzzes', translations.ugettext('Fuzzes'))
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
示例#10
0
    def test_po_with_multiline_obsolete_message(self):
        catalog = Catalog()
        catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)])
        msgid = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
        msgstr = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
        catalog.obsolete[msgid] = Message(msgid,
                                          msgstr,
                                          locations=[('utils.py', 3)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b(r'''#: main.py:1
msgid "foo"
msgstr "Voh"

#~ msgid ""
#~ "Here's a message that covers\n"
#~ "multiple lines, and should still be handled\n"
#~ "correctly.\n"
#~ msgstr ""
#~ "Here's a message that covers\n"
#~ "multiple lines, and should still be handled\n"
#~ "correctly.\n"'''),
            buf.getvalue().strip())
示例#11
0
文件: pofile.py 项目: vsajip/babel3
    def test_po_with_multiline_obsolete_message(self):
        catalog = Catalog()
        catalog.add("foo", "Voh", locations=[("main.py", 1)])
        msgid = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
        msgstr = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
        catalog.obsolete[msgid] = Message(msgid, msgstr, locations=[("utils.py", 3)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            br'''#: main.py:1
msgid "foo"
msgstr "Voh"

#~ msgid ""
#~ "Here's a message that covers\n"
#~ "multiple lines, and should still be handled\n"
#~ "correctly.\n"
#~ msgstr ""
#~ "Here's a message that covers\n"
#~ "multiple lines, and should still be handled\n"
#~ "correctly.\n"''',
            buf.getvalue().strip(),
        )
示例#12
0
 def test_write_po_file_with_specified_charset(self):
     catalog = Catalog(charset='iso-8859-1')
     catalog.add('foo', '\xe4\xf6\xfc', locations=[('main.py', 1)])
     buf = BytesIO()
     pofile.write_po(buf, catalog, omit_header=False)
     po_file = buf.getvalue().strip()
     assert br'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
     assert 'msgstr "\xe4\xf6\xfc"'.encode('iso-8859-1') in po_file
示例#13
0
文件: pofile.py 项目: vsajip/babel3
 def test_write_po_file_with_specified_charset(self):
     catalog = Catalog(charset="iso-8859-1")
     catalog.add("foo", "\xe4\xf6\xfc", locations=[("main.py", 1)])
     buf = BytesIO()
     pofile.write_po(buf, catalog, omit_header=False)
     po_file = buf.getvalue().strip()
     assert br'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
     assert 'msgstr "\xe4\xf6\xfc"'.encode("iso-8859-1") in po_file
示例#14
0
 def test_write_po_file_with_specified_charset(self):
     catalog = Catalog(charset='iso-8859-1')
     catalog.add('foo', u('\xe4\xf6\xfc'), locations=[('main.py', 1)])
     buf = BytesIO()
     pofile.write_po(buf, catalog, omit_header=False)
     po_file = buf.getvalue().strip()
     assert b(r'"Content-Type: text/plain; charset=iso-8859-1\n"') in po_file
     assert u('msgstr "\xe4\xf6\xfc"').encode('iso-8859-1') in po_file
示例#15
0
    def test_duplicate_comments(self):
        catalog = Catalog()
        catalog.add(u('foo'), auto_comments=['A comment'])
        catalog.add(u('foo'), auto_comments=['A comment'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(b('''#. A comment
msgid "foo"
msgstr ""'''), buf.getvalue().strip())
示例#16
0
    def test_join_locations(self):
        catalog = Catalog()
        catalog.add(u('foo'), locations=[('main.py', 1)])
        catalog.add(u('foo'), locations=[('utils.py', 3)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(b('''#: main.py:1 utils.py:3
msgid "foo"
msgstr ""'''), buf.getvalue().strip())
示例#17
0
    def test_join_locations(self):
        catalog = Catalog()
        catalog.add('foo', locations=[('main.py', 1)])
        catalog.add('foo', locations=[('utils.py', 3)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(b'''#: main.py:1 utils.py:3
msgid "foo"
msgstr ""''',
                         buf.getvalue().strip())
示例#18
0
    def test_po_with_previous_msgid(self):
        catalog = Catalog()
        catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)],
                    previous_id=u('fo'))
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
        self.assertEqual(b('''#: main.py:1
#| msgid "fo"
msgid "foo"
msgstr "Voh"'''), buf.getvalue().strip())
示例#19
0
    def test_duplicate_comments(self):
        catalog = Catalog()
        catalog.add(u('foo'), auto_comments=['A comment'])
        catalog.add(u('foo'), auto_comments=['A comment'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(b('''#. A comment
msgid "foo"
msgstr ""'''),
                         buf.getvalue().strip())
示例#20
0
    def test_po_with_previous_msgid(self):
        catalog = Catalog()
        catalog.add('foo', 'Voh', locations=[('main.py', 1)], previous_id='fo')
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
        self.assertEqual(
            b'''#: main.py:1
#| msgid "fo"
msgid "foo"
msgstr "Voh"''',
            buf.getvalue().strip())
示例#21
0
    def test_po_with_obsolete_message_ignored(self):
        catalog = Catalog()
        catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)])
        catalog.obsolete['bar'] = Message(u('bar'), u('Bahr'),
                                          locations=[('utils.py', 3)],
                                          user_comments=['User comment'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True)
        self.assertEqual(b('''#: main.py:1
msgid "foo"
msgstr "Voh"'''), buf.getvalue().strip())
示例#22
0
文件: pofile.py 项目: vsajip/babel3
    def test_po_with_previous_msgid(self):
        catalog = Catalog()
        catalog.add("foo", "Voh", locations=[("main.py", 1)], previous_id="fo")
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
        self.assertEqual(
            b'''#: main.py:1
#| msgid "fo"
msgid "foo"
msgstr "Voh"''',
            buf.getvalue().strip(),
        )
示例#23
0
文件: pofile.py 项目: vsajip/babel3
    def test_po_with_obsolete_message_ignored(self):
        catalog = Catalog()
        catalog.add("foo", "Voh", locations=[("main.py", 1)])
        catalog.obsolete["bar"] = Message("bar", "Bahr", locations=[("utils.py", 3)], user_comments=["User comment"])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True)
        self.assertEqual(
            b'''#: main.py:1
msgid "foo"
msgstr "Voh"''',
            buf.getvalue().strip(),
        )
示例#24
0
文件: pofile.py 项目: vsajip/babel3
    def test_wrap_locations_with_hyphens(self):
        catalog = Catalog()
        catalog.add("foo", locations=[("doupy/templates/base/navmenu.inc.html.py", 60)])
        catalog.add("foo", locations=[("doupy/templates/job-offers/helpers.html", 22)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b'''#: doupy/templates/base/navmenu.inc.html.py:60
#: doupy/templates/job-offers/helpers.html:22
msgid "foo"
msgstr ""''',
            buf.getvalue().strip(),
        )
示例#25
0
    def test_po_with_obsolete_message_ignored(self):
        catalog = Catalog()
        catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)])
        catalog.obsolete['bar'] = Message(u('bar'),
                                          u('Bahr'),
                                          locations=[('utils.py', 3)],
                                          user_comments=['User comment'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True)
        self.assertEqual(b('''#: main.py:1
msgid "foo"
msgstr "Voh"'''),
                         buf.getvalue().strip())
示例#26
0
    def test_wrap_long_lines_with_long_word(self):
        text = """Here's some text that
includesareallylongwordthatmightbutshouldnt throw us into an infinite loop
"""
        catalog = Catalog()
        catalog.add(text, locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, no_location=True, omit_header=True,
                         width=32)
        self.assertEqual(b(r'''msgid ""
"Here's some text that\n"
"includesareallylongwordthatmightbutshouldnt"
" throw us into an infinite "
"loop\n"
msgstr ""'''), buf.getvalue().strip())
示例#27
0
    def test_wrap_locations_with_hyphens(self):
        catalog = Catalog()
        catalog.add(u('foo'),
                    locations=[('doupy/templates/base/navmenu.inc.html.py', 60)
                               ])
        catalog.add(u('foo'),
                    locations=[('doupy/templates/job-offers/helpers.html', 22)
                               ])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b('''#: doupy/templates/base/navmenu.inc.html.py:60
#: doupy/templates/job-offers/helpers.html:22
msgid "foo"
msgstr ""'''),
            buf.getvalue().strip())
示例#28
0
    def test_po_with_previous_msgid_plural(self):
        catalog = Catalog()
        catalog.add((u('foo'), u('foos')), (u('Voh'), u('Voeh')),
                    locations=[('main.py', 1)],
                    previous_id=(u('fo'), u('fos')))
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
        self.assertEqual(
            b('''#: main.py:1
#| msgid "fo"
#| msgid_plural "fos"
msgid "foo"
msgid_plural "foos"
msgstr[0] "Voh"
msgstr[1] "Voeh"'''),
            buf.getvalue().strip())
示例#29
0
    def test_wrap_long_lines_in_header(self):
        """
        Verify that long lines in the header comment are wrapped correctly.
        """
        catalog = Catalog(project='AReallyReallyLongNameForAProject',
                          revision_date=datetime(2007, 4, 1))
        buf = BytesIO()
        pofile.write_po(buf, catalog)
        self.assertEqual(b('''\
# Translations template for AReallyReallyLongNameForAProject.
# Copyright (C) 2007 ORGANIZATION
# This file is distributed under the same license as the
# AReallyReallyLongNameForAProject project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
#, fuzzy'''), b('\n').join(buf.getvalue().splitlines()[:7]))
示例#30
0
    def test_utf8_raw_strings_match_unicode_strings(self):
        buf = BytesIO(codecs.BOM_UTF8 + u("""
msg = _('Bonjour \xe0 tous')
msgu = _(u'Bonjour \xe0 tous')
""").encode('utf-8'))
        messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {}))
        self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2])
        self.assertEqual(messages[0][2], messages[1][2])
示例#31
0
    def test_utf8_message_with_utf8_bom(self):
        buf = BytesIO(codecs.BOM_UTF8 + u("""
# NOTE: hello
msg = _('Bonjour \xe0 tous')
""").encode('utf-8'))
        messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {}))
        self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2])
        self.assertEqual([u('NOTE: hello')], messages[0][3])
示例#32
0
    def test_wrap_long_lines_in_header(self):
        """
        Verify that long lines in the header comment are wrapped correctly.
        """
        catalog = Catalog(project='AReallyReallyLongNameForAProject',
                          revision_date=datetime(2007, 4, 1))
        buf = BytesIO()
        pofile.write_po(buf, catalog)
        self.assertEqual(
            b'''\
# Translations template for AReallyReallyLongNameForAProject.
# Copyright (C) 2007 ORGANIZATION
# This file is distributed under the same license as the
# AReallyReallyLongNameForAProject project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
#, fuzzy''', b'\n'.join(buf.getvalue().splitlines()[:7]))
示例#33
0
    def test_po_with_obsolete_message(self):
        catalog = Catalog()
        catalog.add('foo', 'Voh', locations=[('main.py', 1)])
        catalog.obsolete['bar'] = Message('bar',
                                          'Bahr',
                                          locations=[('utils.py', 3)],
                                          user_comments=['User comment'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b'''#: main.py:1
msgid "foo"
msgstr "Voh"

# User comment
#~ msgid "bar"
#~ msgstr "Bahr"''',
            buf.getvalue().strip())
示例#34
0
文件: pofile.py 项目: vsajip/babel3
    def test_pot_with_translator_comments(self):
        catalog = Catalog()
        catalog.add("foo", locations=[("main.py", 1)], auto_comments=["Comment About `foo`"])
        catalog.add("bar", locations=[("utils.py", 3)], user_comments=["Comment About `bar` with", "multiple lines."])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b'''#. Comment About `foo`
#: main.py:1
msgid "foo"
msgstr ""

# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""''',
            buf.getvalue().strip(),
        )
示例#35
0
    def test_pot_with_translator_comments(self):
        catalog = Catalog()
        catalog.add(u('foo'), locations=[('main.py', 1)],
                    auto_comments=['Comment About `foo`'])
        catalog.add(u('bar'), locations=[('utils.py', 3)],
                    user_comments=['Comment About `bar` with',
                                   'multiple lines.'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(b('''#. Comment About `foo`
#: main.py:1
msgid "foo"
msgstr ""

# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""'''), buf.getvalue().strip())
示例#36
0
    def test_1_num_plurals_checkers(self):
        for _locale in [p for p in PLURALS if PLURALS[p][0] == 1]:
            try:
                locale = Locale.parse(_locale)
            except UnknownLocaleError:
                # Just an alias? Not what we're testing here, let's continue
                continue
            po_file = (u(r"""\
# %(english_name)s translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2007-04-01 15:30+0200\n"
"PO-Revision-Date: %(date)s\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: %(locale)s <*****@*****.**>\n"
"Plural-Forms: nplurals=%(num_plurals)s; plural=%(plural_expr)s\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel %(version)s\n"

#. This will be a translator comment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""

#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""

""") % dict(locale=_locale,
            english_name=locale.english_name,
            version=VERSION,
            year=time.strftime('%Y'),
            date=format_datetime(datetime.now(LOCALTZ),
                                 'yyyy-MM-dd HH:mmZ',
                                 tzinfo=LOCALTZ,
                                 locale=_locale),
            num_plurals=PLURALS[_locale][0],
            plural_expr=PLURALS[_locale][0])).encode('utf-8')

            # This test will fail for revisions <= 406 because so far
            # catalog.num_plurals was neglected
            catalog = read_po(BytesIO(po_file), _locale)
            message = catalog['foobar']
            checkers.num_plurals(catalog, message)
示例#37
0
    def test_wrap_long_lines(self):
        text = """Here's some text where       
white space and line breaks matter, and should

not be removed

"""
        catalog = Catalog()
        catalog.add(text, locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, no_location=True, omit_header=True,
                         width=42)
        self.assertEqual(b(r'''msgid ""
"Here's some text where       \n"
"white space and line breaks matter, and"
" should\n"
"\n"
"not be removed\n"
"\n"
msgstr ""'''), buf.getvalue().strip())
示例#38
0
    def test_wrap_long_lines_with_long_word(self):
        text = """Here's some text that
includesareallylongwordthatmightbutshouldnt throw us into an infinite loop
"""
        catalog = Catalog()
        catalog.add(text, locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf,
                        catalog,
                        no_location=True,
                        omit_header=True,
                        width=32)
        self.assertEqual(
            b(r'''msgid ""
"Here's some text that\n"
"includesareallylongwordthatmightbutshouldnt"
" throw us into an infinite "
"loop\n"
msgstr ""'''),
            buf.getvalue().strip())
示例#39
0
    def test_with_context_two(self):
        buf = StringIO(r'''msgctxt "Menu"
msgid "foo"
msgstr "Voh"

msgctxt "Mannu"
msgid "bar"
msgstr "Bahr"
''')
        catalog = pofile.read_po(buf, ignore_obsolete=True)
        self.assertEqual(2, len(catalog))
        message = catalog.get('foo', context='Menu')
        self.assertEqual('Menu', message.context)
        message = catalog.get('bar', context='Mannu')
        self.assertEqual('Mannu', message.context)

        # And verify it pass through write_po
        out_buf = BytesIO()
        pofile.write_po(out_buf, catalog, omit_header=True)
        assert out_buf.getvalue().strip() == buf.getvalue().strip().encode(
            'latin-1'), out_buf.getvalue()
示例#40
0
    def test_no_wrap_and_width_behaviour_on_comments(self):
        catalog = Catalog()
        catalog.add(
            "Pretty dam long message id, which must really be big "
            "to test this wrap behaviour, if not it won't work.",
            locations=[("fake.py", n) for n in range(1, 30)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, width=None, omit_header=True)
        self.assertEqual(
            b("""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7
#: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14
#: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21
#: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work."
msgstr ""

"""),
            buf.getvalue().lower())
        buf = BytesIO()
        pofile.write_po(buf, catalog, width=100, omit_header=True)
        self.assertEqual(
            b("""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10
#: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19
#: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid ""
"pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't"
" work."
msgstr ""

"""),
            buf.getvalue().lower())
示例#41
0
    def obtainFilter(self, processing, uri):
        '''
        Checks the filter URI.

        @param processing: Processing
            The processing used for delivering the request.
        @param uri: string
            The URI to call, parameters are allowed.
        @return: tuple(boolean|None, integer, string)
            A tuple containing as the first True if the filter URI provided a True value, None if the filter cannot be fetched,
            on the second position the response status and on the last position the response text.
        '''
        assert isinstance(processing,
                          Processing), 'Invalid processing %s' % processing
        assert isinstance(uri, str), 'Invalid URI %s' % uri

        request = processing.ctx.request()
        assert isinstance(request,
                          RequestFilter), 'Invalid request %s' % request

        url = urlparse(uri)
        request.scheme, request.method = self.scheme, HTTP_GET
        request.headers = {}
        request.uri = url.path.lstrip('/')
        request.parameters = parse_qsl(url.query, True, False)
        request.accTypes = [self.mimeTypeJson]
        request.accCharSets = [self.encodingJson]

        chain = Chain(processing)
        chain.process(request=request,
                      requestCnt=processing.ctx.requestCnt(),
                      response=processing.ctx.response(),
                      responseCnt=processing.ctx.responseCnt()).doAll()

        response, responseCnt = chain.arg.response, chain.arg.responseCnt
        assert isinstance(response,
                          ResponseHTTP), 'Invalid response %s' % response
        assert isinstance(
            responseCnt,
            ResponseContentHTTP), 'Invalid response content %s' % responseCnt

        if ResponseHTTP.text in response and response.text:
            text = response.text
        elif ResponseHTTP.code in response and response.code:
            text = response.code
        else:
            text = None
        if ResponseContentHTTP.source not in responseCnt or responseCnt.source is None or not isSuccess(
                response.status):
            return None, response.status, text

        if isinstance(responseCnt.source, IInputStream):
            source = responseCnt.source
        else:
            source = BytesIO()
            for bytes in responseCnt.source:
                source.write(bytes)
            source.seek(0)
        allowed = json.load(codecs.getreader(self.encodingJson)(source))
        return allowed['HasAccess'] == 'True', response.status, text
示例#42
0
文件: support.py 项目: vsajip/babel3
 def setUp(self):
     # Use a locale which won't fail to run the tests
     os.environ['LANG'] = 'en_US.UTF-8'
     messages1 = [
         ('foo', {
             'string': 'Voh'
         }),
         ('foo', {
             'string': 'VohCTX',
             'context': 'foo'
         }),
         (('foo1', 'foos1'), {
             'string': ('Voh1', 'Vohs1')
         }),
         (('foo1', 'foos1'), {
             'string': ('VohCTX1', 'VohsCTX1'),
             'context': 'foo'
         }),
     ]
     messages2 = [
         ('foo', {
             'string': 'VohD'
         }),
         ('foo', {
             'string': 'VohCTXD',
             'context': 'foo'
         }),
         (('foo1', 'foos1'), {
             'string': ('VohD1', 'VohsD1')
         }),
         (('foo1', 'foos1'), {
             'string': ('VohCTXD1', 'VohsCTXD1'),
             'context': 'foo'
         }),
     ]
     catalog1 = Catalog(locale='en_GB', domain='messages')
     catalog2 = Catalog(locale='en_GB', domain='messages1')
     for ids, kwargs in messages1:
         catalog1.add(ids, **kwargs)
     for ids, kwargs in messages2:
         catalog2.add(ids, **kwargs)
     catalog1_fp = BytesIO()
     catalog2_fp = BytesIO()
     write_mo(catalog1_fp, catalog1)
     catalog1_fp.seek(0)
     write_mo(catalog2_fp, catalog2)
     catalog2_fp.seek(0)
     translations1 = support.Translations(catalog1_fp)
     translations2 = support.Translations(catalog2_fp, domain='messages1')
     self.translations = translations1.add(translations2, merge=False)
示例#43
0
文件: support.py 项目: vsajip/babel3
 def setUp(self):
     # Use a locale which won't fail to run the tests
     os.environ['LANG'] = 'en_US.UTF-8'
     messages1 = [
         ('foo', {'string': 'Voh'}),
         ('foo', {'string': 'VohCTX', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}),
         (('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}),
     ]
     messages2 = [
         ('foo', {'string': 'VohD'}),
         ('foo', {'string': 'VohCTXD', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}),
         (('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}),
     ]
     catalog1 = Catalog(locale='en_GB', domain='messages')
     catalog2 = Catalog(locale='en_GB', domain='messages1')
     for ids, kwargs in messages1:
         catalog1.add(ids, **kwargs)
     for ids, kwargs in messages2:
         catalog2.add(ids, **kwargs)
     catalog1_fp = BytesIO()
     catalog2_fp = BytesIO()
     write_mo(catalog1_fp, catalog1)
     catalog1_fp.seek(0)
     write_mo(catalog2_fp, catalog2)
     catalog2_fp.seek(0)
     translations1 = support.Translations(catalog1_fp)
     translations2 = support.Translations(catalog2_fp, domain='messages1')
     self.translations = translations1.add(translations2, merge=False)
示例#44
0
文件: pofile.py 项目: vsajip/babel3
    def test_sorted_po(self):
        catalog = Catalog()
        catalog.add("bar", locations=[("utils.py", 3)], user_comments=["Comment About `bar` with", "multiple lines."])
        catalog.add(("foo", "foos"), ("Voh", "Voeh"), locations=[("main.py", 1)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, sort_output=True)
        value = buf.getvalue().strip()
        assert (
            b'''\
# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""

#: main.py:1
msgid "foo"
msgid_plural "foos"
msgstr[0] "Voh"
msgstr[1] "Voeh"'''
            in value
        )
        assert value.find(b'msgid ""') < value.find(b'msgid "bar"') < value.find(b'msgid "foo"')
示例#45
0
    def test_pot_with_translator_comments(self):
        catalog = Catalog()
        catalog.add(u('foo'),
                    locations=[('main.py', 1)],
                    auto_comments=['Comment About `foo`'])
        catalog.add(
            u('bar'),
            locations=[('utils.py', 3)],
            user_comments=['Comment About `bar` with', 'multiple lines.'])
        buf = BytesIO()
        pofile.write_po(buf, catalog, omit_header=True)
        self.assertEqual(
            b('''#. Comment About `foo`
#: main.py:1
msgid "foo"
msgstr ""

# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""'''),
            buf.getvalue().strip())
示例#46
0
    def test_sorted_po(self):
        catalog = Catalog()
        catalog.add(u('bar'), locations=[('utils.py', 3)],
                    user_comments=['Comment About `bar` with',
                                   'multiple lines.'])
        catalog.add((u('foo'), u('foos')), (u('Voh'), u('Voeh')),
                    locations=[('main.py', 1)])
        buf = BytesIO()
        pofile.write_po(buf, catalog, sort_output=True)
        value = buf.getvalue().strip()
        assert b('''\
# Comment About `bar` with
# multiple lines.
#: utils.py:3
msgid "bar"
msgstr ""

#: main.py:1
msgid "foo"
msgid_plural "foos"
msgstr[0] "Voh"
msgstr[1] "Voeh"''') in value
        assert value.find(b('msgid ""')) < value.find(b('msgid "bar"')) < value.find(b('msgid "foo"'))
示例#47
0
    def test_applies_specified_encoding_during_read(self):
        buf = BytesIO('''
msgid ""
msgstr ""
"Project-Id-Version:  3.15\\n"
"Report-Msgid-Bugs-To: Fliegender Zirkus <*****@*****.**>\\n"
"POT-Creation-Date: 2007-09-27 11:19+0700\\n"
"PO-Revision-Date: 2007-09-27 21:42-0700\\n"
"Last-Translator: John <*****@*****.**>\\n"
"Language-Team: German Lang <*****@*****.**>\\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=iso-8859-1\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Generated-By: Babel 1.0dev-r313\\n"

msgid "foo"
msgstr "bär"'''.encode('iso-8859-1'))
        catalog = pofile.read_po(buf, locale='de_DE')
        self.assertEqual('b\xe4r', catalog.get('foo').string)
示例#48
0
 def test_more_plural_forms(self):
     catalog2 = Catalog(locale='ru_RU')
     catalog2.add(('Fuzz', 'Fuzzes'), ('', '', ''))
     buf = BytesIO()
     mofile.write_mo(buf, catalog2)