Example #1
0
 def test_wrapAndQuoteString(self):
     self.assertEqual(wrapAndQuoteString(''), '""')
     lineA = 'a' * 50
     self.assertEqual(wrapAndQuoteString(lineA), '"{0}"'.format(lineA))
     lineB = 'b' * 50
     lineAB = lineA + ' ' + lineB
     self.assertEqual(wrapAndQuoteString(lineAB),
                      '"{0} "\n"{1}"'.format(lineA, lineB))
Example #2
0
 def test_wrapAndQuoteString_singleline_unicode(self):
     # disable wrapping
     i18ndude.utils.WRAP = False
     val = u'ø' * 100
     try:
         wrapAndQuoteString(val)
     except UnicodeEncodeError:
         self.fail("wrapAndQuoteString raised UnicodeEncodeError unexpectedly!")  # noqa
Example #3
0
 def test_wrapAndQuoteString(self):
     self.assertEqual(wrapAndQuoteString(''), '""')
     lineA = 'a' * 50
     self.assertEqual(wrapAndQuoteString(lineA), '"{0}"'.format(lineA))
     lineB = 'b' * 50
     lineAB = lineA + ' ' + lineB
     self.assertEqual(wrapAndQuoteString(lineAB),
                      '"{0} "\n"{1}"'.format(lineA, lineB))
Example #4
0
 def test_wrapAndQuoteString_singleline_unicode(self):
     # disable wrapping
     i18ndude.utils.WRAP = False
     val = u'ø' * 100
     try:
         wrapAndQuoteString(val)
     except UnicodeEncodeError:
         self.fail(
             "wrapAndQuoteString raised UnicodeEncodeError unexpectedly!"
         )  # noqa
Example #5
0
    def _print_entry(self, f, id, entry, msgstrToComment, sync):
        """Writes a MessageEntry to file."""
        self._printToFile(f, False)

        msgstr = entry.msgstr
        comments = entry.comments
        automatic_comments = entry.automatic_comments

        msg_changed = False
        fuzzy = False

        for comment in comments:
            if not comment.startswith(', fuzzy')\
                    and not comment.startswith(' , fuzzy'):
                if comment.startswith('#'):
                    self._printToFile(f, '#%s' % comment)
                else:
                    self._printToFile(f, '# %s' % comment)
            else:
                fuzzy = True

        # used in pot methods
        if msgstrToComment and msgstr:
            # no html markup in the default comments as these are not
            # allowed in msgstr's either
            msgstr = msgstr.replace('"', '\\\"')
            msgstr = msgstr.replace('\n', '\\n')
            msgstr = msgstr.replace('"', '\\\"')
            msgstr = msgstr.replace(' ', ' ')
            msgstr = msgstr.replace('&', '&')
            msgstr = msgstr.replace('…', u'\u2026')
            msgstr = msgstr.replace('…', u'\u2026')
            msgstr = msgstr.replace('—', u'\u2014')
            msgstr = msgstr.replace('■', u'\u25A0')
            msgstr = msgstr.replace('○', u'\u25CB')
            msgstr = msgstr.replace('●', u'\u25CF')
            self._printToFile(f, '#. %s"%s"' % (DEFAULT_COMMENT, msgstr))
            msgstr = ''

        # used in sync to filter duplicate default comments
        if sync:
            default_comments = [
                o for o in automatic_comments if o.startswith(DEFAULT_COMMENT)]
            if len(default_comments) > 1:
                msg_changed = True
                # the first element is the old comment, the second the new one
                automatic_comments.remove(default_comments[0])

        for ac in automatic_comments:
            self._printToFile(f, '#. %s' % ac)

        # key is the filename, value is the filename or filename:lineno
        refs = {}
        for ref in entry.references:
            pparts = ref.split('Products%s' % os.sep)
            p2parts = ref.split('products%s' % os.sep)
            sparts = ref.split('src%s' % os.sep, 1)
            if len(pparts) > 1:
                ref = pparts[1]
            elif len(p2parts) > 1:
                ref = p2parts[1]
            elif len(sparts) > 1:
                ref = sparts[1]
            # Normalize path separators to unix-style
            ref.replace(os.sep, '/')

            # We can have two references to the same file
            # but with different line number. We only include
            # the reference once.
            filename = ref.split(':')[0]
            if filename not in refs:
                refs[filename] = ref

        # Support for max number of references
        refs_values = sorted(refs.values())
#        include_ellipsis = MAX_OCCUR is not None and \
#                           len(refs_values[MAX_OCCUR:])
        for idx, ref in enumerate(refs_values[:MAX_OCCUR]):
            self._printToFile(f, '#: %s' % ref)
#            if include_ellipsis and idx == MAX_OCCUR - 1:
# self._printToFile(f, '#: %s' % ref)
# self._printToFile(f, '#: ...')

        if msgstr and (msg_changed or fuzzy):
            self._printToFile(f, '#, fuzzy')

        # Add backslash escape to id.
        if '"' in id and u'\\"' not in id:
            id = id.replace('"', '\\"')

        self._printToFile(f, self._create_msgid(id))
        if '\\n' not in msgstr:
            self._printToFile(f, self._create_msgstr(msgstr))
        else:
            self._printToFile(f, 'msgstr ""')
            lines = msgstr.split('\\n')
            for line in lines[:-1]:
                # Restore the literal backslash-n at the end
                line += '\\n'
                # Wrap over multiple lines if needed.
                newline = wrapAndQuoteString(line)
                self._printToFile(f, newline)
            if lines[-1]:
                # This is the part after the last literal backslash-n
                newline = wrapAndQuoteString(lines[-1])
                self._printToFile(f, newline)
Example #6
0
    def _print_entry(self, f, id, entry, msgstrToComment, sync):
        """Writes a MessageEntry to file."""
        self._printToFile(f, False)

        msgstr = entry.msgstr
        comments = entry.comments
        automatic_comments = entry.automatic_comments

        msg_changed = False
        fuzzy = False

        for comment in comments:
            if not comment.startswith(', fuzzy')\
                    and not comment.startswith(' , fuzzy'):
                if comment.startswith('#'):
                    self._printToFile(f, '#%s' % comment)
                else:
                    self._printToFile(f, '# %s' % comment)
            else:
                fuzzy = True

        # used in pot methods
        if msgstrToComment and msgstr:
            # no html markup in the default comments as these are not
            # allowed in msgstr's either
            msgstr = msgstr.replace('"', '\\\"')
            msgstr = msgstr.replace('\n', '\\n')
            msgstr = msgstr.replace('"', '\\\"')
            msgstr = msgstr.replace(' ', ' ')
            msgstr = msgstr.replace('&', '&')
            msgstr = msgstr.replace('…', u'\u2026')
            msgstr = msgstr.replace('…', u'\u2026')
            msgstr = msgstr.replace('—', u'\u2014')
            msgstr = msgstr.replace('■', u'\u25A0')
            msgstr = msgstr.replace('○', u'\u25CB')
            msgstr = msgstr.replace('●', u'\u25CF')
            self._printToFile(f, '#. %s"%s"' % (DEFAULT_COMMENT, msgstr))
            msgstr = ''

        # used in sync to filter duplicate default comments
        if sync:
            default_comments = [
                o for o in automatic_comments if o.startswith(DEFAULT_COMMENT)]
            if len(default_comments) > 1:
                msg_changed = True
                # the first element is the old comment, the second the new one
                automatic_comments.remove(default_comments[0])

        for ac in automatic_comments:
            self._printToFile(f, '#. %s' % ac)

        # key is the filename, value is the filename or filename:lineno
        refs = {}
        for ref in entry.references:
            pparts = ref.split('Products%s' % os.sep)
            p2parts = ref.split('products%s' % os.sep)
            sparts = ref.split('src%s' % os.sep, 1)
            if len(pparts) > 1:
                ref = pparts[1]
            elif len(p2parts) > 1:
                ref = p2parts[1]
            elif len(sparts) > 1:
                ref = sparts[1]
            # Normalize path separators to unix-style
            ref.replace(os.sep, '/')

            # We can have two references to the same file
            # but with different line number. We only include
            # the reference once.
            filename = ref.split(':')[0]
            if filename not in refs:
                refs[filename] = ref

        # Support for max number of references
        refs_values = sorted(refs.values())
#        include_ellipsis = MAX_OCCUR is not None and \
#                           len(refs_values[MAX_OCCUR:])
        for idx, ref in enumerate(refs_values[:MAX_OCCUR]):
            self._printToFile(f, '#: %s' % ref)
#            if include_ellipsis and idx == MAX_OCCUR - 1:
# self._printToFile(f, '#: %s' % ref)
# self._printToFile(f, '#: ...')

        if msgstr and (msg_changed or fuzzy):
            self._printToFile(f, '#, fuzzy')

        # Add backslash escape to id.
        if '"' in id and u'\\"' not in id:
            id = id.replace('"', '\\"')

        self._printToFile(f, self._create_msgid(id))
        if '\\n' not in msgstr:
            self._printToFile(f, self._create_msgstr(msgstr))
        else:
            self._printToFile(f, 'msgstr ""')
            lines = msgstr.split('\\n')
            for line in lines[:-1]:
                # Restore the literal backslash-n at the end
                line += '\\n'
                # Wrap over multiple lines if needed.
                newline = wrapAndQuoteString(line)
                self._printToFile(f, newline)
            if lines[-1]:
                # This is the part after the last literal backslash-n
                newline = wrapAndQuoteString(lines[-1])
                self._printToFile(f, newline)