Mercurial > babel > mirror
annotate babel/messages/frontend.py @ 606:c5dd3752bf2a trunk
ensure .mo file header contains the same information as the source .po file (#199)
author | fschwarz |
---|---|
date | Mon, 27 Aug 2012 21:15:27 +0000 |
parents | 6dc7e067bafc |
children |
rev | line source |
---|---|
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1 #!/usr/bin/env python |
1 | 2 # -*- coding: utf-8 -*- |
3 # | |
530 | 4 # Copyright (C) 2007-2011 Edgewall Software |
1 | 5 # All rights reserved. |
6 # | |
7 # This software is licensed as described in the file COPYING, which | |
8 # you should have received as part of this distribution. The terms | |
9 # are also available at http://babel.edgewall.org/wiki/License. | |
10 # | |
11 # This software consists of voluntary contributions made by many | |
12 # individuals. For the exact contribution history, see the revision | |
13 # history and logs, available at http://babel.edgewall.org/log/. | |
14 | |
15 """Frontends for the message extraction functionality.""" | |
16 | |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
17 from ConfigParser import RawConfigParser |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
18 from datetime import datetime |
1 | 19 from distutils import log |
20 from distutils.cmd import Command | |
49
37bd476dafe4
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
48
diff
changeset
|
21 from distutils.errors import DistutilsOptionError, DistutilsSetupError |
298
03b4ed33d413
When using sys.stdout with a pipe or redirection the sys.stdout.encoding value
jruigrok
parents:
290
diff
changeset
|
22 from locale import getpreferredencoding |
232 | 23 import logging |
1 | 24 from optparse import OptionParser |
25 import os | |
605
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
26 import re |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
27 import shutil |
49
37bd476dafe4
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
48
diff
changeset
|
28 from StringIO import StringIO |
1 | 29 import sys |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
30 import tempfile |
1 | 31 |
32 from babel import __version__ as VERSION | |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
33 from babel import Locale, localedata |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
34 from babel.core import UnknownLocaleError |
56
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
35 from babel.messages.catalog import Catalog |
54
7dbcbc3f07e0
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
52
diff
changeset
|
36 from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \ |
7dbcbc3f07e0
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
52
diff
changeset
|
37 DEFAULT_MAPPING |
160 | 38 from babel.messages.mofile import write_mo |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
39 from babel.messages.pofile import read_po, write_po |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
40 from babel.util import odict, LOCALTZ |
1 | 41 |
178
749c0f6863bc
Minor change to what symbols are ?exported?, primarily for the generated docs.
cmlenz
parents:
177
diff
changeset
|
42 __all__ = ['CommandLineInterface', 'compile_catalog', 'extract_messages', |
234 | 43 'init_catalog', 'check_message_extractors', 'update_catalog'] |
1 | 44 __docformat__ = 'restructuredtext en' |
45 | |
46 | |
160 | 47 class compile_catalog(Command): |
48 """Catalog compilation command for use in ``setup.py`` scripts. | |
49 | |
50 If correctly installed, this command is available to Setuptools-using | |
51 setup scripts automatically. For projects using plain old ``distutils``, | |
52 the command needs to be registered explicitly in ``setup.py``:: | |
53 | |
54 from babel.messages.frontend import compile_catalog | |
55 | |
56 setup( | |
57 ... | |
169 | 58 cmdclass = {'compile_catalog': compile_catalog} |
160 | 59 ) |
60 | |
234 | 61 :since: version 0.9 |
160 | 62 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
63 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
64 """ | |
65 | |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
66 description = 'compile message catalogs to binary MO files' |
160 | 67 user_options = [ |
68 ('domain=', 'D', | |
69 "domain of PO file (default 'messages')"), | |
70 ('directory=', 'd', | |
71 'path to base directory containing the catalogs'), | |
72 ('input-file=', 'i', | |
73 'name of the input file'), | |
74 ('output-file=', 'o', | |
75 "name of the output file (default " | |
76 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), | |
77 ('locale=', 'l', | |
78 'locale of the catalog to compile'), | |
79 ('use-fuzzy', 'f', | |
80 'also include fuzzy translations'), | |
207 | 81 ('statistics', None, |
82 'print statistics about translations') | |
160 | 83 ] |
207 | 84 boolean_options = ['use-fuzzy', 'statistics'] |
160 | 85 |
86 def initialize_options(self): | |
87 self.domain = 'messages' | |
88 self.directory = None | |
89 self.input_file = None | |
90 self.output_file = None | |
91 self.locale = None | |
92 self.use_fuzzy = False | |
207 | 93 self.statistics = False |
160 | 94 |
95 def finalize_options(self): | |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
96 if not self.input_file and not self.directory: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
97 raise DistutilsOptionError('you must specify either the input file ' |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
98 'or the base directory') |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
99 if not self.output_file and not self.directory: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
100 raise DistutilsOptionError('you must specify either the input file ' |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
101 'or the base directory') |
160 | 102 |
103 def run(self): | |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
104 po_files = [] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
105 mo_files = [] |
175
5d32098d8352
Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents:
170
diff
changeset
|
106 |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
107 if not self.input_file: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
108 if self.locale: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
109 po_files.append((self.locale, |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
110 os.path.join(self.directory, self.locale, |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
111 'LC_MESSAGES', |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
112 self.domain + '.po'))) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
113 mo_files.append(os.path.join(self.directory, self.locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
114 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
115 self.domain + '.mo')) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
116 else: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
117 for locale in os.listdir(self.directory): |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
118 po_file = os.path.join(self.directory, locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
119 'LC_MESSAGES', self.domain + '.po') |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
120 if os.path.exists(po_file): |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
121 po_files.append((locale, po_file)) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
122 mo_files.append(os.path.join(self.directory, locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
123 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
124 self.domain + '.mo')) |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
125 else: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
126 po_files.append((self.locale, self.input_file)) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
127 if self.output_file: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
128 mo_files.append(self.output_file) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
129 else: |
288
b6efe6a8e2ec
Fix error in `compile_catalog` distutils command. Closes #65.
cmlenz
parents:
279
diff
changeset
|
130 mo_files.append(os.path.join(self.directory, self.locale, |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
131 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
132 self.domain + '.mo')) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
133 |
208
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
134 if not po_files: |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
135 raise DistutilsOptionError('no message catalogs found') |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
136 |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
137 for idx, (locale, po_file) in enumerate(po_files): |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
138 mo_file = mo_files[idx] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
139 infile = open(po_file, 'r') |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
140 try: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
141 catalog = read_po(infile, locale) |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
142 finally: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
143 infile.close() |
175
5d32098d8352
Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents:
170
diff
changeset
|
144 |
207 | 145 if self.statistics: |
146 translated = 0 | |
147 for message in list(catalog)[1:]: | |
148 if message.string: | |
149 translated +=1 | |
317 | 150 percentage = 0 |
151 if len(catalog): | |
152 percentage = translated * 100 // len(catalog) | |
231
052d29963c1c
Use proper logging in distutils `compile_catalog` command.
cmlenz
parents:
220
diff
changeset
|
153 log.info('%d of %d messages (%d%%) translated in %r', |
317 | 154 translated, len(catalog), percentage, po_file) |
207 | 155 |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
156 if catalog.fuzzy and not self.use_fuzzy: |
231
052d29963c1c
Use proper logging in distutils `compile_catalog` command.
cmlenz
parents:
220
diff
changeset
|
157 log.warn('catalog %r is marked as fuzzy, skipping', po_file) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
158 continue |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
159 |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
160 for message, errors in catalog.check(): |
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
161 for error in errors: |
231
052d29963c1c
Use proper logging in distutils `compile_catalog` command.
cmlenz
parents:
220
diff
changeset
|
162 log.error('error: %s:%d: %s', po_file, message.lineno, |
052d29963c1c
Use proper logging in distutils `compile_catalog` command.
cmlenz
parents:
220
diff
changeset
|
163 error) |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
164 |
231
052d29963c1c
Use proper logging in distutils `compile_catalog` command.
cmlenz
parents:
220
diff
changeset
|
165 log.info('compiling catalog %r to %r', po_file, mo_file) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
166 |
279 | 167 outfile = open(mo_file, 'wb') |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
168 try: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
169 write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy) |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
170 finally: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
171 outfile.close() |
160 | 172 |
173 | |
1 | 174 class extract_messages(Command): |
175 """Message extraction command for use in ``setup.py`` scripts. | |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
176 |
1 | 177 If correctly installed, this command is available to Setuptools-using |
178 setup scripts automatically. For projects using plain old ``distutils``, | |
179 the command needs to be registered explicitly in ``setup.py``:: | |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
180 |
54
7dbcbc3f07e0
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
52
diff
changeset
|
181 from babel.messages.frontend import extract_messages |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
182 |
1 | 183 setup( |
184 ... | |
185 cmdclass = {'extract_messages': extract_messages} | |
186 ) | |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
187 |
1 | 188 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
189 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
190 """ | |
191 | |
192 description = 'extract localizable strings from the project code' | |
193 user_options = [ | |
194 ('charset=', None, | |
195 'charset to use in the output file'), | |
196 ('keywords=', 'k', | |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
197 'space-separated list of keywords to look for in addition to the ' |
1 | 198 'defaults'), |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
199 ('no-default-keywords', None, |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
200 'do not include the default keywords'), |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
201 ('mapping-file=', 'F', |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
202 'path to the mapping configuration file'), |
1 | 203 ('no-location', None, |
204 'do not include location comments with filename and line number'), | |
205 ('omit-header', None, | |
206 'do not include msgid "" entry in header'), | |
5
132526dcd074
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
207 ('output-file=', 'o', |
1 | 208 'name of the output file'), |
23 | 209 ('width=', 'w', |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
210 'set output line width (default 76)'), |
23 | 211 ('no-wrap', None, |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
212 'do not break long message lines, longer than the output line width, ' |
58 | 213 'into several lines'), |
71 | 214 ('sort-output', None, |
215 'generate sorted output (default False)'), | |
216 ('sort-by-file', None, | |
217 'sort output by file location (default False)'), | |
78
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
218 ('msgid-bugs-address=', None, |
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
219 'set report address for msgid'), |
79
450ac2291ca5
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
78
diff
changeset
|
220 ('copyright-holder=', None, |
450ac2291ca5
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
78
diff
changeset
|
221 'set copyright holder in output'), |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
222 ('add-comments=', 'c', |
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
223 'place comment block with TAG (or those preceding keyword lines) in ' |
604 | 224 'output file. Separate multiple TAGs with commas(,)'), |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
225 ('strip-comments', None, |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
226 'strip the comment TAGs from the comments.'), |
59 | 227 ('input-dirs=', None, |
605
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
228 'directories that should be scanned for messages. Separate multiple ' |
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
229 'directories with commas(,)'), |
1 | 230 ] |
23 | 231 boolean_options = [ |
71 | 232 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap', |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
233 'sort-output', 'sort-by-file', 'strip-comments' |
23 | 234 ] |
1 | 235 |
236 def initialize_options(self): | |
237 self.charset = 'utf-8' | |
117
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
238 self.keywords = '' |
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
239 self._keywords = DEFAULT_KEYWORDS.copy() |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
240 self.no_default_keywords = False |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
241 self.mapping_file = None |
1 | 242 self.no_location = False |
243 self.omit_header = False | |
244 self.output_file = None | |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
245 self.input_dirs = None |
423
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
246 self.width = None |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
247 self.no_wrap = False |
71 | 248 self.sort_output = False |
249 self.sort_by_file = False | |
78
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
250 self.msgid_bugs_address = None |
79
450ac2291ca5
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
78
diff
changeset
|
251 self.copyright_holder = None |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
252 self.add_comments = None |
95
f9007588a860
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
90
diff
changeset
|
253 self._add_comments = [] |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
254 self.strip_comments = False |
1 | 255 |
256 def finalize_options(self): | |
23 | 257 if self.no_default_keywords and not self.keywords: |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
258 raise DistutilsOptionError('you must specify new keywords if you ' |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
259 'disable the default ones') |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
260 if self.no_default_keywords: |
23 | 261 self._keywords = {} |
117
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
262 if self.keywords: |
23 | 263 self._keywords.update(parse_keywords(self.keywords.split())) |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
264 |
117
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
265 if not self.output_file: |
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
266 raise DistutilsOptionError('no output file specified') |
23 | 267 if self.no_wrap and self.width: |
71 | 268 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
269 "exclusive") |
423
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
270 if not self.no_wrap and not self.width: |
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
271 self.width = 76 |
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
272 elif self.width is not None: |
23 | 273 self.width = int(self.width) |
95
f9007588a860
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
90
diff
changeset
|
274 |
71 | 275 if self.sort_output and self.sort_by_file: |
276 raise DistutilsOptionError("'--sort-output' and '--sort-by-file' " | |
277 "are mutually exclusive") | |
1 | 278 |
605
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
279 if self.input_dirs: |
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
280 self.input_dirs = re.split(',\s*', self.input_dirs) |
6dc7e067bafc
fix 'input_dirs' option for setuptools integration (#232, initial patch by ?tienne Bersac)
fschwarz
parents:
604
diff
changeset
|
281 else: |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
282 self.input_dirs = dict.fromkeys([k.split('.',1)[0] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
283 for k in self.distribution.packages |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
284 ]).keys() |
95
f9007588a860
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
90
diff
changeset
|
285 |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
286 if self.add_comments: |
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
287 self._add_comments = self.add_comments.split(',') |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
288 |
1 | 289 def run(self): |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
290 mappings = self._get_mappings() |
1 | 291 outfile = open(self.output_file, 'w') |
292 try: | |
102
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
293 catalog = Catalog(project=self.distribution.get_name(), |
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
294 version=self.distribution.get_version(), |
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
295 msgid_bugs_address=self.msgid_bugs_address, |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
296 copyright_holder=self.copyright_holder, |
102
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
297 charset=self.charset) |
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
298 |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
299 for dirname, (method_map, options_map) in mappings.items(): |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
300 def callback(filename, method, options): |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
301 if method == 'ignore': |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
302 return |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
303 filepath = os.path.normpath(os.path.join(dirname, filename)) |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
304 optstr = '' |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
305 if options: |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
306 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for |
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
307 k, v in options.items()]) |
232 | 308 log.info('extracting messages from %s%s', filepath, optstr) |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
309 |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
310 extracted = extract_from_dir(dirname, method_map, options_map, |
117
e1dffa3423a0
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
112
diff
changeset
|
311 keywords=self._keywords, |
84
3ae316b58231
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
80
diff
changeset
|
312 comment_tags=self._add_comments, |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
313 callback=callback, |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
314 strip_comment_tags= |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
315 self.strip_comments) |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
316 for filename, lineno, message, comments, context in extracted: |
57
d930a3dfbf3d
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
56
diff
changeset
|
317 filepath = os.path.normpath(os.path.join(dirname, filename)) |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
318 catalog.add(message, None, [(filepath, lineno)], |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
319 auto_comments=comments, context=context) |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
320 |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
321 log.info('writing PO template file to %s' % self.output_file) |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
322 write_po(outfile, catalog, width=self.width, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
323 no_location=self.no_location, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
324 omit_header=self.omit_header, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
325 sort_output=self.sort_output, |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
326 sort_by_file=self.sort_by_file) |
1 | 327 finally: |
328 outfile.close() | |
329 | |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
330 def _get_mappings(self): |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
331 mappings = {} |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
332 |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
333 if self.mapping_file: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
334 fileobj = open(self.mapping_file, 'U') |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
335 try: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
336 method_map, options_map = parse_mapping(fileobj) |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
337 for dirname in self.input_dirs: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
338 mappings[dirname] = method_map, options_map |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
339 finally: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
340 fileobj.close() |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
341 |
123
31ca37101a24
Make the check for the `message_extractors` setup keyword more robst.
cmlenz
parents:
120
diff
changeset
|
342 elif getattr(self.distribution, 'message_extractors', None): |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
343 message_extractors = self.distribution.message_extractors |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
344 for dirname, mapping in message_extractors.items(): |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
345 if isinstance(mapping, basestring): |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
346 method_map, options_map = parse_mapping(StringIO(mapping)) |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
347 else: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
348 method_map, options_map = [], {} |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
349 for pattern, method, options in mapping: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
350 method_map.append((pattern, method)) |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
351 options_map[pattern] = options or {} |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
352 mappings[dirname] = method_map, options_map |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
353 |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
354 else: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
355 for dirname in self.input_dirs: |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
356 mappings[dirname] = DEFAULT_MAPPING, {} |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
357 |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
358 return mappings |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
359 |
1 | 360 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
361 def check_message_extractors(dist, name, value): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
362 """Validate the ``message_extractors`` keyword argument to ``setup()``. |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
363 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
364 :param dist: the distutils/setuptools ``Distribution`` object |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
365 :param name: the name of the keyword argument (should always be |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
366 "message_extractors") |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
367 :param value: the value of the keyword argument |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
368 :raise `DistutilsSetupError`: if the value is not valid |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
369 :see: `Adding setup() arguments |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
370 <http://peak.telecommunity.com/DevCenter/setuptools#adding-setup-arguments>`_ |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
371 """ |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
372 assert name == 'message_extractors' |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
373 if not isinstance(value, dict): |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
374 raise DistutilsSetupError('the value of the "message_extractors" ' |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
375 'parameter must be a dictionary') |
1 | 376 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
377 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
378 class init_catalog(Command): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
379 """New catalog initialization command for use in ``setup.py`` scripts. |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
380 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
381 If correctly installed, this command is available to Setuptools-using |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
382 setup scripts automatically. For projects using plain old ``distutils``, |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
383 the command needs to be registered explicitly in ``setup.py``:: |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
384 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
385 from babel.messages.frontend import init_catalog |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
386 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
387 setup( |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
388 ... |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
389 cmdclass = {'init_catalog': init_catalog} |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
390 ) |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
391 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
392 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
393 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
394 """ |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
395 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
396 description = 'create a new catalog based on a POT file' |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
397 user_options = [ |
55
a258e4015eb6
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
398 ('domain=', 'D', |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
399 "domain of PO file (default 'messages')"), |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
400 ('input-file=', 'i', |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
401 'name of the input file'), |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
402 ('output-dir=', 'd', |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
403 'path to output directory'), |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
404 ('output-file=', 'o', |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
405 "name of the output file (default " |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
406 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
407 ('locale=', 'l', |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
408 'locale for the new localized catalog'), |
600 | 409 ('width=', 'w', |
410 'set output line width (default 76)'), | |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
411 ('no-wrap', None, |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
412 'do not break long message lines, longer than the output line width, ' |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
413 'into several lines'), |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
414 ] |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
415 boolean_options = ['no-wrap'] |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
416 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
417 def initialize_options(self): |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
418 self.output_dir = None |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
419 self.output_file = None |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
420 self.input_file = None |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
421 self.locale = None |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
422 self.domain = 'messages' |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
423 self.no_wrap = False |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
424 self.width = None |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
425 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
426 def finalize_options(self): |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
427 if not self.input_file: |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
428 raise DistutilsOptionError('you must specify the input file') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
429 |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
430 if not self.locale: |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
431 raise DistutilsOptionError('you must provide a locale for the ' |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
432 'new catalog') |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
433 try: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
434 self._locale = Locale.parse(self.locale) |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
435 except UnknownLocaleError, e: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
436 raise DistutilsOptionError(e) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
437 |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
438 if not self.output_file and not self.output_dir: |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
439 raise DistutilsOptionError('you must specify the output directory') |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
440 if not self.output_file: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
441 self.output_file = os.path.join(self.output_dir, self.locale, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
442 'LC_MESSAGES', self.domain + '.po') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
443 |
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
444 if not os.path.exists(os.path.dirname(self.output_file)): |
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
445 os.makedirs(os.path.dirname(self.output_file)) |
601
3eeaadb290be
raise exception if setuptools integration for init catalog uses both 'width' and 'no-wrap' parameter
fschwarz
parents:
600
diff
changeset
|
446 if self.no_wrap and self.width: |
3eeaadb290be
raise exception if setuptools integration for init catalog uses both 'width' and 'no-wrap' parameter
fschwarz
parents:
600
diff
changeset
|
447 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " |
3eeaadb290be
raise exception if setuptools integration for init catalog uses both 'width' and 'no-wrap' parameter
fschwarz
parents:
600
diff
changeset
|
448 "exclusive") |
600 | 449 if not self.no_wrap and not self.width: |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
450 self.width = 76 |
600 | 451 elif self.width is not None: |
452 self.width = int(self.width) | |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
453 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
454 def run(self): |
90 | 455 log.info('creating catalog %r based on %r', self.output_file, |
55
a258e4015eb6
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
456 self.input_file) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
457 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
458 infile = open(self.input_file, 'r') |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
459 try: |
370
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
338
diff
changeset
|
460 # Although reading from the catalog template, read_po must be fed |
563 | 461 # the locale in order to correctly calculate plurals |
370
bc18179832b7
We no longer neglect `catalog.plurals`. Added tests for it. Fixes #120.
palgarvio
parents:
338
diff
changeset
|
462 catalog = read_po(infile, locale=self.locale) |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
463 finally: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
464 infile.close() |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
465 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
466 catalog.locale = self._locale |
606
c5dd3752bf2a
ensure .mo file header contains the same information as the source .po file (#199)
fschwarz
parents:
605
diff
changeset
|
467 catalog.revision_date = datetime.now(LOCALTZ) |
251 | 468 catalog.fuzzy = False |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
469 |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
470 outfile = open(self.output_file, 'w') |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
471 try: |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
472 write_po(outfile, catalog, width=self.width) |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
473 finally: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
474 outfile.close() |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
475 |
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
476 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
477 class update_catalog(Command): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
478 """Catalog merging command for use in ``setup.py`` scripts. |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
479 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
480 If correctly installed, this command is available to Setuptools-using |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
481 setup scripts automatically. For projects using plain old ``distutils``, |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
482 the command needs to be registered explicitly in ``setup.py``:: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
483 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
484 from babel.messages.frontend import update_catalog |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
485 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
486 setup( |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
487 ... |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
488 cmdclass = {'update_catalog': update_catalog} |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
489 ) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
490 |
234 | 491 :since: version 0.9 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
492 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
493 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
494 """ |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
495 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
496 description = 'update message catalogs from a POT file' |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
497 user_options = [ |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
498 ('domain=', 'D', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
499 "domain of PO file (default 'messages')"), |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
500 ('input-file=', 'i', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
501 'name of the input file'), |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
502 ('output-dir=', 'd', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
503 'path to base directory containing the catalogs'), |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
504 ('output-file=', 'o', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
505 "name of the output file (default " |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
506 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
507 ('locale=', 'l', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
508 'locale of the catalog to compile'), |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
509 ('width=', 'w', |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
510 'set output line width (default 76)'), |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
511 ('no-wrap', None, |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
512 'do not break long message lines, longer than the output line width, ' |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
513 'into several lines'), |
191
c171a0041ad2
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
185
diff
changeset
|
514 ('ignore-obsolete=', None, |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
515 'whether to omit obsolete messages from the output'), |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
516 ('no-fuzzy-matching', 'N', |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
517 'do not use fuzzy matching'), |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
518 ('previous', None, |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
519 'keep previous msgids of translated messages') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
520 ] |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
521 boolean_options = ['ignore_obsolete', 'no_fuzzy_matching', 'previous'] |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
522 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
523 def initialize_options(self): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
524 self.domain = 'messages' |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
525 self.input_file = None |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
526 self.output_dir = None |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
527 self.output_file = None |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
528 self.locale = None |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
529 self.width = None |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
530 self.no_wrap = False |
191
c171a0041ad2
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
185
diff
changeset
|
531 self.ignore_obsolete = False |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
532 self.no_fuzzy_matching = False |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
533 self.previous = False |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
534 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
535 def finalize_options(self): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
536 if not self.input_file: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
537 raise DistutilsOptionError('you must specify the input file') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
538 if not self.output_file and not self.output_dir: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
539 raise DistutilsOptionError('you must specify the output file or ' |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
540 'directory') |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
541 if self.output_file and not self.locale: |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
542 raise DistutilsOptionError('you must specify the locale') |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
543 if self.no_wrap and self.width: |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
544 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
545 "exclusive") |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
546 if not self.no_wrap and not self.width: |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
547 self.width = 76 |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
548 elif self.width is not None: |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
549 self.width = int(self.width) |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
550 if self.no_fuzzy_matching and self.previous: |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
551 self.previous = False |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
552 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
553 def run(self): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
554 po_files = [] |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
555 if not self.output_file: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
556 if self.locale: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
557 po_files.append((self.locale, |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
558 os.path.join(self.output_dir, self.locale, |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
559 'LC_MESSAGES', |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
560 self.domain + '.po'))) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
561 else: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
562 for locale in os.listdir(self.output_dir): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
563 po_file = os.path.join(self.output_dir, locale, |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
564 'LC_MESSAGES', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
565 self.domain + '.po') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
566 if os.path.exists(po_file): |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
567 po_files.append((locale, po_file)) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
568 else: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
569 po_files.append((self.locale, self.output_file)) |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
570 |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
571 domain = self.domain |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
572 if not domain: |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
573 domain = os.path.splitext(os.path.basename(self.input_file))[0] |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
574 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
575 infile = open(self.input_file, 'U') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
576 try: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
577 template = read_po(infile) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
578 finally: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
579 infile.close() |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
580 |
208
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
581 if not po_files: |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
582 raise DistutilsOptionError('no message catalogs found') |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
583 |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
584 for locale, filename in po_files: |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
585 log.info('updating catalog %r based on %r', filename, |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
586 self.input_file) |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
587 infile = open(filename, 'U') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
588 try: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
589 catalog = read_po(infile, locale=locale, domain=domain) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
590 finally: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
591 infile.close() |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
592 |
204 | 593 catalog.update(template, self.no_fuzzy_matching) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
594 |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
595 tmpname = os.path.join(os.path.dirname(filename), |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
596 tempfile.gettempprefix() + |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
597 os.path.basename(filename)) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
598 tmpfile = open(tmpname, 'w') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
599 try: |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
600 try: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
601 write_po(tmpfile, catalog, |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
602 ignore_obsolete=self.ignore_obsolete, |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
603 include_previous=self.previous, width=self.width) |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
604 finally: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
605 tmpfile.close() |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
606 except: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
607 os.remove(tmpname) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
608 raise |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
609 |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
610 try: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
611 os.rename(tmpname, filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
612 except OSError: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
613 # We're probably on Windows, which doesn't support atomic |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
614 # renames, at least not through Python |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
615 # If the error is in fact due to a permissions problem, that |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
616 # same error is going to be raised from one of the following |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
617 # operations |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
618 os.remove(filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
619 shutil.copy(tmpname, filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
620 os.remove(tmpname) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
621 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
622 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
623 class CommandLineInterface(object): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
624 """Command-line interface. |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
625 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
626 This class provides a simple command-line interface to the message |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
627 extraction and PO file generation functionality. |
51
d484eb9a70d5
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
49
diff
changeset
|
628 """ |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
629 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
630 usage = '%%prog %s [options] %s' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
631 version = '%%prog %s' % VERSION |
161 | 632 commands = { |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
633 'compile': 'compile message catalogs to MO files', |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
634 'extract': 'extract messages from source files and generate a POT file', |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
635 'init': 'create new message catalogs from a POT file', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
636 'update': 'update existing message catalogs from a POT file' |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
637 } |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
638 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
639 def run(self, argv=sys.argv): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
640 """Main entry point of the command-line interface. |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
641 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
642 :param argv: list of arguments passed on the command-line |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
643 """ |
127 | 644 self.parser = OptionParser(usage=self.usage % ('command', '[args]'), |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
645 version=self.version) |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
646 self.parser.disable_interspersed_args() |
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
647 self.parser.print_help = self._help |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
648 self.parser.add_option('--list-locales', dest='list_locales', |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
649 action='store_true', |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
650 help="print all known locales and exit") |
232 | 651 self.parser.add_option('-v', '--verbose', action='store_const', |
652 dest='loglevel', const=logging.DEBUG, | |
653 help='print as much as possible') | |
654 self.parser.add_option('-q', '--quiet', action='store_const', | |
655 dest='loglevel', const=logging.ERROR, | |
656 help='print as little as possible') | |
657 self.parser.set_defaults(list_locales=False, loglevel=logging.INFO) | |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
658 |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
659 options, args = self.parser.parse_args(argv[1:]) |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
660 |
523
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
661 self._configure_logging(options.loglevel) |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
662 if options.list_locales: |
546
fd3bda2d3d34
rename babel.localedata.list() to ease Python 3 transition (fixes #250)
fschwarz
parents:
533
diff
changeset
|
663 identifiers = localedata.locale_identifiers() |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
664 longest = max([len(identifier) for identifier in identifiers]) |
440
2e8fa2145a13
remove sorted and don't assume dict ordering (Python 2.3 & Jython compat)
pjenvey
parents:
429
diff
changeset
|
665 identifiers.sort() |
210
9c237f83d7cb
When parsing catalog headers, look for the content-type first, to be able to use a specified encoding on all other headers.
cmlenz
parents:
209
diff
changeset
|
666 format = u'%%-%ds %%s' % (longest + 1) |
440
2e8fa2145a13
remove sorted and don't assume dict ordering (Python 2.3 & Jython compat)
pjenvey
parents:
429
diff
changeset
|
667 for identifier in identifiers: |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
668 locale = Locale.parse(identifier) |
267
385083341519
Default the encoding of output by the `pybabel --list-locales` command to ASCII with errors replaced, if the output stream has no encoding set. For a reason unknown to me I got an encoding error, but only when when running the output through a pipe on the shell.
cmlenz
parents:
251
diff
changeset
|
669 output = format % (identifier, locale.english_name) |
298
03b4ed33d413
When using sys.stdout with a pipe or redirection the sys.stdout.encoding value
jruigrok
parents:
290
diff
changeset
|
670 print output.encode(sys.stdout.encoding or |
03b4ed33d413
When using sys.stdout with a pipe or redirection the sys.stdout.encoding value
jruigrok
parents:
290
diff
changeset
|
671 getpreferredencoding() or |
03b4ed33d413
When using sys.stdout with a pipe or redirection the sys.stdout.encoding value
jruigrok
parents:
290
diff
changeset
|
672 'ascii', 'replace') |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
673 return 0 |
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
674 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
675 if not args: |
424
e24e6a4d3512
Use a more explicit error message if no option or argument(command) is passed to `pybabel`. Fixes #81.
palgarvio
parents:
423
diff
changeset
|
676 self.parser.error('no valid command or option passed. ' |
e24e6a4d3512
Use a more explicit error message if no option or argument(command) is passed to `pybabel`. Fixes #81.
palgarvio
parents:
423
diff
changeset
|
677 'Try the -h/--help option for more information.') |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
678 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
679 cmdname = args[0] |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
680 if cmdname not in self.commands: |
127 | 681 self.parser.error('unknown command "%s"' % cmdname) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
682 |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
683 return getattr(self, cmdname)(args[1:]) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
684 |
523
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
685 def _configure_logging(self, loglevel): |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
686 self.log = logging.getLogger('babel') |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
687 self.log.setLevel(loglevel) |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
688 # Don't add a new handler for every instance initialization (#227), this |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
689 # would cause duplicated output when the CommandLineInterface as an |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
690 # normal Python class. |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
691 if self.log.handlers: |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
692 handler = self.log.handlers[0] |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
693 else: |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
694 handler = logging.StreamHandler() |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
695 self.log.addHandler(handler) |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
696 handler.setLevel(loglevel) |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
697 formatter = logging.Formatter('%(message)s') |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
698 handler.setFormatter(formatter) |
e5a2231ce176
shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
fschwarz
parents:
521
diff
changeset
|
699 |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
700 def _help(self): |
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
701 print self.parser.format_help() |
127 | 702 print "commands:" |
63
47353672f1a8
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
62
diff
changeset
|
703 longest = max([len(command) for command in self.commands]) |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
704 format = " %%-%ds %%s" % max(8, longest + 1) |
161 | 705 commands = self.commands.items() |
706 commands.sort() | |
707 for name, description in commands: | |
708 print format % (name, description) | |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
709 |
160 | 710 def compile(self, argv): |
711 """Subcommand for compiling a message catalog to a MO file. | |
712 | |
713 :param argv: the command arguments | |
234 | 714 :since: version 0.9 |
160 | 715 """ |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
716 parser = OptionParser(usage=self.usage % ('compile', ''), |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
717 description=self.commands['compile']) |
160 | 718 parser.add_option('--domain', '-D', dest='domain', |
719 help="domain of MO and PO files (default '%default')") | |
720 parser.add_option('--directory', '-d', dest='directory', | |
721 metavar='DIR', help='base directory of catalog files') | |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
722 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
723 help='locale of the catalog') |
160 | 724 parser.add_option('--input-file', '-i', dest='input_file', |
725 metavar='FILE', help='name of the input file') | |
726 parser.add_option('--output-file', '-o', dest='output_file', | |
727 metavar='FILE', | |
728 help="name of the output file (default " | |
729 "'<output_dir>/<locale>/LC_MESSAGES/" | |
730 "<domain>.mo')") | |
731 parser.add_option('--use-fuzzy', '-f', dest='use_fuzzy', | |
732 action='store_true', | |
733 help='also include fuzzy translations (default ' | |
207 | 734 '%default)') |
735 parser.add_option('--statistics', dest='statistics', | |
736 action='store_true', | |
737 help='print statistics about translations') | |
160 | 738 |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
739 parser.set_defaults(domain='messages', use_fuzzy=False, |
207 | 740 compile_all=False, statistics=False) |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
741 options, args = parser.parse_args(argv) |
160 | 742 |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
743 po_files = [] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
744 mo_files = [] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
745 if not options.input_file: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
746 if not options.directory: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
747 parser.error('you must specify either the input file or the ' |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
748 'base directory') |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
749 if options.locale: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
750 po_files.append((options.locale, |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
751 os.path.join(options.directory, |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
752 options.locale, 'LC_MESSAGES', |
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
753 options.domain + '.po'))) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
754 mo_files.append(os.path.join(options.directory, options.locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
755 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
756 options.domain + '.mo')) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
757 else: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
758 for locale in os.listdir(options.directory): |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
759 po_file = os.path.join(options.directory, locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
760 'LC_MESSAGES', options.domain + '.po') |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
761 if os.path.exists(po_file): |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
762 po_files.append((locale, po_file)) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
763 mo_files.append(os.path.join(options.directory, locale, |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
764 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
765 options.domain + '.mo')) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
766 else: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
767 po_files.append((options.locale, options.input_file)) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
768 if options.output_file: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
769 mo_files.append(options.output_file) |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
770 else: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
771 if not options.directory: |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
772 parser.error('you must specify either the input file or ' |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
773 'the base directory') |
290 | 774 mo_files.append(os.path.join(options.directory, options.locale, |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
775 'LC_MESSAGES', |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
776 options.domain + '.mo')) |
208
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
777 if not po_files: |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
778 parser.error('no message catalogs found') |
160 | 779 |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
780 for idx, (locale, po_file) in enumerate(po_files): |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
781 mo_file = mo_files[idx] |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
782 infile = open(po_file, 'r') |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
783 try: |
331
afc5070a623c
Fix message catalog compilation for locales with more than two plural forms. Closes #95. Many thanks to Victor Safronovich for the patch.
cmlenz
parents:
317
diff
changeset
|
784 catalog = read_po(infile, locale) |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
785 finally: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
786 infile.close() |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
787 |
207 | 788 if options.statistics: |
789 translated = 0 | |
790 for message in list(catalog)[1:]: | |
791 if message.string: | |
792 translated +=1 | |
317 | 793 percentage = 0 |
794 if len(catalog): | |
795 percentage = translated * 100 // len(catalog) | |
232 | 796 self.log.info("%d of %d messages (%d%%) translated in %r", |
317 | 797 translated, len(catalog), percentage, po_file) |
207 | 798 |
175
5d32098d8352
Changed the `__repr__` output to include the flags(it can be changed back, but it was usefull to implement the fuzzy header parsing).
palgarvio
parents:
170
diff
changeset
|
799 if catalog.fuzzy and not options.use_fuzzy: |
232 | 800 self.log.warn('catalog %r is marked as fuzzy, skipping', |
801 po_file) | |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
802 continue |
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
803 |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
804 for message, errors in catalog.check(): |
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
805 for error in errors: |
232 | 806 self.log.error('error: %s:%d: %s', po_file, message.lineno, |
807 error) | |
220
97b4b289e792
Added infrastructure for adding catalog checkers, and implement a checker that validations Python format parameters in translations, closing #19.
cmlenz
parents:
210
diff
changeset
|
808 |
232 | 809 self.log.info('compiling catalog %r to %r', po_file, mo_file) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
810 |
279 | 811 outfile = open(mo_file, 'wb') |
170
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
812 try: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
813 write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy) |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
814 finally: |
3f9d9268537d
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
169
diff
changeset
|
815 outfile.close() |
160 | 816 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
817 def extract(self, argv): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
818 """Subcommand for extracting messages from source files and generating |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
819 a POT file. |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
820 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
821 :param argv: the command arguments |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
822 """ |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
823 parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...'), |
161 | 824 description=self.commands['extract']) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
825 parser.add_option('--charset', dest='charset', |
160 | 826 help='charset to use in the output (default ' |
827 '"%default")') | |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
828 parser.add_option('-k', '--keyword', dest='keywords', action='append', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
829 help='keywords to look for in addition to the ' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
830 'defaults. You can specify multiple -k flags on ' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
831 'the command line.') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
832 parser.add_option('--no-default-keywords', dest='no_default_keywords', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
833 action='store_true', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
834 help="do not include the default keywords") |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
835 parser.add_option('--mapping', '-F', dest='mapping_file', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
836 help='path to the extraction mapping file') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
837 parser.add_option('--no-location', dest='no_location', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
838 action='store_true', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
839 help='do not include location comments with filename ' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
840 'and line number') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
841 parser.add_option('--omit-header', dest='omit_header', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
842 action='store_true', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
843 help='do not include msgid "" entry in header') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
844 parser.add_option('-o', '--output', dest='output', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
845 help='path to the output POT file') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
846 parser.add_option('-w', '--width', dest='width', type='int', |
423
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
847 help="set output line width (default 76)") |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
848 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true', |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
849 help='do not break long message lines, longer than ' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
850 'the output line width, into several lines') |
71 | 851 parser.add_option('--sort-output', dest='sort_output', |
852 action='store_true', | |
853 help='generate sorted output (default False)') | |
854 parser.add_option('--sort-by-file', dest='sort_by_file', | |
855 action='store_true', | |
856 help='sort output by file location (default False)') | |
78
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
857 parser.add_option('--msgid-bugs-address', dest='msgid_bugs_address', |
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
858 metavar='EMAIL@ADDRESS', |
d0d8d6cd8601
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
71
diff
changeset
|
859 help='set report address for msgid') |
79
450ac2291ca5
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
78
diff
changeset
|
860 parser.add_option('--copyright-holder', dest='copyright_holder', |
450ac2291ca5
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
78
diff
changeset
|
861 help='set copyright holder in output') |
429
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
862 parser.add_option('--project', dest='project', |
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
863 help='set project name in output') |
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
864 parser.add_option('--version', dest='version', |
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
865 help='set project version in output') |
95
f9007588a860
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
90
diff
changeset
|
866 parser.add_option('--add-comments', '-c', dest='comment_tags', |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
867 metavar='TAG', action='append', |
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
868 help='place comment block with TAG (or those ' |
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
869 'preceding keyword lines) in output file. One ' |
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
870 'TAG per argument call') |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
871 parser.add_option('--strip-comment-tags', '-s', |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
872 dest='strip_comment_tags', action='store_true', |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
873 help='Strip the comment tags from the comments.') |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
874 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
875 parser.set_defaults(charset='utf-8', keywords=[], |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
876 no_default_keywords=False, no_location=False, |
423
fb926f48efba
Now, the `--width` option, although with a default value of 76, it's not set to any value initially so that the `--no-wrap` option can be passed without throwing an error. Fixes #145.
palgarvio
parents:
417
diff
changeset
|
877 omit_header = False, width=None, no_wrap=False, |
80
116e34b8cefa
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
79
diff
changeset
|
878 sort_output=False, sort_by_file=False, |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
879 comment_tags=[], strip_comment_tags=False) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
880 options, args = parser.parse_args(argv) |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
881 if not args: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
882 parser.error('incorrect number of arguments') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
883 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
884 if options.output not in (None, '-'): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
885 outfile = open(options.output, 'w') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
886 else: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
887 outfile = sys.stdout |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
888 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
889 keywords = DEFAULT_KEYWORDS.copy() |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
890 if options.no_default_keywords: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
891 if not options.keywords: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
892 parser.error('you must specify new keywords if you disable the ' |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
893 'default ones') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
894 keywords = {} |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
895 if options.keywords: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
896 keywords.update(parse_keywords(options.keywords)) |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
897 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
898 if options.mapping_file: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
899 fileobj = open(options.mapping_file, 'U') |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
900 try: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
901 method_map, options_map = parse_mapping(fileobj) |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
902 finally: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
903 fileobj.close() |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
904 else: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
905 method_map = DEFAULT_MAPPING |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
906 options_map = {} |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
907 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
908 if options.width and options.no_wrap: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
909 parser.error("'--no-wrap' and '--width' are mutually exclusive.") |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
910 elif not options.width and not options.no_wrap: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
911 options.width = 76 |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
912 |
71 | 913 if options.sort_output and options.sort_by_file: |
914 parser.error("'--sort-output' and '--sort-by-file' are mutually " | |
915 "exclusive") | |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
916 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
917 try: |
429
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
918 catalog = Catalog(project=options.project, |
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
919 version=options.version, |
28755da4bf13
add `--project` and `--version` options for commandline (fixes #173)
dfraser
parents:
424
diff
changeset
|
920 msgid_bugs_address=options.msgid_bugs_address, |
105
c62b68a0b65e
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
104
diff
changeset
|
921 copyright_holder=options.copyright_holder, |
102
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
922 charset=options.charset) |
14a3d766a701
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
95
diff
changeset
|
923 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
924 for dirname in args: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
925 if not os.path.isdir(dirname): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
926 parser.error('%r is not a directory' % dirname) |
232 | 927 |
928 def callback(filename, method, options): | |
929 if method == 'ignore': | |
930 return | |
931 filepath = os.path.normpath(os.path.join(dirname, filename)) | |
932 optstr = '' | |
933 if options: | |
934 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for | |
935 k, v in options.items()]) | |
936 self.log.info('extracting messages from %s%s', filepath, | |
937 optstr) | |
938 | |
95
f9007588a860
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
90
diff
changeset
|
939 extracted = extract_from_dir(dirname, method_map, options_map, |
232 | 940 keywords, options.comment_tags, |
338
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
941 callback=callback, |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
942 strip_comment_tags= |
b39145076d8a
Stripping of comment tags is optional now. If enabled it will strip the tags from all lines of a comment now.
aronacher
parents:
335
diff
changeset
|
943 options.strip_comment_tags) |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
944 for filename, lineno, message, comments, context in extracted: |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
945 filepath = os.path.normpath(os.path.join(dirname, filename)) |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
946 catalog.add(message, None, [(filepath, lineno)], |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
947 auto_comments=comments, context=context) |
56
f40fc143439c
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
55
diff
changeset
|
948 |
232 | 949 if options.output not in (None, '-'): |
950 self.log.info('writing PO template file to %s' % options.output) | |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
951 write_po(outfile, catalog, width=options.width, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
952 no_location=options.no_location, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
953 omit_header=options.omit_header, |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
954 sort_output=options.sort_output, |
112
5c55ca587c89
copyright_holder arg is no longer needed for write_po
pjenvey
parents:
110
diff
changeset
|
955 sort_by_file=options.sort_by_file) |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
956 finally: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
957 if options.output: |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
958 outfile.close() |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
959 |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
960 def init(self, argv): |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
961 """Subcommand for creating new message catalogs from a template. |
177
beb8a9eabe6d
* Instead of an extra `compile-all` option on the `compile` (or `compile_catalog`) command, compilation of all catalogs is performed when neither an `input_file` nor a `locale` is specified.
cmlenz
parents:
176
diff
changeset
|
962 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
963 :param argv: the command arguments |
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
964 """ |
165
628bc271ece4
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
161
diff
changeset
|
965 parser = OptionParser(usage=self.usage % ('init', ''), |
161 | 966 description=self.commands['init']) |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
967 parser.add_option('--domain', '-D', dest='domain', |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
968 help="domain of PO file (default '%default')") |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
969 parser.add_option('--input-file', '-i', dest='input_file', |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
970 metavar='FILE', help='name of the input file') |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
971 parser.add_option('--output-dir', '-d', dest='output_dir', |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
972 metavar='DIR', help='path to output directory') |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
973 parser.add_option('--output-file', '-o', dest='output_file', |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
974 metavar='FILE', |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
975 help="name of the output file (default " |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
976 "'<output_dir>/<locale>/LC_MESSAGES/" |
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
977 "<domain>.po')") |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
978 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
979 help='locale for the new localized catalog') |
600 | 980 parser.add_option('-w', '--width', dest='width', type='int', |
981 help="set output line width (default 76)") | |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
982 parser.add_option('--no-wrap', dest='no_wrap', action='store_true', |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
983 help='do not break long message lines, longer than ' |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
984 'the output line width, into several lines') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
985 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
986 parser.set_defaults(domain='messages') |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
987 options, args = parser.parse_args(argv) |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
988 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
989 if not options.locale: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
990 parser.error('you must provide a locale for the new catalog') |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
991 try: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
992 locale = Locale.parse(options.locale) |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
993 except UnknownLocaleError, e: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
994 parser.error(e) |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
995 |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
996 if not options.input_file: |
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
997 parser.error('you must specify the input file') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
998 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
999 if not options.output_file and not options.output_dir: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1000 parser.error('you must specify the output file or directory') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
1001 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1002 if not options.output_file: |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
1003 options.output_file = os.path.join(options.output_dir, |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1004 options.locale, 'LC_MESSAGES', |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
1005 options.domain + '.po') |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
1006 if not os.path.exists(os.path.dirname(options.output_file)): |
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
1007 os.makedirs(os.path.dirname(options.output_file)) |
600 | 1008 if options.width and options.no_wrap: |
1009 parser.error("'--no-wrap' and '--width' are mutually exclusive.") | |
1010 elif not options.width and not options.no_wrap: | |
1011 options.width = 76 | |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
1012 |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
1013 infile = open(options.input_file, 'r') |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1014 try: |
371 | 1015 # Although reading from the catalog template, read_po must be fed |
563 | 1016 # the locale in order to correctly calculate plurals |
371 | 1017 catalog = read_po(infile, locale=options.locale) |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1018 finally: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1019 infile.close() |
87
72c7ac384609
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
86
diff
changeset
|
1020 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1021 catalog.locale = locale |
132 | 1022 catalog.revision_date = datetime.now(LOCALTZ) |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
1023 |
232 | 1024 self.log.info('creating catalog %r based on %r', options.output_file, |
1025 options.input_file) | |
66
2504451884f3
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
63
diff
changeset
|
1026 |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1027 outfile = open(options.output_file, 'w') |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1028 try: |
600 | 1029 write_po(outfile, catalog, width=options.width) |
104
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1030 finally: |
395704fda00b
Merged `write_pot` and `write_po` functions by moving more functionality to the `Catalog` class. This is certainly not perfect yet, but moves us in the right direction.
cmlenz
parents:
103
diff
changeset
|
1031 outfile.close() |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1032 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1033 def update(self, argv): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1034 """Subcommand for updating existing message catalogs from a template. |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1035 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1036 :param argv: the command arguments |
234 | 1037 :since: version 0.9 |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1038 """ |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1039 parser = OptionParser(usage=self.usage % ('update', ''), |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1040 description=self.commands['update']) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1041 parser.add_option('--domain', '-D', dest='domain', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1042 help="domain of PO file (default '%default')") |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1043 parser.add_option('--input-file', '-i', dest='input_file', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1044 metavar='FILE', help='name of the input file') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1045 parser.add_option('--output-dir', '-d', dest='output_dir', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1046 metavar='DIR', help='path to output directory') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1047 parser.add_option('--output-file', '-o', dest='output_file', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1048 metavar='FILE', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1049 help="name of the output file (default " |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1050 "'<output_dir>/<locale>/LC_MESSAGES/" |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1051 "<domain>.po')") |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1052 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1053 help='locale of the translations catalog') |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1054 parser.add_option('-w', '--width', dest='width', type='int', |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1055 help="set output line width (default 76)") |
574
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
1056 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true', |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
1057 help='do not break long message lines, longer than ' |
99c48a6ca1d6
add "--no-wrap" option for init/update commands (#289)
fschwarz
parents:
569
diff
changeset
|
1058 'the output line width, into several lines') |
191
c171a0041ad2
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
185
diff
changeset
|
1059 parser.add_option('--ignore-obsolete', dest='ignore_obsolete', |
c171a0041ad2
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
185
diff
changeset
|
1060 action='store_true', |
c171a0041ad2
Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
cmlenz
parents:
185
diff
changeset
|
1061 help='do not include obsolete messages in the output ' |
417 | 1062 '(default %default)') |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1063 parser.add_option('--no-fuzzy-matching', '-N', dest='no_fuzzy_matching', |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1064 action='store_true', |
417 | 1065 help='do not use fuzzy matching (default %default)') |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1066 parser.add_option('--previous', dest='previous', action='store_true', |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1067 help='keep previous msgids of translated messages ' |
417 | 1068 '(default %default)') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1069 |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1070 parser.set_defaults(domain='messages', ignore_obsolete=False, |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1071 no_fuzzy_matching=False, previous=False) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1072 options, args = parser.parse_args(argv) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1073 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1074 if not options.input_file: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1075 parser.error('you must specify the input file') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1076 if not options.output_file and not options.output_dir: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1077 parser.error('you must specify the output file or directory') |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1078 if options.output_file and not options.locale: |
417 | 1079 parser.error('you must specify the locale') |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1080 if options.no_fuzzy_matching and options.previous: |
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1081 options.previous = False |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1082 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1083 po_files = [] |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1084 if not options.output_file: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1085 if options.locale: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1086 po_files.append((options.locale, |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1087 os.path.join(options.output_dir, |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1088 options.locale, 'LC_MESSAGES', |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1089 options.domain + '.po'))) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1090 else: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1091 for locale in os.listdir(options.output_dir): |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1092 po_file = os.path.join(options.output_dir, locale, |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1093 'LC_MESSAGES', |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1094 options.domain + '.po') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1095 if os.path.exists(po_file): |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1096 po_files.append((locale, po_file)) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1097 else: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1098 po_files.append((options.locale, options.output_file)) |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1099 |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1100 domain = options.domain |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1101 if not domain: |
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1102 domain = os.path.splitext(os.path.basename(options.input_file))[0] |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1103 |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1104 infile = open(options.input_file, 'U') |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1105 try: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1106 template = read_po(infile) |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1107 finally: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1108 infile.close() |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1109 |
208
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
1110 if not po_files: |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
1111 parser.error('no message catalogs found') |
d17f4b876f95
Make frontends that make use of a ''loop all'' fail if no message catalogs are found.
palgarvio
parents:
207
diff
changeset
|
1112 |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1113 if options.width and options.no_wrap: |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1114 parser.error("'--no-wrap' and '--width' are mutually exclusive.") |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1115 elif not options.width and not options.no_wrap: |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1116 options.width = 76 |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1117 for locale, filename in po_files: |
232 | 1118 self.log.info('updating catalog %r based on %r', filename, |
1119 options.input_file) | |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1120 infile = open(filename, 'U') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1121 try: |
196
b38a6b220ea2
Fix for #35, and a minor improvement to how we parse the catalog fuzzy bit.
cmlenz
parents:
191
diff
changeset
|
1122 catalog = read_po(infile, locale=locale, domain=domain) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1123 finally: |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1124 infile.close() |
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1125 |
204 | 1126 catalog.update(template, options.no_fuzzy_matching) |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1127 |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1128 tmpname = os.path.join(os.path.dirname(filename), |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1129 tempfile.gettempprefix() + |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1130 os.path.basename(filename)) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1131 tmpfile = open(tmpname, 'w') |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1132 try: |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1133 try: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1134 write_po(tmpfile, catalog, |
200
1c778cccd330
Added `--no-fuzzy-matching` to the frontends and also `--previous` which adds the old msgid's as comments. The latest closes #31.
palgarvio
parents:
197
diff
changeset
|
1135 ignore_obsolete=options.ignore_obsolete, |
602
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1136 include_previous=options.previous, |
7a8ee579f31e
"update" command now supports "--width" option (#284)
fschwarz
parents:
601
diff
changeset
|
1137 width=options.width) |
197
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1138 finally: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1139 tmpfile.close() |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1140 except: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1141 os.remove(tmpname) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1142 raise |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1143 |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1144 try: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1145 os.rename(tmpname, filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1146 except OSError: |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1147 # We're probably on Windows, which doesn't support atomic |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1148 # renames, at least not through Python |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1149 # If the error is in fact due to a permissions problem, that |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1150 # same error is going to be raised from one of the following |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1151 # operations |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1152 os.remove(filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1153 shutil.copy(tmpname, filename) |
0eb3eb447256
Fix for #36: avoid corrupting the catalog on update when there's an error in the writing process.
cmlenz
parents:
196
diff
changeset
|
1154 os.remove(tmpname) |
181
8a762ce37bf7
The frontends now provide ways to update existing translations catalogs from a template. Closes #22.
cmlenz
parents:
178
diff
changeset
|
1155 |
165
628bc271ece4
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
161
diff
changeset
|
1156 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1157 def main(): |
185
6503a227ba93
Add a command-line option that prints out all available locales. Closes #24.
cmlenz
parents:
181
diff
changeset
|
1158 return CommandLineInterface().run(sys.argv) |
1 | 1159 |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1160 def parse_mapping(fileobj, filename=None): |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1161 """Parse an extraction method mapping from a file-like object. |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1162 |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1163 >>> buf = StringIO(''' |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1164 ... [extractors] |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1165 ... custom = mypackage.module:myfunc |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1166 ... |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1167 ... # Python source files |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1168 ... [python: **.py] |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1169 ... |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1170 ... # Genshi templates |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1171 ... [genshi: **/templates/**.html] |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1172 ... include_attrs = |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1173 ... [genshi: **/templates/**.txt] |
144 | 1174 ... template_class = genshi.template:TextTemplate |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1175 ... encoding = latin-1 |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1176 ... |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1177 ... # Some custom extractor |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1178 ... [custom: **/custom/*.*] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1179 ... ''') |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1180 |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1181 >>> method_map, options_map = parse_mapping(buf) |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1182 >>> len(method_map) |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1183 4 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1184 |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1185 >>> method_map[0] |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1186 ('**.py', 'python') |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1187 >>> options_map['**.py'] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1188 {} |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1189 >>> method_map[1] |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1190 ('**/templates/**.html', 'genshi') |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1191 >>> options_map['**/templates/**.html']['include_attrs'] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1192 '' |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1193 >>> method_map[2] |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1194 ('**/templates/**.txt', 'genshi') |
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1195 >>> options_map['**/templates/**.txt']['template_class'] |
144 | 1196 'genshi.template:TextTemplate' |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1197 >>> options_map['**/templates/**.txt']['encoding'] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1198 'latin-1' |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1199 |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1200 >>> method_map[3] |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1201 ('**/custom/*.*', 'mypackage.module:myfunc') |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1202 >>> options_map['**/custom/*.*'] |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1203 {} |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1204 |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1205 :param fileobj: a readable file-like object containing the configuration |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1206 text to parse |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1207 :return: a `(method_map, options_map)` tuple |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1208 :rtype: `tuple` |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1209 :see: `extract_from_directory` |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1210 """ |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1211 extractors = {} |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1212 method_map = [] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1213 options_map = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1214 |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1215 parser = RawConfigParser() |
62
2df27f49c320
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
1216 parser._sections = odict(parser._sections) # We need ordered sections |
48
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1217 parser.readfp(fileobj, filename) |
22b90b3b161a
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
47
diff
changeset
|
1218 for section in parser.sections(): |
250
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1219 if section == 'extractors': |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1220 extractors = dict(parser.items(section)) |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1221 else: |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1222 method, pattern = [part.strip() for part in section.split(':', 1)] |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1223 method_map.append((pattern, method)) |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1224 options_map[pattern] = dict(parser.items(section)) |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1225 |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1226 if extractors: |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1227 for idx, (pattern, method) in enumerate(method_map): |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1228 if method in extractors: |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1229 method = extractors[method] |
6c06570af1b9
Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
cmlenz
parents:
234
diff
changeset
|
1230 method_map[idx] = (pattern, method) |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1231 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1232 return (method_map, options_map) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
1233 |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1234 def parse_keywords(strings=[]): |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1235 """Parse keywords specifications from the given list of strings. |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1236 |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1237 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items() |
414
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
371
diff
changeset
|
1238 >>> kw.sort() |
05487ae7696e
fix Python 2.3 compat: rearrange set/itemgetter/rsplit/sorted/unicode.decode
pjenvey
parents:
371
diff
changeset
|
1239 >>> for keyword, indices in kw: |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1240 ... print (keyword, indices) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1241 ('_', None) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1242 ('dgettext', (2,)) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1243 ('dngettext', (2, 3)) |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1244 ('pgettext', ((1, 'c'), 2)) |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1245 """ |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1246 keywords = {} |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1247 for string in strings: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1248 if ':' in string: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1249 funcname, indices = string.split(':') |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1250 else: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1251 funcname, indices = string, None |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1252 if funcname not in keywords: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1253 if indices: |
569
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1254 inds = [] |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1255 for x in indices.split(','): |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1256 if x[-1] == 'c': |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1257 inds.append((int(x[:-1]), 'c')) |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1258 else: |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1259 inds.append(int(x)) |
1b801a0cb2cb
Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
fschwarz
parents:
563
diff
changeset
|
1260 indices = tuple(inds) |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1261 keywords[funcname] = indices |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1262 return keywords |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
1263 |
52
6b9c32893007
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
51
diff
changeset
|
1264 |
1 | 1265 if __name__ == '__main__': |
55
a258e4015eb6
`new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents:
54
diff
changeset
|
1266 main() |