annotate babel/messages/extract.py @ 75:0f74337264ce trunk

Fixed MIME type of new doc page.
author cmlenz
date Fri, 08 Jun 2007 21:54:56 +0000
parents 2df27f49c320
children 116e34b8cefa
rev   line source
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
2 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
5 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
9 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
13
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
14 """Basic infrastructure for extracting localizable messages from source files.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
15
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
16 This module defines an extensible system for collecting localizable message
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
17 strings from a variety of sources. A native extractor for Python source files
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
18 is builtin, extractors for other sources can be added using very simple plugins.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
19
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
20 The main entry points into the extraction functionality are the functions
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
21 `extract_from_dir` and `extract_from_file`.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
22 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
23
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
24 import os
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
25 try:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
26 set
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
27 except NameError:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
28 from sets import Set as set
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
29 import sys
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
30 from tokenize import generate_tokens, NAME, OP, STRING
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
31
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
32 from babel.util import pathmatch, relpath
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
33
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
34 __all__ = ['extract', 'extract_from_dir', 'extract_from_file']
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
35 __docformat__ = 'restructuredtext en'
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
36
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
37 GROUP_NAME = 'babel.extractors'
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
38
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
39 DEFAULT_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: 1
diff changeset
40 '_': None,
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: 1
diff changeset
41 'gettext': None,
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: 1
diff changeset
42 'ngettext': (1, 2),
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: 1
diff changeset
43 'ugettext': None,
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: 1
diff changeset
44 'ungettext': (1, 2),
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: 1
diff changeset
45 'dgettext': (2,),
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: 1
diff changeset
46 'dngettext': (2, 3),
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: 1
diff changeset
47 }
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
48
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
49 DEFAULT_MAPPING = [('**.py', 'python')]
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
50
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: 44
diff changeset
51 def extract_from_dir(dirname=os.getcwd(), 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: 44
diff changeset
52 options_map=None, keywords=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: 44
diff changeset
53 callback=None):
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
54 """Extract messages from any source files found in the given directory.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
55
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
56 This function generates tuples of the form:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
57
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
58 ``(filename, lineno, message)``
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
59
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
60 Which extraction method is used per file is determined by the `method_map`
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
61 parameter, which maps extended glob patterns to extraction method names.
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
62 For example, the following is the default mapping:
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
63
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
64 >>> method_map = [
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
65 ... ('**.py', 'python')
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
66 ... ]
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
67
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
68 This basically says that files with the filename extension ".py" at any
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
69 level inside the directory should be processed by the "python" extraction
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
70 method. Files that don't match any of the mapping patterns are ignored. See
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
71 the documentation of the `pathmatch` function for details on the pattern
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
72 syntax.
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
73
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
74 The following extended mapping would also use the "genshi" extraction
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
75 method on any file in "templates" subdirectory:
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
76
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
77 >>> method_map = [
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
78 ... ('**/templates/**.*', 'genshi'),
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
79 ... ('**.py', 'python')
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
80 ... ]
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
81
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
82 The dictionary provided by the optional `options_map` parameter augments
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
83 these mappings. It uses extended glob patterns as keys, and the values are
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
84 dictionaries mapping options names to option values (both strings).
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
85
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
86 The glob patterns of the `options_map` do not necessarily need to be the
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
87 same as those used in the method mapping. For example, while all files in
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
88 the ``templates`` folders in an application may be Genshi applications, the
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
89 options for those files may differ based on extension:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
90
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
91 >>> options_map = {
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
92 ... '**/templates/**.txt': {
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
93 ... 'template_class': 'genshi.template.text.TextTemplate',
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
94 ... 'encoding': 'latin-1'
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
95 ... },
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
96 ... '**/templates/**.html': {
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
97 ... 'include_attrs': ''
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
98 ... }
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
99 ... }
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
100
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
101 :param dirname: the path to the directory to extract messages from
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
102 :param method_map: a list of ``(pattern, method)`` tuples that maps of
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
103 extraction method names to extended glob patterns
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
104 :param options_map: a dictionary of additional options (optional)
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
105 :param keywords: a dictionary mapping keywords (i.e. names of functions
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
106 that should be recognized as translation functions) to
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
107 tuples that specify which of their arguments contain
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
108 localizable strings
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: 44
diff changeset
109 :param callback: a function that is called for every file that message are
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: 44
diff changeset
110 extracted from, just before the extraction itself is
75
0f74337264ce Fixed MIME type of new doc page.
cmlenz
parents: 62
diff changeset
111 performed; the function is passed the filename, the name
0f74337264ce Fixed MIME type of new doc page.
cmlenz
parents: 62
diff changeset
112 of the extraction method and and the options dictionary as
0f74337264ce Fixed MIME type of new doc page.
cmlenz
parents: 62
diff changeset
113 positional arguments, in that order
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
114 :return: an iterator over ``(filename, lineno, funcname, message)`` tuples
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
115 :rtype: ``iterator``
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
116 :see: `pathmatch`
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
117 """
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
118 if options_map is None:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
119 options_map = {}
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
120
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
121 absname = os.path.abspath(dirname)
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
122 for root, dirnames, filenames in os.walk(absname):
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
123 for subdir in dirnames:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
124 if subdir.startswith('.') or subdir.startswith('_'):
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
125 dirnames.remove(subdir)
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
126 for filename in filenames:
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
127 filename = relpath(
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
128 os.path.join(root, filename).replace(os.sep, '/'),
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
129 dirname
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
130 )
62
2df27f49c320 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 57
diff changeset
131 for pattern, method in method_map:
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
132 if pathmatch(pattern, filename):
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
133 filepath = os.path.join(absname, filename)
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
134 options = {}
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
135 for opattern, odict in options_map.items():
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
136 if pathmatch(opattern, filename):
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
137 options = odict
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: 44
diff changeset
138 if callback:
57
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
139 callback(filename, method, options)
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
140 for lineno, message in extract_from_file(method, filepath,
44
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
141 keywords=keywords,
a524b547ea7e Some work towards #4.
cmlenz
parents: 36
diff changeset
142 options=options):
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
143 yield filename, lineno, message
57
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
144 break
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
145
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
146 def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS,
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
147 options=None):
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
148 """Extract messages from a specific file.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
149
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
150 This function returns a list of tuples of the form:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
151
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
152 ``(lineno, funcname, message)``
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
153
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
154 :param filename: the path to the file to extract messages from
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
155 :param method: a string specifying the extraction method (.e.g. "python")
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
156 :param keywords: a dictionary mapping keywords (i.e. names of functions
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
157 that should be recognized as translation functions) to
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
158 tuples that specify which of their arguments contain
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
159 localizable strings
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
160 :param options: a dictionary of additional options (optional)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
161 :return: the list of extracted messages
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
162 :rtype: `list`
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
163 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
164 fileobj = open(filename, 'U')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
165 try:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
166 return list(extract(method, fileobj, keywords, options=options))
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
167 finally:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
168 fileobj.close()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
169
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
170 def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, options=None):
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
171 """Extract messages from the given file-like object using the specified
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
172 extraction method.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
173
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
174 This function returns a list of tuples of the form:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
175
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
176 ``(lineno, message)``
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
177
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
178 The implementation dispatches the actual extraction to plugins, based on the
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
179 value of the ``method`` parameter.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
180
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
181 >>> source = '''# foo module
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
182 ... def run(argv):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
183 ... print _('Hello, world!')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
184 ... '''
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: 1
diff changeset
185
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
186 >>> from StringIO import StringIO
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
187 >>> for message in extract('python', StringIO(source)):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
188 ... print message
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
189 (3, 'Hello, world!')
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
190
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
191 :param method: a string specifying the extraction method (.e.g. "python")
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
192 :param fileobj: the file-like object the messages should be extracted from
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
193 :param keywords: a dictionary mapping keywords (i.e. names of functions
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
194 that should be recognized as translation functions) to
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
195 tuples that specify which of their arguments contain
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
196 localizable strings
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
197 :param options: a dictionary of additional options (optional)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
198 :return: the list of extracted messages
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
199 :rtype: `list`
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
200 :raise ValueError: if the extraction method is not registered
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
201 """
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
202 from pkg_resources import working_set
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
203
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
204 for entry_point in working_set.iter_entry_points(GROUP_NAME, method):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
205 func = entry_point.load(require=True)
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: 1
diff changeset
206 m = []
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: 1
diff changeset
207 for lineno, funcname, messages in func(fileobj, keywords.keys(),
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: 1
diff changeset
208 options=options or {}):
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: 1
diff changeset
209 if isinstance(messages, (list, tuple)):
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: 1
diff changeset
210 msgs = []
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
211 for index in keywords[funcname]:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
212 msgs.append(messages[index - 1])
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: 1
diff changeset
213 messages = tuple(msgs)
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: 1
diff changeset
214 if len(messages) == 1:
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: 1
diff changeset
215 messages = messages[0]
56
f40fc143439c Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 54
diff changeset
216 yield lineno, messages
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: 1
diff changeset
217 return
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
218
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
219 raise ValueError('Unknown extraction method %r' % method)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
220
57
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
221 def extract_nothing(fileobj, keywords, options):
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
222 """Pseudo extractor that does not actually extract anything, but simply
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
223 returns an empty list.
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
224 """
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
225 return []
d930a3dfbf3d * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
226
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
227 def extract_genshi(fileobj, keywords, options):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
228 """Extract messages from Genshi templates.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
229
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
230 :param fileobj: the file-like object the messages should be extracted from
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
231 :param keywords: a list of keywords (i.e. function names) that should be
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
232 recognized as translation functions
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
233 :param options: a dictionary of additional options (optional)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
234 :return: an iterator over ``(lineno, funcname, message)`` tuples
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
235 :rtype: ``iterator``
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
236 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
237 from genshi.filters.i18n import Translator
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
238 from genshi.template import MarkupTemplate
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: 44
diff changeset
239
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: 44
diff changeset
240 template_class = options.get('template_class', MarkupTemplate)
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: 44
diff changeset
241 if isinstance(template_class, basestring):
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: 44
diff changeset
242 module, clsname = template_class.split(':', 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: 44
diff changeset
243 template_class = getattr(__import__(module, {}, {}, [clsname]), clsname)
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: 44
diff changeset
244 encoding = options.get('encoding', 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: 44
diff changeset
245
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: 44
diff changeset
246 ignore_tags = options.get('ignore_tags', Translator.IGNORE_TAGS)
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: 44
diff changeset
247 if isinstance(ignore_tags, basestring):
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: 44
diff changeset
248 ignore_tags = ignore_tags.split()
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: 44
diff changeset
249 include_attrs = options.get('include_attrs', Translator.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: 44
diff changeset
250 if isinstance(include_attrs, basestring):
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: 44
diff changeset
251 include_attrs = include_attrs.split()
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: 44
diff changeset
252
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: 44
diff changeset
253 tmpl = template_class(fileobj, filename=getattr(fileobj, 'name'),
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: 44
diff changeset
254 encoding=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: 44
diff changeset
255 translator = Translator(None, ignore_tags, include_attrs)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
256 for message in translator.extract(tmpl.stream, gettext_functions=keywords):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
257 yield message
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
258
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
259 def extract_python(fileobj, keywords, options):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
260 """Extract messages from Python source code.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
261
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
262 :param fileobj: the file-like object the messages should be extracted from
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
263 :param keywords: a list of keywords (i.e. function names) that should be
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
264 recognized as translation functions
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
265 :param options: a dictionary of additional options (optional)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
266 :return: an iterator over ``(lineno, funcname, message)`` tuples
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
267 :rtype: ``iterator``
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
268 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
269 funcname = None
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
270 lineno = None
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
271 buf = []
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
272 messages = []
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
273 in_args = False
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
274
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
275 tokens = generate_tokens(fileobj.readline)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
276 for tok, value, (lineno, _), _, _ in tokens:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
277 if funcname and tok == OP and value == '(':
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
278 in_args = True
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
279 elif funcname and in_args:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
280 if tok == OP and value == ')':
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
281 in_args = False
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
282 if buf:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
283 messages.append(''.join(buf))
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
284 del buf[:]
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
285 if filter(None, messages):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
286 if len(messages) > 1:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
287 messages = tuple(messages)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
288 else:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
289 messages = messages[0]
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
290 yield lineno, funcname, messages
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
291 funcname = lineno = None
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
292 messages = []
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
293 elif tok == STRING:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
294 if lineno is None:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
295 lineno = stup[0]
36
cfd15b7921f9 Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents: 12
diff changeset
296 # Unwrap quotes in a safe manner
cfd15b7921f9 Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents: 12
diff changeset
297 buf.append(eval(value, {'__builtins__':{}}, {}))
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
298 elif tok == OP and value == ',':
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
299 messages.append(''.join(buf))
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
300 del buf[:]
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
301 elif funcname:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
302 funcname = None
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
303 elif tok == NAME and value in keywords:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
304 funcname = value
Copyright (C) 2012-2017 Edgewall Software