comparison babel/messages/frontend.py @ 569:1b801a0cb2cb trunk

Support for context-aware methods during message extraction (fixes #229, patch by David Rios)
author fschwarz
date Mon, 26 Sep 2011 20:01:01 +0000
parents c6bc419cc32a
children 99c48a6ca1d6
comparison
equal deleted inserted replaced
568:39ff5164e8ea 569:1b801a0cb2cb
307 keywords=self._keywords, 307 keywords=self._keywords,
308 comment_tags=self._add_comments, 308 comment_tags=self._add_comments,
309 callback=callback, 309 callback=callback,
310 strip_comment_tags= 310 strip_comment_tags=
311 self.strip_comments) 311 self.strip_comments)
312 for filename, lineno, message, comments in extracted: 312 for filename, lineno, message, comments, context in extracted:
313 filepath = os.path.normpath(os.path.join(dirname, filename)) 313 filepath = os.path.normpath(os.path.join(dirname, filename))
314 catalog.add(message, None, [(filepath, lineno)], 314 catalog.add(message, None, [(filepath, lineno)],
315 auto_comments=comments) 315 auto_comments=comments, context=context)
316 316
317 log.info('writing PO template file to %s' % self.output_file) 317 log.info('writing PO template file to %s' % self.output_file)
318 write_po(outfile, catalog, width=self.width, 318 write_po(outfile, catalog, width=self.width,
319 no_location=self.no_location, 319 no_location=self.no_location,
320 omit_header=self.omit_header, 320 omit_header=self.omit_header,
905 extracted = extract_from_dir(dirname, method_map, options_map, 905 extracted = extract_from_dir(dirname, method_map, options_map,
906 keywords, options.comment_tags, 906 keywords, options.comment_tags,
907 callback=callback, 907 callback=callback,
908 strip_comment_tags= 908 strip_comment_tags=
909 options.strip_comment_tags) 909 options.strip_comment_tags)
910 for filename, lineno, message, comments in extracted: 910 for filename, lineno, message, comments, context in extracted:
911 filepath = os.path.normpath(os.path.join(dirname, filename)) 911 filepath = os.path.normpath(os.path.join(dirname, filename))
912 catalog.add(message, None, [(filepath, lineno)], 912 catalog.add(message, None, [(filepath, lineno)],
913 auto_comments=comments) 913 auto_comments=comments, context=context)
914 914
915 if options.output not in (None, '-'): 915 if options.output not in (None, '-'):
916 self.log.info('writing PO template file to %s' % options.output) 916 self.log.info('writing PO template file to %s' % options.output)
917 write_po(outfile, catalog, width=options.width, 917 write_po(outfile, catalog, width=options.width,
918 no_location=options.no_location, 918 no_location=options.no_location,
1179 return (method_map, options_map) 1179 return (method_map, options_map)
1180 1180
1181 def parse_keywords(strings=[]): 1181 def parse_keywords(strings=[]):
1182 """Parse keywords specifications from the given list of strings. 1182 """Parse keywords specifications from the given list of strings.
1183 1183
1184 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3']).items() 1184 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items()
1185 >>> kw.sort() 1185 >>> kw.sort()
1186 >>> for keyword, indices in kw: 1186 >>> for keyword, indices in kw:
1187 ... print (keyword, indices) 1187 ... print (keyword, indices)
1188 ('_', None) 1188 ('_', None)
1189 ('dgettext', (2,)) 1189 ('dgettext', (2,))
1190 ('dngettext', (2, 3)) 1190 ('dngettext', (2, 3))
1191 ('pgettext', ((1, 'c'), 2))
1191 """ 1192 """
1192 keywords = {} 1193 keywords = {}
1193 for string in strings: 1194 for string in strings:
1194 if ':' in string: 1195 if ':' in string:
1195 funcname, indices = string.split(':') 1196 funcname, indices = string.split(':')
1196 else: 1197 else:
1197 funcname, indices = string, None 1198 funcname, indices = string, None
1198 if funcname not in keywords: 1199 if funcname not in keywords:
1199 if indices: 1200 if indices:
1200 indices = tuple([(int(x)) for x in indices.split(',')]) 1201 inds = []
1202 for x in indices.split(','):
1203 if x[-1] == 'c':
1204 inds.append((int(x[:-1]), 'c'))
1205 else:
1206 inds.append(int(x))
1207 indices = tuple(inds)
1201 keywords[funcname] = indices 1208 keywords[funcname] = indices
1202 return keywords 1209 return keywords
1203 1210
1204 1211
1205 if __name__ == '__main__': 1212 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software