Mercurial > babel > mirror
annotate babel/catalog/frontend.py @ 47:f8469ab4b257 trunk
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.
author | cmlenz |
---|---|
date | Wed, 06 Jun 2007 21:03:24 +0000 |
parents | b09e90803d1b |
children | 22b90b3b161a |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Frontends for the message extraction functionality.""" | |
15 | |
16 from distutils import log | |
17 from distutils.cmd import Command | |
23 | 18 from distutils.errors import DistutilsOptionError |
1 | 19 from optparse import OptionParser |
20 import os | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
21 import re |
1 | 22 import sys |
23 | |
24 from babel import __version__ as VERSION | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
25 from babel.catalog.extract import extract_from_dir, DEFAULT_KEYWORDS, \ |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
26 DEFAULT_MAPPING |
1 | 27 from babel.catalog.pofile import write_po |
28 | |
29 __all__ = ['extract_messages', 'main'] | |
30 __docformat__ = 'restructuredtext en' | |
31 | |
32 | |
33 class extract_messages(Command): | |
34 """Message extraction command for use in ``setup.py`` scripts. | |
35 | |
36 If correctly installed, this command is available to Setuptools-using | |
37 setup scripts automatically. For projects using plain old ``distutils``, | |
38 the command needs to be registered explicitly in ``setup.py``:: | |
39 | |
40 from babel.catalog.frontend import extract_messages | |
41 | |
42 setup( | |
43 ... | |
44 cmdclass = {'extract_messages': extract_messages} | |
45 ) | |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
46 |
1 | 47 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_ |
48 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_ | |
49 """ | |
50 | |
51 description = 'extract localizable strings from the project code' | |
52 user_options = [ | |
53 ('charset=', None, | |
54 'charset to use in the output file'), | |
55 ('keywords=', 'k', | |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
56 'space-separated list of keywords to look for in addition to the ' |
1 | 57 'defaults'), |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
58 ('no-default-keywords', None, |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
59 'do not include the default keywords'), |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
60 ('mapping-file=', 'F', |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
61 'path to the mapping configuration file'), |
1 | 62 ('no-location', None, |
63 'do not include location comments with filename and line number'), | |
64 ('omit-header', None, | |
65 'do not include msgid "" entry in header'), | |
5
132526dcd074
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
66 ('output-file=', 'o', |
1 | 67 'name of the output file'), |
23 | 68 ('width=', 'w', |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
69 'set output line width (default 76)'), |
23 | 70 ('no-wrap', None, |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
71 'do not break long message lines, longer than the output line width, ' |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
72 'into several lines') |
1 | 73 ] |
23 | 74 boolean_options = [ |
75 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap' | |
76 ] | |
1 | 77 |
78 def initialize_options(self): | |
79 self.charset = 'utf-8' | |
23 | 80 self.keywords = self._keywords = DEFAULT_KEYWORDS.copy() |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
81 self.no_default_keywords = False |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
82 self.mapping_file = None |
1 | 83 self.no_location = False |
84 self.omit_header = False | |
85 self.output_file = None | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
86 self.width = 76 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
87 self.no_wrap = False |
1 | 88 |
89 def finalize_options(self): | |
23 | 90 if self.no_default_keywords and not self.keywords: |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
91 raise DistutilsOptionError('you must specify new keywords if you ' |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
92 'disable the default ones') |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
93 if self.no_default_keywords: |
23 | 94 self._keywords = {} |
1 | 95 if isinstance(self.keywords, basestring): |
23 | 96 self._keywords.update(parse_keywords(self.keywords.split())) |
97 self.keywords = self._keywords | |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
98 |
23 | 99 if self.no_wrap and self.width: |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
100 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually" |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
101 "exclusive") |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
102 if self.no_wrap: |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
103 self.width = None |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
104 else: |
23 | 105 self.width = int(self.width) |
1 | 106 |
107 def run(self): | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
108 if self.mapping_file: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
109 fileobj = open(self.mapping_file, 'U') |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
110 try: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
111 method_map, options_map = parse_mapping(fileobj) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
112 finally: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
113 fileobj.close() |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
114 else: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
115 method_map = DEFAULT_MAPPING |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
116 options_map = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
117 |
1 | 118 outfile = open(self.output_file, 'w') |
119 try: | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
120 def callback(filename, options): |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
121 optstr = '' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
122 if options: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
123 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for k, v |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
124 in options.items()]) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
125 log.info('extracting messages from %s%s' % (filename, optstr)) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
126 |
1 | 127 messages = [] |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
128 extracted = extract_from_dir(method_map=method_map, |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
129 options_map=options_map, |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
130 keywords=self.keywords, |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
131 callback=callback) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
132 for filename, lineno, funcname, message in extracted: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
133 filepath = os.path.normpath(filename) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
134 messages.append((filepath, lineno, funcname, message, None)) |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
135 |
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
136 log.info('writing PO file to %s' % self.output_file) |
5
132526dcd074
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
137 write_po(outfile, messages, project=self.distribution.get_name(), |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
138 version=self.distribution.get_version(), width=self.width, |
5
132526dcd074
* The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents:
1
diff
changeset
|
139 charset=self.charset, no_location=self.no_location, |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
140 omit_header=self.omit_header) |
1 | 141 finally: |
142 outfile.close() | |
143 | |
144 def main(argv=sys.argv): | |
145 """Command-line interface. | |
146 | |
147 This function provides a simple command-line interface to the message | |
148 extraction and PO file generation functionality. | |
149 | |
150 :param argv: list of arguments passed on the command-line | |
151 """ | |
152 parser = OptionParser(usage='%prog [options] dirname1 <dirname2> ...', | |
153 version='%%prog %s' % VERSION) | |
154 parser.add_option('--charset', dest='charset', default='utf-8', | |
155 help='charset to use in the output') | |
156 parser.add_option('-k', '--keyword', dest='keywords', | |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
157 default=[], action='append', |
1 | 158 help='keywords to look for in addition to the defaults. ' |
159 'You can specify multiple -k flags on the command ' | |
160 'line.') | |
10
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
161 parser.add_option('--no-default-keywords', dest='no_default_keywords', |
4130d9c6cb34
Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents:
7
diff
changeset
|
162 action='store_true', default=False, |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
163 help="do not include the default keywords") |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
164 parser.add_option('--mapping', '-F', dest='mapping_file', |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
165 help='path to the extraction mapping file') |
1 | 166 parser.add_option('--no-location', dest='no_location', default=False, |
167 action='store_true', | |
168 help='do not include location comments with filename and ' | |
169 'line number') | |
170 parser.add_option('--omit-header', dest='omit_header', default=False, | |
171 action='store_true', | |
172 help='do not include msgid "" entry in header') | |
173 parser.add_option('-o', '--output', dest='output', | |
174 help='path to the output POT file') | |
23 | 175 parser.add_option('-w', '--width', dest='width', type='int', |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
176 help="set output line width (default 76)") |
23 | 177 parser.add_option('--no-wrap', dest='no_wrap', default=False, |
178 action = 'store_true', help='do not break long message ' | |
179 'lines, longer than the output line width, into several ' | |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
180 'lines') |
1 | 181 options, args = parser.parse_args(argv[1:]) |
182 if not args: | |
183 parser.error('incorrect number of arguments') | |
184 | |
185 if options.output not in (None, '-'): | |
186 outfile = open(options.output, 'w') | |
187 else: | |
188 outfile = sys.stdout | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
189 |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
190 keywords = DEFAULT_KEYWORDS.copy() |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
191 if options.no_default_keywords: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
192 if not options.keywords: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
193 parser.error('you must specify new keywords if you disable the ' |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
194 'default ones') |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
195 keywords = {} |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
196 if options.keywords: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
197 keywords.update(parse_keywords(options.keywords)) |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
198 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
199 if options.mapping_file: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
200 fileobj = open(options.mapping_file, 'U') |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
201 try: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
202 method_map, options_map = parse_mapping(fileobj) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
203 finally: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
204 fileobj.close() |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
205 else: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
206 method_map = DEFAULT_MAPPING |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
207 options_map = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
208 |
23 | 209 if options.width and options.no_wrap: |
210 parser.error("'--no-wrap' and '--width' are mutually exclusive.") | |
211 elif not options.width and not options.no_wrap: | |
212 options.width = 76 | |
213 elif not options.width and options.no_wrap: | |
214 options.width = 0 | |
1 | 215 |
216 try: | |
217 messages = [] | |
218 for dirname in args: | |
219 if not os.path.isdir(dirname): | |
220 parser.error('%r is not a directory' % dirname) | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
221 extracted = extract_from_dir(dirname, method_map, options_map, |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
222 keywords) |
1 | 223 for filename, lineno, funcname, message in extracted: |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
224 filepath = os.path.normpath(os.path.join(dirname, filename)) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
225 messages.append((filepath, lineno, funcname, message, None)) |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
226 write_po(outfile, messages, width=options.width, |
1 | 227 charset=options.charset, no_location=options.no_location, |
24
b09e90803d1b
Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents:
23
diff
changeset
|
228 omit_header=options.omit_header) |
1 | 229 finally: |
230 if options.output: | |
231 outfile.close() | |
232 | |
47
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
233 def parse_mapping(fileobj): |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
234 """Parse an extraction method mapping from a file-like object. |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
235 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
236 >>> from StringIO import StringIO |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
237 >>> buf = StringIO(''' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
238 ... # Python source files |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
239 ... python: foobar/**.py |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
240 ... |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
241 ... # Genshi templates |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
242 ... genshi: foobar/**/templates/**.html |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
243 ... include_attrs = |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
244 ... genshi: foobar/**/templates/**.txt |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
245 ... template_class = genshi.template.text.TextTemplate |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
246 ... encoding = latin-1 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
247 ... ''') |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
248 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
249 >>> method_map, options_map = parse_mapping(buf) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
250 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
251 >>> method_map['foobar/**.py'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
252 'python' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
253 >>> options_map['foobar/**.py'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
254 {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
255 >>> method_map['foobar/**/templates/**.html'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
256 'genshi' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
257 >>> options_map['foobar/**/templates/**.html']['include_attrs'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
258 '' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
259 >>> method_map['foobar/**/templates/**.txt'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
260 'genshi' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
261 >>> options_map['foobar/**/templates/**.txt']['template_class'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
262 'genshi.template.text.TextTemplate' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
263 >>> options_map['foobar/**/templates/**.txt']['encoding'] |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
264 'latin-1' |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
265 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
266 :param fileobj: a readable file-like object containing the configuration |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
267 text to parse |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
268 :return: a `(method_map, options_map)` tuple |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
269 :rtype: `tuple` |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
270 :see: `extract_from_directory` |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
271 """ |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
272 method_map = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
273 options_map = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
274 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
275 method = None |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
276 for line in fileobj.readlines(): |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
277 if line.startswith('#'): # comment |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
278 continue |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
279 match = re.match('(\w+): (.+)', line) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
280 if match: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
281 method, pattern = match.group(1, 2) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
282 method_map[pattern] = method |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
283 options_map[pattern] = {} |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
284 elif method: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
285 match = re.match('\s+(\w+)\s*=\s*(.*)', line) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
286 if match: |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
287 option, value = match.group(1, 2) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
288 options_map[pattern][option] = value.strip() |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
289 |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
290 return (method_map, options_map) |
f8469ab4b257
Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents:
24
diff
changeset
|
291 |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
292 def parse_keywords(strings=[]): |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
293 """Parse keywords specifications from the given list of strings. |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
294 |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
295 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3']) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
296 >>> for keyword, indices in sorted(kw.items()): |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
297 ... print (keyword, indices) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
298 ('_', None) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
299 ('dgettext', (2,)) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
300 ('dngettext', (2, 3)) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
301 """ |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
302 keywords = {} |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
303 for string in strings: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
304 if ':' in string: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
305 funcname, indices = string.split(':') |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
306 else: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
307 funcname, indices = string, None |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
308 if funcname not in keywords: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
309 if indices: |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
310 indices = tuple([(int(x)) for x in indices.split(',')]) |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
311 keywords[funcname] = indices |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
312 return keywords |
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
10
diff
changeset
|
313 |
1 | 314 if __name__ == '__main__': |
315 main() |