Exemple #1
0
    def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
                 user_comments=(), previous_id=(), lineno=None, context=None):
        """Create the message object.

        :param id: the message ID, or a ``(singular, plural)`` tuple for
                   pluralizable messages
        :param string: the translated message string, or a
                       ``(singular, plural)`` tuple for pluralizable messages
        :param locations: a sequence of ``(filename, lineno)`` tuples
        :param flags: a set or sequence of flags
        :param auto_comments: a sequence of automatic comments for the message
        :param user_comments: a sequence of user comments for the message
        :param previous_id: the previous message ID, or a ``(singular, plural)``
                            tuple for pluralizable messages
        :param lineno: the line number on which the msgid line was found in the
                       PO file, if any
        :param context: the message context
        """
        self.id = id
        if not string and self.pluralizable:
            string = (u'', u'')
        self.string = string
        self.locations = list(distinct(locations))
        self.flags = set(flags)

        self.auto_comments = list(distinct(auto_comments))
        self.user_comments = list(distinct(user_comments))
        if isinstance(previous_id, string_types):
            self.previous_id = [previous_id]
        else:
            self.previous_id = list(previous_id)
        self.lineno = lineno
        self.context = context
    def __init__(
        self, id, string=u"", locations=(), flags=(), auto_comments=(), user_comments=(), previous_id=(), lineno=None
    ):
        """Create the message object.

        :param id: the message ID, or a ``(singular, plural)`` tuple for
                   pluralizable messages
        :param string: the translated message string, or a
                       ``(singular, plural)`` tuple for pluralizable messages
        :param locations: a sequence of ``(filenname, lineno)`` tuples
        :param flags: a set or sequence of flags
        :param auto_comments: a sequence of automatic comments for the message
        :param user_comments: a sequence of user comments for the message
        :param previous_id: the previous message ID, or a ``(singular, plural)``
                            tuple for pluralizable messages
        :param lineno: the line number on which the msgid line was found in the
                       PO file, if any
        """
        self.id = id  #: The message ID
        if not string and self.pluralizable:
            string = (u"", u"")
        self.string = string  #: The message translation
        self.locations = list(distinct(locations))
        self.flags = set(flags)
        if id and self.python_format:
            self.flags.add("python-format")
        else:
            self.flags.discard("python-format")
        self.auto_comments = list(distinct(auto_comments))
        self.user_comments = list(distinct(user_comments))
        if isinstance(previous_id, basestring):
            self.previous_id = [previous_id]
        else:
            self.previous_id = list(previous_id)
        self.lineno = lineno
Exemple #3
0
    def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
                 user_comments=(), previous_id=(), lineno=None):
        """Create the message object.

        :param id: the message ID, or a ``(singular, plural)`` tuple for
                   pluralizable messages
        :param string: the translated message string, or a
                       ``(singular, plural)`` tuple for pluralizable messages
        :param locations: a sequence of ``(filenname, lineno)`` tuples
        :param flags: a set or sequence of flags
        :param auto_comments: a sequence of automatic comments for the message
        :param user_comments: a sequence of user comments for the message
        :param previous_id: the previous message ID, or a ``(singular, plural)``
                            tuple for pluralizable messages
        :param lineno: the line number on which the msgid line was found in the
                       PO file, if any
        """
        self.id = id #: The message ID
        if not string and self.pluralizable:
            string = (u'', u'')
        self.string = string #: The message translation
        self.locations = list(distinct(locations))
        self.flags = set(flags)
        if id and self.python_format:
            self.flags.add('python-format')
        else:
            self.flags.discard('python-format')
        self.auto_comments = list(distinct(auto_comments))
        self.user_comments = list(distinct(user_comments))
        if isinstance(previous_id, basestring):
            self.previous_id = [previous_id]
        else:
            self.previous_id = list(previous_id)
        self.lineno = lineno
Exemple #4
0
    def __setitem__(self, id, message):
        """Add or update the message with the specified ID.

        >>> catalog = Catalog()
        >>> catalog[u('foo')] = Message(u('foo'))
        >>> catalog[u('foo')]
        <Message foo (flags: [])>

        If a message with that ID is already in the catalog, it is updated
        to include the locations and flags of the new message.

        >>> catalog = Catalog()
        >>> catalog[u('foo')] = Message(u('foo'), locations=[('main.py', 1)])
        >>> catalog[u('foo')].locations
        [('main.py', 1)]
        >>> catalog[u('foo')] = Message(u('foo'), locations=[('utils.py', 5)])
        >>> catalog[u('foo')].locations
        [('main.py', 1), ('utils.py', 5)]

        :param id: the message ID
        :param message: the `Message` object
        """
        assert isinstance(message, Message), 'expected a Message object'
        key = self._key_for(id, message.context)
        current = self._messages.get(key)
        if current:
            if message.pluralizable and not current.pluralizable:
                # The new message adds pluralization
                current.id = message.id
                current.string = message.string
            current.locations = list(distinct(current.locations +
                                              message.locations))
            current.auto_comments = list(distinct(current.auto_comments +
                                                  message.auto_comments))
            current.user_comments = list(distinct(current.user_comments +
                                                  message.user_comments))
            current.flags |= message.flags
            message = current
        elif id == '':
            # special treatment for the header message
            def _parse_header(header_string):
                # message_from_string only works for str, not for unicode
                if not PY3:
                    header_string = header_string.encode('utf8')
                headers = message_from_string(header_string)
                decoded_headers = {}
                for name, value in headers.items():
                    if not PY3:
                        name, value = name.decode('utf8'), value.decode('utf8')
                    decoded_headers[name] = value
                return decoded_headers
            self.mime_headers = list(_parse_header(message.string).items())
            self.header_comment = '\n'.join(['# %s' % comment for comment
                                             in message.user_comments])
            self.fuzzy = message.fuzzy
        else:
            if isinstance(id, (list, tuple)):
                assert isinstance(message.string, (list, tuple)), \
                    'Expected sequence but got %s' % type(message.string)
            self._messages[key] = message
Exemple #5
0
 def __setitem__(self, id, message):
     assert isinstance(message, Message), 'expected a Message object'
     key = self._key_for(id, message.context)
     current = self._messages.get(key)
     if current:
         if message.pluralizable and not current.pluralizable:
             # The new message adds pluralization
             current.id = message.id
         if message.string:
             current.string = message.string
         current.locations = list(
             distinct(current.locations + message.locations))
         current.auto_comments = list(
             distinct(current.auto_comments + message.auto_comments))
         current.user_comments = list(
             distinct(current.user_comments + message.user_comments))
         current.flags |= message.flags
     elif id == '':
         # special treatment for the header message
         self.mime_headers = self._parse_header(message.string).items()
         self.header_comment = '\n'.join([('# %s' % c).rstrip()
                                          for c in message.user_comments])
         self.fuzzy = message.fuzzy
     else:
         if isinstance(id, (list, tuple)):
             assert isinstance(message.string, (list, tuple)), \
                 'Expected sequence but got %s' % type(message.string)
         self._messages[key] = message
Exemple #6
0
    def __setitem__(self, id, message):
        """Add or update the message with the specified ID.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo')
        >>> catalog[u'foo']
        <Message u'foo' (flags: [])>

        If a message with that ID is already in the catalog, it is updated
        to include the locations and flags of the new message.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo', locations=[('main.py', 1)])
        >>> catalog[u'foo'].locations
        [('main.py', 1)]
        >>> catalog[u'foo'] = Message(u'foo', locations=[('utils.py', 5)])
        >>> catalog[u'foo'].locations
        [('main.py', 1), ('utils.py', 5)]

        :param id: the message ID
        :param message: the `Message` object
        """
        assert isinstance(message, Message), 'expected a Message object'
        key = self._key_for(id, message.context)
        current = self._messages.get(key)
        if current:
            if message.pluralizable and not current.pluralizable:
                # The new message adds pluralization
                current.id = message.id
                current.string = message.string
            current.locations = list(
                distinct(current.locations + message.locations))
            current.extracted_comments = list(
                distinct(current.extracted_comments +
                         message.extracted_comments))
            current.translator_comments = list(
                distinct(current.translator_comments +
                         message.translator_comments))
            current.flags |= message.flags
            message = current
        elif id == '':
            # special treatment for the header message
            self.mime_headers = _parse_header(message.string).items()
            self.header_comment = '\n'.join([
                ('# %s' % c).rstrip() for c in message.translator_comments
            ])
            self.fuzzy = message.fuzzy
        else:
            if isinstance(id, (list, tuple)):
                assert isinstance(message.string, (list, tuple)), \
                    'Expected sequence but got %s' % type(message.string)
            self._messages[key] = message
Exemple #7
0
    def __setitem__(self, id, message):
        """Add or update the message with the specified ID.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo')
        >>> catalog[u'foo']
        <Message u'foo' (flags: [])>

        If a message with that ID is already in the catalog, it is updated
        to include the locations and flags of the new message.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo', locations=[('main.py', 1)])
        >>> catalog[u'foo'].locations
        [('main.py', 1)]
        >>> catalog[u'foo'] = Message(u'foo', locations=[('utils.py', 5)])
        >>> catalog[u'foo'].locations
        [('main.py', 1), ('utils.py', 5)]

        :param id: the message ID
        :param message: the `Message` object
        """
        assert isinstance(message, Message), 'expected a Message object'
        key = self._key_for(id, message.context)
        current = self._messages.get(key)
        if current:
            if message.pluralizable and not current.pluralizable:
                # The new message adds pluralization
                current.id = message.id
                current.string = message.string
            current.locations = list(distinct(current.locations +
                                              message.locations))
            current.auto_comments = list(distinct(current.auto_comments +
                                                  message.auto_comments))
            current.user_comments = list(distinct(current.user_comments +
                                                  message.user_comments))
            current.flags |= message.flags
            message = current
        elif id == '':
            # special treatment for the header message
            headers = message_from_string(message.string.encode(self.charset))
            self.mime_headers = headers.items()
            self.header_comment = '\n'.join(['# %s' % comment for comment
                                             in message.user_comments])
            self.fuzzy = message.fuzzy
        else:
            if isinstance(id, (list, tuple)):
                assert isinstance(message.string, (list, tuple)), \
                    'Expected sequence but got %s' % type(message.string)
            self._messages[key] = message
Exemple #8
0
 def _merge(message, oldkey, newkey):
     message = message.clone()
     fuzzy = False
     if oldkey != newkey:
         fuzzy = True
         fuzzy_matches.add(oldkey)
         oldmsg = messages.get(oldkey)
         if isinstance(oldmsg.id, string_types):
             message.previous_id = [oldmsg.id]
         else:
             message.previous_id = list(oldmsg.id)
     else:
         oldmsg = remaining.pop(oldkey, None)
     message.string = oldmsg.string
     message.user_comments = list(distinct(oldmsg.user_comments))
     if isinstance(message.id, (list, tuple)):
         if not isinstance(message.string, (list, tuple)):
             fuzzy = True
             message.string = tuple([message.string] +
                                    ([u''] * (len(message.id) - 1)))
         elif len(message.string) != self.num_plurals:
             fuzzy = True
             message.string = tuple(message.string[:len(oldmsg.string)])
     elif isinstance(message.string, (list, tuple)):
         fuzzy = True
         message.string = message.string[0]
     message.flags |= oldmsg.flags
     if fuzzy:
         message.flags |= set([u'fuzzy'])
     self[message.id] = message
Exemple #9
0
 def _merge(message, oldkey, newkey):
     message = message.clone()
     fuzzy = False
     if oldkey != newkey:
         fuzzy = True
         fuzzy_matches.add(oldkey)
         oldmsg = messages.get(oldkey)
         if isinstance(oldmsg.id, string_types):
             message.previous_id = [oldmsg.id]
         else:
             message.previous_id = list(oldmsg.id)
     else:
         oldmsg = remaining.pop(oldkey, None)
     message.string = oldmsg.string
     message.user_comments = list(distinct(oldmsg.user_comments))
     if isinstance(message.id, (list, tuple)):
         if not isinstance(message.string, (list, tuple)):
             fuzzy = True
             message.string = tuple(
                 [message.string] + ([u''] * (len(message.id) - 1))
             )
         elif len(message.string) != self.num_plurals:
             fuzzy = True
             message.string = tuple(message.string[:len(oldmsg.string)])
     elif isinstance(message.string, (list, tuple)):
         fuzzy = True
         message.string = message.string[0]
     message.flags |= oldmsg.flags
     if fuzzy:
         message.flags |= set([u'fuzzy'])
     self[message.id] = message
Exemple #10
0
    def __init__(self,
                 id,
                 string=u'',
                 locations=(),
                 flags=(),
                 extracted_comments=(),
                 translator_comments=(),
                 previous_id=(),
                 previous_context=None,
                 lineno=None,
                 context=None):
        """Create the message object.

        :param id: the message ID, or a ``(singular, plural)`` tuple for
                   pluralizable messages
        :param string: the translated message string, or a
                       ``(singular, plural)`` tuple for pluralizable messages
        :param locations: a sequence of ``(filename, lineno)`` tuples
        :param flags: a set or sequence of flags
        :param extracted_comments: a sequence of extracted comments for the message
        :param translator_comments: a sequence of translator comments for the message
        :param previous_id: the previous message ID, or a ``(singular, plural)``
                            tuple for pluralizable messages
        :param previous_context: the previous message context
        :param lineno: the line number on which the msgid line was found in the
                       PO file, if any
        :param context: the message context
        """
        self.id = id
        if not string and self.pluralizable:
            string = (u'', u'')
        self.string = string
        self.locations = list(distinct(locations))
        self.flags = set(flags)
        if id and self.python_format:
            self.flags.add('python-format')
        else:
            self.flags.discard('python-format')
        self.extracted_comments = list(distinct(extracted_comments))
        self.translator_comments = list(distinct(translator_comments))
        self.previous_id = previous_id
        self.previous_context = previous_context
        self.lineno = lineno
        self.context = context
Exemple #11
0
    def _get_primary_locales():
        preferred = _get_preferred_languages() or ['en']
        primary = []

        for language in preferred:
            if language in available_locales:
                primary.append(available_locales[language])
            elif language[:2] in available_locales:
                primary.append(available_locales[language[:2]])

        return map(None, distinct(primary))
Exemple #12
0
        def get_primary_languages():
            available = [lang['language_code'] for lang in languages()]
            requested = distinct(get_lang() or ['en'])

            # add primary languages
            primary = []
            for language in requested:
                if language in available:
                    primary.append(language)
                else:
                    try:
                        locale = parse_locale(language)
                    except:
                        continue

                    if locale[0] in available:
                        primary.append(locale[0])

            if len(primary) == 0:
                return [language_info('en')]

            return [language_info(lang) for lang in distinct(primary)]
Exemple #13
0
def test_distinct():
    assert list(util.distinct([1, 2, 1, 3, 4, 4])) == [1, 2, 3, 4]
    assert list(util.distinct('foobar')) == ['f', 'o', 'b', 'a', 'r']
Exemple #14
0
def test_distinct():
    assert list(util.distinct([1, 2, 1, 3, 4, 4])) == [1, 2, 3, 4]
    assert list(util.distinct('foobar')) == ['f', 'o', 'b', 'a', 'r']
Exemple #15
0
    def __setitem__(self, id, message):
        """Add or update the message with the specified ID.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo')
        >>> catalog[u'foo']
        <Message u'foo' (flags: [])>

        If a message with that ID is already in the catalog, it is updated
        to include the locations and flags of the new message.

        >>> catalog = Catalog()
        >>> catalog[u'foo'] = Message(u'foo', locations=[('main.py', 1)])
        >>> catalog[u'foo'].locations
        [('main.py', 1)]
        >>> catalog[u'foo'] = Message(u'foo', locations=[('utils.py', 5)])
        >>> catalog[u'foo'].locations
        [('main.py', 1), ('utils.py', 5)]

        :param id: the message ID
        :param message: the `Message` object
        """
        assert isinstance(message, Message), 'expected a Message object'
        key = self._key_for(id, message.context)
        current = self._messages.get(key)
        if current:
            if (message.pluralizable and current.pluralizable and
                message.id != current.id):
                # The messages have conflicting pluralization.
                plural_1 = message.id[1]
                location_1 = ':'.join(map(str, message.locations[0]))
                plural_2 = current.id[1]
                location_2 = ':'.join(map(str, current.locations[0]))
                raise TranslationError(
                    "Found conflicting plurals for '%s': '%s' at %s and "
                    "'%s' at %s. "
                    "(Perhaps solve by replacing '1' with <var> in '%s')"
                    % (key, plural_1, location_1, plural_2, location_2, key))
            if message.pluralizable and not current.pluralizable:
                # The new message adds pluralization
                current.id = message.id
                current.string = message.string
            current.locations = list(distinct(current.locations +
                                              message.locations))
            current.auto_comments = list(distinct(current.auto_comments +
                                                  message.auto_comments))
            current.user_comments = list(distinct(current.user_comments +
                                                  message.user_comments))
            current.flags |= message.flags
            message = current
        elif id == '':
            # special treatment for the header message
            self.mime_headers = _parse_header(message.string).items()
            self.header_comment = '\n'.join([('# %s' % c).rstrip() for c
                                             in message.user_comments])
            self.fuzzy = message.fuzzy
        else:
            if isinstance(id, (list, tuple)):
                assert isinstance(message.string, (list, tuple)), \
                    'Expected sequence but got %s' % type(message.string)
            self._messages[key] = message
Exemple #16
0
def test_distinct():
    assert list(util.distinct([1, 2, 1, 3, 4, 4])) == [1, 2, 3, 4]
    assert list(util.distinct("foobar")) == ["f", "o", "b", "a", "r"]