diff babel/messages/frontend.py @ 252:2398fc97675b

Soften dependency on setuptools. Extraction methods can now be referenced using a special section in the mapping configuration, mapping short names to fully-qualified function references.
author cmlenz
date Mon, 13 Aug 2007 22:29:03 +0000
parents bf9579c4b0ee
children 72351fd5fc29
line wrap: on
line diff
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -1073,6 +1073,9 @@
     """Parse an extraction method mapping from a file-like object.
 
     >>> buf = StringIO('''
+    ... [extractors]
+    ... custom = mypackage.module:myfunc
+    ... 
     ... # Python source files
     ... [python: **.py]
     ...
@@ -1082,9 +1085,14 @@
     ... [genshi: **/templates/**.txt]
     ... template_class = genshi.template:TextTemplate
     ... encoding = latin-1
+    ... 
+    ... # Some custom extractor
+    ... [custom: **/custom/*.*]
     ... ''')
 
     >>> method_map, options_map = parse_mapping(buf)
+    >>> len(method_map)
+    4
 
     >>> method_map[0]
     ('**.py', 'python')
@@ -1101,12 +1109,18 @@
     >>> options_map['**/templates/**.txt']['encoding']
     'latin-1'
 
+    >>> method_map[3]
+    ('**/custom/*.*', 'mypackage.module:myfunc')
+    >>> options_map['**/custom/*.*']
+    {}
+
     :param fileobj: a readable file-like object containing the configuration
                     text to parse
     :return: a `(method_map, options_map)` tuple
     :rtype: `tuple`
     :see: `extract_from_directory`
     """
+    extractors = {}
     method_map = []
     options_map = {}
 
@@ -1114,9 +1128,18 @@
     parser._sections = odict(parser._sections) # We need ordered sections
     parser.readfp(fileobj, filename)
     for section in parser.sections():
-        method, pattern = [part.strip() for part in section.split(':', 1)]
-        method_map.append((pattern, method))
-        options_map[pattern] = dict(parser.items(section))
+        if section == 'extractors':
+            extractors = dict(parser.items(section))
+        else:
+            method, pattern = [part.strip() for part in section.split(':', 1)]
+            method_map.append((pattern, method))
+            options_map[pattern] = dict(parser.items(section))
+
+    if extractors:
+        for idx, (pattern, method) in enumerate(method_map):
+            if method in extractors:
+                method = extractors[method]
+            method_map[idx] = (pattern, method)
 
     return (method_map, options_map)
 
Copyright (C) 2012-2017 Edgewall Software