Mercurial > babel > old > mirror
annotate babel/messages/frontend.py @ 65:b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
author | palgarvio |
---|---|
date | Fri, 08 Jun 2007 12:53:57 +0000 |
parents | 7eb6fea17864 |
children | 22e30e5a6736 |
rev | line source |
---|---|
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
1 #!/usr/bin/env python |
3 | 2 # -*- coding: utf-8 -*- |
3 # | |
4 # Copyright (C) 2007 Edgewall Software | |
5 # All rights reserved. | |
6 # | |
7 # This software is licensed as described in the file COPYING, which | |
8 # you should have received as part of this distribution. The terms | |
9 # are also available at http://babel.edgewall.org/wiki/License. | |
10 # | |
11 # This software consists of voluntary contributions made by many | |
12 # individuals. For the exact contribution history, see the revision | |
13 # history and logs, available at http://babel.edgewall.org/log/. | |
14 | |
15 """Frontends for the message extraction functionality.""" | |
16 | |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
17 from ConfigParser import RawConfigParser |
3 | 18 from distutils import log |
19 from distutils.cmd import Command | |
51
3664c93860f1
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
50
diff
changeset
|
20 from distutils.errors import DistutilsOptionError, DistutilsSetupError |
3 | 21 from optparse import OptionParser |
22 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
|
23 import re |
51
3664c93860f1
Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents:
50
diff
changeset
|
24 from StringIO import StringIO |
3 | 25 import sys |
26 | |
27 from babel import __version__ as VERSION | |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
28 from babel import Locale |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 DEFAULT_MAPPING |
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
33 from babel.messages.pofile import write_po, write_pot |
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
34 from babel.messages.plurals import PLURALS |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
35 from babel.util import odict |
3 | 36 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
37 __all__ = ['CommandLineInterface', 'extract_messages', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
38 'check_message_extractors', 'main'] |
3 | 39 __docformat__ = 'restructuredtext en' |
40 | |
41 | |
42 class extract_messages(Command): | |
43 """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
|
44 |
3 | 45 If correctly installed, this command is available to Setuptools-using |
46 setup scripts automatically. For projects using plain old ``distutils``, | |
47 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
|
48 |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
49 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
|
50 |
3 | 51 setup( |
52 ... | |
53 cmdclass = {'extract_messages': extract_messages} | |
54 ) | |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
55 |
3 | 56 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
57 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
58 """ | |
59 | |
60 description = 'extract localizable strings from the project code' | |
61 user_options = [ | |
62 ('charset=', None, | |
63 'charset to use in the output file'), | |
64 ('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
|
65 'space-separated list of keywords to look for in addition to the ' |
3 | 66 '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
|
67 ('no-default-keywords', None, |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
68 '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
|
69 ('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
|
70 'path to the mapping configuration file'), |
3 | 71 ('no-location', None, |
72 'do not include location comments with filename and line number'), | |
73 ('omit-header', None, | |
74 '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
|
75 ('output-file=', 'o', |
3 | 76 'name of the output file'), |
25 | 77 ('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
|
78 'set output line width (default 76)'), |
25 | 79 ('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
|
80 'do not break long message lines, longer than the output line width, ' |
60 | 81 'into several lines'), |
61 | 82 ('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
|
83 'directories that should be scanned for messages'), |
3 | 84 ] |
25 | 85 boolean_options = [ |
86 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap' | |
87 ] | |
3 | 88 |
89 def initialize_options(self): | |
90 self.charset = 'utf-8' | |
25 | 91 self.keywords = self._keywords = DEFAULT_KEYWORDS.copy() |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
92 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
|
93 self.mapping_file = None |
3 | 94 self.no_location = False |
95 self.omit_header = False | |
96 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
|
97 self.input_dirs = None |
49
9979a409589e
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
26
diff
changeset
|
98 self.width = 76 |
9979a409589e
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
26
diff
changeset
|
99 self.no_wrap = False |
3 | 100 |
101 def finalize_options(self): | |
25 | 102 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
|
103 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
|
104 'disable the default ones') |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
105 if self.no_default_keywords: |
25 | 106 self._keywords = {} |
3 | 107 if isinstance(self.keywords, basestring): |
25 | 108 self._keywords.update(parse_keywords(self.keywords.split())) |
109 self.keywords = 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
|
110 |
25 | 111 if self.no_wrap and self.width: |
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
|
112 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually" |
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
|
113 "exclusive") |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
114 if self.no_wrap: |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
115 self.width = None |
93eaa2f4a0a2
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
25
diff
changeset
|
116 else: |
25 | 117 self.width = int(self.width) |
3 | 118 |
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
|
119 if not self.input_dirs: |
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
120 self.input_dirs = dict.fromkeys([k.split('.',1)[0] |
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
121 for k in self.distribution.packages |
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
122 ]).keys() |
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
|
123 |
3 | 124 def run(self): |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
125 mappings = self._get_mappings() |
3 | 126 outfile = open(self.output_file, 'w') |
127 try: | |
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
|
128 catalog = Catalog() |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 k, v in options.items()]) |
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
138 log.info('extracting messages from %s%s' |
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
139 % (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
|
140 |
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
|
141 extracted = extract_from_dir(dirname, method_map, options_map, |
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
|
142 keywords=self.keywords, |
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
|
143 callback=callback) |
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
|
144 for filename, lineno, message in extracted: |
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
|
145 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
|
146 catalog.add(message, None, [(filepath, lineno)]) |
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
|
147 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
148 log.info('writing PO template file to %s' % self.output_file) |
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
|
149 write_pot(outfile, catalog, project=self.distribution.get_name(), |
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
|
150 version=self.distribution.get_version(), width=self.width, |
7
8d7b3077e6d1
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
3
diff
changeset
|
151 charset=self.charset, no_location=self.no_location, |
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
|
152 omit_header=self.omit_header) |
3 | 153 finally: |
154 outfile.close() | |
155 | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
156 def _get_mappings(self): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
157 mappings = {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
158 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
159 if self.mapping_file: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
160 fileobj = open(self.mapping_file, 'U') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
161 try: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
162 method_map, options_map = parse_mapping(fileobj) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
163 for dirname in self.input_dirs: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
164 mappings[dirname] = method_map, options_map |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
165 finally: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
166 fileobj.close() |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
167 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
168 elif self.distribution.message_extractors: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
169 message_extractors = self.distribution.message_extractors |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
170 for dirname, mapping in message_extractors.items(): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
171 if isinstance(mapping, basestring): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
172 method_map, options_map = parse_mapping(StringIO(mapping)) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
173 else: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
174 method_map, options_map = [], {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
175 for pattern, method, options in mapping: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
176 method_map.append((pattern, method)) |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
177 options_map[pattern] = options or {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
178 mappings[dirname] = method_map, options_map |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
179 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
180 else: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
181 for dirname in self.input_dirs: |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
182 mappings[dirname] = DEFAULT_MAPPING, {} |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
183 |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
184 return mappings |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
185 |
3 | 186 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
187 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
|
188 """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
|
189 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
190 :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
|
191 :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
|
192 "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
|
193 :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
|
194 :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
|
195 :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
|
196 <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
|
197 """ |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
198 assert name == 'message_extractors' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
199 if not isinstance(value, dict): |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
200 raise DistutilsSetupError('the value of the "message_extractors" ' |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
201 'parameter must be a dictionary') |
3 | 202 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
203 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
204 class new_catalog(Command): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
205 """New catalog command for use in ``setup.py`` scripts. |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
206 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
207 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
|
208 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
|
209 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
|
210 |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
211 from babel.messages.frontend import new_catalog |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
212 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
213 setup( |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
214 ... |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
215 cmdclass = {'new_catalog': new_catalog} |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
216 ) |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
217 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
218 :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
|
219 :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
|
220 """ |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
221 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
222 description = 'create new catalogs based on a catalog template' |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
223 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
|
224 ('domain=', 'D', |
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
|
225 "domain of PO file (defaults to lower-cased project name"), |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
226 ('input-file=', 'i', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
227 'name of the input file'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
228 ('output-dir=', 'd', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
229 'path to output directory'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
230 ('output-file=', 'o', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
231 "name of the output file (default " |
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
|
232 "'<output_dir>/<locale>/<domain>.po')"), |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
233 ('locale=', 'l', |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
234 '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
|
235 ('first-author=', None, |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
236 'name of first author'), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
237 ('first-author-email=', None, |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
238 'email of first author') |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
239 ] |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
240 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
241 def initialize_options(self): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
242 self.output_dir = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
243 self.output_file = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
244 self.input_file = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
245 self.locale = None |
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
|
246 self.domain = None |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
247 self.first_author = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
248 self.first_author_email = None |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
249 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
250 def finalize_options(self): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
251 if not self.input_file: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
252 raise DistutilsOptionError('you must specify the input 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
|
253 |
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
|
254 if not self.domain: |
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
|
255 self.domain = self.distribution.get_name().lower() |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
256 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
257 if not self.locale: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
258 raise DistutilsOptionError('you must provide a locale for the ' |
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
|
259 'new catalog') |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
260 else: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
261 try: |
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
|
262 self._locale = Locale.parse(self.locale) |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
263 except UnknownLocaleError, error: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
264 log.error(error) |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
265 sys.exit(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
|
266 |
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
|
267 if self._locale.territory.lower() == self._locale.language: |
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
|
268 # Remove country part if equal to language |
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
|
269 # XXX: This might not be the best behaviour, investigate |
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
|
270 self.locale = self._locale.language |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
271 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
272 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
|
273 raise DistutilsOptionError('you must specify the output directory') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
274 |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
275 if not self.output_file and self.output_dir: |
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
|
276 self.output_file = os.path.join(self.output_dir, |
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
|
277 self.locale, |
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
|
278 self.domain + '.po') |
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
|
279 if not os.path.exists(os.path.dirname(self.output_file)): |
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
|
280 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
|
281 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
282 def run(self): |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
283 outfile = open(self.output_file, 'w') |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
284 infile = open(self.input_file, 'r') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
285 |
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
|
286 if PLURALS.has_key(str(self._locale)): |
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
|
287 # Try <language>_<COUNTRY> if passed by user |
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
|
288 plurals = PLURALS[str(self._locale)] |
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
|
289 elif PLURALS.has_key(self._locale.language): |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
290 # Try <language> |
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
|
291 plurals = PLURALS[self._locale.language] |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
292 else: |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
293 plurals = ('INTEGER', 'EXPRESSION') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
294 |
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
|
295 log.info('Creating %s %r PO from %r PO template', |
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
|
296 self._locale.english_name, |
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
|
297 self.output_file, |
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
|
298 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
|
299 |
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
|
300 write_po(outfile, infile, self._locale, |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
301 project=self.distribution.get_name(), |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
302 version=self.distribution.get_version(), |
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
|
303 plurals=plurals, |
53
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
304 first_author=self.first_author, |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
305 first_author_email=self.first_author_email) |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
306 infile.close() |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
307 outfile.close() |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
308 |
52dbebdd3789
Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents:
51
diff
changeset
|
309 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
310 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
|
311 """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
|
312 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
313 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
|
314 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
|
315 """ |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
316 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
317 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
|
318 version = '%%prog %s' % VERSION |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
319 commands = ['extract', 'init'] |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
320 command_descriptions = { |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
321 'extract': 'extract messages from source files and generate a POT file', |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
322 'init': 'create new message catalogs from a template' |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
323 } |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
324 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
325 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
|
326 """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
|
327 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
328 :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
|
329 """ |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
330 self.parser = OptionParser(usage=self.usage % ('subcommand', '[args]'), |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
331 version=self.version) |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
332 self.parser.disable_interspersed_args() |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
333 self.parser.print_help = self._help |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
334 options, args = self.parser.parse_args(argv[1:]) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
335 if not args: |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
336 self.parser.error('incorrect number of arguments') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
337 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
338 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
|
339 if cmdname not in self.commands: |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
340 self.parser.error('unknown subcommand "%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
|
341 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
342 getattr(self, cmdname)(args[1:]) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
343 |
65
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
344 def _help(self): |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
345 print self.parser.format_help() |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
346 print "Commands:" |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
347 longest = max([len(command) for command in self.commands]) |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
348 format = " %" + str(longest) + "s %s" |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
349 self.commands.sort() |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
350 for command in self.commands: |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
351 print format % (command, self.command_descriptions[command]) |
b7b61e0fd23e
Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents:
64
diff
changeset
|
352 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
353 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
|
354 """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
|
355 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
|
356 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
357 :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
|
358 """ |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
359 parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...')) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
360 parser.add_option('--charset', dest='charset', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
361 help='charset to use in the output') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
362 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
|
363 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
|
364 '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
|
365 '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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 '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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 parser.add_option('-w', '--width', dest='width', type='int', |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
381 help="set output line width (default %default)") |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
382 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
|
383 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
|
384 'the output line width, into several lines') |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
385 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
386 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
|
387 no_default_keywords=False, no_location=False, |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
388 omit_header = False, width=76, no_wrap=False) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
389 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
|
390 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
|
391 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
|
392 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
393 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
|
394 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
|
395 else: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
396 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
|
397 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
398 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
|
399 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
|
400 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
|
401 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
|
402 '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
|
403 keywords = {} |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
404 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
|
405 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
|
406 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
407 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
|
408 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
|
409 try: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
410 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
|
411 finally: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
412 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
|
413 else: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
414 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
|
415 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
|
416 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
417 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
|
418 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
|
419 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
|
420 options.width = 76 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
421 elif not options.width and options.no_wrap: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
422 options.width = 0 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
423 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
424 try: |
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
|
425 catalog = Catalog() |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
426 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
|
427 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
|
428 parser.error('%r is not a directory' % dirname) |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
429 extracted = extract_from_dir(dirname, method_map, 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
|
430 keywords) |
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
|
431 for filename, lineno, message 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
|
432 filepath = os.path.normpath(os.path.join(dirname, filename)) |
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
|
433 catalog.add(message, None, [(filepath, lineno)]) |
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
|
434 |
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
|
435 write_pot(outfile, catalog, width=options.width, |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
436 charset=options.charset, no_location=options.no_location, |
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
54
diff
changeset
|
437 omit_header=options.omit_header) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
438 finally: |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
439 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
|
440 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
|
441 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
442 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
|
443 """Subcommand for creating new message catalogs from a template. |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
444 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
445 :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
|
446 """ |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
447 raise NotImplementedError |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
448 |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
449 def main(): |
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
450 CommandLineInterface().run(sys.argv) |
3 | 451 |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
452 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
|
453 """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
|
454 |
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
|
455 >>> buf = StringIO(''' |
9979a409589e
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
26
diff
changeset
|
456 ... # Python source files |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
457 ... [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
|
458 ... |
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
|
459 ... # Genshi templates |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
460 ... [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
|
461 ... include_attrs = |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
462 ... [genshi: **/templates/**.txt] |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
463 ... template_class = genshi.template.text.TextTemplate |
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
464 ... encoding = latin-1 |
49
9979a409589e
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
26
diff
changeset
|
465 ... ''') |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
466 |
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
|
467 >>> method_map, options_map = parse_mapping(buf) |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
468 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
469 >>> method_map[0] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
470 ('**.py', 'python') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
471 >>> 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
|
472 {} |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
473 >>> method_map[1] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
474 ('**/templates/**.html', 'genshi') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
475 >>> 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
|
476 '' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
477 >>> method_map[2] |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
478 ('**/templates/**.txt', 'genshi') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
479 >>> options_map['**/templates/**.txt']['template_class'] |
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
|
480 'genshi.template.text.TextTemplate' |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
481 >>> 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
|
482 '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
|
483 |
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
|
484 :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
|
485 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
|
486 :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
|
487 :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
|
488 :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
|
489 """ |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
490 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
|
491 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
|
492 |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
493 parser = RawConfigParser() |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
494 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
|
495 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
|
496 for section in parser.sections(): |
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
497 method, pattern = [part.strip() for part in section.split(':', 1)] |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
61
diff
changeset
|
498 method_map.append((pattern, method)) |
50
b6497cfd06d6
Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents:
49
diff
changeset
|
499 options_map[pattern] = dict(parser.items(section)) |
49
9979a409589e
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
26
diff
changeset
|
500 |
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
|
501 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
|
502 |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
503 def parse_keywords(strings=[]): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
504 """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
|
505 |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
506 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3']) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
507 >>> for keyword, indices in sorted(kw.items()): |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
508 ... print (keyword, indices) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
509 ('_', None) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
510 ('dgettext', (2,)) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
511 ('dngettext', (2, 3)) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
512 """ |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
513 keywords = {} |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
514 for string in strings: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
515 if ':' in string: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
516 funcname, indices = string.split(':') |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
517 else: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
518 funcname, indices = string, None |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
519 if funcname not in keywords: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
520 if indices: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
521 indices = tuple([(int(x)) for x in indices.split(',')]) |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
522 keywords[funcname] = indices |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
523 return keywords |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
524 |
54
cc3c6cfe909d
Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents:
53
diff
changeset
|
525 |
3 | 526 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
|
527 main() |