i18n/polib.py
author pacien <pacien.trangirard@pacien.net>
Thu, 17 Mar 2022 11:00:05 +0100
changeset 48969 f64bbba2ee59
parent 48946 642e31cb55f0
child 51690 493034cc3265
permissions -rw-r--r--
tests: fix glob pattern for dynamic timer alignment The number of space characters varies depending on the number of digits of the timer, making some tests fail on slow machines in an unintended way: ```diff --- /build/mercurial-6.1/tests/test-merge-halt.t +++ /build/mercurial-6.1/tests/test-merge-halt.t.err @@ -210,6 +210,6 @@ merge halted after failed merge (see hg resolve) [240] $ hg shelve --list - default (* ago) changes to: foo (glob) + default (11s ago) changes to: foo $ hg unshelve --abort unshelve of 'default' aborted ERROR: test-merge-halt.t output changed ``` Differential Revision: https://phab.mercurial-scm.org/D12381
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
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    17
__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
    18
__version__ = '1.0.7'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    19
__all__ = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    20
    'pofile',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    21
    'POFile',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    22
    'POEntry',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    23
    'mofile',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    24
    'MOFile',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    25
    'MOEntry',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    26
    'default_encoding',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    27
    'escape',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    28
    'unescape',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    29
    'detect_encoding',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    30
]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    31
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    32
import array
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    33
import codecs
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    34
import os
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    35
import re
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    36
import struct
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    37
import sys
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    38
import textwrap
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    39
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    40
try:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    41
    import io
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    42
except ImportError:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    43
    # 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
    44
    # we use codecs instead
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    45
    class io:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    46
        @staticmethod
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    47
        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
    48
            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
    49
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    50
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    51
# 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
    52
default_encoding = 'utf-8'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    53
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    54
# 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
    55
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    56
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    57
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
    58
    PY3 = False
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    59
    text_type = unicode
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    60
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    61
    def b(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    62
        return s
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    63
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    64
    def u(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    65
        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
    66
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    67
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    68
else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    69
    PY3 = True
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    70
    text_type = str
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    71
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    72
    def b(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    73
        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
    74
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    75
    def u(s):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    76
        return s
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    77
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    78
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    79
# }}}
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    80
# _pofile_or_mofile {{{
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    81
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    82
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    83
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
    84
    """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    85
    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
    86
    honor the DRY concept.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    87
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    88
    # get the file encoding
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    89
    enc = kwargs.get('encoding')
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    90
    if enc is None:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    91
        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
    92
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    93
    # parse the file
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    94
    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
    95
    parser = kls(
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
    96
        f,
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
    97
        encoding=enc,
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
    98
        check_for_duplicates=kwargs.get('check_for_duplicates', False),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
    99
        klass=kwargs.get('klass'),
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   100
    )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   101
    instance = parser.parse()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   102
    instance.wrapwidth = kwargs.get('wrapwidth', 78)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   103
    return instance
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   104
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   105
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   106
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   107
# _is_file {{{
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   108
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   109
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   110
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
   111
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   112
    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
   113
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   114
    Arguments:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   115
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   116
    ``filename_or_contents``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   117
        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
   118
        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
   119
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   120
    try:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   121
        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
   122
    except (ValueError, UnicodeEncodeError):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   123
        return False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   124
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   125
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   126
# }}}
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   127
# function pofile() {{{
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   128
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   129
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   130
def pofile(pofile, **kwargs):
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
    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
   133
    a :class:`~polib.POFile` instance.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   134
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   135
    Arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   136
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   137
    ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   138
        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
   139
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   140
    ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   141
        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
   142
        to xgettext (optional, default: ``78``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   143
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   144
    ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   145
        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
   146
        encoding will be auto-detected).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   147
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   148
    ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   149
        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
   150
        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
   151
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   152
    ``klass``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   153
        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
   154
        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
   155
        instance).
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   156
    """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   157
    return _pofile_or_mofile(pofile, 'pofile', **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   158
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   159
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   160
# }}}
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   161
# function mofile() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   162
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   163
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   164
def mofile(mofile, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   165
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   166
    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
   167
    :class:`~polib.MOFile` instance.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   168
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   169
    Arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   170
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   171
    ``mofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   172
        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
   173
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   174
    ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   175
        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
   176
        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
   177
        (optional, default: ``78``).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   178
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   179
    ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   180
        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
   181
        encoding will be auto-detected).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   182
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   183
    ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   184
        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
   185
        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
   186
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   187
    ``klass``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   188
        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
   189
        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
   190
        instance).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   191
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   192
    return _pofile_or_mofile(mofile, 'mofile', **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   193
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   194
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   195
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   196
# function detect_encoding() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   197
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   198
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   199
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
   200
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   201
    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
   202
    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
   203
    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
   204
    ``default_encoding``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   205
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   206
    Arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   207
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   208
    ``file``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   209
        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
   210
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   211
    ``binary_mode``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   212
        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
   213
    """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   214
    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
   215
    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
   216
    rxb = re.compile(b(PATTERN))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   217
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   218
    def charset_exists(charset):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   219
        """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
   220
        try:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   221
            codecs.lookup(charset)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   222
        except LookupError:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   223
            return False
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   224
        return True
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   225
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 _is_file(file):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   227
        match = rxt.search(file)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   228
        if match:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   229
            enc = match.group(1).strip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   230
            if charset_exists(enc):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   231
                return enc
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   232
    else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   233
        # 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
   234
        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
   235
            mode = 'rb'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   236
            rx = rxb
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   237
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   238
            mode = 'r'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   239
            rx = rxt
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   240
        f = open(file, mode)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   241
        for l in f.readlines():
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   242
            match = rx.search(l)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   243
            if match:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   244
                f.close()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   245
                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
   246
                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
   247
                    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
   248
                if charset_exists(enc):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   249
                    return enc
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   250
        f.close()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   251
    return default_encoding
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   252
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   253
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   254
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   255
# function escape() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   256
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   257
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   258
def escape(st):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   259
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   260
    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
   261
    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
   262
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   263
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   264
        st.replace('\\', r'\\')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   265
        .replace('\t', r'\t')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   266
        .replace('\r', r'\r')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   267
        .replace('\n', r'\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   268
        .replace('\"', r'\"')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   269
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   270
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   271
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   272
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   273
# function unescape() {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   274
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   275
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   276
def unescape(st):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   277
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   278
    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
   279
    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
   280
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   281
11388
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   282
    def unescape_repl(m):
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   283
        m = m.group(1)
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   284
        if m == 'n':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   285
            return '\n'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   286
        if m == 't':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   287
            return '\t'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   288
        if m == 'r':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   289
            return '\r'
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   290
        if m == '\\':
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   291
            return '\\'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   292
        return m  # handles escaped double quote
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   293
11388
db957a72fbd7 i18n: patch polib.unescape
Martin Geisler <mg@lazybytes.net>
parents: 11387
diff changeset
   294
    return re.sub(r'\\(\\|n|t|r|")', unescape_repl, st)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   295
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   296
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   297
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   298
# class _BaseFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   299
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   300
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   301
class _BaseFile(list):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   302
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   303
    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
   304
    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
   305
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   306
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   307
    def __init__(self, *args, **kwargs):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   308
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   309
        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
   310
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   311
        ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   312
            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
   313
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   314
        ``wrapwidth``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   315
            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
   316
            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
   317
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   318
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   319
            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
   320
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   321
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   322
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   323
            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
   324
            file, (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   325
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   326
        list.__init__(self)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   327
        # the opened file handle
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   328
        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
   329
        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
   330
            self.fpath = pofile
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   331
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   332
            self.fpath = kwargs.get('fpath')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   333
        # 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
   334
        self.wrapwidth = kwargs.get('wrapwidth', 78)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   335
        # the file encoding
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   336
        self.encoding = kwargs.get('encoding', default_encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   337
        # 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
   338
        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
   339
        # header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   340
        self.header = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   341
        # both po and mo files have metadata
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   342
        self.metadata = {}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   343
        self.metadata_is_fuzzy = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   344
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   345
    def __unicode__(self):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   346
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   347
        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
   348
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   349
        ret = []
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   350
        entries = [self.metadata_as_entry()] + [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   351
            e for e in self if not e.obsolete
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   352
        ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   353
        for entry in entries:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   354
            ret.append(entry.__unicode__(self.wrapwidth))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   355
        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
   356
            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
   357
        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
   358
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   359
        assert isinstance(ret, text_type)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   360
        # if type(ret) != text_type:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   361
        #    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
   362
        return ret
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
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
    if PY3:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   365
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   366
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   367
            return self.__unicode__()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   368
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   369
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   370
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   371
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   372
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   373
            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
   374
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   375
            return unicode(self).encode(self.encoding)
11387
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 __contains__(self, 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 ``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
   380
        not in).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   381
        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
   382
        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
   383
        msgctxt (or none for both entries).
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
        Argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   386
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   387
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   388
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   389
        """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   390
        return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   391
            self.find(entry.msgid, by='msgid', msgctxt=entry.msgctxt)
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   392
            is not None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   393
        )
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   394
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   395
    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
   396
        return str(self) == str(other)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   397
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   398
    def append(self, entry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   399
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   400
        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
   401
        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
   402
        ``ValueError`` exception.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   403
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   404
        Argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   405
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   406
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   407
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   408
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   409
        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
   410
            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
   411
        super(_BaseFile, self).append(entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   412
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   413
    def insert(self, index, entry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   414
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   415
        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
   416
        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
   417
        ``ValueError`` exception.
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
        Arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   420
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   421
        ``index``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   422
            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
   423
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   424
        ``entry``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   425
            an instance of :class:`~polib._BaseEntry`.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   426
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   427
        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
   428
            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
   429
        super(_BaseFile, self).insert(index, entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   430
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   431
    def metadata_as_entry(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   432
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   433
        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
   434
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   435
        e = POEntry(msgid='')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   436
        mdata = self.ordered_metadata()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   437
        if mdata:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   438
            strs = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   439
            for name, value in mdata:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   440
                # 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
   441
                strs.append('%s: %s' % (name, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   442
            e.msgstr = '\n'.join(strs) + '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   443
        if self.metadata_is_fuzzy:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   444
            e.flags.append('fuzzy')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   445
        return e
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   446
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   447
    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
   448
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   449
        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
   450
        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
   451
        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
   452
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   453
        Keyword arguments:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   454
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   455
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   456
            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
   457
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   458
        ``repr_method``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   459
            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
   460
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   461
        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
   462
            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
   463
        contents = getattr(self, repr_method)()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   464
        if fpath is None:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   465
            fpath = self.fpath
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   466
        if repr_method == 'to_binary':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   467
            fhandle = open(fpath, 'wb')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   468
        else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   469
            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
   470
            if not isinstance(contents, text_type):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   471
                contents = contents.decode(self.encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   472
        fhandle.write(contents)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   473
        fhandle.close()
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   474
        # 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
   475
        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
   476
            self.fpath = fpath
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   477
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   478
    def find(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   479
        self, st, by='msgid', include_obsolete_entries=False, msgctxt=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   480
    ):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   481
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   482
        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
   483
        argument) matches the string ``st``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   484
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   485
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   486
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   487
        ``st``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   488
            string, the string to search for.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   489
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   490
        ``by``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   491
            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
   492
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   493
        ``include_obsolete_entries``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   494
            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
   495
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   496
        ``msgctxt``
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   497
            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
   498
            search.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   499
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   500
        if include_obsolete_entries:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   501
            entries = self[:]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   502
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   503
            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
   504
        for e in entries:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   505
            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
   506
                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
   507
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   508
                return e
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   509
        return None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   510
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   511
    def ordered_metadata(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   512
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   513
        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
   514
        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
   515
        metadata_value).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   516
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   517
        # copy the dict first
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   518
        metadata = self.metadata.copy()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   519
        data_order = [
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   520
            'Project-Id-Version',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   521
            'Report-Msgid-Bugs-To',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   522
            'POT-Creation-Date',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   523
            'PO-Revision-Date',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   524
            'Last-Translator',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   525
            'Language-Team',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   526
            'MIME-Version',
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   527
            'Content-Type',
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   528
            'Content-Transfer-Encoding',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   529
            'Language',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   530
            'Plural-Forms',
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   531
        ]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   532
        ordered_data = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   533
        for data in data_order:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   534
            try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   535
                value = metadata.pop(data)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   536
                ordered_data.append((data, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   537
            except KeyError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   538
                pass
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   539
        # 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
   540
        # 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
   541
        for data in sorted(metadata.keys()):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   542
            value = metadata[data]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   543
            ordered_data.append((data, value))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   544
        return ordered_data
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   545
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   546
    def to_binary(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   547
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   548
        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
   549
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   550
        offsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   551
        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
   552
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   553
        # 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
   554
        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
   555
        mentry = self.metadata_as_entry()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   556
        # 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
   557
        entries = [mentry] + entries
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   558
        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
   559
        ids, strs = b(''), b('')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   560
        for e in entries:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   561
            # 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
   562
            # 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
   563
            msgid = b('')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   564
            if e.msgctxt:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   565
                # 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
   566
                # 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
   567
                msgid = self._encode(e.msgctxt + '\4')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   568
            if e.msgid_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   569
                msgstr = []
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   570
                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
   571
                    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
   572
                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
   573
                msgstr = self._encode('\0'.join(msgstr))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   574
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   575
                msgid += self._encode(e.msgid)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   576
                msgstr = self._encode(e.msgstr)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   577
            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
   578
            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
   579
            strs += msgstr + b('\0')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   580
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   581
        # 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
   582
        keystart = 7 * 4 + 16 * entries_len
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   583
        # and the values start after the keys
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   584
        valuestart = keystart + len(ids)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   585
        koffsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   586
        voffsets = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   587
        # 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
   588
        # 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
   589
        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
   590
            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
   591
            voffsets += [l2, o2 + valuestart]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   592
        offsets = koffsets + voffsets
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   593
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   594
        output = struct.pack(
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   595
            "Iiiiiii",
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   596
            # Magic number
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   597
            MOFile.MAGIC,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   598
            # Version
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   599
            0,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   600
            # number of entries
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   601
            entries_len,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   602
            # 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
   603
            7 * 4,
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   604
            # 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
   605
            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
   606
            # size and offset of hash table, we don't use hash tables
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   607
            0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   608
            keystart,
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   609
        )
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   610
        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
   611
            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
   612
        else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   613
            output += array.array("i", offsets).tostring()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   614
        output += ids
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   615
        output += strs
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   616
        return output
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   617
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   618
    def _encode(self, mixed):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   619
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   620
        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
   621
        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
   622
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   623
        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
   624
            mixed = mixed.encode(self.encoding)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   625
        return mixed
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   626
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   627
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
# class POFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   630
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   631
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   632
class POFile(_BaseFile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   633
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   634
    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
   635
    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
   636
    the python ``list`` type.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   637
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   638
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   639
    def __unicode__(self):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   640
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   641
        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
   642
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   643
        ret, headers = '', self.header.split('\n')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   644
        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
   645
            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
   646
                ret += "#\n"
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   647
            elif header[:1] in [',', ':']:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   648
                ret += '#%s\n' % header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   649
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   650
                ret += '# %s\n' % header
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   651
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   652
        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
   653
            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
   654
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   655
        return ret + _BaseFile.__unicode__(self)
11387
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 save_as_mofile(self, fpath):
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
        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
   660
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   661
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   662
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   663
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   664
            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
   665
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   666
        _BaseFile.save(self, fpath, 'to_binary')
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
    def percent_translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   669
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   670
        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
   671
        messages.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   672
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   673
        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
   674
        if total == 0:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   675
            return 100
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   676
        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
   677
        return int(translated * 100 / float(total))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   678
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   679
    def translated_entries(self):
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
        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
   682
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   683
        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
   684
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   685
    def untranslated_entries(self):
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
        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
   688
        """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   689
        return [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   690
            e
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   691
            for e in self
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   692
            if not e.translated() and not e.obsolete and not 'fuzzy' in e.flags
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   693
        ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   694
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   695
    def fuzzy_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   696
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   697
        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
   698
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   699
        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
   700
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   701
    def obsolete_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   702
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   703
        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
   704
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   705
        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
   706
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   707
    def merge(self, refpot):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   708
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   709
        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
   710
        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
   711
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   712
        * 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
   713
          occurrences will be discarded;
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   714
        * 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
   715
          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
   716
        * the fuzzy flags are preserved.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   717
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   718
        Keyword argument:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   719
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   720
        ``refpot``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   721
            object POFile, the reference catalog.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   722
        """
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   723
        # Store entries in dict/set for faster access
44452
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43977
diff changeset
   724
        self_entries = {entry.msgid: entry for entry in self}
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43977
diff changeset
   725
        refpot_msgids = {entry.msgid 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
   726
        # 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
   727
        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
   728
            e = self_entries.get(entry.msgid)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   729
            if e is None:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   730
                e = POEntry()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   731
                self.append(e)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   732
            e.merge(entry)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   733
        # 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
   734
        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
   735
            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
   736
                entry.obsolete = True
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   737
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   738
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   739
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   740
# class MOFile {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   741
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   742
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   743
class MOFile(_BaseFile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   744
    """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   745
    Mo file reader/writer.
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   746
    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
   747
    extension, the python ``list`` type.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   748
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   749
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   750
    MAGIC = 0x950412DE
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   751
    MAGIC_SWAPPED = 0xDE120495
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   752
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   753
    def __init__(self, *args, **kwargs):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   754
        """
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   755
        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
   756
        :class:`~polib._BaseFile` class.
11387
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
        _BaseFile.__init__(self, *args, **kwargs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   759
        self.magic_number = None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   760
        self.version = 0
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 save_as_pofile(self, fpath):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   763
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   764
        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
   765
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   766
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   767
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   768
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   769
            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
   770
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   771
        _BaseFile.save(self, fpath)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   772
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   773
    def save(self, fpath=None):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   774
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   775
        Saves the mofile to ``fpath``.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   776
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   777
        Keyword argument:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   778
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   779
        ``fpath``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   780
            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
   781
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   782
        _BaseFile.save(self, fpath, 'to_binary')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   783
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   784
    def percent_translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   785
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   786
        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
   787
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   788
        return 100
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   789
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   790
    def translated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   791
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   792
        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
   793
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   794
        return self
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   795
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   796
    def untranslated_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   797
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   798
        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
   799
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   800
        return []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   801
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   802
    def fuzzy_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   803
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   804
        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
   805
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   806
        return []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   807
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   808
    def obsolete_entries(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   809
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   810
        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
   811
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   812
        return []
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   813
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   814
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   815
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   816
# class _BaseEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   817
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   818
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   819
class _BaseEntry:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   820
    """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   821
    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
   822
    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
   823
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   824
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   825
    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
   826
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   827
        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
   828
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   829
        ``msgid``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   830
            string, the entry msgid.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   831
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   832
        ``msgstr``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   833
            string, the entry msgstr.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   834
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   835
        ``msgid_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   836
            string, the entry msgid_plural.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   837
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   838
        ``msgstr_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   839
            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
   840
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   841
        ``msgctxt``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   842
            string, the entry context (msgctxt).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   843
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   844
        ``obsolete``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   845
            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
   846
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   847
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   848
            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
   849
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   850
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   851
        self.msgid = kwargs.get('msgid', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   852
        self.msgstr = kwargs.get('msgstr', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   853
        self.msgid_plural = kwargs.get('msgid_plural', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   854
        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
   855
        self.msgctxt = kwargs.get('msgctxt', None)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   856
        self.obsolete = kwargs.get('obsolete', False)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   857
        self.encoding = kwargs.get('encoding', default_encoding)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   858
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   859
    def __unicode__(self, wrapwidth=78):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   860
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   861
        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
   862
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   863
        if self.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   864
            delflag = '#~ '
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   865
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   866
            delflag = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   867
        ret = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   868
        # write the msgctxt if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   869
        if self.msgctxt is not None:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   870
            ret += self._str_field(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   871
                "msgctxt", delflag, "", self.msgctxt, wrapwidth
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   872
            )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   873
        # write the msgid
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   874
        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
   875
        # write the msgid_plural if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   876
        if self.msgid_plural:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   877
            ret += self._str_field(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   878
                "msgid_plural", delflag, "", self.msgid_plural, wrapwidth
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   879
            )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   880
        if self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   881
            # write the msgstr_plural if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   882
            msgstrs = self.msgstr_plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   883
            keys = list(msgstrs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   884
            keys.sort()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   885
            for index in keys:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   886
                msgstr = msgstrs[index]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   887
                plural_index = '[%s]' % index
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   888
                ret += self._str_field(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   889
                    "msgstr", delflag, plural_index, msgstr, wrapwidth
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   890
                )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   891
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   892
            # otherwise write the msgstr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   893
            ret += self._str_field(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   894
                "msgstr", delflag, "", self.msgstr, wrapwidth
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   895
            )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   896
        ret.append('')
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   897
        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
   898
        return ret
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   899
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   900
    if PY3:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   901
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   902
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   903
            return self.__unicode__()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   904
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   905
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   906
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   907
        def __str__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   908
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   909
            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
   910
            """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   911
            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
   912
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   913
    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
   914
        return str(self) == str(other)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   915
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   916
    def _str_field(self, fieldname, delflag, plural_index, field, wrapwidth=78):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   917
        lines = field.splitlines(True)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   918
        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
   919
            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
   920
        else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   921
            escaped_field = escape(field)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   922
            specialchars_count = 0
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   923
            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
   924
                specialchars_count += field.count(c)
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
   925
            # 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
   926
            # + 2 quotes (eg. msgid "<string>")
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   927
            flength = len(fieldname) + 3
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   928
            if plural_index:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   929
                flength += len(plural_index)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   930
            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
   931
            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
   932
                # Wrap the line but take field name into account
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   933
                lines = [''] + [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   934
                    unescape(item)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   935
                    for item in wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   936
                        escaped_field,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   937
                        wrapwidth - 2,  # 2 for quotes ""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   938
                        drop_whitespace=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   939
                        break_long_words=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   940
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   941
                ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   942
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   943
                lines = [field]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   944
        if fieldname.startswith('previous_'):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   945
            # 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
   946
            fieldname = fieldname[9:]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   947
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   948
        ret = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   949
            '%s%s%s "%s"'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   950
            % (delflag, fieldname, plural_index, escape(lines.pop(0)))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   951
        ]
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   952
        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
   953
            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
   954
        return ret
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   955
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
   956
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   957
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   958
# class POEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   959
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   960
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   961
class POEntry(_BaseEntry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   962
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   963
    Represents a po file entry.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   964
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   965
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   966
    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
   967
        """
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   968
        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
   969
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   970
        ``comment``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   971
            string, the entry comment.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   972
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   973
        ``tcomment``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   974
            string, the entry translator comment.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   975
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   976
        ``occurrences``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   977
            list, the entry occurrences.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   978
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   979
        ``flags``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   980
            list, the entry flags.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   981
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   982
        ``previous_msgctxt``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   983
            string, the entry previous context.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   984
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   985
        ``previous_msgid``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   986
            string, the entry previous msgid.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   987
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   988
        ``previous_msgid_plural``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
   989
            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
   990
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   991
        ``linenum``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
   992
            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
   993
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   994
        _BaseEntry.__init__(self, *args, **kwargs)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   995
        self.comment = kwargs.get('comment', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   996
        self.tcomment = kwargs.get('tcomment', '')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   997
        self.occurrences = kwargs.get('occurrences', [])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   998
        self.flags = kwargs.get('flags', [])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
   999
        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
  1000
        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
  1001
        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
  1002
        self.linenum = kwargs.get('linenum', None)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1003
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1004
    def __unicode__(self, wrapwidth=78):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1005
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1006
        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
  1007
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1008
        if self.obsolete:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1009
            return _BaseEntry.__unicode__(self, wrapwidth)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1010
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1011
        ret = []
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1012
        # 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
  1013
        comments = [('comment', '#. '), ('tcomment', '# ')]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1014
        for c in comments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1015
            val = getattr(self, c[0])
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1016
            if val:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1017
                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
  1018
                    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
  1019
                        ret += wrap(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1020
                            comment,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1021
                            wrapwidth,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1022
                            initial_indent=c[1],
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1023
                            subsequent_indent=c[1],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1024
                            break_long_words=False,
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1025
                        )
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1026
                    else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1027
                        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
  1028
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1029
        # occurrences (with text wrapping as xgettext does)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1030
        if self.occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1031
            filelist = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1032
            for fpath, lineno in self.occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1033
                if lineno:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1034
                    filelist.append('%s:%s' % (fpath, lineno))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1035
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1036
                    filelist.append(fpath)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1037
            filestr = ' '.join(filelist)
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1038
            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
  1039
                # 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
  1040
                # 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
  1041
                # 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
  1042
                # contain, like "*"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1043
                ret += [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1044
                    l.replace('*', '-')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1045
                    for l in wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1046
                        filestr.replace('-', '*'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1047
                        wrapwidth,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1048
                        initial_indent='#: ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1049
                        subsequent_indent='#: ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1050
                        break_long_words=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1051
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1052
                ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1053
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1054
                ret.append('#: ' + filestr)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1055
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1056
        # flags (TODO: wrapping ?)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1057
        if self.flags:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1058
            ret.append('#, %s' % ', '.join(self.flags))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1059
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1060
        # previous context and previous msgid/msgid_plural
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1061
        fields = ['previous_msgctxt', 'previous_msgid', 'previous_msgid_plural']
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1062
        for f in fields:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1063
            val = getattr(self, f)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1064
            if val:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1065
                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
  1066
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1067
        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
  1068
        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
  1069
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1070
        assert isinstance(ret, text_type)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1071
        # if type(ret) != types.UnicodeType:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1072
        #    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
  1073
        return ret
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1074
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1075
    def __cmp__(self, other):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1076
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1077
        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
  1078
        """
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1079
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1080
        # First: Obsolete test
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1081
        if self.obsolete != other.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1082
            if self.obsolete:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1083
                return -1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1084
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1085
                return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1086
        # 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
  1087
        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
  1088
        occ2 = sorted(other.occurrences[:])
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1089
        pos = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1090
        for entry1 in occ1:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1091
            try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1092
                entry2 = occ2[pos]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1093
            except IndexError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1094
                return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1095
            pos = pos + 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1096
            if entry1[0] != entry2[0]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1097
                if entry1[0] > entry2[0]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1098
                    return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1099
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1100
                    return -1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1101
            if entry1[1] != entry2[1]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1102
                if entry1[1] > entry2[1]:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1103
                    return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1104
                else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1105
                    return -1
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1106
        # 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
  1107
        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
  1108
            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
  1109
                return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1110
            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
  1111
                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
  1112
                    return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1113
                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
  1114
                    return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1115
                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
  1116
                    return -1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1117
        # 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
  1118
        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
  1119
            return 1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1120
        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
  1121
            return -1
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1122
        return 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1123
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1124
    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
  1125
        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
  1126
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1127
    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
  1128
        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
  1129
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1130
    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
  1131
        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
  1132
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1133
    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
  1134
        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
  1135
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1136
    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
  1137
        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
  1138
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1139
    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
  1140
        return self.__cmp__(other) != 0
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1141
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1142
    def translated(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1143
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1144
        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
  1145
        otherwise.
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1146
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1147
        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
  1148
            return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1149
        if self.msgstr != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1150
            return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1151
        if self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1152
            for pos in self.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1153
                if self.msgstr_plural[pos] == '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1154
                    return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1155
            return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1156
        return False
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1157
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1158
    def merge(self, other):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1159
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1160
        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
  1161
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1162
        self.msgid = other.msgid
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1163
        self.msgctxt = other.msgctxt
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1164
        self.occurrences = other.occurrences
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1165
        self.comment = other.comment
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1166
        fuzzy = 'fuzzy' in self.flags
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1167
        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
  1168
        if fuzzy:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1169
            self.flags.append('fuzzy')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1170
        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
  1171
        self.obsolete = other.obsolete
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1172
        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
  1173
        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
  1174
        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
  1175
        if other.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1176
            for pos in other.msgstr_plural:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1177
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1178
                    # keep existing translation at pos if any
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1179
                    self.msgstr_plural[pos]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1180
                except KeyError:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1181
                    self.msgstr_plural[pos] = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1182
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1183
    def __hash__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1184
        return hash((self.msgid, self.msgstr))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1185
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1186
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1187
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1188
# class MOEntry {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1189
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1190
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1191
class MOEntry(_BaseEntry):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1192
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1193
    Represents a mo file entry.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1194
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1195
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1196
    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
  1197
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1198
        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
  1199
        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
  1200
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1201
        ``comment``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1202
        ``tcomment``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1203
        ``occurrences``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1204
        ``flags``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1205
        ``previous_msgctxt``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1206
        ``previous_msgid``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1207
        ``previous_msgid_plural``
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1208
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1209
        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
  1210
        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
  1211
        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
  1212
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1213
        _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
  1214
        self.comment = ''
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1215
        self.tcomment = ''
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1216
        self.occurrences = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1217
        self.flags = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1218
        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
  1219
        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
  1220
        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
  1221
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1222
    def __hash__(self):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1223
        return hash((self.msgid, self.msgstr))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1224
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1225
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1226
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1227
# class _POFileParser {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1228
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1229
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
  1230
class _POFileParser:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1231
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1232
    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
  1233
    file format.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1234
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1235
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1236
    def __init__(self, pofile, *args, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1237
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1238
        Constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1239
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1240
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1241
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1242
        ``pofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1243
            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
  1244
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1245
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1246
            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
  1247
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1248
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1249
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1250
            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
  1251
            file (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1252
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1253
        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
  1254
        if _is_file(pofile):
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1255
            try:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1256
                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
  1257
            except LookupError:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1258
                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
  1259
                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
  1260
        else:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1261
            self.fhandle = pofile.splitlines()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1262
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1263
        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
  1264
        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
  1265
            klass = POFile
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1266
        self.instance = klass(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1267
            pofile=pofile,
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1268
            encoding=enc,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1269
            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
  1270
        )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1271
        self.transitions = {}
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1272
        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
  1273
        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
  1274
        self.current_state = 'st'
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1275
        self.current_token = None
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1276
        # two memo flags used in handlers
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1277
        self.msgstr_index = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1278
        self.entry_obsolete = 0
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1279
        # Configure the state machine, by adding transitions.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1280
        # Signification of symbols:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1281
        #     * ST: Beginning of the file (start)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1282
        #     * HE: Header
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1283
        #     * TC: a translation comment
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1284
        #     * 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
  1285
        #     * OC: a file/line occurrence
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1286
        #     * FL: a flags line
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1287
        #     * CT: a message context
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1288
        #     * PC: a previous msgctxt
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1289
        #     * PM: a previous msgid
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1290
        #     * PP: a previous msgid_plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1291
        #     * MI: a msgid
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1292
        #     * MP: a msgid plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1293
        #     * MS: a msgstr
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1294
        #     * MX: a msgstr plural
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1295
        #     * MC: a msgid or msgstr continuation line
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1296
        all = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1297
            'st',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1298
            'he',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1299
            'gc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1300
            'oc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1301
            'fl',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1302
            'ct',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1303
            'pc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1304
            'pm',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1305
            'pp',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1306
            'tc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1307
            'ms',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1308
            'mp',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1309
            'mx',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1310
            'mi',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1311
        ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1312
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1313
        self.add('tc', ['st', 'he'], 'he')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1314
        self.add(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1315
            'tc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1316
            ['gc', 'oc', 'fl', 'tc', 'pc', 'pm', 'pp', 'ms', 'mp', 'mx', 'mi'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1317
            'tc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1318
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1319
        self.add('gc', all, 'gc')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1320
        self.add('oc', all, 'oc')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1321
        self.add('fl', all, 'fl')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1322
        self.add('pc', all, 'pc')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1323
        self.add('pm', all, 'pm')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1324
        self.add('pp', all, 'pp')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1325
        self.add(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1326
            'ct',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1327
            ['st', 'he', 'gc', 'oc', 'fl', 'tc', 'pc', 'pm', 'pp', 'ms', 'mx'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1328
            'ct',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1329
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1330
        self.add(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1331
            'mi',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1332
            [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1333
                'st',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1334
                'he',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1335
                'gc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1336
                'oc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1337
                'fl',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1338
                'ct',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1339
                'tc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1340
                'pc',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1341
                'pm',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1342
                'pp',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1343
                'ms',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1344
                'mx',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1345
            ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1346
            'mi',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1347
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1348
        self.add('mp', ['tc', 'gc', 'pc', 'pm', 'pp', 'mi'], 'mp')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1349
        self.add('ms', ['mi', 'mp', 'tc'], 'ms')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1350
        self.add('mx', ['mi', 'mx', 'mp', 'tc'], 'mx')
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1351
        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
  1352
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1353
    def parse(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1354
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1355
        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
  1356
        with the current matched symbol.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1357
        """
15290
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
        keywords = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1360
            'msgctxt': 'ct',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1361
            'msgid': 'mi',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1362
            'msgstr': 'ms',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1363
            'msgid_plural': 'mp',
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1364
        }
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1365
        prev_keywords = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1366
            'msgid_plural': 'pp',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1367
            'msgid': 'pm',
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1368
            'msgctxt': 'pc',
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1369
        }
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1370
        tokens = []
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1371
        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
  1372
            self.current_line += 1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1373
            line = line.strip()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1374
            if line == '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1375
                continue
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1376
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1377
            tokens = line.split(None, 2)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1378
            nb_tokens = len(tokens)
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1379
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1380
            if tokens[0] == '#~|':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1381
                continue
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1382
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1383
            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
  1384
                line = line[3:].strip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1385
                tokens = tokens[1:]
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1386
                nb_tokens -= 1
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1387
                self.entry_obsolete = 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1388
            else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1389
                self.entry_obsolete = 0
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1390
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1391
            # Take care of keywords like
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1392
            # msgid, msgid_plural, msgctxt & msgstr.
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1393
            if tokens[0] in keywords and nb_tokens > 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1394
                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
  1395
                if re.search(r'([^\\]|^)"', line[1:-1]):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1396
                    raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1397
                        'Syntax error in po file %s (line %s): '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1398
                        'unescaped double quote found'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1399
                        % (self.instance.fpath, self.current_line)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1400
                    )
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1401
                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
  1402
                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
  1403
                continue
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1404
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1405
            self.current_token = line
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1406
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1407
            if tokens[0] == '#:':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1408
                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
  1409
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1410
                # 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
  1411
                self.process('oc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1412
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1413
            elif line[:1] == '"':
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1414
                # 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
  1415
                if re.search(r'([^\\]|^)"', line[1:-1]):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1416
                    raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1417
                        'Syntax error in po file %s (line %s): '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1418
                        'unescaped double quote found'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1419
                        % (self.instance.fpath, self.current_line)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1420
                    )
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1421
                self.process('mc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1422
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1423
            elif line[:7] == 'msgstr[':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1424
                # 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
  1425
                self.process('mx')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1426
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1427
            elif tokens[0] == '#,':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1428
                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
  1429
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1430
                # 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
  1431
                self.process('fl')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1432
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1433
            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
  1434
                if line == '#':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1435
                    line += ' '
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1436
                # 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
  1437
                self.process('tc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1438
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1439
            elif tokens[0] == '#.':
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1440
                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
  1441
                    continue
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1442
                # 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
  1443
                self.process('gc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1444
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1445
            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
  1446
                if nb_tokens <= 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1447
                    raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1448
                        'Syntax error in po file %s (line %s)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1449
                        % (self.instance.fpath, self.current_line)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1450
                    )
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1451
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1452
                # 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
  1453
                line = line[2:].lstrip()
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1454
                self.current_token = line
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1455
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1456
                if tokens[1].startswith('"'):
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1457
                    # 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
  1458
                    self.process('mc')
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1459
                    continue
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1460
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1461
                if nb_tokens == 2:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1462
                    # Invalid continuation line.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1463
                    raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1464
                        'Syntax error in po file %s (line %s): '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1465
                        'invalid continuation line'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1466
                        % (self.instance.fpath, self.current_line)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1467
                    )
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1468
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1469
                # 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
  1470
                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
  1471
                    # Unknown keyword in previous translation comment.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1472
                    raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1473
                        'Syntax error in po file %s (line %s): '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1474
                        'unknown keyword %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1475
                        % (self.instance.fpath, self.current_line, tokens[1])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1476
                    )
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1477
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1478
                # 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
  1479
                # between it and the starting quote.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1480
                line = line[len(tokens[1]) :].lstrip()
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1481
                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
  1482
                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
  1483
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1484
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1485
                raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1486
                    'Syntax error in po file %s (line %s)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1487
                    % (self.instance.fpath, self.current_line)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1488
                )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1489
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1490
        if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1491
            self.current_entry
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1492
            and len(tokens) > 0
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1493
            and not tokens[0].startswith('#')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1494
        ):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1495
            # 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
  1496
            # 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
  1497
            # are ignored
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1498
            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
  1499
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
  1500
        # 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
  1501
        # 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
  1502
        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
  1503
        if metadataentry:  # metadata found
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1504
            # 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
  1505
            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
  1506
            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
  1507
            key = None
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1508
            for msg in metadataentry.msgstr.splitlines():
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1509
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1510
                    key, val = msg.split(':', 1)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1511
                    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
  1512
                except (ValueError, KeyError):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1513
                    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
  1514
                        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
  1515
        # 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
  1516
        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
  1517
            self.fhandle.close()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1518
        return self.instance
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1519
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1520
    def add(self, symbol, states, next_state):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1521
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1522
        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
  1523
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1524
        Keywords arguments:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1525
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1526
        ``symbol``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1527
            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
  1528
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1529
        ``states``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1530
            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
  1531
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1532
        ``next_state``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1533
            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
  1534
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1535
        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
  1536
            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
  1537
            self.transitions[(symbol, state)] = (action, next_state)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1538
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1539
    def process(self, symbol):
11387
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
        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
  1542
        symbol provided.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1543
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1544
        Keywords arguments:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1545
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1546
        ``symbol``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1547
            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
  1548
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1549
        ``linenum``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1550
            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
  1551
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1552
        try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1553
            (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
  1554
            if action():
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1555
                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
  1556
        except Exception:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1557
            raise IOError(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1558
                'Syntax error in po file (line %s)' % self.current_line
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1559
            )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1560
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1561
    # state handlers
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_he(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1564
        """Handle a header comment."""
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1565
        if self.instance.header != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1566
            self.instance.header += '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1567
        self.instance.header += self.current_token[2:]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1568
        return 1
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1569
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1570
    def handle_tc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1571
        """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
  1572
        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
  1573
            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
  1574
            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
  1575
        if self.current_entry.tcomment != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1576
            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
  1577
        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
  1578
        if tcomment.startswith(' '):
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1579
            tcomment = tcomment[1:]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1580
        self.current_entry.tcomment += tcomment
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1581
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1582
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1583
    def handle_gc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1584
        """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
  1585
        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
  1586
            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
  1587
            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
  1588
        if self.current_entry.comment != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1589
            self.current_entry.comment += '\n'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1590
        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
  1591
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1592
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1593
    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
  1594
        """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
  1595
        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
  1596
            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
  1597
            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
  1598
        occurrences = self.current_token[3:].split()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1599
        for occurrence in occurrences:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1600
            if occurrence != '':
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1601
                try:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1602
                    fil, line = occurrence.split(':')
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1603
                    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
  1604
                        fil = fil + line
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1605
                        line = ''
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1606
                    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
  1607
                except (ValueError, AttributeError):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1608
                    self.current_entry.occurrences.append((occurrence, ''))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1609
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1610
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1611
    def handle_fl(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1612
        """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
  1613
        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
  1614
            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
  1615
            self.current_entry = POEntry(linenum=self.current_line)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1616
        self.current_entry.flags += [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1617
            c.strip() for c in self.current_token[3:].split(',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1618
        ]
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1619
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1620
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1621
    def handle_pp(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1622
        """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
  1623
        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
  1624
            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
  1625
            self.current_entry = POEntry(linenum=self.current_line)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1626
        self.current_entry.previous_msgid_plural = unescape(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1627
            self.current_token[1:-1]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1628
        )
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1629
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1630
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1631
    def handle_pm(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1632
        """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
  1633
        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
  1634
            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
  1635
            self.current_entry = POEntry(linenum=self.current_line)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1636
        self.current_entry.previous_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
  1637
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1638
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1639
    def handle_pc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1640
        """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
  1641
        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
  1642
            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
  1643
            self.current_entry = POEntry(linenum=self.current_line)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1644
        self.current_entry.previous_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
  1645
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1646
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1647
    def handle_ct(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1648
        """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
  1649
        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
  1650
            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
  1651
            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
  1652
        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
  1653
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1654
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1655
    def handle_mi(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1656
        """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
  1657
        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
  1658
            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
  1659
            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
  1660
        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
  1661
        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
  1662
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1663
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1664
    def handle_mp(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1665
        """Handle a msgid plural."""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1666
        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
  1667
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1668
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1669
    def handle_ms(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1670
        """Handle a msgstr."""
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1671
        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
  1672
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1673
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1674
    def handle_mx(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1675
        """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
  1676
        index = self.current_token[7]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1677
        value = self.current_token[self.current_token.find('"') + 1 : -1]
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1678
        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
  1679
        self.msgstr_index = int(index)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1680
        return True
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1681
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1682
    def handle_mc(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1683
        """Handle a msgid or msgstr continuation line."""
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1684
        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
  1685
        if self.current_state == 'ct':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1686
            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
  1687
        elif self.current_state == 'mi':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1688
            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
  1689
        elif self.current_state == 'mp':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1690
            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
  1691
        elif self.current_state == 'ms':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1692
            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
  1693
        elif self.current_state == 'mx':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1694
            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
  1695
        elif self.current_state == 'pp':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1696
            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
  1697
        elif self.current_state == 'pm':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1698
            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
  1699
        elif self.current_state == 'pc':
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1700
            self.current_entry.previous_msgctxt += token
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1701
        # don't change the current state
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1702
        return False
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1703
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1704
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1705
# }}}
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1706
# class _MOFileParser {{{
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1707
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1708
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
  1709
class _MOFileParser:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1710
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1711
    A class to parse binary mo files.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1712
    """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1713
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1714
    def __init__(self, mofile, *args, **kwargs):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1715
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1716
        Constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1717
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1718
        Keyword arguments:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1719
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1720
        ``mofile``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1721
            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
  1722
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1723
        ``encoding``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1724
            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
  1725
            global variable (optional).
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1726
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1727
        ``check_for_duplicates``
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1728
            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
  1729
            file (optional, default: ``False``).
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1730
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1731
        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
  1732
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1733
        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
  1734
        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
  1735
            klass = MOFile
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1736
        self.instance = klass(
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1737
            fpath=mofile,
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1738
            encoding=kwargs.get('encoding', default_encoding),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1739
            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
  1740
        )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1741
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1742
    def __del__(self):
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
        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
  1745
        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
  1746
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1747
        if self.fhandle:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1748
            self.fhandle.close()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1749
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1750
    def parse(self):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1751
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1752
        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
  1753
        constructor.
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1754
        """
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1755
        # parse magic number
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1756
        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
  1757
        if magic_number == MOFile.MAGIC:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1758
            ii = '<II'
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1759
        elif magic_number == MOFile.MAGIC_SWAPPED:
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1760
            ii = '>II'
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1761
        else:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1762
            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
  1763
        self.instance.magic_number = magic_number
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1764
        # 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
  1765
        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
  1766
        # 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
  1767
        # 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
  1768
        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
  1769
            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
  1770
        self.instance.version = version
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1771
        # 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
  1772
        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
  1773
        # 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
  1774
        self.fhandle.seek(msgids_hash_offset)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1775
        msgids_index = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1776
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1777
            msgids_index.append(self._readbinary(ii, 8))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1778
        # 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
  1779
        self.fhandle.seek(msgstrs_hash_offset)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1780
        msgstrs_index = []
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1781
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1782
            msgstrs_index.append(self._readbinary(ii, 8))
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1783
        # build entries
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1784
        encoding = self.instance.encoding
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1785
        for i in range(numofstrings):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1786
            self.fhandle.seek(msgids_index[i][1])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1787
            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
  1788
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1789
            self.fhandle.seek(msgstrs_index[i][1])
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1790
            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
  1791
            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
  1792
                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
  1793
                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
  1794
                    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
  1795
                    if tokens[0] != b(''):
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1796
                        try:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1797
                            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
  1798
                            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
  1799
                            metadata[k] = v.strip()
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1800
                        except IndexError:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1801
                            metadata[k] = u('')
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1802
                self.instance.metadata = metadata
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1803
                continue
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1804
            # 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
  1805
            msgid_tokens = msgid.split(b('\0'))
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1806
            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
  1807
                entry = self._build_entry(
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1808
                    msgid=msgid_tokens[0],
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1809
                    msgid_plural=msgid_tokens[1],
44452
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43977
diff changeset
  1810
                    msgstr_plural={
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43977
diff changeset
  1811
                        k: v for k, v in enumerate(msgstr.split(b('\0')))
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43977
diff changeset
  1812
                    },
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1813
                )
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1814
            else:
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1815
                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
  1816
            self.instance.append(entry)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1817
        # close opened file
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1818
        self.fhandle.close()
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1819
        return self.instance
19023
86531a7038ed codingstyle: remove trailing spaces in various text files
Mads Kiilerich <madski@unity3d.com>
parents: 15290
diff changeset
  1820
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1821
    def _build_entry(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1822
        self, msgid, msgstr=None, msgid_plural=None, msgstr_plural=None
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1823
    ):
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1824
        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
  1825
        encoding = self.instance.encoding
15290
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1826
        if len(msgctxt_msgid) > 1:
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1827
            kwargs = {
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1828
                '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
  1829
                '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
  1830
            }
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1831
        else:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1832
            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
  1833
        if msgstr:
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1834
            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
  1835
        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
  1836
            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
  1837
        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
  1838
            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
  1839
                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
  1840
            kwargs['msgstr_plural'] = msgstr_plural
e40430fb787b i18n: import polib 0.6.4 (rev 84598f2b5365)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13030
diff changeset
  1841
        return MOEntry(**kwargs)
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1842
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1843
    def _readbinary(self, fmt, numbytes):
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1844
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1845
        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
  1846
        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
  1847
        """
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1848
        bytes = self.fhandle.read(numbytes)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1849
        tup = struct.unpack(fmt, bytes)
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1850
        if len(tup) == 1:
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1851
            return tup[0]
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1852
        return tup
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1853
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1854
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1855
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1856
# class TextWrapper {{{
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1857
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1858
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1859
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
  1860
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1861
    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
  1862
    drop_whitespace option.
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1863
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1864
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1865
    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
  1866
        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
  1867
        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
  1868
        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
  1869
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1870
    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
  1871
        """_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
  1872
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1873
        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
  1874
        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
  1875
        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
  1876
        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
  1877
        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
  1878
        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
  1879
        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
  1880
        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
  1881
        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
  1882
        """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1883
        lines = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1884
        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
  1885
            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
  1886
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1887
        # 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
  1888
        # 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
  1889
        chunks.reverse()
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1890
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1891
        while chunks:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1892
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1893
            # 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
  1894
            # 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
  1895
            cur_line = []
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1896
            cur_len = 0
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1897
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1898
            # 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
  1899
            if lines:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1900
                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
  1901
            else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1902
                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
  1903
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1904
            # 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
  1905
            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
  1906
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1907
            # 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
  1908
            # 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
  1909
            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
  1910
                del chunks[-1]
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1911
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1912
            while chunks:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1913
                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
  1914
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1915
                # 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
  1916
                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
  1917
                    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
  1918
                    cur_len += l
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1919
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1920
                # 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
  1921
                else:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1922
                    break
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1923
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1924
            # 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
  1925
            # 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
  1926
            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
  1927
                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
  1928
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1929
            # 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
  1930
            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
  1931
                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
  1932
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1933
            # 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
  1934
            # 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
  1935
            if cur_line:
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1936
                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
  1937
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1938
        return lines
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1939
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1940
40185
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1941
# }}}
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1942
# function wrap() {{{
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1943
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1944
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1945
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
  1946
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1947
    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
  1948
    """
19fc5a986669 polib: update to latest release 1.0.7 (upstream rev d75ce6dbbc2a)
Augie Fackler <augie@google.com>
parents: 32889
diff changeset
  1949
    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
  1950
        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
  1951
    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
  1952
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40185
diff changeset
  1953
11387
181936ec9bfb i18n: import polib 0.5.2 (rev b0ab301cfdcc)
Wagner Bruna <wbruna@yahoo.com>
parents:
diff changeset
  1954
# }}}