annotate i18n/polib.py @ 45084:07b3166e94ca

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