comparison 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
comparison
equal deleted inserted replaced
251:3b9d993b7aa3 252:2398fc97675b
1071 1071
1072 def parse_mapping(fileobj, filename=None): 1072 def parse_mapping(fileobj, filename=None):
1073 """Parse an extraction method mapping from a file-like object. 1073 """Parse an extraction method mapping from a file-like object.
1074 1074
1075 >>> buf = StringIO(''' 1075 >>> buf = StringIO('''
1076 ... [extractors]
1077 ... custom = mypackage.module:myfunc
1078 ...
1076 ... # Python source files 1079 ... # Python source files
1077 ... [python: **.py] 1080 ... [python: **.py]
1078 ... 1081 ...
1079 ... # Genshi templates 1082 ... # Genshi templates
1080 ... [genshi: **/templates/**.html] 1083 ... [genshi: **/templates/**.html]
1081 ... include_attrs = 1084 ... include_attrs =
1082 ... [genshi: **/templates/**.txt] 1085 ... [genshi: **/templates/**.txt]
1083 ... template_class = genshi.template:TextTemplate 1086 ... template_class = genshi.template:TextTemplate
1084 ... encoding = latin-1 1087 ... encoding = latin-1
1088 ...
1089 ... # Some custom extractor
1090 ... [custom: **/custom/*.*]
1085 ... ''') 1091 ... ''')
1086 1092
1087 >>> method_map, options_map = parse_mapping(buf) 1093 >>> method_map, options_map = parse_mapping(buf)
1094 >>> len(method_map)
1095 4
1088 1096
1089 >>> method_map[0] 1097 >>> method_map[0]
1090 ('**.py', 'python') 1098 ('**.py', 'python')
1091 >>> options_map['**.py'] 1099 >>> options_map['**.py']
1092 {} 1100 {}
1099 >>> options_map['**/templates/**.txt']['template_class'] 1107 >>> options_map['**/templates/**.txt']['template_class']
1100 'genshi.template:TextTemplate' 1108 'genshi.template:TextTemplate'
1101 >>> options_map['**/templates/**.txt']['encoding'] 1109 >>> options_map['**/templates/**.txt']['encoding']
1102 'latin-1' 1110 'latin-1'
1103 1111
1112 >>> method_map[3]
1113 ('**/custom/*.*', 'mypackage.module:myfunc')
1114 >>> options_map['**/custom/*.*']
1115 {}
1116
1104 :param fileobj: a readable file-like object containing the configuration 1117 :param fileobj: a readable file-like object containing the configuration
1105 text to parse 1118 text to parse
1106 :return: a `(method_map, options_map)` tuple 1119 :return: a `(method_map, options_map)` tuple
1107 :rtype: `tuple` 1120 :rtype: `tuple`
1108 :see: `extract_from_directory` 1121 :see: `extract_from_directory`
1109 """ 1122 """
1123 extractors = {}
1110 method_map = [] 1124 method_map = []
1111 options_map = {} 1125 options_map = {}
1112 1126
1113 parser = RawConfigParser() 1127 parser = RawConfigParser()
1114 parser._sections = odict(parser._sections) # We need ordered sections 1128 parser._sections = odict(parser._sections) # We need ordered sections
1115 parser.readfp(fileobj, filename) 1129 parser.readfp(fileobj, filename)
1116 for section in parser.sections(): 1130 for section in parser.sections():
1117 method, pattern = [part.strip() for part in section.split(':', 1)] 1131 if section == 'extractors':
1118 method_map.append((pattern, method)) 1132 extractors = dict(parser.items(section))
1119 options_map[pattern] = dict(parser.items(section)) 1133 else:
1134 method, pattern = [part.strip() for part in section.split(':', 1)]
1135 method_map.append((pattern, method))
1136 options_map[pattern] = dict(parser.items(section))
1137
1138 if extractors:
1139 for idx, (pattern, method) in enumerate(method_map):
1140 if method in extractors:
1141 method = extractors[method]
1142 method_map[idx] = (pattern, method)
1120 1143
1121 return (method_map, options_map) 1144 return (method_map, options_map)
1122 1145
1123 def parse_keywords(strings=[]): 1146 def parse_keywords(strings=[]):
1124 """Parse keywords specifications from the given list of strings. 1147 """Parse keywords specifications from the given list of strings.
Copyright (C) 2012-2017 Edgewall Software