annotate babel/messages/frontend.py @ 525:f73435181ae3

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