annotate babel/messages/frontend.py @ 531:f642c81ce8cc

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