Edgewall Software

Ticket #1: keyword_indices.diff

File keyword_indices.diff, 4.8 KB (added by palgarvio, 5 years ago)
  • babel/catalog/extract.py

     
    3333 
    3434GROUP_NAME = 'babel.extractors' 
    3535 
    36 KEYWORDS = ( 
    37     '_', 'gettext', 'ngettext', 
    38     'dgettext', 'dngettext', 
    39     'ugettext', 'ungettext' 
    40 ) 
     36KEYWORDS = { 
     37    '_': None, 
     38    'gettext': None, 
     39    'ngettext': (1, 2), 
     40    'ugettext': None, 
     41    'ungettext': (1, 2), 
     42    'dgettext': (2,), 
     43    'dngettext': (2, 3), 
     44} 
    4145 
    4246DEFAULT_MAPPING = { 
    4347    'genshi': ['*.html', '**/*.html'], 
    4448    'python': ['*.py', '**/*.py'] 
    4549} 
    4650 
     51 
    4752def extract_from_dir(dirname, mapping=DEFAULT_MAPPING, keywords=KEYWORDS, 
    4853                     options=None): 
    4954    """Extract messages from any source files found in the given directory. 
    50      
     55 
    5156    This function generates tuples of the form: 
    52      
     57 
    5358        ``(filename, lineno, funcname, message)`` 
    54      
     59 
    5560    Which extraction method used is per file is determined by the `mapping` 
    5661    parameter, which maps extraction method names to lists of extended glob 
    5762    patterns. For example, the following is the default mapping: 
    58      
     63 
    5964    >>> mapping = { 
    6065    ...     'python': ['*.py', '**/*.py'] 
    6166    ... } 
    62      
     67 
    6368    This basically says that files with the filename extension ".py" at any 
    6469    level inside the directory should be processed by the "python" extraction 
    6570    method. Files that don't match any of the patterns are ignored. 
    66      
     71 
    6772    The following extended mapping would also use the "genshi" extraction method 
    6873    on any file in "templates" subdirectory: 
    69      
     74 
    7075    >>> mapping = { 
    7176    ...     'genshi': ['**/templates/*.*', '**/templates/**/*.*'], 
    7277    ...     'python': ['*.py', '**/*.py'] 
    7378    ... } 
    74      
     79 
    7580    :param dirname: the path to the directory to extract messages from 
    7681    :param mapping: a mapping of extraction method names to extended glob 
    7782                    patterns 
     
    96101 
    97102def extract_from_file(method, filename, keywords=KEYWORDS, options=None): 
    98103    """Extract messages from a specific file. 
    99      
     104 
    100105    This function returns a list of tuples of the form: 
    101      
     106 
    102107        ``(lineno, funcname, message)`` 
    103      
     108 
    104109    :param filename: the path to the file to extract messages from 
    105110    :param method: a string specifying the extraction method (.e.g. "python") 
    106111    :param keywords: a list of keywords (i.e. function names) that should be 
     
    118123def extract(method, fileobj, keywords=KEYWORDS, options=None): 
    119124    """Extract messages from the given file-like object using the specified 
    120125    extraction method. 
    121      
     126 
    122127    This function returns a list of tuples of the form: 
    123      
     128 
    124129        ``(lineno, funcname, message)`` 
    125      
     130 
    126131    The implementation dispatches the actual extraction to plugins, based on the 
    127132    value of the ``method`` parameter. 
    128      
     133 
    129134    >>> source = '''# foo module 
    130135    ... def run(argv): 
    131136    ...    print _('Hello, world!') 
     
    135140    >>> for message in extract('python', StringIO(source)): 
    136141    ...     print message 
    137142    (3, '_', 'Hello, world!') 
    138      
     143 
    139144    :param method: a string specifying the extraction method (.e.g. "python") 
    140145    :param fileobj: the file-like object the messages should be extracted from 
    141146    :param keywords: a list of keywords (i.e. function names) that should be 
     
    147152    """ 
    148153    for entry_point in working_set.iter_entry_points(GROUP_NAME, method): 
    149154        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 {})) 
    151169    raise ValueError('Unknown extraction method %r' % method) 
    152170 
    153171def extract_genshi(fileobj, keywords, options): 
    154172    """Extract messages from Genshi templates. 
    155      
     173 
    156174    :param fileobj: the file-like object the messages should be extracted from 
    157175    :param keywords: a list of keywords (i.e. function names) that should be 
    158176                     recognized as translation functions 
     
    169187 
    170188def extract_python(fileobj, keywords, options): 
    171189    """Extract messages from Python source code. 
    172      
     190 
    173191    :param fileobj: the file-like object the messages should be extracted from 
    174192    :param keywords: a list of keywords (i.e. function names) that should be 
    175193                     recognized as translation functions