Mercurial > babel > old > babel-test
annotate babel/messages/pofile.py @ 220:677147547e2d
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
author | cmlenz |
---|---|
date | Mon, 16 Jul 2007 16:57:49 +0000 |
parents | e50aaaabb3d3 |
children | 236a640d02a6 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
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
50ad95bee876
* 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 |
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
|
24 try: |
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
|
25 set |
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
|
26 except NameError: |
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
|
27 from sets import Set as set |
103
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
28 from textwrap import wrap |
1 | 29 |
30 from babel import __version__ as VERSION | |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
31 from babel.messages.catalog import Catalog, Message |
97 | 32 from babel.util import LOCALTZ |
1 | 33 |
178
4407c6d1b7d7
Minor change to what symbols are ?exported?, primarily for the generated docs.
cmlenz
parents:
175
diff
changeset
|
34 __all__ = ['read_po', 'write_po'] |
161 | 35 __docformat__ = 'restructuredtext en' |
158 | 36 |
37 def unescape(string): | |
38 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
|
39 |
158 | 40 >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"') |
41 Say: | |
42 "hello, world!" | |
43 <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
|
44 |
158 | 45 :param string: the string to unescape |
46 :return: the unescaped string | |
47 :rtype: `str` or `unicode` | |
48 """ | |
49 return string[1:-1].replace('\\\\', '\\') \ | |
50 .replace('\\t', '\t') \ | |
51 .replace('\\r', '\r') \ | |
52 .replace('\\n', '\n') \ | |
53 .replace('\\"', '\"') | |
54 | |
55 def denormalize(string): | |
56 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
|
57 |
158 | 58 >>> print denormalize(r'''"" |
59 ... "Say:\n" | |
60 ... " \"hello, world!\"\n"''') | |
61 Say: | |
62 "hello, world!" | |
63 <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
|
64 |
158 | 65 >>> print denormalize(r'''"" |
66 ... "Say:\n" | |
67 ... " \"Lorem ipsum dolor sit " | |
68 ... "amet, consectetur adipisicing" | |
69 ... " elit, \"\n"''') | |
70 Say: | |
71 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " | |
72 <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
|
73 |
158 | 74 :param string: the string to denormalize |
75 :return: the denormalized string | |
76 :rtype: `unicode` or `str` | |
77 """ | |
78 if string.startswith('""'): | |
79 lines = [] | |
80 for line in string.splitlines()[1:]: | |
81 lines.append(unescape(line)) | |
82 return ''.join(lines) | |
83 else: | |
84 return unescape(string) | |
1 | 85 |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
86 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
|
87 """Read messages from a ``gettext`` PO (portable object) file from the given |
64 | 88 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
|
89 |
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
|
90 >>> 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
|
91 >>> 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
|
92 ... #: 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
|
93 ... #, 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
|
94 ... 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
|
95 ... 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
|
96 ... |
94
b176f325d127
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
97 ... # A user comment |
b176f325d127
Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents:
84
diff
changeset
|
98 ... #. 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
|
99 ... #: 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
|
100 ... 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
|
101 ... 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
|
102 ... 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
|
103 ... 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
|
104 ... ''') |
64 | 105 >>> 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
|
106 >>> 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
|
107 |
64 | 108 >>> for message in catalog: |
67 | 109 ... if message.id: |
110 ... 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
|
111 ... print ' ', (message.locations, message.flags) |
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
112 ... 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
|
113 (u'foo %(name)s', '') |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
114 ([(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
|
115 ([], []) |
149
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
116 ((u'bar', u'baz'), ('', '')) |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
117 ([(u'main.py', 3)], set([])) |
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
118 ([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
|
119 |
1 | 120 :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
|
121 :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
|
122 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
|
123 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
|
124 :param domain: the message domain |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
125 :ignore_obsolete: whether to ignore obsolete messages in the input |
1 | 126 :return: an iterator over ``(message, translation, location)`` tuples |
127 :rtype: ``iterator`` | |
128 """ | |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
129 catalog = Catalog(locale=locale, domain=domain) |
64 | 130 |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 flags = [] |
105
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
137 user_comments = [] |
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
138 auto_comments = [] |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
139 obsolete = [False] |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
140 in_msgid = [False] |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
141 in_msgstr = [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
|
142 |
64 | 143 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
|
144 translations.sort() |
64 | 145 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
|
146 msgid = tuple([denormalize(m) for m in messages]) |
64 | 147 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
148 msgid = denormalize(messages[0]) |
64 | 149 if len(translations) > 1: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
150 string = tuple([denormalize(t[1]) for t in translations]) |
64 | 151 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
152 string = denormalize(translations[0][1]) |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
153 message = Message(msgid, string, list(locations), set(flags), |
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
|
154 list(auto_comments), list(user_comments), |
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
|
155 lineno=offset[0] + 1) |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
156 if obsolete[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
157 if not ignore_obsolete: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
158 catalog.obsolete[msgid] = message |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
159 else: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
160 catalog[msgid] = message |
84
4ff9cc26c11b
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
80
diff
changeset
|
161 del messages[:]; del translations[:]; del locations[:]; |
105
f744dd56573d
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
162 del flags[:]; del auto_comments[:]; del user_comments[:] |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
163 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
|
164 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
|
165 |
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
|
166 def _process_message_line(lineno, line): |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
167 if line.startswith('msgid_plural'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
168 in_msgid[0] = True |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
169 msg = line[12:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
170 messages.append(msg) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
171 elif line.startswith('msgid'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
172 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
|
173 offset[0] = lineno |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
174 txt = line[5:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
175 if messages: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
176 _add_message() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
177 messages.append(txt) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
178 elif line.startswith('msgstr'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
179 in_msgid[0] = False |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
180 in_msgstr[0] = True |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
181 msg = line[6:].lstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
182 if msg.startswith('['): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
183 idx, msg = msg[1:].split(']') |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
184 translations.append([int(idx), msg.lstrip()]) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
185 else: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
186 translations.append([0, msg]) |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
187 elif line.startswith('"'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
188 if in_msgid[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
189 messages[-1] += u'\n' + line.rstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
190 elif in_msgstr[0]: |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
191 translations[-1][1] += u'\n' + line.rstrip() |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
192 |
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
|
193 for lineno, line in enumerate(fileobj.readlines()): |
149
ba5150e9544e
Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents:
134
diff
changeset
|
194 line = line.strip().decode(catalog.charset) |
1 | 195 if line.startswith('#'): |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
196 in_msgid[0] = in_msgstr[0] = False |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
197 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
|
198 _add_message() |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
199 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
|
200 for location 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
|
201 filename, lineno = location.split(':', 1) |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
202 locations.append((filename, int(lineno))) |
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
203 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
|
204 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
|
205 flags.append(flag.strip()) |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
206 elif line[1:].startswith('~'): |
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
207 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
|
208 _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
|
209 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
|
210 # 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
|
211 comment = line[2:].strip() |
199
ace575fff5ac
Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents:
196
diff
changeset
|
212 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
|
213 auto_comments.append(comment) |
120 | 214 else: |
106
9b22b36066f6
Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents:
105
diff
changeset
|
215 # These are called user comments |
120 | 216 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
|
217 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
|
218 _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
|
219 |
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
|
220 if messages: |
64 | 221 _add_message() |
196
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
222 |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
223 # 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
|
224 # 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
|
225 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
|
226 messages.append(u'') |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
227 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
|
228 _add_message() |
93a922d31eca
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
229 |
64 | 230 return catalog |
1 | 231 |
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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 ')') |
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
|
237 |
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
|
238 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
|
239 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
|
240 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
|
241 |
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
|
242 >>> 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
|
243 ... "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
|
244 ... ''') |
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
|
245 '"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
|
246 |
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
|
247 :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
|
248 :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
|
249 :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
|
250 """ |
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
|
251 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
|
252 .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
|
253 .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
|
254 .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
|
255 .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
|
256 |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
257 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
|
258 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
|
259 |
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
|
260 >>> 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
|
261 ... "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
|
262 ... ''', 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
|
263 "" |
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 "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
|
265 " \"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 >>> 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
|
268 ... "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
|
269 ... ''', 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
|
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 "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
|
272 " \"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
|
273 "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
|
274 " 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
|
275 |
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
|
276 :param string: the string to normalize |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
277 :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
|
278 :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
|
279 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
|
280 :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
|
281 :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
|
282 """ |
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 if width and width > 0: |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 while chunks: |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
294 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 # 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
|
301 # 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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 |
67 | 310 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
|
311 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
|
312 |
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 # Remove empty trailing line |
67 | 314 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
|
315 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
|
316 lines[-1] += '\n' |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
317 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
|
318 |
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
|
319 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
|
320 sort_output=False, sort_by_file=False, ignore_obsolete=False, |
203 | 321 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
|
322 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
|
323 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
|
324 |
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
|
325 >>> 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
|
326 >>> 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
|
327 ... 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
|
328 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) |
1 | 329 >>> from StringIO import StringIO |
330 >>> 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
|
331 >>> write_po(buf, catalog, omit_header=True) |
1 | 332 >>> print buf.getvalue() |
333 #: 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
|
334 #, 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
|
335 msgid "foo %(name)s" |
1 | 336 msgstr "" |
337 <BLANKLINE> | |
338 #: main.py:3 | |
339 msgid "bar" | |
340 msgid_plural "baz" | |
341 msgstr[0] "" | |
342 msgstr[1] "" | |
343 <BLANKLINE> | |
344 <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
|
345 |
1 | 346 :param fileobj: the file-like object to write to |
67 | 347 :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
|
348 :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
|
349 0, or a negative number to completely disable line wrapping |
1 | 350 :param no_location: do not emit a location comment for every message |
351 :param omit_header: do not include the ``msgid ""`` entry at the top of the | |
352 output | |
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
|
353 :sort_output: whether to sort the messages in the output by msgid |
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
|
354 :sort_by_file: whether to sort the messages in the output by their locations |
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
|
355 :ignore_obsolete: whether to ignore obsolete messages and not include them |
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
|
356 in the output; by default they are included as comments |
203 | 357 :param include_previous: include the old msgid as a comment when |
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
|
358 updating the catalog |
1 | 359 """ |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
360 def _normalize(key, prefix=''): |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
361 return normalize(key, prefix=prefix, width=width) \ |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
362 .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
|
363 |
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
|
364 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
|
365 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
|
366 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
|
367 fileobj.write(text) |
1 | 368 |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
369 def _write_comment(comment, prefix=''): |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
370 lines = comment |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
371 if width and width > 0: |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
372 lines = wrap(comment, width, break_long_words=False) |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
373 for line in lines: |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
374 _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
|
375 |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
376 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
|
377 if isinstance(message.id, (list, tuple)): |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
378 _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
|
379 _write('%smsgid_plural %s\n' % ( |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
380 prefix, _normalize(message.id[1], prefix) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
381 )) |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
382 for i, string in enumerate(message.string): |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
383 _write('%smsgstr[%d] %s\n' % ( |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
384 prefix, i, _normalize(message.string[i], prefix) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
385 )) |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
386 else: |
190
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
387 _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
|
388 _write('%smsgstr %s\n' % ( |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
389 prefix, _normalize(message.string or '', prefix) |
84193e6612f8
Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents:
181
diff
changeset
|
390 )) |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
391 |
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
|
392 messages = list(catalog) |
71 | 393 if sort_output: |
394 messages.sort(lambda x,y: cmp(x.id, y.id)) | |
395 elif sort_by_file: | |
396 messages.sort(lambda x,y: cmp(x.locations, y.locations)) | |
68 | 397 |
71 | 398 for message in messages: |
67 | 399 if not message.id: # This is the header "message" |
400 if omit_header: | |
401 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
|
402 comment_header = catalog.header_comment |
103
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
403 if width and width > 0: |
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
404 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
|
405 for line in comment_header.splitlines(): |
103
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
406 lines += wrap(line, width=width, subsequent_indent='# ', |
7cdf89eb9007
Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents:
102
diff
changeset
|
407 break_long_words=False) |
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
|
408 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
|
409 _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
|
410 |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
411 for comment in message.user_comments: |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
412 _write_comment(comment) |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
413 for comment in message.auto_comments: |
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
414 _write_comment(comment, prefix='.') |
1 | 415 |
416 if not no_location: | |
134 | 417 locs = u' '.join([u'%s:%d' % (filename.replace(os.sep, '/'), lineno) |
418 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
|
419 _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
|
420 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
|
421 _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
|
422 |
203 | 423 if message.previous_id and include_previous: |
424 _write_comment(u'msgid %s' % _normalize(message.previous_id[0]), | |
425 prefix='|') | |
426 if len(message.previous_id) > 1: | |
427 _write_comment(u'msgid_plural %s' % _normalize( | |
428 message.previous_id[1] | |
429 ), 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
|
430 |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
431 _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
|
432 _write('\n') |
181
9a1acb41e7dd
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
433 |
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
|
434 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
|
435 for message in catalog.obsolete.values(): |
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
|
436 for comment in message.user_comments: |
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
|
437 _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
|
438 _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
|
439 _write('\n') |