annotate i18n/polib.py @ 51740:7226f2626fb1

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