Mercurial > babel > mirror
annotate babel/messages/pofile.py @ 442:af7b13202b11 trunk
Fix testcase name typo.
Switch to use a locale that uses a different singular and plural form instead
of just one form for both singular and plural.
author | jruigrok |
---|---|
date | Fri, 19 Feb 2010 13:16:08 +0000 |
parents | 942afedbb3d7 |
children | ac8961d40e61 2baa2cedd6f9 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
335 | 3 # Copyright (C) 2007-2008 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 | |
5
132526dcd074
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
21 from datetime import date, datetime |
134 | 22 import os |
1 | 23 import re |
24 | |
25 from babel import __version__ as VERSION | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
26 from babel.messages.catalog import Catalog, Message |
414
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
27 from babel.util import set, wraptext, LOCALTZ |
1 | 28 |
178
749c0f6863bc
Minor change to what symbols are ?exported?, primarily for the generated docs.
cmlenz
parents:
175
diff
changeset
|
29 __all__ = ['read_po', 'write_po'] |
161 | 30 __docformat__ = 'restructuredtext en' |
158 | 31 |
32 def unescape(string): | |
33 r"""Reverse `escape` the given string. | |
200
1c778cccd330
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
|
34 |
158 | 35 >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"') |
36 Say: | |
37 "hello, world!" | |
38 <BLANKLINE> | |
200
1c778cccd330
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
|
39 |
158 | 40 :param string: the string to unescape |
41 :return: the unescaped string | |
42 :rtype: `str` or `unicode` | |
43 """ | |
44 return string[1:-1].replace('\\\\', '\\') \ | |
45 .replace('\\t', '\t') \ | |
46 .replace('\\r', '\r') \ | |
47 .replace('\\n', '\n') \ | |
48 .replace('\\"', '\"') | |
49 | |
50 def denormalize(string): | |
51 r"""Reverse the normalization done by the `normalize` function. | |
200
1c778cccd330
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
|
52 |
158 | 53 >>> print denormalize(r'''"" |
54 ... "Say:\n" | |
55 ... " \"hello, world!\"\n"''') | |
56 Say: | |
57 "hello, world!" | |
58 <BLANKLINE> | |
200
1c778cccd330
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
|
59 |
158 | 60 >>> print denormalize(r'''"" |
61 ... "Say:\n" | |
62 ... " \"Lorem ipsum dolor sit " | |
63 ... "amet, consectetur adipisicing" | |
64 ... " elit, \"\n"''') | |
65 Say: | |
66 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " | |
67 <BLANKLINE> | |
200
1c778cccd330
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
|
68 |
158 | 69 :param string: the string to denormalize |
70 :return: the denormalized string | |
71 :rtype: `unicode` or `str` | |
72 """ | |
73 if string.startswith('""'): | |
74 lines = [] | |
75 for line in string.splitlines()[1:]: | |
76 lines.append(unescape(line)) | |
77 return ''.join(lines) | |
78 else: | |
79 return unescape(string) | |
1 | 80 |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
81 def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False): |
6
c3b1b0b3d129
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
|
82 """Read messages from a ``gettext`` PO (portable object) file from the given |
64 | 83 file-like object and return a `Catalog`. |
200
1c778cccd330
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
|
84 |
6
c3b1b0b3d129
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
|
85 >>> from StringIO import StringIO |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
86 >>> buf = StringIO(''' |
6
c3b1b0b3d129
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 ... #: main.py:1 |
c3b1b0b3d129
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 ... #, fuzzy, python-format |
c3b1b0b3d129
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 ... msgid "foo %(name)s" |
c3b1b0b3d129
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
|
90 ... msgstr "" |
21
cd9aa202568e
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
|
91 ... |
94
96037779b518
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
92 ... # A user comment |
96037779b518
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
93 ... #. An auto comment |
6
c3b1b0b3d129
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 ... #: main.py:3 |
c3b1b0b3d129
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 "bar" |
c3b1b0b3d129
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 ... msgid_plural "baz" |
c3b1b0b3d129
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[0] "" |
c3b1b0b3d129
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 ... msgstr[1] "" |
c3b1b0b3d129
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
|
99 ... ''') |
64 | 100 >>> catalog = read_po(buf) |
104
395704fda00b
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
|
101 >>> catalog.revision_date = datetime(2007, 04, 01) |
200
1c778cccd330
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
|
102 |
64 | 103 >>> for message in catalog: |
67 | 104 ... if message.id: |
105 ... print (message.id, message.string) | |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
106 ... print ' ', (message.locations, message.flags) |
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
107 ... print ' ', (message.user_comments, message.auto_comments) |
149
d62c63280e81
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
108 (u'foo %(name)s', '') |
d62c63280e81
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
109 ([(u'main.py', 1)], set([u'fuzzy', u'python-format'])) |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
110 ([], []) |
149
d62c63280e81
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
111 ((u'bar', u'baz'), ('', '')) |
d62c63280e81
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
112 ([(u'main.py', 3)], set([])) |
d62c63280e81
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
113 ([u'A user comment'], [u'An auto comment']) |
200
1c778cccd330
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
|
114 |
1 | 115 :param fileobj: the file-like object to read the PO file from |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
116 :param locale: the locale identifier or `Locale` object, or `None` |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
117 if the catalog is not bound to a locale (which basically |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
118 means it's a template) |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
119 :param domain: the message domain |
227 | 120 :param ignore_obsolete: whether to ignore obsolete messages in the input |
334 | 121 :return: a catalog object representing the parsed PO file |
122 :rtype: `Catalog` | |
1 | 123 """ |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
124 catalog = Catalog(locale=locale, domain=domain) |
64 | 125 |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
126 counter = [0] |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
127 offset = [0] |
6
c3b1b0b3d129
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 messages = [] |
c3b1b0b3d129
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 translations = [] |
c3b1b0b3d129
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 locations = [] |
c3b1b0b3d129
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
|
131 flags = [] |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
132 user_comments = [] |
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
133 auto_comments = [] |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
134 obsolete = [False] |
335 | 135 context = [] |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
136 in_msgid = [False] |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
137 in_msgstr = [False] |
342
2ee7dc04836c
Fixed a bug in pofile (in_msgctxt was not defined). Test follows.
aronacher
parents:
335
diff
changeset
|
138 in_msgctxt = [False] |
6
c3b1b0b3d129
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
|
139 |
64 | 140 def _add_message(): |
6
c3b1b0b3d129
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
|
141 translations.sort() |
64 | 142 if len(messages) > 1: |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
143 msgid = tuple([denormalize(m) for m in messages]) |
64 | 144 else: |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
145 msgid = denormalize(messages[0]) |
370
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
146 if isinstance(msgid, (list, tuple)): |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
147 string = [] |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
148 for idx in range(catalog.num_plurals): |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
149 try: |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
150 string.append(translations[idx]) |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
151 except IndexError: |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
152 string.append((idx, '')) |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
153 string = tuple([denormalize(t[1]) for t in string]) |
64 | 154 else: |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
155 string = denormalize(translations[0][1]) |
335 | 156 if context: |
157 msgctxt = denormalize('\n'.join(context)) | |
158 else: | |
159 msgctxt = None | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
160 message = Message(msgid, string, list(locations), set(flags), |
335 | 161 auto_comments, user_comments, lineno=offset[0] + 1, |
162 context=msgctxt) | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
163 if obsolete[0]: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
164 if not ignore_obsolete: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
165 catalog.obsolete[msgid] = message |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
166 else: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
167 catalog[msgid] = message |
335 | 168 del messages[:]; del translations[:]; del context[:]; del locations[:]; |
169 del flags[:]; del auto_comments[:]; del user_comments[:]; | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
170 obsolete[0] = False |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
171 counter[0] += 1 |
6
c3b1b0b3d129
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
|
172 |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
173 def _process_message_line(lineno, line): |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
174 if line.startswith('msgid_plural'): |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
175 in_msgid[0] = True |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
176 msg = line[12:].lstrip() |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
177 messages.append(msg) |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
178 elif line.startswith('msgid'): |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
179 in_msgid[0] = True |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
180 offset[0] = lineno |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
181 txt = line[5:].lstrip() |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
182 if messages: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
183 _add_message() |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
184 messages.append(txt) |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
185 elif line.startswith('msgstr'): |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
186 in_msgid[0] = False |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
187 in_msgstr[0] = True |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
188 msg = line[6:].lstrip() |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
189 if msg.startswith('['): |
441
942afedbb3d7
Make sure to only strip on the first occurence of ].
jruigrok
parents:
428
diff
changeset
|
190 idx, msg = msg[1:].split(']', 1) |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
191 translations.append([int(idx), msg.lstrip()]) |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
192 else: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
193 translations.append([0, msg]) |
335 | 194 elif line.startswith('msgctxt'): |
428
a974d298400f
Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents:
423
diff
changeset
|
195 if messages: |
a974d298400f
Fix for msgctxt parsing in PO files. Thanks to Asheesh Laroia for the patch. Closes #159.
cmlenz
parents:
423
diff
changeset
|
196 _add_message() |
335 | 197 in_msgid[0] = in_msgstr[0] = False |
198 context.append(line[7:].lstrip()) | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
199 elif line.startswith('"'): |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
200 if in_msgid[0]: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
201 messages[-1] += u'\n' + line.rstrip() |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
202 elif in_msgstr[0]: |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
203 translations[-1][1] += u'\n' + line.rstrip() |
335 | 204 elif in_msgctxt[0]: |
205 context.append(line.rstrip()) | |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
206 |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
207 for lineno, line in enumerate(fileobj.readlines()): |
414
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
208 line = line.strip() |
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
209 if not isinstance(line, unicode): |
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
370
diff
changeset
|
210 line = line.decode(catalog.charset) |
1 | 211 if line.startswith('#'): |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
212 in_msgid[0] = in_msgstr[0] = False |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
213 if messages and translations: |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
214 _add_message() |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
215 if line[1:].startswith(':'): |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
216 for location in line[2:].lstrip().split(): |
356
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
217 pos = location.rfind(':') |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
218 if pos >= 0: |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
219 try: |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
220 lineno = int(location[pos + 1:]) |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
221 except ValueError: |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
222 continue |
4cdca48fc832
Fixed #59 by falling back silently on invalid location comments.
aronacher
parents:
342
diff
changeset
|
223 locations.append((location[:pos], lineno)) |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
224 elif line[1:].startswith(','): |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
225 for flag in line[2:].lstrip().split(','): |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
226 flags.append(flag.strip()) |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
227 elif line[1:].startswith('~'): |
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
228 obsolete[0] = True |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
229 _process_message_line(lineno, line[2:].lstrip()) |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
230 elif line[1:].startswith('.'): |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
231 # These are called auto-comments |
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
232 comment = line[2:].strip() |
199
a0d22f2f2df0
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
233 if comment: # Just check that we're not adding empty comments |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
234 auto_comments.append(comment) |
120 | 235 else: |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
236 # These are called user comments |
120 | 237 user_comments.append(line[1:].strip()) |
104
395704fda00b
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
|
238 else: |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
203
diff
changeset
|
239 _process_message_line(lineno, line) |
6
c3b1b0b3d129
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 |
c3b1b0b3d129
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
|
241 if messages: |
64 | 242 _add_message() |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
243 |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
244 # No actual messages found, but there was some info in comments, from which |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
245 # we'll construct an empty header message |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
246 elif not counter[0] and (flags or user_comments or auto_comments): |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
247 messages.append(u'') |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
248 translations.append([0, u'']) |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
249 _add_message() |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
250 |
64 | 251 return catalog |
1 | 252 |
24
b09e90803d1b
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 WORD_SEP = re.compile('(' |
b09e90803d1b
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+|' # any whitespace |
b09e90803d1b
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'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words |
b09e90803d1b
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 r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash |
b09e90803d1b
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 ')') |
b09e90803d1b
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 |
b09e90803d1b
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 def escape(string): |
b09e90803d1b
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 r"""Escape the given string so that it can be included in double-quoted |
b09e90803d1b
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
|
261 strings in ``PO`` files. |
200
1c778cccd330
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
|
262 |
24
b09e90803d1b
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 >>> escape('''Say: |
b09e90803d1b
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 ... "hello, world!" |
b09e90803d1b
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 ... ''') |
b09e90803d1b
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
|
266 '"Say:\\n \\"hello, world!\\"\\n"' |
200
1c778cccd330
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
|
267 |
24
b09e90803d1b
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 :param string: the string to escape |
b09e90803d1b
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 :return: the escaped string |
b09e90803d1b
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 :rtype: `str` or `unicode` |
b09e90803d1b
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 """ |
b09e90803d1b
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 return '"%s"' % string.replace('\\', '\\\\') \ |
b09e90803d1b
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('\t', '\\t') \ |
b09e90803d1b
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('\r', '\\r') \ |
b09e90803d1b
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('\n', '\\n') \ |
b09e90803d1b
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 .replace('\"', '\\"') |
b09e90803d1b
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
|
277 |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
278 def normalize(string, prefix='', width=76): |
106
2cd83f77cc98
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
279 r"""Convert a string into a format that is appropriate for .po files. |
200
1c778cccd330
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
|
280 |
24
b09e90803d1b
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 >>> print normalize('''Say: |
b09e90803d1b
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 ... "hello, world!" |
b09e90803d1b
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 ... ''', width=None) |
b09e90803d1b
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 "" |
b09e90803d1b
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 "Say:\n" |
b09e90803d1b
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
|
286 " \"hello, world!\"\n" |
200
1c778cccd330
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
|
287 |
24
b09e90803d1b
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 >>> print normalize('''Say: |
b09e90803d1b
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 ... "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " |
b09e90803d1b
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 ... ''', width=32) |
b09e90803d1b
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 "" |
b09e90803d1b
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 "Say:\n" |
b09e90803d1b
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 " \"Lorem ipsum dolor sit " |
b09e90803d1b
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 "amet, consectetur adipisicing" |
b09e90803d1b
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
|
295 " elit, \"\n" |
200
1c778cccd330
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
|
296 |
24
b09e90803d1b
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
|
297 :param string: the string to normalize |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
298 :param prefix: a string that should be prepended to every line |
24
b09e90803d1b
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 :param width: the maximum line width; use `None`, 0, or a negative number |
b09e90803d1b
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 to completely disable line wrapping |
b09e90803d1b
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 :return: the normalized string |
b09e90803d1b
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 :rtype: `unicode` |
b09e90803d1b
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 """ |
b09e90803d1b
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
|
304 if width and width > 0: |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
305 prefixlen = len(prefix) |
24
b09e90803d1b
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 lines = [] |
b09e90803d1b
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
|
307 for idx, line in enumerate(string.splitlines(True)): |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
308 if len(escape(line)) + prefixlen > width: |
24
b09e90803d1b
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 = WORD_SEP.split(line) |
b09e90803d1b
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 chunks.reverse() |
b09e90803d1b
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 while chunks: |
b09e90803d1b
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 buf = [] |
b09e90803d1b
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 size = 2 |
b09e90803d1b
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
|
314 while chunks: |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
315 l = len(escape(chunks[-1])) - 2 + prefixlen |
24
b09e90803d1b
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 if size + l < width: |
b09e90803d1b
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 buf.append(chunks.pop()) |
b09e90803d1b
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 size += l |
b09e90803d1b
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 else: |
b09e90803d1b
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 if not buf: |
b09e90803d1b
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 # handle long chunks by putting them on a |
b09e90803d1b
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 # separate line |
b09e90803d1b
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 buf.append(chunks.pop()) |
b09e90803d1b
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 break |
b09e90803d1b
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 lines.append(u''.join(buf)) |
b09e90803d1b
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 else: |
b09e90803d1b
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 lines.append(line) |
b09e90803d1b
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 else: |
b09e90803d1b
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 lines = string.splitlines(True) |
b09e90803d1b
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
|
330 |
67 | 331 if len(lines) <= 1: |
24
b09e90803d1b
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 return escape(string) |
b09e90803d1b
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 |
b09e90803d1b
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
|
334 # Remove empty trailing line |
67 | 335 if lines and not lines[-1]: |
24
b09e90803d1b
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 del lines[-1] |
b09e90803d1b
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
|
337 lines[-1] += '\n' |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
338 return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines]) |
24
b09e90803d1b
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
|
339 |
104
395704fda00b
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
|
340 def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, |
200
1c778cccd330
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
|
341 sort_output=False, sort_by_file=False, ignore_obsolete=False, |
203 | 342 include_previous=False): |
56
f40fc143439c
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 r"""Write a ``gettext`` PO (portable object) template file for a given |
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
344 message catalog to the provided file-like object. |
200
1c778cccd330
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
|
345 |
56
f40fc143439c
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 = Catalog() |
f40fc143439c
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 >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)], |
f40fc143439c
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 ... flags=('fuzzy',)) |
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
349 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) |
1 | 350 >>> from StringIO import StringIO |
351 >>> buf = StringIO() | |
104
395704fda00b
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
|
352 >>> write_po(buf, catalog, omit_header=True) |
1 | 353 >>> print buf.getvalue() |
354 #: main.py:1 | |
6
c3b1b0b3d129
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 #, fuzzy, python-format |
c3b1b0b3d129
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
|
356 msgid "foo %(name)s" |
1 | 357 msgstr "" |
358 <BLANKLINE> | |
359 #: main.py:3 | |
360 msgid "bar" | |
361 msgid_plural "baz" | |
362 msgstr[0] "" | |
363 msgstr[1] "" | |
364 <BLANKLINE> | |
365 <BLANKLINE> | |
200
1c778cccd330
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
|
366 |
1 | 367 :param fileobj: the file-like object to write to |
67 | 368 :param catalog: the `Catalog` instance |
24
b09e90803d1b
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 :param width: the maximum line width for the generated output; use `None`, |
b09e90803d1b
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
|
370 0, or a negative number to completely disable line wrapping |
1 | 371 :param no_location: do not emit a location comment for every message |
372 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
373 output | |
227 | 374 :param sort_output: whether to sort the messages in the output by msgid |
375 :param sort_by_file: whether to sort the messages in the output by their | |
376 locations | |
377 :param ignore_obsolete: whether to ignore obsolete messages and not include | |
378 them in the output; by default they are included as | |
379 comments | |
203 | 380 :param include_previous: include the old msgid as a comment when |
229 | 381 updating the catalog |
1 | 382 """ |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
383 def _normalize(key, prefix=''): |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
384 return normalize(key, prefix=prefix, width=width) \ |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
385 .encode(catalog.charset, 'backslashreplace') |
24
b09e90803d1b
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 |
b09e90803d1b
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 def _write(text): |
b09e90803d1b
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
|
388 if isinstance(text, unicode): |
102
14a3d766a701
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
|
389 text = text.encode(catalog.charset) |
24
b09e90803d1b
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
|
390 fileobj.write(text) |
1 | 391 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
392 def _write_comment(comment, prefix=''): |
423
fb926f48efba
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 # xgettext always wraps comments even if --no-wrap is passed; |
fb926f48efba
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
|
394 # provide the same behaviour |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
395 if width and width > 0: |
423
fb926f48efba
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 _width = width |
fb926f48efba
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 else: |
fb926f48efba
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 _width = 76 |
fb926f48efba
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
|
399 for line in wraptext(comment, _width): |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
400 _write('#%s %s\n' % (prefix, line.strip())) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
401 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
402 def _write_message(message, prefix=''): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
403 if isinstance(message.id, (list, tuple)): |
421 | 404 if message.context: |
405 _write('%smsgctxt %s\n' % (prefix, | |
406 _normalize(message.context, prefix))) | |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
407 _write('%smsgid %s\n' % (prefix, _normalize(message.id[0], prefix))) |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
408 _write('%smsgid_plural %s\n' % ( |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
409 prefix, _normalize(message.id[1], prefix) |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
410 )) |
370
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
411 |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
412 for idx in range(catalog.num_plurals): |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
413 try: |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
414 string = message.string[idx] |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
415 except IndexError: |
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
416 string = '' |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
417 _write('%smsgstr[%d] %s\n' % ( |
370
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
356
diff
changeset
|
418 prefix, idx, _normalize(string, prefix) |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
419 )) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
420 else: |
421 | 421 if message.context: |
422 _write('%smsgctxt %s\n' % (prefix, | |
423 _normalize(message.context, prefix))) | |
190
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
424 _write('%smsgid %s\n' % (prefix, _normalize(message.id, prefix))) |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
425 _write('%smsgstr %s\n' % ( |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
426 prefix, _normalize(message.string or '', prefix) |
5041d90edf0c
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
427 )) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
428 |
104
395704fda00b
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
|
429 messages = list(catalog) |
71 | 430 if sort_output: |
248
f0b1ee94628c
add a __cmp__ to Message that correctly sorts by id, taking into account plurals
pjenvey
parents:
229
diff
changeset
|
431 messages.sort() |
71 | 432 elif sort_by_file: |
433 messages.sort(lambda x,y: cmp(x.locations, y.locations)) | |
68 | 434 |
71 | 435 for message in messages: |
67 | 436 if not message.id: # This is the header "message" |
437 if omit_header: | |
438 continue | |
104
395704fda00b
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
|
439 comment_header = catalog.header_comment |
103
dacfbaf0d1e0
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
440 if width and width > 0: |
dacfbaf0d1e0
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
441 lines = [] |
104
395704fda00b
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
|
442 for line in comment_header.splitlines(): |
315 | 443 lines += wraptext(line, width=width, |
444 subsequent_indent='# ') | |
104
395704fda00b
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 comment_header = u'\n'.join(lines) + u'\n' |
395704fda00b
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
|
446 _write(comment_header) |
102
14a3d766a701
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
|
447 |
227 | 448 for comment in message.user_comments: |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
449 _write_comment(comment) |
227 | 450 for comment in message.auto_comments: |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
451 _write_comment(comment, prefix='.') |
1 | 452 |
453 if not no_location: | |
134 | 454 locs = u' '.join([u'%s:%d' % (filename.replace(os.sep, '/'), lineno) |
455 for filename, lineno in message.locations]) | |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
456 _write_comment(locs, prefix=':') |
56
f40fc143439c
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 if message.flags: |
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
458 _write('#%s\n' % ', '.join([''] + list(message.flags))) |
24
b09e90803d1b
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
|
459 |
203 | 460 if message.previous_id and include_previous: |
309
43dca73da5b5
Fix for unicode problem when the previous message id is included as a comment in PO serialization. Closes #78.
cmlenz
parents:
248
diff
changeset
|
461 _write_comment('msgid %s' % _normalize(message.previous_id[0]), |
203 | 462 prefix='|') |
463 if len(message.previous_id) > 1: | |
309
43dca73da5b5
Fix for unicode problem when the previous message id is included as a comment in PO serialization. Closes #78.
cmlenz
parents:
248
diff
changeset
|
464 _write_comment('msgid_plural %s' % _normalize( |
203 | 465 message.previous_id[1] |
466 ), prefix='|') | |
200
1c778cccd330
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
|
467 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
468 _write_message(message) |
24
b09e90803d1b
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
|
469 _write('\n') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
470 |
191
c171a0041ad2
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 if not ignore_obsolete: |
c171a0041ad2
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
|
472 for message in catalog.obsolete.values(): |
227 | 473 for comment in message.user_comments: |
191
c171a0041ad2
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_comment(comment) |
c171a0041ad2
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_message(message, prefix='#~ ') |
c171a0041ad2
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
|
476 _write('\n') |