annotate babel/messages/pofile.py @ 507:b29ff192b610 stable-0.9.x

Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
author fschwarz
date Fri, 04 Mar 2011 13:14:03 +0000
parents 2c6f5e610e86
children
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Reading and writing of files in the ``gettext`` PO (portable object)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15 format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 :see: `The Format of PO Files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 <http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
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
60565dc8495d More fixes for Windows compatibility:
cmlenz
parents: 120
diff changeset
22 import os
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
23 import re
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
24
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25 from babel import __version__ as VERSION
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
26 from babel.messages.catalog import Catalog, Message
507
b29ff192b610 Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
fschwarz
parents: 476
diff changeset
27 from babel.util import set, wraptext, LOCALTZ
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
28
178
4407c6d1b7d7 Minor change to what symbols are ?exported?, primarily for the generated docs.
cmlenz
parents: 175
diff changeset
29 __all__ = ['read_po', 'write_po']
161
04c56e82c98b Slightly simplified CLI-frontend class.
cmlenz
parents: 158
diff changeset
30 __docformat__ = 'restructuredtext en'
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
31
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
32 def unescape(string):
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
33 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
34
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
35 >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"')
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
36 Say:
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
37 "hello, world!"
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
38 <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
39
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
40 :param string: the string to unescape
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
41 :return: the unescaped string
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
42 :rtype: `str` or `unicode`
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
43 """
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
44 return string[1:-1].replace('\\\\', '\\') \
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
45 .replace('\\t', '\t') \
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
46 .replace('\\r', '\r') \
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
47 .replace('\\n', '\n') \
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
48 .replace('\\"', '\"')
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
49
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
50 def denormalize(string):
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
51 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
52
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
53 >>> print denormalize(r'''""
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
54 ... "Say:\n"
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
55 ... " \"hello, world!\"\n"''')
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
56 Say:
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
57 "hello, world!"
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
58 <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
59
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
60 >>> print denormalize(r'''""
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
61 ... "Say:\n"
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
62 ... " \"Lorem ipsum dolor sit "
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
63 ... "amet, consectetur adipisicing"
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
64 ... " elit, \"\n"''')
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
65 Say:
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
66 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
67 <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
68
158
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
69 :param string: the string to denormalize
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
70 :return: the denormalized string
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
71 :rtype: `unicode` or `str`
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
72 """
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
73 if string.startswith('""'):
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
74 lines = []
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
75 for line in string.splitlines()[1:]:
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
76 lines.append(unescape(line))
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
77 return ''.join(lines)
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
78 else:
17dd31f104f5 Minor cleanup in the `pofile` module.
cmlenz
parents: 149
diff changeset
79 return unescape(string)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
80
199
ace575fff5ac 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
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
82 """Read messages from a ``gettext`` PO (portable object) file from the given
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
83 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
84
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
85 >>> 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
86 >>> 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
87 ... #: 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
88 ... #, 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
89 ... 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
90 ... 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
91 ...
94
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
92 ... # A user comment
b176f325d127 Updated `read_po` to add user comments besides just auto comments.
palgarvio
parents: 84
diff changeset
93 ... #. 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
94 ... #: 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
95 ... 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
96 ... 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
97 ... 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
98 ... 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
99 ... ''')
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
100 >>> 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
101 >>> 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
102
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
103 >>> for message in catalog:
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
104 ... if message.id:
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
105 ... 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
106 ... print ' ', (message.locations, message.flags)
f744dd56573d `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
ba5150e9544e Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents: 134
diff changeset
108 (u'foo %(name)s', '')
ba5150e9544e 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
f744dd56573d `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 104
diff changeset
110 ([], [])
149
ba5150e9544e Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents: 134
diff changeset
111 ((u'bar', u'baz'), ('', ''))
ba5150e9544e Respect charset specified in PO headers in `read_po()`. Fixes #17.
cmlenz
parents: 134
diff changeset
112 ([(u'main.py', 3)], set([]))
ba5150e9544e 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
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
114
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
115 :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
116 :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
117 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
118 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
119 :param domain: the message domain
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
120 :param ignore_obsolete: whether to ignore obsolete messages in the input
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
121 :return: an iterator over ``(message, translation, location)`` tuples
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
122 :rtype: ``iterator``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
123 """
196
93a922d31eca 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
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
125
196
93a922d31eca Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 191
diff changeset
126 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
127 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
128 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
129 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
130 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
131 flags = []
105
f744dd56573d `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 104
diff changeset
132 user_comments = []
f744dd56573d `Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents: 104
diff changeset
133 auto_comments = []
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
134 obsolete = [False]
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
135 in_msgid = [False]
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
136 in_msgstr = [False]
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
137
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
138 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
139 translations.sort()
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
140 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
141 msgid = tuple([denormalize(m) for m in messages])
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
142 else:
106
9b22b36066f6 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 105
diff changeset
143 msgid = denormalize(messages[0])
379
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
144 if isinstance(msgid, (list, tuple)):
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
145 string = []
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
146 for idx in range(catalog.num_plurals):
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
147 try:
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
148 string.append(translations[idx])
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
149 except IndexError:
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
150 string.append((idx, ''))
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
151 string = tuple([denormalize(t[1]) for t in string])
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
152 else:
106
9b22b36066f6 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 105
diff changeset
153 string = denormalize(translations[0][1])
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
154 message = Message(msgid, string, list(locations), set(flags),
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
155 auto_comments, user_comments, 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('['):
448
c6babc3bbc10 Backport r483. This fixes messages containing square brackets from failing
jruigrok
parents: 379
diff changeset
183 idx, msg = msg[1:].split(']', 1)
199
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()):
507
b29ff192b610 Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
fschwarz
parents: 476
diff changeset
194 line = line.strip()
b29ff192b610 Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
fschwarz
parents: 476
diff changeset
195 if not isinstance(line, unicode):
b29ff192b610 Python 2.3 compatibility: backporting r456 and r457 to 0.9 branch (see #233)
fschwarz
parents: 476
diff changeset
196 line = line.decode(catalog.charset)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
197 if line.startswith('#'):
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
198 in_msgid[0] = in_msgstr[0] = False
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
199 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
200 _add_message()
9b22b36066f6 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 105
diff changeset
201 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
202 for location in line[2:].lstrip().split():
369
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
203 pos = location.rfind(':')
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
204 if pos >= 0:
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
205 try:
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
206 lineno = int(location[pos + 1:])
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
207 except ValueError:
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
208 continue
c2ae38340540 Ported [388:405/trunk] to 0.9.x branch.
cmlenz
parents: 316
diff changeset
209 locations.append((location[:pos], lineno))
106
9b22b36066f6 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 105
diff changeset
210 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
211 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
212 flags.append(flag.strip())
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
213 elif line[1:].startswith('~'):
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
214 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
215 _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
216 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
217 # 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
218 comment = line[2:].strip()
199
ace575fff5ac Handle obsolete messages when parsing catalogs. Closes #32.
cmlenz
parents: 196
diff changeset
219 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
220 auto_comments.append(comment)
120
733cca7ff6a5 Added tests for `new_catalog` distutils command.
cmlenz
parents: 108
diff changeset
221 else:
106
9b22b36066f6 Fix for #16: the header message (`msgid = ""`) is now treated specially by `read_po` and `Catalog`.
cmlenz
parents: 105
diff changeset
222 # These are called user comments
120
733cca7ff6a5 Added tests for `new_catalog` distutils command.
cmlenz
parents: 108
diff changeset
223 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
224 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
225 _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
226
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
227 if messages:
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
228 _add_message()
196
93a922d31eca Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 191
diff changeset
229
93a922d31eca Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 191
diff changeset
230 # 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
231 # 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
232 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
233 messages.append(u'')
93a922d31eca Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 191
diff changeset
234 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
235 _add_message()
93a922d31eca Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents: 191
diff changeset
236
64
0406c51c5463 `read_po` now returns a `Catalog`.
cmlenz
parents: 56
diff changeset
237 return catalog
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
238
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
239 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
240 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
241 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
242 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
243 ')')
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 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
246 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
247 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
248
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
249 >>> 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
250 ... "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
251 ... ''')
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 '"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
253
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
254 :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
255 :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
256 :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
257 """
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
258 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
259 .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
260 .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
261 .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
262 .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
263
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
264 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
265 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
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 ... "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
269 ... ''', 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
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 " \"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
273
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
274 >>> 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
275 ... "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
276 ... ''', 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
277 ""
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 "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
279 " \"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
280 "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
281 " 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
282
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
283 :param string: the string to normalize
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
284 :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
285 :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
286 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
287 :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
288 :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
289 """
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 if width and width > 0:
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
291 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
292 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
293 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
294 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
295 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
296 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
297 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
298 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
299 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
300 while chunks:
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
301 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
302 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
303 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
304 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
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 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
307 # 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
308 # 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
317 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
318 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
319
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
320 # Remove empty trailing line
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
321 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
322 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
323 lines[-1] += '\n'
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
324 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
325
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
326 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
327 sort_output=False, sort_by_file=False, ignore_obsolete=False,
203
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
328 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
329 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
330 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
331
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
332 >>> 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
333 >>> 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
334 ... 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
335 >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
336 >>> from StringIO import StringIO
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
337 >>> 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
338 >>> write_po(buf, catalog, omit_header=True)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
339 >>> print buf.getvalue()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
340 #: 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
341 #, 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
342 msgid "foo %(name)s"
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
343 msgstr ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
344 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
345 #: main.py:3
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
346 msgid "bar"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
347 msgid_plural "baz"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
348 msgstr[0] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
349 msgstr[1] ""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
350 <BLANKLINE>
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
351 <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
352
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
353 :param fileobj: the file-like object to write to
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
354 :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
355 :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
356 0, or a negative number to completely disable line wrapping
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
357 :param no_location: do not emit a location comment for every message
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
358 :param omit_header: do not include the ``msgid ""`` entry at the top of the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
359 output
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
360 :param sort_output: whether to sort the messages in the output by msgid
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
361 :param sort_by_file: whether to sort the messages in the output by their
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
362 locations
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
363 :param ignore_obsolete: whether to ignore obsolete messages and not include
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
364 them in the output; by default they are included as
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
365 comments
203
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
366 :param include_previous: include the old msgid as a comment when
229
9a3f2acb55e6 Remove duplicate locations of catalog messages.
cmlenz
parents: 227
diff changeset
367 updating the catalog
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
368 """
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
369 def _normalize(key, prefix=''):
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
370 return normalize(key, prefix=prefix, width=width) \
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
371 .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
372
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
373 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
374 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
375 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
376 fileobj.write(text)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
377
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
378 def _write_comment(comment, prefix=''):
476
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
379 # xgettext always wraps comments even if --no-wrap is passed;
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
380 # provide the same behaviour
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
381 if width and width > 0:
476
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
382 _width = width
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
383 else:
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
384 _width = 76
2c6f5e610e86 Merged revisions 465 via svnmerge from
jruigrok
parents: 448
diff changeset
385 for line in wraptext(comment, _width):
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
386 _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
387
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
388 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
389 if isinstance(message.id, (list, tuple)):
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
390 _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
391 _write('%smsgid_plural %s\n' % (
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
392 prefix, _normalize(message.id[1], prefix)
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
393 ))
379
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
394
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
395 for idx in range(catalog.num_plurals):
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
396 try:
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
397 string = message.string[idx]
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
398 except IndexError:
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
399 string = ''
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
400 _write('%smsgstr[%d] %s\n' % (
379
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 369
diff changeset
401 prefix, idx, _normalize(string, prefix)
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
402 ))
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
403 else:
190
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
404 _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
405 _write('%smsgstr %s\n' % (
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
406 prefix, _normalize(message.string or '', prefix)
84193e6612f8 Correctly write out obsolete messages spanning multiple lines. Fixes #33.
cmlenz
parents: 181
diff changeset
407 ))
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
408
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
409 messages = list(catalog)
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
410 if sort_output:
248
bedaaeadc1db add a __cmp__ to Message that correctly sorts by id, taking into account plurals
pjenvey
parents: 229
diff changeset
411 messages.sort()
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
412 elif sort_by_file:
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
413 messages.sort(lambda x,y: cmp(x.locations, y.locations))
68
7e64668126d9 Add back POT header broken in previous check-in.
cmlenz
parents: 67
diff changeset
414
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 68
diff changeset
415 for message in messages:
67
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
416 if not message.id: # This is the header "message"
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
417 if omit_header:
5496b9127a07 Enhance catalog to also manage the MIME headers.
cmlenz
parents: 64
diff changeset
418 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
419 comment_header = catalog.header_comment
103
7cdf89eb9007 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 102
diff changeset
420 if width and width > 0:
7cdf89eb9007 Implement wrapping of header comments in PO(T) output. Related to #14.
cmlenz
parents: 102
diff changeset
421 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
422 for line in comment_header.splitlines():
316
3d67cf6d8022 Ported [349] to 0.9.x branch.
cmlenz
parents: 310
diff changeset
423 lines += wraptext(line, width=width,
3d67cf6d8022 Ported [349] to 0.9.x branch.
cmlenz
parents: 310
diff changeset
424 subsequent_indent='# ')
104
22f222e23b86 Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents: 103
diff changeset
425 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
426 _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
427
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
428 for comment in message.user_comments:
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
429 _write_comment(comment)
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
430 for comment in message.auto_comments:
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
431 _write_comment(comment, prefix='.')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
432
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
433 if not no_location:
134
60565dc8495d More fixes for Windows compatibility:
cmlenz
parents: 120
diff changeset
434 locs = u' '.join([u'%s:%d' % (filename.replace(os.sep, '/'), lineno)
60565dc8495d More fixes for Windows compatibility:
cmlenz
parents: 120
diff changeset
435 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
436 _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
437 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
438 _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
439
203
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
440 if message.previous_id and include_previous:
310
fc1fcf6b002c Ported [343] to stable branch.
cmlenz
parents: 248
diff changeset
441 _write_comment('msgid %s' % _normalize(message.previous_id[0]),
203
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
442 prefix='|')
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
443 if len(message.previous_id) > 1:
310
fc1fcf6b002c Ported [343] to stable branch.
cmlenz
parents: 248
diff changeset
444 _write_comment('msgid_plural %s' % _normalize(
203
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
445 message.previous_id[1]
e50aaaabb3d3 Minor changes to how previous msgids are processed.
cmlenz
parents: 200
diff changeset
446 ), 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
447
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
448 _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
449 _write('\n')
181
9a1acb41e7dd The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents: 178
diff changeset
450
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
451 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
452 for message in catalog.obsolete.values():
227
01dd895f396c Fix tests broken by [233], and add new tests.
cmlenz
parents: 226
diff changeset
453 for comment in message.user_comments:
191
cf09490f22b3 Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents: 190
diff changeset
454 _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
455 _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
456 _write('\n')
Copyright (C) 2012-2017 Edgewall Software