Ticket #1: keyword_indices.diff
| File keyword_indices.diff, 4.8 KB (added by palgarvio, 5 years ago) |
|---|
-
babel/catalog/extract.py
33 33 34 34 GROUP_NAME = 'babel.extractors' 35 35 36 KEYWORDS = ( 37 '_', 'gettext', 'ngettext', 38 'dgettext', 'dngettext', 39 'ugettext', 'ungettext' 40 ) 36 KEYWORDS = { 37 '_': None, 38 'gettext': None, 39 'ngettext': (1, 2), 40 'ugettext': None, 41 'ungettext': (1, 2), 42 'dgettext': (2,), 43 'dngettext': (2, 3), 44 } 41 45 42 46 DEFAULT_MAPPING = { 43 47 'genshi': ['*.html', '**/*.html'], 44 48 'python': ['*.py', '**/*.py'] 45 49 } 46 50 51 47 52 def extract_from_dir(dirname, mapping=DEFAULT_MAPPING, keywords=KEYWORDS, 48 53 options=None): 49 54 """Extract messages from any source files found in the given directory. 50 55 51 56 This function generates tuples of the form: 52 57 53 58 ``(filename, lineno, funcname, message)`` 54 59 55 60 Which extraction method used is per file is determined by the `mapping` 56 61 parameter, which maps extraction method names to lists of extended glob 57 62 patterns. For example, the following is the default mapping: 58 63 59 64 >>> mapping = { 60 65 ... 'python': ['*.py', '**/*.py'] 61 66 ... } 62 67 63 68 This basically says that files with the filename extension ".py" at any 64 69 level inside the directory should be processed by the "python" extraction 65 70 method. Files that don't match any of the patterns are ignored. 66 71 67 72 The following extended mapping would also use the "genshi" extraction method 68 73 on any file in "templates" subdirectory: 69 74 70 75 >>> mapping = { 71 76 ... 'genshi': ['**/templates/*.*', '**/templates/**/*.*'], 72 77 ... 'python': ['*.py', '**/*.py'] 73 78 ... } 74 79 75 80 :param dirname: the path to the directory to extract messages from 76 81 :param mapping: a mapping of extraction method names to extended glob 77 82 patterns … … 96 101 97 102 def extract_from_file(method, filename, keywords=KEYWORDS, options=None): 98 103 """Extract messages from a specific file. 99 104 100 105 This function returns a list of tuples of the form: 101 106 102 107 ``(lineno, funcname, message)`` 103 108 104 109 :param filename: the path to the file to extract messages from 105 110 :param method: a string specifying the extraction method (.e.g. "python") 106 111 :param keywords: a list of keywords (i.e. function names) that should be … … 118 123 def extract(method, fileobj, keywords=KEYWORDS, options=None): 119 124 """Extract messages from the given file-like object using the specified 120 125 extraction method. 121 126 122 127 This function returns a list of tuples of the form: 123 128 124 129 ``(lineno, funcname, message)`` 125 130 126 131 The implementation dispatches the actual extraction to plugins, based on the 127 132 value of the ``method`` parameter. 128 133 129 134 >>> source = '''# foo module 130 135 ... def run(argv): 131 136 ... print _('Hello, world!') … … 135 140 >>> for message in extract('python', StringIO(source)): 136 141 ... print message 137 142 (3, '_', 'Hello, world!') 138 143 139 144 :param method: a string specifying the extraction method (.e.g. "python") 140 145 :param fileobj: the file-like object the messages should be extracted from 141 146 :param keywords: a list of keywords (i.e. function names) that should be … … 147 152 """ 148 153 for entry_point in working_set.iter_entry_points(GROUP_NAME, method): 149 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 168 #return list(func(fileobj, keywords, options=options or {})) 151 169 raise ValueError('Unknown extraction method %r' % method) 152 170 153 171 def extract_genshi(fileobj, keywords, options): 154 172 """Extract messages from Genshi templates. 155 173 156 174 :param fileobj: the file-like object the messages should be extracted from 157 175 :param keywords: a list of keywords (i.e. function names) that should be 158 176 recognized as translation functions … … 169 187 170 188 def extract_python(fileobj, keywords, options): 171 189 """Extract messages from Python source code. 172 190 173 191 :param fileobj: the file-like object the messages should be extracted from 174 192 :param keywords: a list of keywords (i.e. function names) that should be 175 193 recognized as translation functions
