i18n/polib.py
author Augie Fackler <augie@google.com>
Fri, 12 Oct 2018 11:44:27 -0400
changeset 40185 19fc5a986669
parent 32889 a7310a477966
child 43076 2372284d9457
permissions -rw-r--r--
polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a) # no-check-commit third-party code that doesn't match our style Differential Revision: https://phab.mercurial-scm.org/D5001
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11432
0d548beb9bca i18n: disable check-code on polib.py
Matt Mackall <mpm@selenic.com>
parents: 11388
diff changeset
     1
# no-check-code
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
     2
# -* coding: utf-8 -*-
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
     3
#
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
     4
# License: MIT (see LICENSE file provided)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
     5
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
     6
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
     7
"""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
     8
**polib** allows you to manipulate, create, modify gettext files (pot, po and
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
     9
mo files).  You can load existing files, iterate through it's entries, add,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    10
modify entries, comments or metadata, etc. or create new po files from scratch.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    11
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    12
**polib** provides a simple and pythonic API via the :func:`~polib.pofile` and
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    13
:func:`~polib.mofile` convenience functions.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    14
"""
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    15
29485
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25660
diff changeset
    16
from __future__ import absolute_import
6a98f9408a50 py3: make files use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25660
diff changeset
    17
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    18
__author__ = 'David Jean Louis <izimobil@gmail.com>'
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    19
__version__ = '1.0.7'
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    20
__all__ = ['pofile', 'POFile', 'POEntry', 'mofile', 'MOFile', 'MOEntry',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    21
           'default_encoding', 'escape', 'unescape', 'detect_encoding', ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    22
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    23
import array
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    24
import codecs
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    25
import os
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    26
import re
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    27
import struct
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    28
import sys
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    29
import textwrap
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    30
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    31
try:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    32
    import io
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    33
except ImportError:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    34
    # replacement of io.open() for python < 2.6
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    35
    # we use codecs instead
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    36
    class io(object):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    37
        @staticmethod
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    38
        def open(fpath, mode='r', encoding=None):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    39
            return codecs.open(fpath, mode, encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    40
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    41
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    42
# the default encoding to use when encoding cannot be detected
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    43
default_encoding = 'utf-8'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    44
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    45
# python 2/3 compatibility helpers {{{
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    46
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    47
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    48
if sys.version_info[:2] < (3, 0):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    49
    PY3 = False
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    50
    text_type = unicode
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    51
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    52
    def b(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    53
        return s
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    54
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    55
    def u(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    56
        return unicode(s, "unicode_escape")
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    57
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    58
else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    59
    PY3 = True
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    60
    text_type = str
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    61
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    62
    def b(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    63
        return s.encode("latin-1")
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    64
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    65
    def u(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    66
        return s
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    67
# }}}
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    68
# _pofile_or_mofile {{{
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    69
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    70
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    71
def _pofile_or_mofile(f, type, **kwargs):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    72
    """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    73
    Internal function used by :func:`polib.pofile` and :func:`polib.mofile` to
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    74
    honor the DRY concept.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    75
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    76
    # get the file encoding
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    77
    enc = kwargs.get('encoding')
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    78
    if enc is None:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    79
        enc = detect_encoding(f, type == 'mofile')
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    80
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    81
    # parse the file
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    82
    kls = type == 'pofile' and _POFileParser or _MOFileParser
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    83
    parser = kls(
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    84
        f,
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    85
        encoding=enc,
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    86
        check_for_duplicates=kwargs.get('check_for_duplicates', False),
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    87
        klass=kwargs.get('klass')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    88
    )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    89
    instance = parser.parse()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    90
    instance.wrapwidth = kwargs.get('wrapwidth', 78)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    91
    return instance
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    92
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    93
# _is_file {{{
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    94
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    95
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    96
def _is_file(filename_or_contents):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    97
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    98
    Safely returns the value of os.path.exists(filename_or_contents).
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    99
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   100
    Arguments:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   101
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   102
    ``filename_or_contents``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   103
        either a filename, or a string holding the contents of some file.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   104
        In the latter case, this function will always return False.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   105
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   106
    try:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   107
        return os.path.exists(filename_or_contents)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   108
    except (ValueError, UnicodeEncodeError):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   109
        return False
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   110
# }}}
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   111
# function pofile() {{{
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   112
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   113
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   114
def pofile(pofile, **kwargs):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   115
    """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   116
    Convenience function that parses the po or pot file ``pofile`` and returns
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   117
    a :class:`~polib.POFile` instance.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   118
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   119
    Arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   120
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   121
    ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   122
        string, full or relative path to the po/pot file or its content (data).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   123
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   124
    ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   125
        integer, the wrap width, only useful when the ``-w`` option was passed
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   126
        to xgettext (optional, default: ``78``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   127
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   128
    ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   129
        string, the encoding to use (e.g. "utf-8") (default: ``None``, the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   130
        encoding will be auto-detected).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   131
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   132
    ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   133
        whether to check for duplicate entries when adding entries to the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   134
        file (optional, default: ``False``).
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   135
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   136
    ``klass``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   137
        class which is used to instantiate the return value (optional,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   138
        default: ``None``, the return value with be a :class:`~polib.POFile`
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   139
        instance).
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   140
    """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   141
    return _pofile_or_mofile(pofile, 'pofile', **kwargs)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   142
# }}}
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   143
# function mofile() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   144
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   145
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   146
def mofile(mofile, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   147
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   148
    Convenience function that parses the mo file ``mofile`` and returns a
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   149
    :class:`~polib.MOFile` instance.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   150
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   151
    Arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   152
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   153
    ``mofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   154
        string, full or relative path to the mo file or its content (data).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   155
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   156
    ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   157
        integer, the wrap width, only useful when the ``-w`` option was passed
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   158
        to xgettext to generate the po file that was used to format the mo file
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   159
        (optional, default: ``78``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   160
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   161
    ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   162
        string, the encoding to use (e.g. "utf-8") (default: ``None``, the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   163
        encoding will be auto-detected).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   164
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   165
    ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   166
        whether to check for duplicate entries when adding entries to the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   167
        file (optional, default: ``False``).
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   168
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   169
    ``klass``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   170
        class which is used to instantiate the return value (optional,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   171
        default: ``None``, the return value with be a :class:`~polib.POFile`
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   172
        instance).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   173
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   174
    return _pofile_or_mofile(mofile, 'mofile', **kwargs)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   175
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   176
# function detect_encoding() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   177
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   178
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   179
def detect_encoding(file, binary_mode=False):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   180
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   181
    Try to detect the encoding used by the ``file``. The ``file`` argument can
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   182
    be a PO or MO file path or a string containing the contents of the file.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   183
    If the encoding cannot be detected, the function will return the value of
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   184
    ``default_encoding``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   185
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   186
    Arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   187
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   188
    ``file``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   189
        string, full or relative path to the po/mo file or its content.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   190
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   191
    ``binary_mode``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   192
        boolean, set this to True if ``file`` is a mo file.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   193
    """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   194
    PATTERN = r'"?Content-Type:.+? charset=([\w_\-:\.]+)'
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   195
    rxt = re.compile(u(PATTERN))
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   196
    rxb = re.compile(b(PATTERN))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   197
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   198
    def charset_exists(charset):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   199
        """Check whether ``charset`` is valid or not."""
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   200
        try:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   201
            codecs.lookup(charset)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   202
        except LookupError:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   203
            return False
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   204
        return True
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   205
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   206
    if not _is_file(file):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   207
        match = rxt.search(file)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   208
        if match:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   209
            enc = match.group(1).strip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   210
            if charset_exists(enc):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   211
                return enc
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   212
    else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   213
        # For PY3, always treat as binary
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   214
        if binary_mode or PY3:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   215
            mode = 'rb'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   216
            rx = rxb
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   217
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   218
            mode = 'r'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   219
            rx = rxt
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   220
        f = open(file, mode)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   221
        for l in f.readlines():
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   222
            match = rx.search(l)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   223
            if match:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   224
                f.close()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   225
                enc = match.group(1).strip()
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   226
                if not isinstance(enc, text_type):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   227
                    enc = enc.decode('utf-8')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   228
                if charset_exists(enc):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   229
                    return enc
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   230
        f.close()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   231
    return default_encoding
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   232
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   233
# function escape() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   234
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   235
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   236
def escape(st):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   237
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   238
    Escapes the characters ``\\\\``, ``\\t``, ``\\n``, ``\\r`` and ``"`` in
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   239
    the given string ``st`` and returns it.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   240
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   241
    return st.replace('\\', r'\\')\
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   242
             .replace('\t', r'\t')\
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   243
             .replace('\r', r'\r')\
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   244
             .replace('\n', r'\n')\
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   245
             .replace('\"', r'\"')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   246
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   247
# function unescape() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   248
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   249
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   250
def unescape(st):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   251
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   252
    Unescapes the characters ``\\\\``, ``\\t``, ``\\n``, ``\\r`` and ``"`` in
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   253
    the given string ``st`` and returns it.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   254
    """
11388
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   255
    def unescape_repl(m):
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   256
        m = m.group(1)
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   257
        if m == 'n':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   258
            return '\n'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   259
        if m == 't':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   260
            return '\t'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   261
        if m == 'r':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   262
            return '\r'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   263
        if m == '\\':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   264
            return '\\'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   265
        return m  # handles escaped double quote
11388
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   266
    return re.sub(r'\\(\\|n|t|r|")', unescape_repl, st)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   267
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   268
# class _BaseFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   269
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   270
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   271
class _BaseFile(list):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   272
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   273
    Common base class for the :class:`~polib.POFile` and :class:`~polib.MOFile`
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   274
    classes. This class should **not** be instanciated directly.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   275
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   276
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   277
    def __init__(self, *args, **kwargs):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   278
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   279
        Constructor, accepts the following keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   280
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   281
        ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   282
            string, the path to the po or mo file, or its content as a string.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   283
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   284
        ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   285
            integer, the wrap width, only useful when the ``-w`` option was
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   286
            passed to xgettext (optional, default: ``78``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   287
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   288
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   289
            string, the encoding to use, defaults to ``default_encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   290
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   291
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   292
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   293
            whether to check for duplicate entries when adding entries to the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   294
            file, (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   295
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   296
        list.__init__(self)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   297
        # the opened file handle
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   298
        pofile = kwargs.get('pofile', None)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   299
        if pofile and _is_file(pofile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   300
            self.fpath = pofile
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   301
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   302
            self.fpath = kwargs.get('fpath')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   303
        # the width at which lines should be wrapped
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   304
        self.wrapwidth = kwargs.get('wrapwidth', 78)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   305
        # the file encoding
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   306
        self.encoding = kwargs.get('encoding', default_encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   307
        # whether to check for duplicate entries or not
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   308
        self.check_for_duplicates = kwargs.get('check_for_duplicates', False)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   309
        # header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   310
        self.header = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   311
        # both po and mo files have metadata
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   312
        self.metadata = {}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   313
        self.metadata_is_fuzzy = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   314
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   315
    def __unicode__(self):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   316
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   317
        Returns the unicode representation of the file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   318
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   319
        ret = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   320
        entries = [self.metadata_as_entry()] + \
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   321
                  [e for e in self if not e.obsolete]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   322
        for entry in entries:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   323
            ret.append(entry.__unicode__(self.wrapwidth))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   324
        for entry in self.obsolete_entries():
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   325
            ret.append(entry.__unicode__(self.wrapwidth))
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   326
        ret = u('\n').join(ret)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   327
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   328
        assert isinstance(ret, text_type)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   329
        #if type(ret) != text_type:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   330
        #    return unicode(ret, self.encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   331
        return ret
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   332
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   333
    if PY3:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   334
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   335
            return self.__unicode__()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   336
    else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   337
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   338
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   339
            Returns the string representation of the file.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   340
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   341
            return unicode(self).encode(self.encoding)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   342
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   343
    def __contains__(self, entry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   344
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   345
        Overridden ``list`` method to implement the membership test (in and
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   346
        not in).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   347
        The method considers that an entry is in the file if it finds an entry
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   348
        that has the same msgid (the test is **case sensitive**) and the same
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   349
        msgctxt (or none for both entries).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   350
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   351
        Argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   352
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   353
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   354
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   355
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   356
        return self.find(entry.msgid, by='msgid', msgctxt=entry.msgctxt) \
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   357
            is not None
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   358
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   359
    def __eq__(self, other):
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   360
        return str(self) == str(other)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   361
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   362
    def append(self, entry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   363
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   364
        Overridden method to check for duplicates entries, if a user tries to
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   365
        add an entry that is already in the file, the method will raise a
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   366
        ``ValueError`` exception.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   367
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   368
        Argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   369
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   370
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   371
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   372
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   373
        if self.check_for_duplicates and entry in self:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   374
            raise ValueError('Entry "%s" already exists' % entry.msgid)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   375
        super(_BaseFile, self).append(entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   376
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   377
    def insert(self, index, entry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   378
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   379
        Overridden method to check for duplicates entries, if a user tries to
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   380
        add an entry that is already in the file, the method will raise a
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   381
        ``ValueError`` exception.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   382
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   383
        Arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   384
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   385
        ``index``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   386
            index at which the entry should be inserted.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   387
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   388
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   389
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   390
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   391
        if self.check_for_duplicates and entry in self:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   392
            raise ValueError('Entry "%s" already exists' % entry.msgid)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   393
        super(_BaseFile, self).insert(index, entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   394
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   395
    def metadata_as_entry(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   396
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   397
        Returns the file metadata as a :class:`~polib.POFile` instance.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   398
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   399
        e = POEntry(msgid='')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   400
        mdata = self.ordered_metadata()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   401
        if mdata:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   402
            strs = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   403
            for name, value in mdata:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   404
                # Strip whitespace off each line in a multi-line entry
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   405
                strs.append('%s: %s' % (name, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   406
            e.msgstr = '\n'.join(strs) + '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   407
        if self.metadata_is_fuzzy:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   408
            e.flags.append('fuzzy')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   409
        return e
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   410
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   411
    def save(self, fpath=None, repr_method='__unicode__'):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   412
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   413
        Saves the po file to ``fpath``.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   414
        If it is an existing file and no ``fpath`` is provided, then the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   415
        existing file is rewritten with the modified data.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   416
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   417
        Keyword arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   418
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   419
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   420
            string, full or relative path to the file.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   421
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   422
        ``repr_method``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   423
            string, the method to use for output.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   424
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   425
        if self.fpath is None and fpath is None:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   426
            raise IOError('You must provide a file path to save() method')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   427
        contents = getattr(self, repr_method)()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   428
        if fpath is None:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   429
            fpath = self.fpath
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   430
        if repr_method == 'to_binary':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   431
            fhandle = open(fpath, 'wb')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   432
        else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   433
            fhandle = io.open(fpath, 'w', encoding=self.encoding)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   434
            if not isinstance(contents, text_type):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   435
                contents = contents.decode(self.encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   436
        fhandle.write(contents)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   437
        fhandle.close()
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   438
        # set the file path if not set
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   439
        if self.fpath is None and fpath:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   440
            self.fpath = fpath
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   441
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   442
    def find(self, st, by='msgid', include_obsolete_entries=False,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   443
             msgctxt=False):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   444
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   445
        Find the entry which msgid (or property identified by the ``by``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   446
        argument) matches the string ``st``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   447
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   448
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   449
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   450
        ``st``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   451
            string, the string to search for.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   452
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   453
        ``by``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   454
            string, the property to use for comparison (default: ``msgid``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   455
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   456
        ``include_obsolete_entries``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   457
            boolean, whether to also search in entries that are obsolete.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   458
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   459
        ``msgctxt``
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   460
            string, allows specifying a specific message context for the
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   461
            search.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   462
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   463
        if include_obsolete_entries:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   464
            entries = self[:]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   465
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   466
            entries = [e for e in self if not e.obsolete]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   467
        for e in entries:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   468
            if getattr(e, by) == st:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   469
                if msgctxt is not False and e.msgctxt != msgctxt:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   470
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   471
                return e
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   472
        return None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   473
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   474
    def ordered_metadata(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   475
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   476
        Convenience method that returns an ordered version of the metadata
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 19023
diff changeset
   477
        dictionary. The return value is list of tuples (metadata name,
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   478
        metadata_value).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   479
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   480
        # copy the dict first
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   481
        metadata = self.metadata.copy()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   482
        data_order = [
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   483
            'Project-Id-Version',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   484
            'Report-Msgid-Bugs-To',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   485
            'POT-Creation-Date',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   486
            'PO-Revision-Date',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   487
            'Last-Translator',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   488
            'Language-Team',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   489
            'MIME-Version',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   490
            'Content-Type',
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   491
            'Content-Transfer-Encoding',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   492
            'Language',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   493
            'Plural-Forms'
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   494
        ]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   495
        ordered_data = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   496
        for data in data_order:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   497
            try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   498
                value = metadata.pop(data)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   499
                ordered_data.append((data, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   500
            except KeyError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   501
                pass
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   502
        # the rest of the metadata will be alphabetically ordered since there
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   503
        # are no specs for this AFAIK
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   504
        for data in sorted(metadata.keys()):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   505
            value = metadata[data]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   506
            ordered_data.append((data, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   507
        return ordered_data
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   508
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   509
    def to_binary(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   510
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   511
        Return the binary representation of the file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   512
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   513
        offsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   514
        entries = self.translated_entries()
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   515
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   516
        # the keys are sorted in the .mo file
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   517
        def cmp(_self, other):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   518
            # msgfmt compares entries with msgctxt if it exists
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   519
            self_msgid = _self.msgctxt and _self.msgctxt or _self.msgid
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   520
            other_msgid = other.msgctxt and other.msgctxt or other.msgid
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   521
            if self_msgid > other_msgid:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   522
                return 1
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   523
            elif self_msgid < other_msgid:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   524
                return -1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   525
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   526
                return 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   527
        # add metadata entry
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   528
        entries.sort(key=lambda o: o.msgctxt or o.msgid)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   529
        mentry = self.metadata_as_entry()
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   530
        #mentry.msgstr = mentry.msgstr.replace('\\n', '').lstrip()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   531
        entries = [mentry] + entries
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   532
        entries_len = len(entries)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   533
        ids, strs = b(''), b('')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   534
        for e in entries:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   535
            # For each string, we need size and file offset.  Each string is
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   536
            # NUL terminated; the NUL does not count into the size.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   537
            msgid = b('')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   538
            if e.msgctxt:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   539
                # Contexts are stored by storing the concatenation of the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   540
                # context, a <EOT> byte, and the original string
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   541
                msgid = self._encode(e.msgctxt + '\4')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   542
            if e.msgid_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   543
                msgstr = []
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   544
                for index in sorted(e.msgstr_plural.keys()):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   545
                    msgstr.append(e.msgstr_plural[index])
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   546
                msgid += self._encode(e.msgid + '\0' + e.msgid_plural)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   547
                msgstr = self._encode('\0'.join(msgstr))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   548
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   549
                msgid += self._encode(e.msgid)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   550
                msgstr = self._encode(e.msgstr)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   551
            offsets.append((len(ids), len(msgid), len(strs), len(msgstr)))
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   552
            ids += msgid + b('\0')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   553
            strs += msgstr + b('\0')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   554
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   555
        # The header is 7 32-bit unsigned integers.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   556
        keystart = 7 * 4 + 16 * entries_len
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   557
        # and the values start after the keys
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   558
        valuestart = keystart + len(ids)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   559
        koffsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   560
        voffsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   561
        # The string table first has the list of keys, then the list of values.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   562
        # Each entry has first the size of the string, then the file offset.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   563
        for o1, l1, o2, l2 in offsets:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   564
            koffsets += [l1, o1 + keystart]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   565
            voffsets += [l2, o2 + valuestart]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   566
        offsets = koffsets + voffsets
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   567
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   568
        output = struct.pack(
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   569
            "Iiiiiii",
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   570
            # Magic number
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   571
            MOFile.MAGIC,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   572
            # Version
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   573
            0,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   574
            # number of entries
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   575
            entries_len,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   576
            # start of key index
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   577
            7 * 4,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   578
            # start of value index
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   579
            7 * 4 + entries_len * 8,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   580
            # size and offset of hash table, we don't use hash tables
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   581
            0, keystart
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   582
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   583
        )
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   584
        if PY3 and sys.version_info.minor > 1:  # python 3.2 or superior
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   585
            output += array.array("i", offsets).tobytes()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   586
        else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   587
            output += array.array("i", offsets).tostring()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   588
        output += ids
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   589
        output += strs
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   590
        return output
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   591
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   592
    def _encode(self, mixed):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   593
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   594
        Encodes the given ``mixed`` argument with the file encoding if and
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   595
        only if it's an unicode string and returns the encoded string.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   596
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   597
        if isinstance(mixed, text_type):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   598
            mixed = mixed.encode(self.encoding)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   599
        return mixed
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   600
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   601
# class POFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   602
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   603
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   604
class POFile(_BaseFile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   605
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   606
    Po (or Pot) file reader/writer.
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   607
    This class inherits the :class:`~polib._BaseFile` class and, by extension,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   608
    the python ``list`` type.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   609
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   610
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   611
    def __unicode__(self):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   612
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   613
        Returns the unicode representation of the po file.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   614
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   615
        ret, headers = '', self.header.split('\n')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   616
        for header in headers:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   617
            if not len(header):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   618
                ret += "#\n"
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   619
            elif header[:1] in [',', ':']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   620
                ret += '#%s\n' % header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   621
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   622
                ret += '# %s\n' % header
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   623
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   624
        if not isinstance(ret, text_type):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   625
            ret = ret.decode(self.encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   626
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   627
        return ret + _BaseFile.__unicode__(self)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   628
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   629
    def save_as_mofile(self, fpath):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   630
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   631
        Saves the binary representation of the file to given ``fpath``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   632
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   633
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   634
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   635
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   636
            string, full or relative path to the mo file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   637
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   638
        _BaseFile.save(self, fpath, 'to_binary')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   639
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   640
    def percent_translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   641
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   642
        Convenience method that returns the percentage of translated
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   643
        messages.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   644
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   645
        total = len([e for e in self if not e.obsolete])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   646
        if total == 0:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   647
            return 100
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   648
        translated = len(self.translated_entries())
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   649
        return int(translated * 100 / float(total))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   650
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   651
    def translated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   652
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   653
        Convenience method that returns the list of translated entries.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   654
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   655
        return [e for e in self if e.translated()]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   656
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   657
    def untranslated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   658
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   659
        Convenience method that returns the list of untranslated entries.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   660
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   661
        return [e for e in self if not e.translated() and not e.obsolete
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   662
                and not 'fuzzy' in e.flags]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   663
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   664
    def fuzzy_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   665
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   666
        Convenience method that returns the list of fuzzy entries.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   667
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   668
        return [e for e in self if 'fuzzy' in e.flags]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   669
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   670
    def obsolete_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   671
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   672
        Convenience method that returns the list of obsolete entries.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   673
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   674
        return [e for e in self if e.obsolete]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   675
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   676
    def merge(self, refpot):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   677
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   678
        Convenience method that merges the current pofile with the pot file
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   679
        provided. It behaves exactly as the gettext msgmerge utility:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   680
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   681
        * comments of this file will be preserved, but extracted comments and
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   682
          occurrences will be discarded;
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   683
        * any translations or comments in the file will be discarded, however,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   684
          dot comments and file positions will be preserved;
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   685
        * the fuzzy flags are preserved.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   686
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   687
        Keyword argument:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   688
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   689
        ``refpot``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   690
            object POFile, the reference catalog.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   691
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   692
        # Store entries in dict/set for faster access
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   693
        self_entries = dict((entry.msgid, entry) for entry in self)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   694
        refpot_msgids = set(entry.msgid for entry in refpot)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   695
        # Merge entries that are in the refpot
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   696
        for entry in refpot:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   697
            e = self_entries.get(entry.msgid)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   698
            if e is None:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   699
                e = POEntry()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   700
                self.append(e)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   701
            e.merge(entry)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   702
        # ok, now we must "obsolete" entries that are not in the refpot anymore
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   703
        for entry in self:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   704
            if entry.msgid not in refpot_msgids:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   705
                entry.obsolete = True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   706
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   707
# class MOFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   708
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   709
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   710
class MOFile(_BaseFile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   711
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   712
    Mo file reader/writer.
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   713
    This class inherits the :class:`~polib._BaseFile` class and, by
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   714
    extension, the python ``list`` type.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   715
    """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   716
    MAGIC = 0x950412de
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   717
    MAGIC_SWAPPED = 0xde120495
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   718
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   719
    def __init__(self, *args, **kwargs):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   720
        """
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   721
        Constructor, accepts all keywords arguments accepted by
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   722
        :class:`~polib._BaseFile` class.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   723
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   724
        _BaseFile.__init__(self, *args, **kwargs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   725
        self.magic_number = None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   726
        self.version = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   727
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   728
    def save_as_pofile(self, fpath):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   729
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   730
        Saves the mofile as a pofile to ``fpath``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   731
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   732
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   733
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   734
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   735
            string, full or relative path to the file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   736
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   737
        _BaseFile.save(self, fpath)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   738
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   739
    def save(self, fpath=None):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   740
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   741
        Saves the mofile to ``fpath``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   742
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   743
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   744
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   745
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   746
            string, full or relative path to the file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   747
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   748
        _BaseFile.save(self, fpath, 'to_binary')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   749
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   750
    def percent_translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   751
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   752
        Convenience method to keep the same interface with POFile instances.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   753
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   754
        return 100
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   755
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   756
    def translated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   757
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   758
        Convenience method to keep the same interface with POFile instances.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   759
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   760
        return self
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   761
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   762
    def untranslated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   763
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   764
        Convenience method to keep the same interface with POFile instances.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   765
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   766
        return []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   767
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   768
    def fuzzy_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   769
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   770
        Convenience method to keep the same interface with POFile instances.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   771
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   772
        return []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   773
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   774
    def obsolete_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   775
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   776
        Convenience method to keep the same interface with POFile instances.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   777
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   778
        return []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   779
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   780
# class _BaseEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   781
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   782
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   783
class _BaseEntry(object):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   784
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   785
    Base class for :class:`~polib.POEntry` and :class:`~polib.MOEntry` classes.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   786
    This class should **not** be instanciated directly.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   787
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   788
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   789
    def __init__(self, *args, **kwargs):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   790
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   791
        Constructor, accepts the following keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   792
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   793
        ``msgid``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   794
            string, the entry msgid.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   795
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   796
        ``msgstr``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   797
            string, the entry msgstr.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   798
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   799
        ``msgid_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   800
            string, the entry msgid_plural.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   801
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   802
        ``msgstr_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   803
            list, the entry msgstr_plural lines.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   804
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   805
        ``msgctxt``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   806
            string, the entry context (msgctxt).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   807
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   808
        ``obsolete``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   809
            bool, whether the entry is "obsolete" or not.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   810
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   811
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   812
            string, the encoding to use, defaults to ``default_encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   813
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   814
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   815
        self.msgid = kwargs.get('msgid', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   816
        self.msgstr = kwargs.get('msgstr', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   817
        self.msgid_plural = kwargs.get('msgid_plural', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   818
        self.msgstr_plural = kwargs.get('msgstr_plural', {})
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   819
        self.msgctxt = kwargs.get('msgctxt', None)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   820
        self.obsolete = kwargs.get('obsolete', False)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   821
        self.encoding = kwargs.get('encoding', default_encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   822
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   823
    def __unicode__(self, wrapwidth=78):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   824
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   825
        Returns the unicode representation of the entry.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   826
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   827
        if self.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   828
            delflag = '#~ '
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   829
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   830
            delflag = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   831
        ret = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   832
        # write the msgctxt if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   833
        if self.msgctxt is not None:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   834
            ret += self._str_field("msgctxt", delflag, "", self.msgctxt,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   835
                                   wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   836
        # write the msgid
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   837
        ret += self._str_field("msgid", delflag, "", self.msgid, wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   838
        # write the msgid_plural if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   839
        if self.msgid_plural:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   840
            ret += self._str_field("msgid_plural", delflag, "",
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   841
                                   self.msgid_plural, wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   842
        if self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   843
            # write the msgstr_plural if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   844
            msgstrs = self.msgstr_plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   845
            keys = list(msgstrs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   846
            keys.sort()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   847
            for index in keys:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   848
                msgstr = msgstrs[index]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   849
                plural_index = '[%s]' % index
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   850
                ret += self._str_field("msgstr", delflag, plural_index, msgstr,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   851
                                       wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   852
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   853
            # otherwise write the msgstr
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   854
            ret += self._str_field("msgstr", delflag, "", self.msgstr,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   855
                                   wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   856
        ret.append('')
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   857
        ret = u('\n').join(ret)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   858
        return ret
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   859
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   860
    if PY3:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   861
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   862
            return self.__unicode__()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   863
    else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   864
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   865
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   866
            Returns the string representation of the entry.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   867
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   868
            return unicode(self).encode(self.encoding)
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   869
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   870
    def __eq__(self, other):
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   871
        return str(self) == str(other)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   872
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   873
    def _str_field(self, fieldname, delflag, plural_index, field,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   874
                   wrapwidth=78):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   875
        lines = field.splitlines(True)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   876
        if len(lines) > 1:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   877
            lines = [''] + lines  # start with initial empty line
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   878
        else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   879
            escaped_field = escape(field)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   880
            specialchars_count = 0
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   881
            for c in ['\\', '\n', '\r', '\t', '"']:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   882
                specialchars_count += field.count(c)
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   883
            # comparison must take into account fieldname length + one space
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   884
            # + 2 quotes (eg. msgid "<string>")
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   885
            flength = len(fieldname) + 3
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   886
            if plural_index:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   887
                flength += len(plural_index)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   888
            real_wrapwidth = wrapwidth - flength + specialchars_count
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   889
            if wrapwidth > 0 and len(field) > real_wrapwidth:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   890
                # Wrap the line but take field name into account
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   891
                lines = [''] + [unescape(item) for item in wrap(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   892
                    escaped_field,
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   893
                    wrapwidth - 2,  # 2 for quotes ""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   894
                    drop_whitespace=False,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   895
                    break_long_words=False
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   896
                )]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   897
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   898
                lines = [field]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   899
        if fieldname.startswith('previous_'):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   900
            # quick and dirty trick to get the real field name
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   901
            fieldname = fieldname[9:]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   902
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   903
        ret = ['%s%s%s "%s"' % (delflag, fieldname, plural_index,
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   904
                                escape(lines.pop(0)))]
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   905
        for line in lines:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   906
            ret.append('%s"%s"' % (delflag, escape(line)))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   907
        return ret
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   908
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   909
# class POEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   910
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   911
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   912
class POEntry(_BaseEntry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   913
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   914
    Represents a po file entry.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   915
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   916
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   917
    def __init__(self, *args, **kwargs):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   918
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   919
        Constructor, accepts the following keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   920
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   921
        ``comment``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   922
            string, the entry comment.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   923
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   924
        ``tcomment``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   925
            string, the entry translator comment.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   926
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   927
        ``occurrences``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   928
            list, the entry occurrences.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   929
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   930
        ``flags``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   931
            list, the entry flags.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   932
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   933
        ``previous_msgctxt``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   934
            string, the entry previous context.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   935
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   936
        ``previous_msgid``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   937
            string, the entry previous msgid.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   938
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   939
        ``previous_msgid_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   940
            string, the entry previous msgid_plural.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   941
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   942
        ``linenum``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   943
            integer, the line number of the entry
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   944
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   945
        _BaseEntry.__init__(self, *args, **kwargs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   946
        self.comment = kwargs.get('comment', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   947
        self.tcomment = kwargs.get('tcomment', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   948
        self.occurrences = kwargs.get('occurrences', [])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   949
        self.flags = kwargs.get('flags', [])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   950
        self.previous_msgctxt = kwargs.get('previous_msgctxt', None)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   951
        self.previous_msgid = kwargs.get('previous_msgid', None)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   952
        self.previous_msgid_plural = kwargs.get('previous_msgid_plural', None)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   953
        self.linenum = kwargs.get('linenum', None)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   954
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   955
    def __unicode__(self, wrapwidth=78):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   956
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   957
        Returns the unicode representation of the entry.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   958
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   959
        if self.obsolete:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   960
            return _BaseEntry.__unicode__(self, wrapwidth)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   961
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   962
        ret = []
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   963
        # comments first, if any (with text wrapping as xgettext does)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   964
        comments = [('comment', '#. '), ('tcomment', '# ')]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   965
        for c in comments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   966
            val = getattr(self, c[0])
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   967
            if val:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   968
                for comment in val.split('\n'):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   969
                    if wrapwidth > 0 and len(comment) + len(c[1]) > wrapwidth:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   970
                        ret += wrap(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   971
                            comment,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   972
                            wrapwidth,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   973
                            initial_indent=c[1],
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   974
                            subsequent_indent=c[1],
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   975
                            break_long_words=False
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   976
                        )
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   977
                    else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   978
                        ret.append('%s%s' % (c[1], comment))
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   979
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   980
        # occurrences (with text wrapping as xgettext does)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   981
        if self.occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   982
            filelist = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   983
            for fpath, lineno in self.occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   984
                if lineno:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   985
                    filelist.append('%s:%s' % (fpath, lineno))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   986
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   987
                    filelist.append(fpath)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   988
            filestr = ' '.join(filelist)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   989
            if wrapwidth > 0 and len(filestr) + 3 > wrapwidth:
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   990
                # textwrap split words that contain hyphen, this is not
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   991
                # what we want for filenames, so the dirty hack is to
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   992
                # temporally replace hyphens with a char that a file cannot
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   993
                # contain, like "*"
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   994
                ret += [l.replace('*', '-') for l in wrap(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   995
                    filestr.replace('-', '*'),
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   996
                    wrapwidth,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   997
                    initial_indent='#: ',
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   998
                    subsequent_indent='#: ',
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   999
                    break_long_words=False
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1000
                )]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1001
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1002
                ret.append('#: ' + filestr)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1003
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1004
        # flags (TODO: wrapping ?)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1005
        if self.flags:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1006
            ret.append('#, %s' % ', '.join(self.flags))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1007
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1008
        # previous context and previous msgid/msgid_plural
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1009
        fields = ['previous_msgctxt', 'previous_msgid',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1010
                  'previous_msgid_plural']
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1011
        for f in fields:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1012
            val = getattr(self, f)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1013
            if val:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1014
                ret += self._str_field(f, "#| ", "", val, wrapwidth)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1015
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1016
        ret.append(_BaseEntry.__unicode__(self, wrapwidth))
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1017
        ret = u('\n').join(ret)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1018
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1019
        assert isinstance(ret, text_type)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1020
        #if type(ret) != types.UnicodeType:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1021
        #    return unicode(ret, self.encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1022
        return ret
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1023
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1024
    def __cmp__(self, other):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1025
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1026
        Called by comparison operations if rich comparison is not defined.
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1027
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1028
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1029
        # First: Obsolete test
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1030
        if self.obsolete != other.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1031
            if self.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1032
                return -1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1033
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1034
                return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1035
        # Work on a copy to protect original
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1036
        occ1 = sorted(self.occurrences[:])
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1037
        occ2 = sorted(other.occurrences[:])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1038
        pos = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1039
        for entry1 in occ1:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1040
            try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1041
                entry2 = occ2[pos]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1042
            except IndexError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1043
                return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1044
            pos = pos + 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1045
            if entry1[0] != entry2[0]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1046
                if entry1[0] > entry2[0]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1047
                    return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1048
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1049
                    return -1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1050
            if entry1[1] != entry2[1]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1051
                if entry1[1] > entry2[1]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1052
                    return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1053
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1054
                    return -1
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1055
        # Compare msgid_plural if set
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1056
        if self.msgid_plural:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1057
            if not other.msgid_plural:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1058
                return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1059
            for pos in self.msgid_plural:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1060
                if pos not in other.msgid_plural:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1061
                    return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1062
                if self.msgid_plural[pos] > other.msgid_plural[pos]:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1063
                    return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1064
                if self.msgid_plural[pos] < other.msgid_plural[pos]:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1065
                    return -1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1066
        # Finally: Compare message ID
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1067
        if self.msgid > other.msgid:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1068
            return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1069
        elif self.msgid < other.msgid:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1070
            return -1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1071
        return 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1072
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1073
    def __gt__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1074
        return self.__cmp__(other) > 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1075
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1076
    def __lt__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1077
        return self.__cmp__(other) < 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1078
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1079
    def __ge__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1080
        return self.__cmp__(other) >= 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1081
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1082
    def __le__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1083
        return self.__cmp__(other) <= 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1084
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1085
    def __eq__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1086
        return self.__cmp__(other) == 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1087
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1088
    def __ne__(self, other):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1089
        return self.__cmp__(other) != 0
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1090
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1091
    def translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1092
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1093
        Returns ``True`` if the entry has been translated or ``False``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1094
        otherwise.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1095
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1096
        if self.obsolete or 'fuzzy' in self.flags:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1097
            return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1098
        if self.msgstr != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1099
            return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1100
        if self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1101
            for pos in self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1102
                if self.msgstr_plural[pos] == '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1103
                    return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1104
            return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1105
        return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1106
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1107
    def merge(self, other):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1108
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1109
        Merge the current entry with the given pot entry.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1110
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1111
        self.msgid = other.msgid
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1112
        self.msgctxt = other.msgctxt
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1113
        self.occurrences = other.occurrences
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1114
        self.comment = other.comment
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1115
        fuzzy = 'fuzzy' in self.flags
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1116
        self.flags = other.flags[:]  # clone flags
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1117
        if fuzzy:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1118
            self.flags.append('fuzzy')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1119
        self.msgid_plural = other.msgid_plural
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1120
        self.obsolete = other.obsolete
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1121
        self.previous_msgctxt = other.previous_msgctxt
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1122
        self.previous_msgid = other.previous_msgid
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1123
        self.previous_msgid_plural = other.previous_msgid_plural
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1124
        if other.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1125
            for pos in other.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1126
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1127
                    # keep existing translation at pos if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1128
                    self.msgstr_plural[pos]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1129
                except KeyError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1130
                    self.msgstr_plural[pos] = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1131
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1132
    def __hash__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1133
        return hash((self.msgid, self.msgstr))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1134
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1135
# class MOEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1136
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1137
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1138
class MOEntry(_BaseEntry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1139
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1140
    Represents a mo file entry.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1141
    """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1142
    def __init__(self, *args, **kwargs):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1143
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1144
        Constructor, accepts the following keyword arguments,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1145
        for consistency with :class:`~polib.POEntry`:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1146
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1147
        ``comment``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1148
        ``tcomment``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1149
        ``occurrences``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1150
        ``flags``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1151
        ``previous_msgctxt``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1152
        ``previous_msgid``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1153
        ``previous_msgid_plural``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1154
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1155
        Note: even though these keyword arguments are accepted,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1156
        they hold no real meaning in the context of MO files
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1157
        and are simply ignored.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1158
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1159
        _BaseEntry.__init__(self, *args, **kwargs)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1160
        self.comment = ''
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1161
        self.tcomment = ''
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1162
        self.occurrences = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1163
        self.flags = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1164
        self.previous_msgctxt = None
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1165
        self.previous_msgid = None
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1166
        self.previous_msgid_plural = None
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1167
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1168
    def __hash__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1169
        return hash((self.msgid, self.msgstr))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1170
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1171
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1172
# class _POFileParser {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1173
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1174
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1175
class _POFileParser(object):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1176
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1177
    A finite state machine to parse efficiently and correctly po
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1178
    file format.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1179
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1180
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1181
    def __init__(self, pofile, *args, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1182
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1183
        Constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1184
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1185
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1186
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1187
        ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1188
            string, path to the po file or its content
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1189
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1190
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1191
            string, the encoding to use, defaults to ``default_encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1192
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1193
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1194
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1195
            whether to check for duplicate entries when adding entries to the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1196
            file (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1197
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1198
        enc = kwargs.get('encoding', default_encoding)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1199
        if _is_file(pofile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1200
            try:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1201
                self.fhandle = io.open(pofile, 'rt', encoding=enc)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1202
            except LookupError:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1203
                enc = default_encoding
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1204
                self.fhandle = io.open(pofile, 'rt', encoding=enc)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1205
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1206
            self.fhandle = pofile.splitlines()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1207
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1208
        klass = kwargs.get('klass')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1209
        if klass is None:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1210
            klass = POFile
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1211
        self.instance = klass(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1212
            pofile=pofile,
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1213
            encoding=enc,
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1214
            check_for_duplicates=kwargs.get('check_for_duplicates', False)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1215
        )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1216
        self.transitions = {}
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1217
        self.current_line = 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1218
        self.current_entry = POEntry(linenum=self.current_line)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1219
        self.current_state = 'st'
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1220
        self.current_token = None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1221
        # two memo flags used in handlers
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1222
        self.msgstr_index = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1223
        self.entry_obsolete = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1224
        # Configure the state machine, by adding transitions.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1225
        # Signification of symbols:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1226
        #     * ST: Beginning of the file (start)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1227
        #     * HE: Header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1228
        #     * TC: a translation comment
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1229
        #     * GC: a generated comment
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1230
        #     * OC: a file/line occurrence
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1231
        #     * FL: a flags line
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1232
        #     * CT: a message context
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1233
        #     * PC: a previous msgctxt
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1234
        #     * PM: a previous msgid
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1235
        #     * PP: a previous msgid_plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1236
        #     * MI: a msgid
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1237
        #     * MP: a msgid plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1238
        #     * MS: a msgstr
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1239
        #     * MX: a msgstr plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1240
        #     * MC: a msgid or msgstr continuation line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1241
        all = ['st', 'he', 'gc', 'oc', 'fl', 'ct', 'pc', 'pm', 'pp', 'tc',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1242
               'ms', 'mp', 'mx', 'mi']
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1243
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1244
        self.add('tc', ['st', 'he'],                                     'he')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1245
        self.add('tc', ['gc', 'oc', 'fl', 'tc', 'pc', 'pm', 'pp', 'ms',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1246
                        'mp', 'mx', 'mi'],                               'tc')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1247
        self.add('gc', all,                                              'gc')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1248
        self.add('oc', all,                                              'oc')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1249
        self.add('fl', all,                                              'fl')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1250
        self.add('pc', all,                                              'pc')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1251
        self.add('pm', all,                                              'pm')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1252
        self.add('pp', all,                                              'pp')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1253
        self.add('ct', ['st', 'he', 'gc', 'oc', 'fl', 'tc', 'pc', 'pm',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1254
                        'pp', 'ms', 'mx'],                               'ct')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1255
        self.add('mi', ['st', 'he', 'gc', 'oc', 'fl', 'ct', 'tc', 'pc',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1256
                 'pm', 'pp', 'ms', 'mx'],                                'mi')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1257
        self.add('mp', ['tc', 'gc', 'pc', 'pm', 'pp', 'mi'],             'mp')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1258
        self.add('ms', ['mi', 'mp', 'tc'],                               'ms')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1259
        self.add('mx', ['mi', 'mx', 'mp', 'tc'],                         'mx')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1260
        self.add('mc', ['ct', 'mi', 'mp', 'ms', 'mx', 'pm', 'pp', 'pc'], 'mc')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1261
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1262
    def parse(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1263
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1264
        Run the state machine, parse the file line by line and call process()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1265
        with the current matched symbol.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1266
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1267
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1268
        keywords = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1269
            'msgctxt': 'ct',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1270
            'msgid': 'mi',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1271
            'msgstr': 'ms',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1272
            'msgid_plural': 'mp',
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1273
        }
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1274
        prev_keywords = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1275
            'msgid_plural': 'pp',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1276
            'msgid': 'pm',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1277
            'msgctxt': 'pc',
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1278
        }
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1279
        tokens = []
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1280
        for line in self.fhandle:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1281
            self.current_line += 1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1282
            line = line.strip()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1283
            if line == '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1284
                continue
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1285
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1286
            tokens = line.split(None, 2)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1287
            nb_tokens = len(tokens)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1288
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1289
            if tokens[0] == '#~|':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1290
                continue
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1291
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1292
            if tokens[0] == '#~' and nb_tokens > 1:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1293
                line = line[3:].strip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1294
                tokens = tokens[1:]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1295
                nb_tokens -= 1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1296
                self.entry_obsolete = 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1297
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1298
                self.entry_obsolete = 0
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1299
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1300
            # Take care of keywords like
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1301
            # msgid, msgid_plural, msgctxt & msgstr.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1302
            if tokens[0] in keywords and nb_tokens > 1:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1303
                line = line[len(tokens[0]):].lstrip()
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1304
                if re.search(r'([^\\]|^)"', line[1:-1]):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1305
                    raise IOError('Syntax error in po file %s (line %s): '
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1306
                                  'unescaped double quote found' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1307
                                  (self.instance.fpath, self.current_line))
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1308
                self.current_token = line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1309
                self.process(keywords[tokens[0]])
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1310
                continue
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1311
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1312
            self.current_token = line
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1313
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1314
            if tokens[0] == '#:':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1315
                if nb_tokens <= 1:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1316
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1317
                # we are on a occurrences line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1318
                self.process('oc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1319
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1320
            elif line[:1] == '"':
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1321
                # we are on a continuation line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1322
                if re.search(r'([^\\]|^)"', line[1:-1]):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1323
                    raise IOError('Syntax error in po file %s (line %s): '
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1324
                                  'unescaped double quote found' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1325
                                  (self.instance.fpath, self.current_line))
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1326
                self.process('mc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1327
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1328
            elif line[:7] == 'msgstr[':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1329
                # we are on a msgstr plural
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1330
                self.process('mx')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1331
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1332
            elif tokens[0] == '#,':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1333
                if nb_tokens <= 1:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1334
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1335
                # we are on a flags line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1336
                self.process('fl')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1337
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1338
            elif tokens[0] == '#' or tokens[0].startswith('##'):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1339
                if line == '#':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1340
                    line += ' '
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1341
                # we are on a translator comment line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1342
                self.process('tc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1343
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1344
            elif tokens[0] == '#.':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1345
                if nb_tokens <= 1:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1346
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1347
                # we are on a generated comment line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1348
                self.process('gc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1349
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1350
            elif tokens[0] == '#|':
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1351
                if nb_tokens <= 1:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1352
                    raise IOError('Syntax error in po file %s (line %s)' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1353
                                  (self.instance.fpath, self.current_line))
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1354
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1355
                # Remove the marker and any whitespace right after that.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1356
                line = line[2:].lstrip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1357
                self.current_token = line
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1358
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1359
                if tokens[1].startswith('"'):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1360
                    # Continuation of previous metadata.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1361
                    self.process('mc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1362
                    continue
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1363
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1364
                if nb_tokens == 2:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1365
                    # Invalid continuation line.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1366
                    raise IOError('Syntax error in po file %s (line %s): '
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1367
                                  'invalid continuation line' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1368
                                  (self.instance.fpath, self.current_line))
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1369
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1370
                # we are on a "previous translation" comment line,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1371
                if tokens[1] not in prev_keywords:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1372
                    # Unknown keyword in previous translation comment.
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1373
                    raise IOError('Syntax error in po file %s (line %s): '
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1374
                                  'unknown keyword %s' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1375
                                  (self.instance.fpath, self.current_line,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1376
                                   tokens[1]))
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1377
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1378
                # Remove the keyword and any whitespace
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1379
                # between it and the starting quote.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1380
                line = line[len(tokens[1]):].lstrip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1381
                self.current_token = line
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1382
                self.process(prev_keywords[tokens[1]])
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1383
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1384
            else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1385
                raise IOError('Syntax error in po file %s (line %s)' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1386
                              (self.instance.fpath, self.current_line))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1387
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1388
        if self.current_entry and len(tokens) > 0 and \
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1389
           not tokens[0].startswith('#'):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1390
            # since entries are added when another entry is found, we must add
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1391
            # the last entry here (only if there are lines). Trailing comments
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1392
            # are ignored
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1393
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1394
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
  1395
        # before returning the instance, check if there's metadata and if
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1396
        # so extract it in a dict
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1397
        metadataentry = self.instance.find('')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1398
        if metadataentry:  # metadata found
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1399
            # remove the entry
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1400
            self.instance.remove(metadataentry)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1401
            self.instance.metadata_is_fuzzy = metadataentry.flags
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1402
            key = None
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1403
            for msg in metadataentry.msgstr.splitlines():
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1404
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1405
                    key, val = msg.split(':', 1)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1406
                    self.instance.metadata[key] = val.strip()
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1407
                except (ValueError, KeyError):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1408
                    if key is not None:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1409
                        self.instance.metadata[key] += '\n' + msg.strip()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1410
        # close opened file
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1411
        if not isinstance(self.fhandle, list):  # must be file
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1412
            self.fhandle.close()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1413
        return self.instance
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1414
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1415
    def add(self, symbol, states, next_state):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1416
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1417
        Add a transition to the state machine.
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1418
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1419
        Keywords arguments:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1420
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1421
        ``symbol``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1422
            string, the matched token (two chars symbol).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1423
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1424
        ``states``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1425
            list, a list of states (two chars symbols).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1426
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1427
        ``next_state``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1428
            the next state the fsm will have after the action.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1429
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1430
        for state in states:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1431
            action = getattr(self, 'handle_%s' % next_state)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1432
            self.transitions[(symbol, state)] = (action, next_state)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1433
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1434
    def process(self, symbol):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1435
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1436
        Process the transition corresponding to the current state and the
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1437
        symbol provided.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1438
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1439
        Keywords arguments:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1440
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1441
        ``symbol``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1442
            string, the matched token (two chars symbol).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1443
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1444
        ``linenum``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1445
            integer, the current line number of the parsed file.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1446
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1447
        try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1448
            (action, state) = self.transitions[(symbol, self.current_state)]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1449
            if action():
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1450
                self.current_state = state
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1451
        except Exception:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1452
            raise IOError('Syntax error in po file (line %s)' %
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1453
                          self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1454
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1455
    # state handlers
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1456
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1457
    def handle_he(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1458
        """Handle a header comment."""
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1459
        if self.instance.header != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1460
            self.instance.header += '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1461
        self.instance.header += self.current_token[2:]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1462
        return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1463
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1464
    def handle_tc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1465
        """Handle a translator comment."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1466
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1467
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1468
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1469
        if self.current_entry.tcomment != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1470
            self.current_entry.tcomment += '\n'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1471
        tcomment = self.current_token.lstrip('#')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1472
        if tcomment.startswith(' '):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1473
            tcomment = tcomment[1:]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1474
        self.current_entry.tcomment += tcomment
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1475
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1476
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1477
    def handle_gc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1478
        """Handle a generated comment."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1479
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1480
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1481
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1482
        if self.current_entry.comment != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1483
            self.current_entry.comment += '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1484
        self.current_entry.comment += self.current_token[3:]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1485
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1486
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1487
    def handle_oc(self):
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1488
        """Handle a file:num occurrence."""
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1489
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1490
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1491
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1492
        occurrences = self.current_token[3:].split()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1493
        for occurrence in occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1494
            if occurrence != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1495
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1496
                    fil, line = occurrence.split(':')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1497
                    if not line.isdigit():
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1498
                        fil = fil + line
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1499
                        line = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1500
                    self.current_entry.occurrences.append((fil, line))
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1501
                except (ValueError, AttributeError):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1502
                    self.current_entry.occurrences.append((occurrence, ''))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1503
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1504
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1505
    def handle_fl(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1506
        """Handle a flags line."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1507
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1508
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1509
            self.current_entry = POEntry(linenum=self.current_line)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1510
        self.current_entry.flags += [c.strip() for c in
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1511
                                     self.current_token[3:].split(',')]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1512
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1513
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1514
    def handle_pp(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1515
        """Handle a previous msgid_plural line."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1516
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1517
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1518
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1519
        self.current_entry.previous_msgid_plural = \
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1520
            unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1521
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1522
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1523
    def handle_pm(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1524
        """Handle a previous msgid line."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1525
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1526
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1527
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1528
        self.current_entry.previous_msgid = \
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1529
            unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1530
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1531
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1532
    def handle_pc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1533
        """Handle a previous msgctxt line."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1534
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1535
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1536
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1537
        self.current_entry.previous_msgctxt = \
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1538
            unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1539
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1540
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1541
    def handle_ct(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1542
        """Handle a msgctxt."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1543
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1544
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1545
            self.current_entry = POEntry(linenum=self.current_line)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1546
        self.current_entry.msgctxt = unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1547
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1548
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1549
    def handle_mi(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1550
        """Handle a msgid."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1551
        if self.current_state in ['mc', 'ms', 'mx']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1552
            self.instance.append(self.current_entry)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1553
            self.current_entry = POEntry(linenum=self.current_line)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1554
        self.current_entry.obsolete = self.entry_obsolete
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1555
        self.current_entry.msgid = unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1556
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1557
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1558
    def handle_mp(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1559
        """Handle a msgid plural."""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1560
        self.current_entry.msgid_plural = unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1561
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1562
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1563
    def handle_ms(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1564
        """Handle a msgstr."""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1565
        self.current_entry.msgstr = unescape(self.current_token[1:-1])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1566
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1567
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1568
    def handle_mx(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1569
        """Handle a msgstr plural."""
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1570
        index = self.current_token[7]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1571
        value = self.current_token[self.current_token.find('"') + 1:-1]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1572
        self.current_entry.msgstr_plural[int(index)] = unescape(value)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1573
        self.msgstr_index = int(index)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1574
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1575
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1576
    def handle_mc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1577
        """Handle a msgid or msgstr continuation line."""
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1578
        token = unescape(self.current_token[1:-1])
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1579
        if self.current_state == 'ct':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1580
            self.current_entry.msgctxt += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1581
        elif self.current_state == 'mi':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1582
            self.current_entry.msgid += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1583
        elif self.current_state == 'mp':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1584
            self.current_entry.msgid_plural += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1585
        elif self.current_state == 'ms':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1586
            self.current_entry.msgstr += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1587
        elif self.current_state == 'mx':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1588
            self.current_entry.msgstr_plural[self.msgstr_index] += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1589
        elif self.current_state == 'pp':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1590
            self.current_entry.previous_msgid_plural += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1591
        elif self.current_state == 'pm':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1592
            self.current_entry.previous_msgid += token
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1593
        elif self.current_state == 'pc':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1594
            self.current_entry.previous_msgctxt += token
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1595
        # don't change the current state
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1596
        return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1597
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1598
# class _MOFileParser {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1599
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1600
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1601
class _MOFileParser(object):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1602
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1603
    A class to parse binary mo files.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1604
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1605
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1606
    def __init__(self, mofile, *args, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1607
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1608
        Constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1609
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1610
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1611
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1612
        ``mofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1613
            string, path to the mo file or its content
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1614
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1615
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1616
            string, the encoding to use, defaults to ``default_encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1617
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1618
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1619
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1620
            whether to check for duplicate entries when adding entries to the
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1621
            file (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1622
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1623
        self.fhandle = open(mofile, 'rb')
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1624
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1625
        klass = kwargs.get('klass')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1626
        if klass is None:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1627
            klass = MOFile
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1628
        self.instance = klass(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1629
            fpath=mofile,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1630
            encoding=kwargs.get('encoding', default_encoding),
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1631
            check_for_duplicates=kwargs.get('check_for_duplicates', False)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1632
        )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1633
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1634
    def __del__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1635
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1636
        Make sure the file is closed, this prevents warnings on unclosed file
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1637
        when running tests with python >= 3.2.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1638
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1639
        if self.fhandle:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1640
            self.fhandle.close()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1641
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1642
    def parse(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1643
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1644
        Build the instance with the file handle provided in the
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1645
        constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1646
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1647
        # parse magic number
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1648
        magic_number = self._readbinary('<I', 4)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1649
        if magic_number == MOFile.MAGIC:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1650
            ii = '<II'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1651
        elif magic_number == MOFile.MAGIC_SWAPPED:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1652
            ii = '>II'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1653
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1654
            raise IOError('Invalid mo file, magic number is incorrect !')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1655
        self.instance.magic_number = magic_number
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1656
        # parse the version number and the number of strings
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1657
        version, numofstrings = self._readbinary(ii, 8)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1658
        # from MO file format specs: "A program seeing an unexpected major
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1659
        # revision number should stop reading the MO file entirely"
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1660
        if version not in (0, 1):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1661
            raise IOError('Invalid mo file, unexpected major revision number')
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1662
        self.instance.version = version
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1663
        # original strings and translation strings hash table offset
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1664
        msgids_hash_offset, msgstrs_hash_offset = self._readbinary(ii, 8)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1665
        # move to msgid hash table and read length and offset of msgids
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1666
        self.fhandle.seek(msgids_hash_offset)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1667
        msgids_index = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1668
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1669
            msgids_index.append(self._readbinary(ii, 8))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1670
        # move to msgstr hash table and read length and offset of msgstrs
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1671
        self.fhandle.seek(msgstrs_hash_offset)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1672
        msgstrs_index = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1673
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1674
            msgstrs_index.append(self._readbinary(ii, 8))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1675
        # build entries
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1676
        encoding = self.instance.encoding
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1677
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1678
            self.fhandle.seek(msgids_index[i][1])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1679
            msgid = self.fhandle.read(msgids_index[i][0])
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1680
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1681
            self.fhandle.seek(msgstrs_index[i][1])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1682
            msgstr = self.fhandle.read(msgstrs_index[i][0])
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1683
            if i == 0 and not msgid:  # metadata
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1684
                raw_metadata, metadata = msgstr.split(b('\n')), {}
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1685
                for line in raw_metadata:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1686
                    tokens = line.split(b(':'), 1)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1687
                    if tokens[0] != b(''):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1688
                        try:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1689
                            k = tokens[0].decode(encoding)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1690
                            v = tokens[1].decode(encoding)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1691
                            metadata[k] = v.strip()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1692
                        except IndexError:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1693
                            metadata[k] = u('')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1694
                self.instance.metadata = metadata
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1695
                continue
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1696
            # test if we have a plural entry
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1697
            msgid_tokens = msgid.split(b('\0'))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1698
            if len(msgid_tokens) > 1:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1699
                entry = self._build_entry(
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1700
                    msgid=msgid_tokens[0],
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1701
                    msgid_plural=msgid_tokens[1],
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1702
                    msgstr_plural=dict((k, v) for k, v in
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1703
                                       enumerate(msgstr.split(b('\0'))))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1704
                )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1705
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1706
                entry = self._build_entry(msgid=msgid, msgstr=msgstr)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1707
            self.instance.append(entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1708
        # close opened file
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1709
        self.fhandle.close()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1710
        return self.instance
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
  1711
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1712
    def _build_entry(self, msgid, msgstr=None, msgid_plural=None,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1713
                     msgstr_plural=None):
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1714
        msgctxt_msgid = msgid.split(b('\x04'))
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1715
        encoding = self.instance.encoding
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1716
        if len(msgctxt_msgid) > 1:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1717
            kwargs = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1718
                'msgctxt': msgctxt_msgid[0].decode(encoding),
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1719
                'msgid': msgctxt_msgid[1].decode(encoding),
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1720
            }
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1721
        else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1722
            kwargs = {'msgid': msgid.decode(encoding)}
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1723
        if msgstr:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1724
            kwargs['msgstr'] = msgstr.decode(encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1725
        if msgid_plural:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1726
            kwargs['msgid_plural'] = msgid_plural.decode(encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1727
        if msgstr_plural:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1728
            for k in msgstr_plural:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1729
                msgstr_plural[k] = msgstr_plural[k].decode(encoding)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1730
            kwargs['msgstr_plural'] = msgstr_plural
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1731
        return MOEntry(**kwargs)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1732
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1733
    def _readbinary(self, fmt, numbytes):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1734
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1735
        Private method that unpack n bytes of data using format <fmt>.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1736
        It returns a tuple or a mixed value if the tuple length is 1.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1737
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1738
        bytes = self.fhandle.read(numbytes)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1739
        tup = struct.unpack(fmt, bytes)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1740
        if len(tup) == 1:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1741
            return tup[0]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1742
        return tup
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1743
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1744
# class TextWrapper {{{
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1745
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1746
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1747
class TextWrapper(textwrap.TextWrapper):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1748
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1749
    Subclass of textwrap.TextWrapper that backport the
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1750
    drop_whitespace option.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1751
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1752
    def __init__(self, *args, **kwargs):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1753
        drop_whitespace = kwargs.pop('drop_whitespace', True)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1754
        textwrap.TextWrapper.__init__(self, *args, **kwargs)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1755
        self.drop_whitespace = drop_whitespace
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1756
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1757
    def _wrap_chunks(self, chunks):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1758
        """_wrap_chunks(chunks : [string]) -> [string]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1759
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1760
        Wrap a sequence of text chunks and return a list of lines of
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1761
        length 'self.width' or less.  (If 'break_long_words' is false,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1762
        some lines may be longer than this.)  Chunks correspond roughly
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1763
        to words and the whitespace between them: each chunk is
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1764
        indivisible (modulo 'break_long_words'), but a line break can
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1765
        come between any two chunks.  Chunks should not have internal
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1766
        whitespace; ie. a chunk is either all whitespace or a "word".
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1767
        Whitespace chunks will be removed from the beginning and end of
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1768
        lines, but apart from that whitespace is preserved.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1769
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1770
        lines = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1771
        if self.width <= 0:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1772
            raise ValueError("invalid width %r (must be > 0)" % self.width)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1773
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1774
        # Arrange in reverse order so items can be efficiently popped
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1775
        # from a stack of chucks.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1776
        chunks.reverse()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1777
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1778
        while chunks:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1779
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1780
            # Start the list of chunks that will make up the current line.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1781
            # cur_len is just the length of all the chunks in cur_line.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1782
            cur_line = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1783
            cur_len = 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1784
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1785
            # Figure out which static string will prefix this line.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1786
            if lines:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1787
                indent = self.subsequent_indent
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1788
            else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1789
                indent = self.initial_indent
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1790
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1791
            # Maximum width for this line.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1792
            width = self.width - len(indent)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1793
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1794
            # First chunk on line is whitespace -- drop it, unless this
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1795
            # is the very beginning of the text (ie. no lines started yet).
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1796
            if self.drop_whitespace and chunks[-1].strip() == '' and lines:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1797
                del chunks[-1]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1798
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1799
            while chunks:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1800
                l = len(chunks[-1])
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1801
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1802
                # Can at least squeeze this chunk onto the current line.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1803
                if cur_len + l <= width:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1804
                    cur_line.append(chunks.pop())
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1805
                    cur_len += l
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1806
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1807
                # Nope, this line is full.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1808
                else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1809
                    break
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1810
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1811
            # The current line is full, and the next chunk is too big to
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1812
            # fit on *any* line (not just this one).
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1813
            if chunks and len(chunks[-1]) > width:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1814
                self._handle_long_word(chunks, cur_line, cur_len, width)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1815
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1816
            # If the last chunk on this line is all whitespace, drop it.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1817
            if self.drop_whitespace and cur_line and not cur_line[-1].strip():
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1818
                del cur_line[-1]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1819
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1820
            # Convert current line back to a string and store it in list
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1821
            # of all lines (return value).
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1822
            if cur_line:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1823
                lines.append(indent + ''.join(cur_line))
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1824
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1825
        return lines
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1826
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1827
# function wrap() {{{
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1828
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1829
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1830
def wrap(text, width=70, **kwargs):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1831
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1832
    Wrap a single paragraph of text, returning a list of wrapped lines.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1833
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1834
    if sys.version_info < (2, 6):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1835
        return TextWrapper(width=width, **kwargs).wrap(text)
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1836
    return textwrap.wrap(text, width=width, **kwargs)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1837
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1838
# }}}