Mercurial > babel > old > mirror
annotate babel/messages/extract.py @ 140:3b3a487cc46f
Genshi extraction method has moved to Genshi project. Closes #13.
author | cmlenz |
---|---|
date | Wed, 20 Jun 2007 10:02:04 +0000 |
parents | a1fcc54ffc89 |
children | 24b5de939850 |
rev | line source |
---|---|
3 | 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 """Basic infrastructure for extracting localizable messages from source files. | |
15 | |
16 This module defines an extensible system for collecting localizable message | |
17 strings from a variety of sources. A native extractor for Python source files | |
18 is builtin, extractors for other sources can be added using very simple plugins. | |
19 | |
20 The main entry points into the extraction functionality are the functions | |
21 `extract_from_dir` and `extract_from_file`. | |
22 """ | |
23 | |
24 import os | |
46 | 25 try: |
26 set | |
27 except NameError: | |
28 from sets import Set as set | |
3 | 29 import sys |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
30 from tokenize import generate_tokens, NAME, OP, STRING, COMMENT |
3 | 31 |
46 | 32 from babel.util import pathmatch, relpath |
3 | 33 |
34 __all__ = ['extract', 'extract_from_dir', 'extract_from_file'] | |
35 __docformat__ = 'restructuredtext en' | |
36 | |
37 GROUP_NAME = 'babel.extractors' | |
38 | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
39 DEFAULT_KEYWORDS = { |
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:
3
diff
changeset
|
40 '_': None, |
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:
3
diff
changeset
|
41 'gettext': None, |
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:
3
diff
changeset
|
42 'ngettext': (1, 2), |
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:
3
diff
changeset
|
43 'ugettext': None, |
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:
3
diff
changeset
|
44 'ungettext': (1, 2), |
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:
3
diff
changeset
|
45 'dgettext': (2,), |
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:
3
diff
changeset
|
46 'dngettext': (2, 3), |
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:
3
diff
changeset
|
47 } |
3 | 48 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
49 DEFAULT_MAPPING = [('**.py', 'python')] |
3 | 50 |
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:
46
diff
changeset
|
51 def extract_from_dir(dirname=os.getcwd(), method_map=DEFAULT_MAPPING, |
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:
46
diff
changeset
|
52 options_map=None, keywords=DEFAULT_KEYWORDS, |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
53 comment_tags=(), callback=None): |
3 | 54 """Extract messages from any source files found in the given directory. |
55 | |
56 This function generates tuples of the form: | |
57 | |
84
3c6421bca777
Missed some param's documentation regarding translator comments.
palgarvio
parents:
83
diff
changeset
|
58 ``(filename, lineno, message, comments)`` |
3 | 59 |
46 | 60 Which extraction method is used per file is determined by the `method_map` |
61 parameter, which maps extended glob patterns to extraction method names. | |
62 For example, the following is the default mapping: | |
3 | 63 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
64 >>> method_map = [ |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
65 ... ('**.py', 'python') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
66 ... ] |
3 | 67 |
68 This basically says that files with the filename extension ".py" at any | |
69 level inside the directory should be processed by the "python" extraction | |
46 | 70 method. Files that don't match any of the mapping patterns are ignored. See |
71 the documentation of the `pathmatch` function for details on the pattern | |
72 syntax. | |
3 | 73 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
74 The following extended mapping would also use the "genshi" extraction |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
75 method on any file in "templates" subdirectory: |
3 | 76 |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
77 >>> method_map = [ |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
78 ... ('**/templates/**.*', 'genshi'), |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
79 ... ('**.py', 'python') |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
80 ... ] |
46 | 81 |
82 The dictionary provided by the optional `options_map` parameter augments | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
83 these mappings. It uses extended glob patterns as keys, and the values are |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
84 dictionaries mapping options names to option values (both strings). |
46 | 85 |
86 The glob patterns of the `options_map` do not necessarily need to be the | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
87 same as those used in the method mapping. For example, while all files in |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
88 the ``templates`` folders in an application may be Genshi applications, the |
46 | 89 options for those files may differ based on extension: |
90 | |
91 >>> options_map = { | |
92 ... '**/templates/**.txt': { | |
93 ... 'template_class': 'genshi.template.text.TextTemplate', | |
94 ... 'encoding': 'latin-1' | |
95 ... }, | |
96 ... '**/templates/**.html': { | |
97 ... 'include_attrs': '' | |
98 ... } | |
3 | 99 ... } |
100 | |
101 :param dirname: the path to the directory to extract messages from | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
102 :param method_map: a list of ``(pattern, method)`` tuples that maps of |
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
103 extraction method names to extended glob patterns |
46 | 104 :param options_map: a dictionary of additional options (optional) |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
105 :param keywords: a dictionary mapping keywords (i.e. names of functions |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
106 that should be recognized as translation functions) to |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
107 tuples that specify which of their arguments contain |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
108 localizable strings |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
109 :param comment_tags: a list of tags of translator comments to search for |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
110 and include in the results |
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:
46
diff
changeset
|
111 :param callback: a function that is called for every file that message are |
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:
46
diff
changeset
|
112 extracted from, just before the extraction itself is |
77 | 113 performed; the function is passed the filename, the name |
114 of the extraction method and and the options dictionary as | |
115 positional arguments, in that order | |
3 | 116 :return: an iterator over ``(filename, lineno, funcname, message)`` tuples |
117 :rtype: ``iterator`` | |
46 | 118 :see: `pathmatch` |
3 | 119 """ |
46 | 120 if options_map is None: |
121 options_map = {} | |
58
068952b4d4c0
Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
56
diff
changeset
|
122 |
46 | 123 absname = os.path.abspath(dirname) |
124 for root, dirnames, filenames in os.walk(absname): | |
125 for subdir in dirnames: | |
126 if subdir.startswith('.') or subdir.startswith('_'): | |
127 dirnames.remove(subdir) | |
128 for filename in filenames: | |
129 filename = relpath( | |
130 os.path.join(root, filename).replace(os.sep, '/'), | |
131 dirname | |
132 ) | |
64
7eb6fea17864
The order of extraction methods is now preserved (see #10).
cmlenz
parents:
59
diff
changeset
|
133 for pattern, method in method_map: |
46 | 134 if pathmatch(pattern, filename): |
135 filepath = os.path.join(absname, filename) | |
136 options = {} | |
137 for opattern, odict in options_map.items(): | |
138 if pathmatch(opattern, filename): | |
139 options = odict | |
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:
46
diff
changeset
|
140 if callback: |
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 callback(filename, method, options) |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
142 for lineno, message, comments in \ |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
143 extract_from_file(method, filepath, |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
144 keywords=keywords, |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
145 comment_tags=comment_tags, |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
146 options=options): |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
147 yield filename, lineno, message, comments |
59
e68fd956ebce
* The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents:
58
diff
changeset
|
148 break |
3 | 149 |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
150 def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS, |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
151 comment_tags=(), options=None): |
3 | 152 """Extract messages from a specific file. |
153 | |
154 This function returns a list of tuples of the form: | |
155 | |
156 ``(lineno, funcname, message)`` | |
157 | |
158 :param filename: the path to the file to extract messages from | |
159 :param method: a string specifying the extraction method (.e.g. "python") | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
160 :param keywords: a dictionary mapping keywords (i.e. names of functions |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
161 that should be recognized as translation functions) to |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
162 tuples that specify which of their arguments contain |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
163 localizable strings |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
164 :param comment_tags: a list of translator tags to search for and include |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
165 in the results |
3 | 166 :param options: a dictionary of additional options (optional) |
167 :return: the list of extracted messages | |
168 :rtype: `list` | |
169 """ | |
170 fileobj = open(filename, 'U') | |
171 try: | |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
172 return list(extract(method, fileobj, keywords, comment_tags, options)) |
3 | 173 finally: |
174 fileobj.close() | |
175 | |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
176 def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comment_tags=(), |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
177 options=None): |
3 | 178 """Extract messages from the given file-like object using the specified |
179 extraction method. | |
180 | |
181 This function returns a list of tuples of the form: | |
182 | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
183 ``(lineno, message, comments)`` |
3 | 184 |
185 The implementation dispatches the actual extraction to plugins, based on the | |
186 value of the ``method`` parameter. | |
187 | |
188 >>> source = '''# foo module | |
189 ... def run(argv): | |
190 ... print _('Hello, world!') | |
191 ... ''' | |
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:
3
diff
changeset
|
192 |
3 | 193 >>> from StringIO import StringIO |
194 >>> for message in extract('python', StringIO(source)): | |
195 ... print message | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
196 (3, 'Hello, world!', []) |
3 | 197 |
198 :param method: a string specifying the extraction method (.e.g. "python") | |
199 :param fileobj: the file-like object the messages should be extracted from | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
200 :param keywords: a dictionary mapping keywords (i.e. names of functions |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
201 that should be recognized as translation functions) to |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
202 tuples that specify which of their arguments contain |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
203 localizable strings |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
204 :param comment_tags: a list of translator tags to search for and include |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
205 in the results |
3 | 206 :param options: a dictionary of additional options (optional) |
207 :return: the list of extracted messages | |
208 :rtype: `list` | |
209 :raise ValueError: if the extraction method is not registered | |
210 """ | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
211 from pkg_resources import working_set |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
212 |
3 | 213 for entry_point in working_set.iter_entry_points(GROUP_NAME, method): |
214 func = entry_point.load(require=True) | |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
215 results = func(fileobj, keywords.keys(), comment_tags, |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
216 options=options or {}) |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
217 for lineno, funcname, messages, comments in results: |
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:
3
diff
changeset
|
218 if isinstance(messages, (list, tuple)): |
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:
3
diff
changeset
|
219 msgs = [] |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
220 for index in keywords[funcname]: |
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
221 msgs.append(messages[index - 1]) |
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:
3
diff
changeset
|
222 messages = tuple(msgs) |
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:
3
diff
changeset
|
223 if len(messages) == 1: |
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:
3
diff
changeset
|
224 messages = messages[0] |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
225 yield lineno, messages, comments |
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:
3
diff
changeset
|
226 return |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
12
diff
changeset
|
227 |
3 | 228 raise ValueError('Unknown extraction method %r' % method) |
229 | |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
230 def extract_nothing(fileobj, keywords, comment_tags, options): |
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
|
231 """Pseudo extractor that does not actually extract anything, but simply |
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
|
232 returns an empty list. |
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
|
233 """ |
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
|
234 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
|
235 |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
236 def extract_python(fileobj, keywords, comment_tags, options): |
3 | 237 """Extract messages from Python source code. |
238 | |
239 :param fileobj: the file-like object the messages should be extracted from | |
240 :param keywords: a list of keywords (i.e. function names) that should be | |
241 recognized as translation functions | |
86
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
242 :param comment_tags: a list of translator tags to search for and include |
8a703ecdba91
Some cosmetic changes for the new translator comments support.
cmlenz
parents:
84
diff
changeset
|
243 in the results |
3 | 244 :param options: a dictionary of additional options (optional) |
83
80e895a498a3
Fixed and added some documentation about the translator comments implemented in [81].
palgarvio
parents:
82
diff
changeset
|
245 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples |
3 | 246 :rtype: ``iterator`` |
247 """ | |
248 funcname = None | |
249 lineno = None | |
250 buf = [] | |
251 messages = [] | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
252 translator_comments = [] |
3 | 253 in_args = False |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
254 in_translator_comments = False |
3 | 255 |
256 tokens = generate_tokens(fileobj.readline) | |
257 for tok, value, (lineno, _), _, _ in tokens: | |
258 if funcname and tok == OP and value == '(': | |
259 in_args = True | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
260 elif tok == COMMENT: |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
261 # Strip the comment token from the line |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
262 value = value[1:].strip() |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
263 if in_translator_comments is True and \ |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
264 translator_comments[-1][0] == lineno - 1: |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
265 # We're already inside a translator comment, continue appending |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
266 # XXX: Should we check if the programmer keeps adding the |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
267 # comment_tag for every comment line??? probably not! |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
268 translator_comments.append((lineno, value)) |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
269 continue |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
270 # If execution reaches this point, let's see if comment line |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
271 # starts with one of the comment tags |
87
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
86
diff
changeset
|
272 for comment_tag in comment_tags: |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
273 if value.startswith(comment_tag): |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
274 if in_translator_comments is not True: |
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
275 in_translator_comments = True |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
276 comment = value[len(comment_tag):].strip() |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
277 translator_comments.append((lineno, comment)) |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
278 break |
3 | 279 elif funcname and in_args: |
280 if tok == OP and value == ')': | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
281 in_args = in_translator_comments = False |
3 | 282 if buf: |
283 messages.append(''.join(buf)) | |
284 del buf[:] | |
285 if filter(None, messages): | |
286 if len(messages) > 1: | |
287 messages = tuple(messages) | |
288 else: | |
289 messages = messages[0] | |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
290 # Comments don't apply unless they immediately preceed the |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
291 # message |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
292 if translator_comments and \ |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
293 translator_comments[-1][0] < lineno - 1: |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
294 translator_comments = [] |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
295 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
296 yield (lineno, funcname, messages, |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
297 [comment[1] for comment in translator_comments]) |
3 | 298 funcname = lineno = None |
299 messages = [] | |
82
f421e5576d26
Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents:
77
diff
changeset
|
300 translator_comments = [] |
3 | 301 elif tok == STRING: |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
302 # Unwrap quotes in a safe manner |
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
303 buf.append(eval(value, {'__builtins__':{}}, {})) |
3 | 304 elif tok == OP and value == ',': |
305 messages.append(''.join(buf)) | |
306 del buf[:] | |
307 elif funcname: | |
308 funcname = None | |
309 elif tok == NAME and value in keywords: | |
310 funcname = value |