Mercurial > babel > mirror
diff babel/catalog/extract.py @ 10:4130d9c6cb34 trunk
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 | 7870274479f5 |
children | e6ba3e878b10 |
line wrap: on
line diff
--- a/babel/catalog/extract.py +++ b/babel/catalog/extract.py @@ -33,17 +33,22 @@ GROUP_NAME = 'babel.extractors' -KEYWORDS = ( - '_', 'gettext', 'ngettext', - 'dgettext', 'dngettext', - 'ugettext', 'ungettext' -) +KEYWORDS = { + '_': None, + 'gettext': None, + 'ngettext': (1, 2), + 'ugettext': None, + 'ungettext': (1, 2), + 'dgettext': (2,), + 'dngettext': (2, 3), +} DEFAULT_MAPPING = { 'genshi': ['*.html', '**/*.html'], 'python': ['*.py', '**/*.py'] } + def extract_from_dir(dirname, mapping=DEFAULT_MAPPING, keywords=KEYWORDS, options=None): """Extract messages from any source files found in the given directory. @@ -130,7 +135,7 @@ ... def run(argv): ... print _('Hello, world!') ... ''' - + >>> from StringIO import StringIO >>> for message in extract('python', StringIO(source)): ... print message @@ -147,7 +152,19 @@ """ for entry_point in working_set.iter_entry_points(GROUP_NAME, method): func = entry_point.load(require=True) - return list(func(fileobj, keywords, options=options or {})) + m = [] + for lineno, funcname, messages in func(fileobj, keywords.keys(), + options=options or {}): + if isinstance(messages, (list, tuple)): + indices = keywords[funcname] + msgs = [] + for indice in indices: + msgs.append(messages[indice-1]) + messages = tuple(msgs) + if len(messages) == 1: + messages = messages[0] + yield lineno, funcname, messages + return raise ValueError('Unknown extraction method %r' % method) def extract_genshi(fileobj, keywords, options):