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