Mercurial > babel > old > babel-test
annotate babel/messages/pofile.py @ 531:f642c81ce8cc
cleanup: remove unused imports
author | fschwarz |
---|---|
date | Sat, 05 Mar 2011 16:40:24 +0000 |
parents | 85e1beadacb0 |
children | 030ddf3f5b13 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Reading and writing of files in the ``gettext`` PO (portable object) | |
15 format. | |
16 | |
17 :see: `The Format of PO Files | |
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_ | |
19 """ | |
20 | |
531 | 21 from datetime import datetime |
134 | 22 import os |
1 | 23 import re |
24 | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
25 from babel.messages.catalog import Catalog, Message |
531 | 26 from babel.util import wraptext |
1 | 27 |
178
4407c6d1b7d7
Minor change to what symbols are ?exported?, primarily for the generated docs.
cmlenz
parents:
175
diff
changeset
|
28 __all__ = ['read_po', 'write_po'] |
161 | 29 __docformat__ = 'restructuredtext en' |
158 | 30 |
31 def unescape(string): | |
32 r"""Reverse `escape` the given string. | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
33 |
158 | 34 >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"') |
35 Say: | |
36 "hello, world!" | |
37 <BLANKLINE> | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
38 |
158 | 39 :param string: the string to unescape |
40 :return: the unescaped string | |
41 :rtype: `str` or `unicode` | |
42 """ | |
43 return string[1:-1].replace('\\\\', '\\') \ | |
44 .replace('\\t', '\t') \ | |
45 .replace('\\r', '\r') \ | |
46 .replace('\\n', '\n') \ | |
47 .replace('\\"', '\"') | |
48 | |
49 def denormalize(string): | |
50 r"""Reverse the normalization done by the `normalize` function. | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
51 |
158 | 52 >>> print denormalize(r'''"" |
53 ... "Say:\n" | |
54 ... " \"hello, world!\"\n"''') | |
55 Say: | |
56 "hello, world!" | |
57 <BLANKLINE> | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
58 |
158 | 59 >>> print denormalize(r'''"" |
60 ... "Say:\n" | |
61 ... " \"Lorem ipsum dolor sit " | |
62 ... "amet, consectetur adipisicing" | |
63 ... " elit, \"\n"''') | |
64 Say: | |
65 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " | |
66 <BLANKLINE> | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
67 |
158 | 68 :param string: the string to denormalize |
69 :return: the denormalized string | |
70 :rtype: `unicode` or `str` | |
71 """ | |
72 if string.startswith('""'): | |
73 lines = [] | |
74 for line in string.splitlines()[1:]: | |
75 lines.append(unescape(line)) | |
76 return ''.join(lines) | |
77 else: | |
78 return unescape(string) | |
1 | 79 |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
80 def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False): |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
81 """Read messages from a ``gettext`` PO (portable object) file from the given |
64 | 82 file-like object and return a `Catalog`. |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
83 |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
84 >>> from StringIO import StringIO |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
85 >>> buf = StringIO(''' |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
86 ... #: main.py:1 |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
87 ... #, fuzzy, python-format |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
88 ... msgid "foo %(name)s" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
89 ... msgstr "" |
21
ddfac856c34f
Change pot header's first line, "Translations Template for %%(project)s." instead of "SOME DESCRIPTIVE TITLE.". '''`project`''' and '''`version`''' now default to '''PROJECT''' and '''VERSION''' respectively. Fixed a bug regarding '''Content-Transfer-Encoding''', it shouldn't be the charset, and we're defaulting to `8bit` untill someone complains.
palgarvio
parents:
17
diff
changeset
|
90 ... |
94
b176f325d127
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
91 ... # A user comment |
b176f325d127
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
92 ... #. An auto comment |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
93 ... #: main.py:3 |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
94 ... msgid "bar" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
95 ... msgid_plural "baz" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
96 ... msgstr[0] "" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
97 ... msgstr[1] "" |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
98 ... ''') |
64 | 99 >>> catalog = read_po(buf) |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
100 >>> catalog.revision_date = datetime(2007, 04, 01) |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
101 |
64 | 102 >>> for message in catalog: |
67 | 103 ... if message.id: |
104 ... print (message.id, message.string) | |
105
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
105 ... print ' ', (message.locations, message.flags) |
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
106 ... print ' ', (message.user_comments, message.auto_comments) |
149
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
107 (u'foo %(name)s', '') |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
108 ([(u'main.py', 1)], set([u'fuzzy', u'python-format'])) |
105
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
109 ([], []) |
149
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
110 ((u'bar', u'baz'), ('', '')) |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
111 ([(u'main.py', 3)], set([])) |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
112 ([u'A user comment'], [u'An auto comment']) |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
113 |
1 | 114 :param fileobj: the file-like object to read the PO file from |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
115 :param locale: the locale identifier or `Locale` object, or `None` |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
116 if the catalog is not bound to a locale (which basically |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
117 means it's a template) |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
118 :param domain: the message domain |
227 | 119 :param ignore_obsolete: whether to ignore obsolete messages in the input |
334 | 120 :return: a catalog object representing the parsed PO file |
121 :rtype: `Catalog` | |
1 | 122 """ |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
123 catalog = Catalog(locale=locale, domain=domain) |
64 | 124 |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
125 counter = [0] |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
126 offset = [0] |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
127 messages = [] |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
128 translations = [] |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
129 locations = [] |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
130 flags = [] |
105
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
131 user_comments = [] |
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
132 auto_comments = [] |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
133 obsolete = [False] |
335 | 134 context = [] |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
135 in_msgid = [False] |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
136 in_msgstr = [False] |
342
d96bd67026e9
Fixed a bug in pofile (in_msgctxt was not defined). Test follows.
aronacher
parents:
335
diff
changeset
|
137 in_msgctxt = [False] |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
138 |
64 | 139 def _add_message(): |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
140 translations.sort() |
64 | 141 if len(messages) > 1: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
142 msgid = tuple([denormalize(m) for m in messages]) |
64 | 143 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
144 msgid = denormalize(messages[0]) |
370
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
145 if isinstance(msgid, (list, tuple)): |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
146 string = [] |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
147 for idx in range(catalog.num_plurals): |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
148 try: |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
149 string.append(translations[idx]) |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
150 except IndexError: |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
151 string.append((idx, '')) |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
152 string = tuple([denormalize(t[1]) for t in string]) |
64 | 153 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
154 string = denormalize(translations[0][1]) |
335 | 155 if context: |
156 msgctxt = denormalize('\n'.join(context)) | |
157 else: | |
158 msgctxt = None | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
159 message = Message(msgid, string, list(locations), set(flags), |
335 | 160 auto_comments, user_comments, lineno=offset[0] + 1, |
161 context=msgctxt) | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
162 if obsolete[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
163 if not ignore_obsolete: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
164 catalog.obsolete[msgid] = message |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
165 else: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
166 catalog[msgid] = message |
335 | 167 del messages[:]; del translations[:]; del context[:]; del locations[:]; |
168 del flags[:]; del auto_comments[:]; del user_comments[:]; | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
169 obsolete[0] = False |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
170 counter[0] += 1 |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
171 |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
172 def _process_message_line(lineno, line): |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
173 if line.startswith('msgid_plural'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
174 in_msgid[0] = True |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
175 msg = line[12:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
176 messages.append(msg) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
177 elif line.startswith('msgid'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
178 in_msgid[0] = True |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
179 offset[0] = lineno |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
180 txt = line[5:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
181 if messages: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
182 _add_message() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
183 messages.append(txt) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
184 elif line.startswith('msgstr'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
185 in_msgid[0] = False |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
186 in_msgstr[0] = True |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
187 msg = line[6:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
188 if msg.startswith('['): |
441
f4c0d8fe8af9
Make sure to only strip on the first occurence of ].
jruigrok
parents:
428
diff
changeset
|
189 idx, msg = msg[1:].split(']', 1) |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
190 translations.append([int(idx), msg.lstrip()]) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
191 else: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
192 translations.append([0, msg]) |
335 | 193 elif line.startswith('msgctxt'): |
428
6a62763f2fc4
Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents:
423
diff
changeset
|
194 if messages: |
6a62763f2fc4
Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents:
423
diff
changeset
|
195 _add_message() |
335 | 196 in_msgid[0] = in_msgstr[0] = False |
197 context.append(line[7:].lstrip()) | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
198 elif line.startswith('"'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
199 if in_msgid[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
200 messages[-1] += u'\n' + line.rstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
201 elif in_msgstr[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
202 translations[-1][1] += u'\n' + line.rstrip() |
335 | 203 elif in_msgctxt[0]: |
204 context.append(line.rstrip()) | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
205 |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
206 for lineno, line in enumerate(fileobj.readlines()): |
414
ea0da9db79ef
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
207 line = line.strip() |
ea0da9db79ef
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
208 if not isinstance(line, unicode): |
ea0da9db79ef
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
209 line = line.decode(catalog.charset) |
1 | 210 if line.startswith('#'): |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
211 in_msgid[0] = in_msgstr[0] = False |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
212 if messages and translations: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
213 _add_message() |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
214 if line[1:].startswith(':'): |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
215 for location in line[2:].lstrip().split(): |
356
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
216 pos = location.rfind(':') |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
217 if pos >= 0: |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
218 try: |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
219 lineno = int(location[pos + 1:]) |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
220 except ValueError: |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
221 continue |
1d75228aaa33
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
222 locations.append((location[:pos], lineno)) |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
223 elif line[1:].startswith(','): |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
224 for flag in line[2:].lstrip().split(','): |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
225 flags.append(flag.strip()) |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
226 elif line[1:].startswith('~'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
227 obsolete[0] = True |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
228 _process_message_line(lineno, line[2:].lstrip()) |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
229 elif line[1:].startswith('.'): |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
230 # These are called auto-comments |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
231 comment = line[2:].strip() |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
232 if comment: # Just check that we're not adding empty comments |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
233 auto_comments.append(comment) |
120 | 234 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
235 # These are called user comments |
120 | 236 user_comments.append(line[1:].strip()) |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
237 else: |
220
677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
238 _process_message_line(lineno, line) |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
239 |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
240 if messages: |
64 | 241 _add_message() |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
242 |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
243 # No actual messages found, but there was some info in comments, from which |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
244 # we'll construct an empty header message |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
245 elif not counter[0] and (flags or user_comments or auto_comments): |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
246 messages.append(u'') |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
247 translations.append([0, u'']) |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
248 _add_message() |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
249 |
64 | 250 return catalog |
1 | 251 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
252 WORD_SEP = re.compile('(' |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
253 r'\s+|' # any whitespace |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
254 r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
255 r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
256 ')') |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
257 |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
258 def escape(string): |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
259 r"""Escape the given string so that it can be included in double-quoted |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
260 strings in ``PO`` files. |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
261 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
262 >>> escape('''Say: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
263 ... "hello, world!" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
264 ... ''') |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
265 '"Say:\\n \\"hello, world!\\"\\n"' |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
266 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
267 :param string: the string to escape |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
268 :return: the escaped string |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
269 :rtype: `str` or `unicode` |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
270 """ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
271 return '"%s"' % string.replace('\\', '\\\\') \ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
272 .replace('\t', '\\t') \ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
273 .replace('\r', '\\r') \ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
274 .replace('\n', '\\n') \ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
275 .replace('\"', '\\"') |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
276 |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
277 def normalize(string, prefix='', width=76): |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
278 r"""Convert a string into a format that is appropriate for .po files. |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
279 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
280 >>> print normalize('''Say: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
281 ... "hello, world!" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
282 ... ''', width=None) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
283 "" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
284 "Say:\n" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
285 " \"hello, world!\"\n" |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
286 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
287 >>> print normalize('''Say: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
288 ... "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
289 ... ''', width=32) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
290 "" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
291 "Say:\n" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
292 " \"Lorem ipsum dolor sit " |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
293 "amet, consectetur adipisicing" |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
294 " elit, \"\n" |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
295 |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
296 :param string: the string to normalize |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
297 :param prefix: a string that should be prepended to every line |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
298 :param width: the maximum line width; use `None`, 0, or a negative number |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
299 to completely disable line wrapping |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
300 :return: the normalized string |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
301 :rtype: `unicode` |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
302 """ |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
303 if width and width > 0: |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
304 prefixlen = len(prefix) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
305 lines = [] |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
306 for idx, line in enumerate(string.splitlines(True)): |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
307 if len(escape(line)) + prefixlen > width: |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
308 chunks = WORD_SEP.split(line) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
309 chunks.reverse() |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
310 while chunks: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
311 buf = [] |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
312 size = 2 |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
313 while chunks: |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
314 l = len(escape(chunks[-1])) - 2 + prefixlen |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
315 if size + l < width: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
316 buf.append(chunks.pop()) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
317 size += l |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
318 else: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
319 if not buf: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
320 # handle long chunks by putting them on a |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
321 # separate line |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
322 buf.append(chunks.pop()) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
323 break |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
324 lines.append(u''.join(buf)) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
325 else: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
326 lines.append(line) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
327 else: |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
328 lines = string.splitlines(True) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
329 |
67 | 330 if len(lines) <= 1: |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
331 return escape(string) |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
332 |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
333 # Remove empty trailing line |
67 | 334 if lines and not lines[-1]: |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
335 del lines[-1] |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
336 lines[-1] += '\n' |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
337 return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines]) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
338 |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
339 def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
340 sort_output=False, sort_by_file=False, ignore_obsolete=False, |
203 | 341 include_previous=False): |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
342 r"""Write a ``gettext`` PO (portable object) template file for a given |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
343 message catalog to the provided file-like object. |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
344 |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
345 >>> catalog = Catalog() |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
346 >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)], |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
347 ... flags=('fuzzy',)) |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
348 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) |
1 | 349 >>> from StringIO import StringIO |
350 >>> buf = StringIO() | |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
351 >>> write_po(buf, catalog, omit_header=True) |
1 | 352 >>> print buf.getvalue() |
353 #: main.py:1 | |
6
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
354 #, fuzzy, python-format |
1801bc2b60ca
Add basic PO file parsing, and change the PO writing procedure to also take flags (such as "python-format" or "fuzzy").
cmlenz
parents:
5
diff
changeset
|
355 msgid "foo %(name)s" |
1 | 356 msgstr "" |
357 <BLANKLINE> | |
358 #: main.py:3 | |
359 msgid "bar" | |
360 msgid_plural "baz" | |
361 msgstr[0] "" | |
362 msgstr[1] "" | |
363 <BLANKLINE> | |
364 <BLANKLINE> | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
365 |
1 | 366 :param fileobj: the file-like object to write to |
67 | 367 :param catalog: the `Catalog` instance |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
368 :param width: the maximum line width for the generated output; use `None`, |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
369 0, or a negative number to completely disable line wrapping |
1 | 370 :param no_location: do not emit a location comment for every message |
371 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
372 output | |
227 | 373 :param sort_output: whether to sort the messages in the output by msgid |
374 :param sort_by_file: whether to sort the messages in the output by their | |
375 locations | |
376 :param ignore_obsolete: whether to ignore obsolete messages and not include | |
377 them in the output; by default they are included as | |
378 comments | |
203 | 379 :param include_previous: include the old msgid as a comment when |
229 | 380 updating the catalog |
1 | 381 """ |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
382 def _normalize(key, prefix=''): |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
383 return normalize(key, prefix=prefix, width=width) \ |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
384 .encode(catalog.charset, 'backslashreplace') |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
385 |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
386 def _write(text): |
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
387 if isinstance(text, unicode): |
102
eb0d9591d555
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
388 text = text.encode(catalog.charset) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
389 fileobj.write(text) |
1 | 390 |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
391 def _write_comment(comment, prefix=''): |
423
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
392 # xgettext always wraps comments even if --no-wrap is passed; |
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
393 # provide the same behaviour |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
394 if width and width > 0: |
423
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
395 _width = width |
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
396 else: |
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
397 _width = 76 |
8f91314df0b9
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
421
diff
changeset
|
398 for line in wraptext(comment, _width): |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
399 _write('#%s %s\n' % (prefix, line.strip())) |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
400 |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
401 def _write_message(message, prefix=''): |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
402 if isinstance(message.id, (list, tuple)): |
421 | 403 if message.context: |
404 _write('%smsgctxt %s\n' % (prefix, | |
405 _normalize(message.context, prefix))) | |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
406 _write('%smsgid %s\n' % (prefix, _normalize(message.id[0], prefix))) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
407 _write('%smsgid_plural %s\n' % ( |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
408 prefix, _normalize(message.id[1], prefix) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
409 )) |
370
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
410 |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
411 for idx in range(catalog.num_plurals): |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
412 try: |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
413 string = message.string[idx] |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
414 except IndexError: |
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
415 string = '' |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
416 _write('%smsgstr[%d] %s\n' % ( |
370
6129d2e770bc
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
417 prefix, idx, _normalize(string, prefix) |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
418 )) |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
419 else: |
421 | 420 if message.context: |
421 _write('%smsgctxt %s\n' % (prefix, | |
422 _normalize(message.context, prefix))) | |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
423 _write('%smsgid %s\n' % (prefix, _normalize(message.id, prefix))) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
424 _write('%smsgstr %s\n' % ( |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
425 prefix, _normalize(message.string or '', prefix) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
426 )) |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
427 |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
428 messages = list(catalog) |
71 | 429 if sort_output: |
248
bedaaeadc1db
add a __cmp__ to Message that correctly sorts by id, taking into account plurals
pjenvey
parents:
229
diff
changeset
|
430 messages.sort() |
71 | 431 elif sort_by_file: |
432 messages.sort(lambda x,y: cmp(x.locations, y.locations)) | |
68 | 433 |
71 | 434 for message in messages: |
67 | 435 if not message.id: # This is the header "message" |
436 if omit_header: | |
437 continue | |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
438 comment_header = catalog.header_comment |
103
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
439 if width and width > 0: |
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
440 lines = [] |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
441 for line in comment_header.splitlines(): |
315 | 442 lines += wraptext(line, width=width, |
443 subsequent_indent='# ') | |
104
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
444 comment_header = u'\n'.join(lines) + u'\n' |
22f222e23b86
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
445 _write(comment_header) |
102
eb0d9591d555
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
446 |
227 | 447 for comment in message.user_comments: |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
448 _write_comment(comment) |
227 | 449 for comment in message.auto_comments: |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
450 _write_comment(comment, prefix='.') |
1 | 451 |
452 if not no_location: | |
134 | 453 locs = u' '.join([u'%s:%d' % (filename.replace(os.sep, '/'), lineno) |
454 for filename, lineno in message.locations]) | |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
455 _write_comment(locs, prefix=':') |
56
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
456 if message.flags: |
27fba894d3ca
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
457 _write('#%s\n' % ', '.join([''] + list(message.flags))) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
458 |
203 | 459 if message.previous_id and include_previous: |
309
089154904d76
Fix for unicode problem when the previous message id is included as a comment in PO serialization. Closes #78.
cmlenz
parents:
248
diff
changeset
|
460 _write_comment('msgid %s' % _normalize(message.previous_id[0]), |
203 | 461 prefix='|') |
462 if len(message.previous_id) > 1: | |
309
089154904d76
Fix for unicode problem when the previous message id is included as a comment in PO serialization. Closes #78.
cmlenz
parents:
248
diff
changeset
|
463 _write_comment('msgid_plural %s' % _normalize( |
203 | 464 message.previous_id[1] |
465 ), prefix='|') | |
200
2983c718f6e2
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
199
diff
changeset
|
466 |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
467 _write_message(message) |
24
4fad20ab7cca
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
468 _write('\n') |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
469 |
191
cf09490f22b3
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
190
diff
changeset
|
470 if not ignore_obsolete: |
cf09490f22b3
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
190
diff
changeset
|
471 for message in catalog.obsolete.values(): |
227 | 472 for comment in message.user_comments: |
191
cf09490f22b3
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
190
diff
changeset
|
473 _write_comment(comment) |
cf09490f22b3
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
190
diff
changeset
|
474 _write_message(message, prefix='#~ ') |
cf09490f22b3
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
190
diff
changeset
|
475 _write('\n') |