Mercurial > babel > old > mirror
annotate babel/messages/frontend.py @ 172:91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
author | palgarvio |
---|---|
date | Fri, 22 Jun 2007 17:56:02 +0000 |
parents | d8106be26e52 |
children | 47f6c31e9a24 |
rev | line source |
---|---|
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
1 #!/usr/bin/env python |
3 | 2 # -*- coding: utf-8 -*- |
3 # | |
4 # Copyright (C) 2007 Edgewall Software | |
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 | |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
17 from ConfigParser import RawConfigParser |
106
2a00e352c986
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:
105
diff
changeset
|
18 from datetime import datetime |
3 | 19 from distutils import log |
20 from distutils.cmd import Command | |
51
3664c93860f1
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
50
diff
changeset
|
21 from distutils.errors import DistutilsOptionError, DistutilsSetupError |
3 | 22 from optparse import OptionParser |
23 import os | |
49
9979a409589e
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:
26
diff
changeset
|
24 import re |
51
3664c93860f1
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
50
diff
changeset
|
25 from StringIO import StringIO |
3 | 26 import sys |
27 | |
28 from babel import __version__ as VERSION | |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
29 from babel import Locale |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
30 from babel.core import UnknownLocaleError |
58
068952b4d4c0
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
57
diff
changeset
|
31 from babel.messages.catalog import Catalog |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
32 from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \ |
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
33 DEFAULT_MAPPING |
162 | 34 from babel.messages.mofile import write_mo |
106
2a00e352c986
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:
105
diff
changeset
|
35 from babel.messages.pofile import read_po, write_po |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
36 from babel.messages.plurals import PLURALS |
106
2a00e352c986
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:
105
diff
changeset
|
37 from babel.util import odict, LOCALTZ |
3 | 38 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
39 __all__ = ['CommandLineInterface', 'extract_messages', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
40 'check_message_extractors', 'main'] |
3 | 41 __docformat__ = 'restructuredtext en' |
42 | |
43 | |
162 | 44 class compile_catalog(Command): |
45 """Catalog compilation command for use in ``setup.py`` scripts. | |
46 | |
47 If correctly installed, this command is available to Setuptools-using | |
48 setup scripts automatically. For projects using plain old ``distutils``, | |
49 the command needs to be registered explicitly in ``setup.py``:: | |
50 | |
51 from babel.messages.frontend import compile_catalog | |
52 | |
53 setup( | |
54 ... | |
171 | 55 cmdclass = {'compile_catalog': compile_catalog} |
162 | 56 ) |
57 | |
58 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ | |
59 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
60 """ | |
61 | |
62 description = 'compile a catalog to a binary MO file' | |
63 user_options = [ | |
64 ('domain=', 'D', | |
65 "domain of PO file (default 'messages')"), | |
66 ('directory=', 'd', | |
67 'path to base directory containing the catalogs'), | |
68 ('input-file=', 'i', | |
69 'name of the input file'), | |
70 ('output-file=', 'o', | |
71 "name of the output file (default " | |
72 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), | |
73 ('locale=', 'l', | |
74 'locale of the catalog to compile'), | |
75 ('use-fuzzy', 'f', | |
76 'also include fuzzy translations'), | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
77 ('compile-all', 'A', |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
78 "compile all available PO's"), |
162 | 79 ] |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
80 boolean_options = ['use-fuzzy', 'compile-all'] |
162 | 81 |
82 def initialize_options(self): | |
83 self.domain = 'messages' | |
84 self.directory = None | |
85 self.input_file = None | |
86 self.output_file = None | |
87 self.locale = None | |
88 self.use_fuzzy = False | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
89 self.compile_all = False |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
90 self._available_pos = [] |
162 | 91 |
92 def finalize_options(self): | |
93 if not self.locale: | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
94 if self.compile_all and not self.directory: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
95 raise DistutilsOptionError('you must specify the locale for the' |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
96 ' catalog to compile') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
97 elif self.compile_all and self.directory: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
98 for locale in os.listdir(self.directory): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
99 po_path = os.path.join(self.directory, locale, |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
100 'LC_MESSAGES', self.domain + '.po') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
101 if os.path.exists(po_path): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
102 self._available_pos.append(po_path) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
103 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
104 raise DistutilsOptionError('you must specify the locale for the' |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
105 ' catalog to compile') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
106 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
107 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
108 self._locale = Locale.parse(self.locale) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
109 except UnknownLocaleError, e: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
110 raise DistutilsOptionError(e) |
162 | 111 |
112 if not self.directory and not self.input_file: | |
113 raise DistutilsOptionError('you must specify the input file') | |
114 if not self.input_file: | |
115 self.input_file = os.path.join(self.directory, self.locale, | |
116 'LC_MESSAGES', self.domain + '.po') | |
117 | |
118 if not self.directory and not self.output_file: | |
119 raise DistutilsOptionError('you must specify the output file') | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
120 if not self.output_file and not self.compile_all: |
162 | 121 self.output_file = os.path.join(self.directory, self.locale, |
122 'LC_MESSAGES', self.domain + '.mo') | |
123 | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
124 if not self.compile_all: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
125 if not os.path.exists(os.path.dirname(self.output_file)): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
126 os.makedirs(os.path.dirname(self.output_file)) |
162 | 127 |
128 def run(self): | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
129 if self.compile_all: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
130 for po in self._available_pos: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
131 log.info('compiling catalog to %s', po.replace('.po', '.mo')) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
132 |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
133 infile = open(po, 'r') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
134 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
135 catalog = read_po(infile) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
136 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
137 infile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
138 |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
139 outfile = open(po.replace('.po', '.mo'), 'w') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
140 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
141 write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
142 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
143 outfile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
144 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
145 log.info('compiling catalog to %s', self.output_file) |
162 | 146 |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
147 infile = open(self.input_file, 'r') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
148 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
149 catalog = read_po(infile) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
150 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
151 infile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
152 |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
153 outfile = open(self.output_file, 'w') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
154 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
155 write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
156 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
157 outfile.close() |
162 | 158 |
159 | |
3 | 160 class extract_messages(Command): |
161 """Message extraction command for use in ``setup.py`` scripts. | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
162 |
3 | 163 If correctly installed, this command is available to Setuptools-using |
164 setup scripts automatically. For projects using plain old ``distutils``, | |
165 the command needs to be registered explicitly in ``setup.py``:: | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
166 |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
167 from babel.messages.frontend import extract_messages |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
168 |
3 | 169 setup( |
170 ... | |
171 cmdclass = {'extract_messages': extract_messages} | |
172 ) | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
173 |
3 | 174 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
175 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
176 """ | |
177 | |
178 description = 'extract localizable strings from the project code' | |
179 user_options = [ | |
180 ('charset=', None, | |
181 'charset to use in the output file'), | |
182 ('keywords=', 'k', | |
12
0ce673c24cb1
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:
9
diff
changeset
|
183 'space-separated list of keywords to look for in addition to the ' |
3 | 184 'defaults'), |
12
0ce673c24cb1
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:
9
diff
changeset
|
185 ('no-default-keywords', None, |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
186 'do not include the default keywords'), |
49
9979a409589e
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:
26
diff
changeset
|
187 ('mapping-file=', 'F', |
9979a409589e
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:
26
diff
changeset
|
188 'path to the mapping configuration file'), |
3 | 189 ('no-location', None, |
190 'do not include location comments with filename and line number'), | |
191 ('omit-header', None, | |
192 'do not include msgid "" entry in header'), | |
7
8d7b3077e6d1
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
3
diff
changeset
|
193 ('output-file=', 'o', |
3 | 194 'name of the output file'), |
25 | 195 ('width=', 'w', |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
196 'set output line width (default 76)'), |
25 | 197 ('no-wrap', None, |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
198 'do not break long message lines, longer than the output line width, ' |
60 | 199 'into several lines'), |
73 | 200 ('sort-output', None, |
201 'generate sorted output (default False)'), | |
202 ('sort-by-file', None, | |
203 'sort output by file location (default False)'), | |
80
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
204 ('msgid-bugs-address=', None, |
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
205 'set report address for msgid'), |
81
51f73a110a84
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
80
diff
changeset
|
206 ('copyright-holder=', None, |
51f73a110a84
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
80
diff
changeset
|
207 'set copyright holder in output'), |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
208 ('add-comments=', 'c', |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
209 'place comment block with TAG (or those preceding keyword lines) in ' |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
210 'output file. Seperate multiple TAGs with commas(,)'), |
61 | 211 ('input-dirs=', None, |
59
e68fd956ebce
* 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:
58
diff
changeset
|
212 'directories that should be scanned for messages'), |
3 | 213 ] |
25 | 214 boolean_options = [ |
73 | 215 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap', |
216 'sort-output', 'sort-by-file' | |
25 | 217 ] |
3 | 218 |
219 def initialize_options(self): | |
220 self.charset = 'utf-8' | |
119
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
221 self.keywords = '' |
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
222 self._keywords = DEFAULT_KEYWORDS.copy() |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
223 self.no_default_keywords = False |
49
9979a409589e
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:
26
diff
changeset
|
224 self.mapping_file = None |
3 | 225 self.no_location = False |
226 self.omit_header = False | |
227 self.output_file = None | |
59
e68fd956ebce
* 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:
58
diff
changeset
|
228 self.input_dirs = None |
49
9979a409589e
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:
26
diff
changeset
|
229 self.width = 76 |
9979a409589e
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:
26
diff
changeset
|
230 self.no_wrap = False |
73 | 231 self.sort_output = False |
232 self.sort_by_file = False | |
80
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
233 self.msgid_bugs_address = None |
81
51f73a110a84
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
80
diff
changeset
|
234 self.copyright_holder = None |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
235 self.add_comments = None |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
236 self._add_comments = [] |
3 | 237 |
238 def finalize_options(self): | |
25 | 239 if self.no_default_keywords and not self.keywords: |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
240 raise DistutilsOptionError('you must specify new keywords if you ' |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
241 'disable the default ones') |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
242 if self.no_default_keywords: |
25 | 243 self._keywords = {} |
119
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
244 if self.keywords: |
25 | 245 self._keywords.update(parse_keywords(self.keywords.split())) |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
246 |
119
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
247 if not self.output_file: |
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
248 raise DistutilsOptionError('no output file specified') |
25 | 249 if self.no_wrap and self.width: |
73 | 250 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
251 "exclusive") |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
252 if self.no_wrap: |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
253 self.width = None |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
254 else: |
25 | 255 self.width = int(self.width) |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
256 |
73 | 257 if self.sort_output and self.sort_by_file: |
258 raise DistutilsOptionError("'--sort-output' and '--sort-by-file' " | |
259 "are mutually exclusive") | |
3 | 260 |
59
e68fd956ebce
* 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:
58
diff
changeset
|
261 if not self.input_dirs: |
e68fd956ebce
* 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:
58
diff
changeset
|
262 self.input_dirs = dict.fromkeys([k.split('.',1)[0] |
e68fd956ebce
* 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:
58
diff
changeset
|
263 for k in self.distribution.packages |
e68fd956ebce
* 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:
58
diff
changeset
|
264 ]).keys() |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
265 |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
266 if self.add_comments: |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
267 self._add_comments = self.add_comments.split(',') |
59
e68fd956ebce
* 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:
58
diff
changeset
|
268 |
3 | 269 def run(self): |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
270 mappings = self._get_mappings() |
3 | 271 outfile = open(self.output_file, 'w') |
272 try: | |
104
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
273 catalog = Catalog(project=self.distribution.get_name(), |
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
274 version=self.distribution.get_version(), |
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
275 msgid_bugs_address=self.msgid_bugs_address, |
107
4b42e23644e5
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
106
diff
changeset
|
276 copyright_holder=self.copyright_holder, |
104
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
277 charset=self.charset) |
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
278 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
279 for dirname, (method_map, options_map) in mappings.items(): |
59
e68fd956ebce
* 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:
58
diff
changeset
|
280 def callback(filename, method, options): |
e68fd956ebce
* 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:
58
diff
changeset
|
281 if method == 'ignore': |
e68fd956ebce
* 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:
58
diff
changeset
|
282 return |
e68fd956ebce
* 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:
58
diff
changeset
|
283 filepath = os.path.normpath(os.path.join(dirname, filename)) |
e68fd956ebce
* 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:
58
diff
changeset
|
284 optstr = '' |
e68fd956ebce
* 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:
58
diff
changeset
|
285 if options: |
e68fd956ebce
* 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:
58
diff
changeset
|
286 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for |
e68fd956ebce
* 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:
58
diff
changeset
|
287 k, v in options.items()]) |
e68fd956ebce
* 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:
58
diff
changeset
|
288 log.info('extracting messages from %s%s' |
e68fd956ebce
* 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:
58
diff
changeset
|
289 % (filepath, optstr)) |
49
9979a409589e
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:
26
diff
changeset
|
290 |
59
e68fd956ebce
* 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:
58
diff
changeset
|
291 extracted = extract_from_dir(dirname, method_map, options_map, |
119
c84f629da9de
Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
cmlenz
parents:
114
diff
changeset
|
292 keywords=self._keywords, |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
82
diff
changeset
|
293 comment_tags=self._add_comments, |
59
e68fd956ebce
* 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:
58
diff
changeset
|
294 callback=callback) |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
295 for filename, lineno, message, comments in extracted: |
59
e68fd956ebce
* 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:
58
diff
changeset
|
296 filepath = os.path.normpath(os.path.join(dirname, filename)) |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
297 catalog.add(message, None, [(filepath, lineno)], |
107
4b42e23644e5
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
106
diff
changeset
|
298 auto_comments=comments) |
26
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
299 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
300 log.info('writing PO template file to %s' % self.output_file) |
106
2a00e352c986
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:
105
diff
changeset
|
301 write_po(outfile, catalog, width=self.width, |
2a00e352c986
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:
105
diff
changeset
|
302 no_location=self.no_location, |
2a00e352c986
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:
105
diff
changeset
|
303 omit_header=self.omit_header, |
2a00e352c986
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:
105
diff
changeset
|
304 sort_output=self.sort_output, |
107
4b42e23644e5
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
106
diff
changeset
|
305 sort_by_file=self.sort_by_file) |
3 | 306 finally: |
307 outfile.close() | |
308 | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
309 def _get_mappings(self): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
310 mappings = {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
311 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
312 if self.mapping_file: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
313 fileobj = open(self.mapping_file, 'U') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
314 try: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
315 method_map, options_map = parse_mapping(fileobj) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
316 for dirname in self.input_dirs: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
317 mappings[dirname] = method_map, options_map |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
318 finally: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
319 fileobj.close() |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
320 |
125
0053443e7cb2
Make the check for the `message_extractors` setup keyword more robst.
cmlenz
parents:
122
diff
changeset
|
321 elif getattr(self.distribution, 'message_extractors', None): |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
322 message_extractors = self.distribution.message_extractors |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
323 for dirname, mapping in message_extractors.items(): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
324 if isinstance(mapping, basestring): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
325 method_map, options_map = parse_mapping(StringIO(mapping)) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
326 else: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
327 method_map, options_map = [], {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
328 for pattern, method, options in mapping: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
329 method_map.append((pattern, method)) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
330 options_map[pattern] = options or {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
331 mappings[dirname] = method_map, options_map |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
332 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
333 else: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
334 for dirname in self.input_dirs: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
335 mappings[dirname] = DEFAULT_MAPPING, {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
336 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
337 return mappings |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
338 |
3 | 339 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
340 def check_message_extractors(dist, name, value): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
341 """Validate the ``message_extractors`` keyword argument to ``setup()``. |
49
9979a409589e
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:
26
diff
changeset
|
342 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
343 :param dist: the distutils/setuptools ``Distribution`` object |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
344 :param name: the name of the keyword argument (should always be |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
345 "message_extractors") |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
346 :param value: the value of the keyword argument |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
347 :raise `DistutilsSetupError`: if the value is not valid |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
348 :see: `Adding setup() arguments |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
349 <http://peak.telecommunity.com/DevCenter/setuptools#adding-setup-arguments>`_ |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
350 """ |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
351 assert name == 'message_extractors' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
352 if not isinstance(value, dict): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
353 raise DistutilsSetupError('the value of the "message_extractors" ' |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
354 'parameter must be a dictionary') |
3 | 355 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
356 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
357 class new_catalog(Command): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
358 """New catalog command for use in ``setup.py`` scripts. |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
359 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
360 If correctly installed, this command is available to Setuptools-using |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
361 setup scripts automatically. For projects using plain old ``distutils``, |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
362 the command needs to be registered explicitly in ``setup.py``:: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
363 |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
364 from babel.messages.frontend import new_catalog |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
365 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
366 setup( |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
367 ... |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
368 cmdclass = {'new_catalog': new_catalog} |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
369 ) |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
370 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
371 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
372 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
373 """ |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
374 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
375 description = 'create new catalogs based on a catalog template' |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
376 user_options = [ |
57
e7080996fc46
`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:
56
diff
changeset
|
377 ('domain=', 'D', |
106
2a00e352c986
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:
105
diff
changeset
|
378 "domain of PO file (default 'messages')"), |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
379 ('input-file=', 'i', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
380 'name of the input file'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
381 ('output-dir=', 'd', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
382 'path to output directory'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
383 ('output-file=', 'o', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
384 "name of the output file (default " |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
385 "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"), |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
386 ('locale=', 'l', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
387 'locale for the new localized catalog'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
388 ] |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
389 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
390 def initialize_options(self): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
391 self.output_dir = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
392 self.output_file = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
393 self.input_file = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
394 self.locale = None |
106
2a00e352c986
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:
105
diff
changeset
|
395 self.domain = 'messages' |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
396 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
397 def finalize_options(self): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
398 if not self.input_file: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
399 raise DistutilsOptionError('you must specify the input file') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
400 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
401 if not self.locale: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
402 raise DistutilsOptionError('you must provide a locale for the ' |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
403 'new catalog') |
106
2a00e352c986
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:
105
diff
changeset
|
404 try: |
2a00e352c986
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:
105
diff
changeset
|
405 self._locale = Locale.parse(self.locale) |
2a00e352c986
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:
105
diff
changeset
|
406 except UnknownLocaleError, e: |
2a00e352c986
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:
105
diff
changeset
|
407 raise DistutilsOptionError(e) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
408 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
409 if not self.output_file and not self.output_dir: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
410 raise DistutilsOptionError('you must specify the output directory') |
106
2a00e352c986
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:
105
diff
changeset
|
411 if not self.output_file: |
2a00e352c986
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:
105
diff
changeset
|
412 self.output_file = os.path.join(self.output_dir, self.locale, |
2a00e352c986
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:
105
diff
changeset
|
413 'LC_MESSAGES', self.domain + '.po') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
414 |
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
415 if not os.path.exists(os.path.dirname(self.output_file)): |
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
416 os.makedirs(os.path.dirname(self.output_file)) |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
417 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
418 def run(self): |
92 | 419 log.info('creating catalog %r based on %r', self.output_file, |
57
e7080996fc46
`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:
56
diff
changeset
|
420 self.input_file) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
421 |
106
2a00e352c986
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:
105
diff
changeset
|
422 infile = open(self.input_file, 'r') |
2a00e352c986
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:
105
diff
changeset
|
423 try: |
2a00e352c986
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:
105
diff
changeset
|
424 catalog = read_po(infile) |
2a00e352c986
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:
105
diff
changeset
|
425 finally: |
2a00e352c986
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:
105
diff
changeset
|
426 infile.close() |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
427 |
106
2a00e352c986
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:
105
diff
changeset
|
428 catalog.locale = self._locale |
2a00e352c986
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:
105
diff
changeset
|
429 |
2a00e352c986
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:
105
diff
changeset
|
430 outfile = open(self.output_file, 'w') |
2a00e352c986
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:
105
diff
changeset
|
431 try: |
2a00e352c986
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:
105
diff
changeset
|
432 write_po(outfile, catalog) |
2a00e352c986
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:
105
diff
changeset
|
433 finally: |
2a00e352c986
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:
105
diff
changeset
|
434 outfile.close() |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
435 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
436 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
437 class CommandLineInterface(object): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
438 """Command-line interface. |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
439 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
440 This class provides a simple command-line interface to the message |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
441 extraction and PO file generation functionality. |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
442 """ |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
443 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
444 usage = '%%prog %s [options] %s' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
445 version = '%%prog %s' % VERSION |
163 | 446 commands = { |
162 | 447 'compile': 'compile a message catalog to a MO file', |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
448 'extract': 'extract messages from source files and generate a POT file', |
167
533baef258bb
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
163
diff
changeset
|
449 'init': 'create new message catalogs from a template', |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
450 } |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
451 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
452 def run(self, argv=sys.argv): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
453 """Main entry point of the command-line interface. |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
454 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
455 :param argv: list of arguments passed on the command-line |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
456 """ |
129 | 457 self.parser = OptionParser(usage=self.usage % ('command', '[args]'), |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
458 version=self.version) |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
459 self.parser.disable_interspersed_args() |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
460 self.parser.print_help = self._help |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
461 options, args = self.parser.parse_args(argv[1:]) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
462 if not args: |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
463 self.parser.error('incorrect number of arguments') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
464 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
465 cmdname = args[0] |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
466 if cmdname not in self.commands: |
129 | 467 self.parser.error('unknown command "%s"' % cmdname) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
468 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
469 getattr(self, cmdname)(args[1:]) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
470 |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
471 def _help(self): |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
472 print self.parser.format_help() |
129 | 473 print "commands:" |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
474 longest = max([len(command) for command in self.commands]) |
129 | 475 format = " %%-%ds %%s" % max(11, longest) |
163 | 476 commands = self.commands.items() |
477 commands.sort() | |
478 for name, description in commands: | |
479 print format % (name, description) | |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
480 |
162 | 481 def compile(self, argv): |
482 """Subcommand for compiling a message catalog to a MO file. | |
483 | |
484 :param argv: the command arguments | |
485 """ | |
167
533baef258bb
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
163
diff
changeset
|
486 parser = OptionParser(usage=self.usage % ('init', ''), |
163 | 487 description=self.commands['init']) |
162 | 488 parser.add_option('--domain', '-D', dest='domain', |
489 help="domain of MO and PO files (default '%default')") | |
490 parser.add_option('--directory', '-d', dest='directory', | |
491 metavar='DIR', help='base directory of catalog files') | |
492 parser.add_option('--input-file', '-i', dest='input_file', | |
493 metavar='FILE', help='name of the input file') | |
494 parser.add_option('--output-file', '-o', dest='output_file', | |
495 metavar='FILE', | |
496 help="name of the output file (default " | |
497 "'<output_dir>/<locale>/LC_MESSAGES/" | |
498 "<domain>.mo')") | |
499 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', | |
500 help='locale of the catalog') | |
501 parser.add_option('--use-fuzzy', '-f', dest='use_fuzzy', | |
502 action='store_true', | |
503 help='also include fuzzy translations (default ' | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
504 '%default)'), |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
505 parser.add_option('--compile-all', '-A', dest='compile_all', |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
506 action='store_true', |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
507 help="compile all available PO's") |
162 | 508 |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
509 parser.set_defaults(domain='messages', use_fuzzy=False, |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
510 compile_all=False) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
511 options, args = parser.parse_args(argv) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
512 available_pos = [] |
162 | 513 if not options.locale: |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
514 if options.compile_all and not options.directory: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
515 parser.error('you must provide a locale for the new catalog') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
516 elif options.compile_all and options.directory: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
517 for locale in os.listdir(options.directory): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
518 po_path = os.path.join(options.directory, locale, |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
519 'LC_MESSAGES', |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
520 options.domain + '.po') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
521 if os.path.exists(po_path): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
522 available_pos.append(po_path) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
523 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
524 parser.error('you must provide a locale for the new catalog') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
525 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
526 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
527 locale = Locale.parse(options.locale) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
528 except UnknownLocaleError, e: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
529 parser.error(e) |
162 | 530 |
531 if not options.directory and not options.input_file: | |
532 parser.error('you must specify the base directory or input file') | |
533 if not options.input_file: | |
534 options.input_file = os.path.join(options.directory, | |
535 options.locale, 'LC_MESSAGES', | |
536 options.domain + '.po') | |
537 | |
538 if not options.directory and not options.output_file: | |
539 parser.error('you must specify the base directory or output file') | |
540 | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
541 if not options.output_file and not options.compile_all: |
162 | 542 options.output_file = os.path.join(options.directory, |
543 options.locale, 'LC_MESSAGES', | |
544 options.domain + '.mo') | |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
545 if not options.compile_all: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
546 if not os.path.exists(os.path.dirname(options.output_file)): |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
547 os.makedirs(os.path.dirname(options.output_file)) |
162 | 548 |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
549 infile = open(options.input_file, 'r') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
550 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
551 catalog = read_po(infile) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
552 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
553 infile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
554 |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
555 print 'compiling catalog to %r' % options.output_file |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
556 |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
557 outfile = open(options.output_file, 'w') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
558 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
559 write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
560 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
561 outfile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
562 else: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
563 for po in available_pos: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
564 infile = open(po, 'r') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
565 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
566 catalog = read_po(infile) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
567 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
568 infile.close() |
162 | 569 |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
570 print 'compiling catalog to %r' % po.replace('.po', '.mo') |
162 | 571 |
172
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
572 outfile = open(po.replace('.po', '.mo'), 'w') |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
573 try: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
574 write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy) |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
575 finally: |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
576 outfile.close() |
91396d14e0b8
Allow the compile catalog frontends to compile all available locales.
palgarvio
parents:
171
diff
changeset
|
577 |
162 | 578 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
579 def extract(self, argv): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
580 """Subcommand for extracting messages from source files and generating |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
581 a POT file. |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
582 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
583 :param argv: the command arguments |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
584 """ |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
585 parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...'), |
163 | 586 description=self.commands['extract']) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
587 parser.add_option('--charset', dest='charset', |
162 | 588 help='charset to use in the output (default ' |
589 '"%default")') | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
590 parser.add_option('-k', '--keyword', dest='keywords', action='append', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
591 help='keywords to look for in addition to the ' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
592 'defaults. You can specify multiple -k flags on ' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
593 'the command line.') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
594 parser.add_option('--no-default-keywords', dest='no_default_keywords', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
595 action='store_true', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
596 help="do not include the default keywords") |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
597 parser.add_option('--mapping', '-F', dest='mapping_file', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
598 help='path to the extraction mapping file') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
599 parser.add_option('--no-location', dest='no_location', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
600 action='store_true', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
601 help='do not include location comments with filename ' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
602 'and line number') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
603 parser.add_option('--omit-header', dest='omit_header', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
604 action='store_true', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
605 help='do not include msgid "" entry in header') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
606 parser.add_option('-o', '--output', dest='output', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
607 help='path to the output POT file') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
608 parser.add_option('-w', '--width', dest='width', type='int', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
609 help="set output line width (default %default)") |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
610 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
611 help='do not break long message lines, longer than ' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
612 'the output line width, into several lines') |
73 | 613 parser.add_option('--sort-output', dest='sort_output', |
614 action='store_true', | |
615 help='generate sorted output (default False)') | |
616 parser.add_option('--sort-by-file', dest='sort_by_file', | |
617 action='store_true', | |
618 help='sort output by file location (default False)') | |
80
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
619 parser.add_option('--msgid-bugs-address', dest='msgid_bugs_address', |
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
620 metavar='EMAIL@ADDRESS', |
8e2e9d549693
Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents:
73
diff
changeset
|
621 help='set report address for msgid') |
81
51f73a110a84
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
80
diff
changeset
|
622 parser.add_option('--copyright-holder', dest='copyright_holder', |
51f73a110a84
Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents:
80
diff
changeset
|
623 help='set copyright holder in output') |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
624 parser.add_option('--add-comments', '-c', dest='comment_tags', |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
625 metavar='TAG', action='append', |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
626 help='place comment block with TAG (or those ' |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
627 'preceding keyword lines) in output file. One ' |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
628 'TAG per argument call') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
629 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
630 parser.set_defaults(charset='utf-8', keywords=[], |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
631 no_default_keywords=False, no_location=False, |
73 | 632 omit_header = False, width=76, no_wrap=False, |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
633 sort_output=False, sort_by_file=False, |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
634 comment_tags=[]) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
635 options, args = parser.parse_args(argv) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
636 if not args: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
637 parser.error('incorrect number of arguments') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
638 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
639 if options.output not in (None, '-'): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
640 outfile = open(options.output, 'w') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
641 else: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
642 outfile = sys.stdout |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
643 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
644 keywords = DEFAULT_KEYWORDS.copy() |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
645 if options.no_default_keywords: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
646 if not options.keywords: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
647 parser.error('you must specify new keywords if you disable the ' |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
648 'default ones') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
649 keywords = {} |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
650 if options.keywords: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
651 keywords.update(parse_keywords(options.keywords)) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
652 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
653 if options.mapping_file: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
654 fileobj = open(options.mapping_file, 'U') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
655 try: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
656 method_map, options_map = parse_mapping(fileobj) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
657 finally: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
658 fileobj.close() |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
659 else: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
660 method_map = DEFAULT_MAPPING |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
661 options_map = {} |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
662 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
663 if options.width and options.no_wrap: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
664 parser.error("'--no-wrap' and '--width' are mutually exclusive.") |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
665 elif not options.width and not options.no_wrap: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
666 options.width = 76 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
667 elif not options.width and options.no_wrap: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
668 options.width = 0 |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
669 |
73 | 670 if options.sort_output and options.sort_by_file: |
671 parser.error("'--sort-output' and '--sort-by-file' are mutually " | |
672 "exclusive") | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
673 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
674 try: |
104
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
675 catalog = Catalog(msgid_bugs_address=options.msgid_bugs_address, |
107
4b42e23644e5
`Message`, `read_po` and `write_po` now all handle user/auto comments correctly.
palgarvio
parents:
106
diff
changeset
|
676 copyright_holder=options.copyright_holder, |
104
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
677 charset=options.charset) |
57d2f21a1fcc
Project name and version, and the charset are available via the `Catalog` object, and do not need to be passed to `write_pot()`.
cmlenz
parents:
97
diff
changeset
|
678 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
679 for dirname in args: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
680 if not os.path.isdir(dirname): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
681 parser.error('%r is not a directory' % dirname) |
97
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
682 extracted = extract_from_dir(dirname, method_map, options_map, |
debd9ac3bb4d
Fix for #11 (use local timezone in timestamps of generated POT).
cmlenz
parents:
92
diff
changeset
|
683 keywords, options.comment_tags) |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
684 for filename, lineno, message, comments in extracted: |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
685 filepath = os.path.normpath(os.path.join(dirname, filename)) |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
81
diff
changeset
|
686 catalog.add(message, None, [(filepath, lineno)], |
112 | 687 auto_comments=comments) |
58
068952b4d4c0
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
57
diff
changeset
|
688 |
106
2a00e352c986
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:
105
diff
changeset
|
689 write_po(outfile, catalog, width=options.width, |
2a00e352c986
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:
105
diff
changeset
|
690 no_location=options.no_location, |
2a00e352c986
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:
105
diff
changeset
|
691 omit_header=options.omit_header, |
2a00e352c986
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:
105
diff
changeset
|
692 sort_output=options.sort_output, |
114
eeed857fa3d5
copyright_holder arg is no longer needed for write_po
pjenvey
parents:
112
diff
changeset
|
693 sort_by_file=options.sort_by_file) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
694 finally: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
695 if options.output: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
696 outfile.close() |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
697 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
698 def init(self, argv): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
699 """Subcommand for creating new message catalogs from a template. |
167
533baef258bb
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
163
diff
changeset
|
700 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
701 :param argv: the command arguments |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
702 """ |
167
533baef258bb
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
163
diff
changeset
|
703 parser = OptionParser(usage=self.usage % ('init', ''), |
163 | 704 description=self.commands['init']) |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
705 parser.add_option('--domain', '-D', dest='domain', |
106
2a00e352c986
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:
105
diff
changeset
|
706 help="domain of PO file (default '%default')") |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
707 parser.add_option('--input-file', '-i', dest='input_file', |
106
2a00e352c986
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:
105
diff
changeset
|
708 metavar='FILE', help='name of the input file') |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
709 parser.add_option('--output-dir', '-d', dest='output_dir', |
106
2a00e352c986
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:
105
diff
changeset
|
710 metavar='DIR', help='path to output directory') |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
711 parser.add_option('--output-file', '-o', dest='output_file', |
106
2a00e352c986
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:
105
diff
changeset
|
712 metavar='FILE', |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
713 help="name of the output file (default " |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
714 "'<output_dir>/<locale>/LC_MESSAGES/" |
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
715 "<domain>.po')") |
106
2a00e352c986
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:
105
diff
changeset
|
716 parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
717 help='locale for the new localized catalog') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
718 |
106
2a00e352c986
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:
105
diff
changeset
|
719 parser.set_defaults(domain='messages') |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
720 options, args = parser.parse_args(argv) |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
721 |
106
2a00e352c986
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:
105
diff
changeset
|
722 if not options.locale: |
2a00e352c986
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:
105
diff
changeset
|
723 parser.error('you must provide a locale for the new catalog') |
2a00e352c986
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:
105
diff
changeset
|
724 try: |
2a00e352c986
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:
105
diff
changeset
|
725 locale = Locale.parse(options.locale) |
2a00e352c986
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:
105
diff
changeset
|
726 except UnknownLocaleError, e: |
2a00e352c986
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:
105
diff
changeset
|
727 parser.error(e) |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
728 |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
729 if not options.input_file: |
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
730 parser.error('you must specify the input file') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
731 |
106
2a00e352c986
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:
105
diff
changeset
|
732 if not options.output_file and not options.output_dir: |
2a00e352c986
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:
105
diff
changeset
|
733 parser.error('you must specify the output file or directory') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
734 |
106
2a00e352c986
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:
105
diff
changeset
|
735 if not options.output_file: |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
736 options.output_file = os.path.join(options.output_dir, |
106
2a00e352c986
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:
105
diff
changeset
|
737 options.locale, 'LC_MESSAGES', |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
738 options.domain + '.po') |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
739 if not os.path.exists(os.path.dirname(options.output_file)): |
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
740 os.makedirs(os.path.dirname(options.output_file)) |
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
741 |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
742 infile = open(options.input_file, 'r') |
106
2a00e352c986
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:
105
diff
changeset
|
743 try: |
2a00e352c986
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:
105
diff
changeset
|
744 catalog = read_po(infile) |
2a00e352c986
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:
105
diff
changeset
|
745 finally: |
2a00e352c986
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:
105
diff
changeset
|
746 infile.close() |
89
31519c52c0fe
Fixed a bug on Catalog. `__setitem__` was not updating the translator comments. Thanks pjenvey!
palgarvio
parents:
88
diff
changeset
|
747 |
106
2a00e352c986
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:
105
diff
changeset
|
748 catalog.locale = locale |
134 | 749 catalog.revision_date = datetime.now(LOCALTZ) |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
750 |
92 | 751 print 'creating catalog %r based on %r' % (options.output_file, |
752 options.input_file) | |
68
22e30e5a6736
Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents:
65
diff
changeset
|
753 |
106
2a00e352c986
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:
105
diff
changeset
|
754 outfile = open(options.output_file, 'w') |
2a00e352c986
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:
105
diff
changeset
|
755 try: |
2a00e352c986
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:
105
diff
changeset
|
756 write_po(outfile, catalog) |
2a00e352c986
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:
105
diff
changeset
|
757 finally: |
2a00e352c986
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:
105
diff
changeset
|
758 outfile.close() |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
759 |
167
533baef258bb
Implement fuzzy matching to catalog updates. No frontend yet.
cmlenz
parents:
163
diff
changeset
|
760 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
761 def main(): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
762 CommandLineInterface().run(sys.argv) |
3 | 763 |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
764 def parse_mapping(fileobj, filename=None): |
49
9979a409589e
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:
26
diff
changeset
|
765 """Parse an extraction method mapping from a file-like object. |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
766 |
49
9979a409589e
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:
26
diff
changeset
|
767 >>> buf = StringIO(''' |
9979a409589e
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:
26
diff
changeset
|
768 ... # Python source files |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
769 ... [python: **.py] |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
770 ... |
49
9979a409589e
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:
26
diff
changeset
|
771 ... # Genshi templates |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
772 ... [genshi: **/templates/**.html] |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
773 ... include_attrs = |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
774 ... [genshi: **/templates/**.txt] |
146 | 775 ... template_class = genshi.template:TextTemplate |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
776 ... encoding = latin-1 |
49
9979a409589e
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:
26
diff
changeset
|
777 ... ''') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
778 |
49
9979a409589e
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:
26
diff
changeset
|
779 >>> method_map, options_map = parse_mapping(buf) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
780 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
781 >>> method_map[0] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
782 ('**.py', 'python') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
783 >>> options_map['**.py'] |
49
9979a409589e
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:
26
diff
changeset
|
784 {} |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
785 >>> method_map[1] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
786 ('**/templates/**.html', 'genshi') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
787 >>> options_map['**/templates/**.html']['include_attrs'] |
49
9979a409589e
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:
26
diff
changeset
|
788 '' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
789 >>> method_map[2] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
790 ('**/templates/**.txt', 'genshi') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
791 >>> options_map['**/templates/**.txt']['template_class'] |
146 | 792 'genshi.template:TextTemplate' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
793 >>> options_map['**/templates/**.txt']['encoding'] |
49
9979a409589e
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:
26
diff
changeset
|
794 'latin-1' |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
795 |
49
9979a409589e
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:
26
diff
changeset
|
796 :param fileobj: a readable file-like object containing the configuration |
9979a409589e
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:
26
diff
changeset
|
797 text to parse |
9979a409589e
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:
26
diff
changeset
|
798 :return: a `(method_map, options_map)` tuple |
9979a409589e
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:
26
diff
changeset
|
799 :rtype: `tuple` |
9979a409589e
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:
26
diff
changeset
|
800 :see: `extract_from_directory` |
9979a409589e
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:
26
diff
changeset
|
801 """ |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
802 method_map = [] |
49
9979a409589e
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:
26
diff
changeset
|
803 options_map = {} |
9979a409589e
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:
26
diff
changeset
|
804 |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
805 parser = RawConfigParser() |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
806 parser._sections = odict(parser._sections) # We need ordered sections |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
807 parser.readfp(fileobj, filename) |
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
808 for section in parser.sections(): |
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
809 method, pattern = [part.strip() for part in section.split(':', 1)] |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
810 method_map.append((pattern, method)) |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
811 options_map[pattern] = dict(parser.items(section)) |
49
9979a409589e
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:
26
diff
changeset
|
812 |
9979a409589e
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:
26
diff
changeset
|
813 return (method_map, options_map) |
9979a409589e
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:
26
diff
changeset
|
814 |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
815 def parse_keywords(strings=[]): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
816 """Parse keywords specifications from the given list of strings. |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
817 |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
818 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3']) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
819 >>> for keyword, indices in sorted(kw.items()): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
820 ... print (keyword, indices) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
821 ('_', None) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
822 ('dgettext', (2,)) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
823 ('dngettext', (2, 3)) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
824 """ |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
825 keywords = {} |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
826 for string in strings: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
827 if ':' in string: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
828 funcname, indices = string.split(':') |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
829 else: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
830 funcname, indices = string, None |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
831 if funcname not in keywords: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
832 if indices: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
833 indices = tuple([(int(x)) for x in indices.split(',')]) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
834 keywords[funcname] = indices |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
835 return keywords |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
836 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
837 |
3 | 838 if __name__ == '__main__': |
57
e7080996fc46
`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:
56
diff
changeset
|
839 main() |