comparison babel/catalog/extract.py @ 10:b24987f7318d

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.
author palgarvio
date Wed, 30 May 2007 22:48:11 +0000
parents f71ca60f2a4a
children a2c54ef107c2
comparison
equal deleted inserted replaced
9:3be73c6f01f1 10:b24987f7318d
31 __all__ = ['extract', 'extract_from_dir', 'extract_from_file'] 31 __all__ = ['extract', 'extract_from_dir', 'extract_from_file']
32 __docformat__ = 'restructuredtext en' 32 __docformat__ = 'restructuredtext en'
33 33
34 GROUP_NAME = 'babel.extractors' 34 GROUP_NAME = 'babel.extractors'
35 35
36 KEYWORDS = ( 36 KEYWORDS = {
37 '_', 'gettext', 'ngettext', 37 '_': None,
38 'dgettext', 'dngettext', 38 'gettext': None,
39 'ugettext', 'ungettext' 39 'ngettext': (1, 2),
40 ) 40 'ugettext': None,
41 'ungettext': (1, 2),
42 'dgettext': (2,),
43 'dngettext': (2, 3),
44 }
41 45
42 DEFAULT_MAPPING = { 46 DEFAULT_MAPPING = {
43 'genshi': ['*.html', '**/*.html'], 47 'genshi': ['*.html', '**/*.html'],
44 'python': ['*.py', '**/*.py'] 48 'python': ['*.py', '**/*.py']
45 } 49 }
50
46 51
47 def extract_from_dir(dirname, mapping=DEFAULT_MAPPING, keywords=KEYWORDS, 52 def extract_from_dir(dirname, mapping=DEFAULT_MAPPING, keywords=KEYWORDS,
48 options=None): 53 options=None):
49 """Extract messages from any source files found in the given directory. 54 """Extract messages from any source files found in the given directory.
50 55
128 133
129 >>> source = '''# foo module 134 >>> source = '''# foo module
130 ... def run(argv): 135 ... def run(argv):
131 ... print _('Hello, world!') 136 ... print _('Hello, world!')
132 ... ''' 137 ... '''
133 138
134 >>> from StringIO import StringIO 139 >>> from StringIO import StringIO
135 >>> for message in extract('python', StringIO(source)): 140 >>> for message in extract('python', StringIO(source)):
136 ... print message 141 ... print message
137 (3, '_', 'Hello, world!') 142 (3, '_', 'Hello, world!')
138 143
145 :rtype: `list` 150 :rtype: `list`
146 :raise ValueError: if the extraction method is not registered 151 :raise ValueError: if the extraction method is not registered
147 """ 152 """
148 for entry_point in working_set.iter_entry_points(GROUP_NAME, method): 153 for entry_point in working_set.iter_entry_points(GROUP_NAME, method):
149 func = entry_point.load(require=True) 154 func = entry_point.load(require=True)
150 return list(func(fileobj, keywords, options=options or {})) 155 m = []
156 for lineno, funcname, messages in func(fileobj, keywords.keys(),
157 options=options or {}):
158 if isinstance(messages, (list, tuple)):
159 indices = keywords[funcname]
160 msgs = []
161 for indice in indices:
162 msgs.append(messages[indice-1])
163 messages = tuple(msgs)
164 if len(messages) == 1:
165 messages = messages[0]
166 yield lineno, funcname, messages
167 return
151 raise ValueError('Unknown extraction method %r' % method) 168 raise ValueError('Unknown extraction method %r' % method)
152 169
153 def extract_genshi(fileobj, keywords, options): 170 def extract_genshi(fileobj, keywords, options):
154 """Extract messages from Genshi templates. 171 """Extract messages from Genshi templates.
155 172
Copyright (C) 2012-2017 Edgewall Software