@@ -222,56 +222,35 @@ def finalize_options(self):
222222
223223 def run (self ):
224224 mappings = self ._get_mappings ()
225- outfile = open (self .output_file , 'wb' )
225+ catalog = Catalog (project = self .distribution .get_name (),
226+ version = self .distribution .get_version (),
227+ msgid_bugs_address = self .msgid_bugs_address ,
228+ copyright_holder = self .copyright_holder ,
229+ charset = self .charset )
230+
226231 try :
227- catalog = Catalog (project = self .distribution .get_name (),
228- version = self .distribution .get_version (),
229- msgid_bugs_address = self .msgid_bugs_address ,
230- copyright_holder = self .copyright_holder ,
231- charset = self .charset )
232-
233- for dirname , (method_map , options_map ) in mappings .items ():
234- def callback (filename , method , options ):
235- if method == 'ignore' :
236- return
237- filepath = os .path .normpath (os .path .join (dirname , filename ))
238- optstr = ''
239- if options :
240- optstr = ' (%s)' % ', ' .join (['%s="%s"' % (k , v ) for
241- k , v in options .items ()])
242- log .info ('extracting messages from %s%s' , filepath , optstr )
243-
244- extracted = extract_from_dir (dirname , method_map , options_map ,
245- keywords = self ._keywords ,
246- comment_tags = self ._add_comments ,
247- callback = callback ,
248- strip_comment_tags =
249- self .strip_comments )
250- for filename , lineno , message , comments , context in extracted :
251- filepath = os .path .normpath (os .path .join (dirname , filename ))
252- catalog .add (message , None , [(filepath , lineno )],
253- auto_comments = comments , context = context )
232+ operations .extract_to_catalog (catalog , mappings , self ._keywords ,
233+ self ._add_comments ,
234+ self .strip_comments , log = log )
235+ except operations .ConfigureError as e :
236+ raise DistutilsOptionError (e .message )
254237
238+ with open (self .output_file , 'wb' ) as outfile :
255239 log .info ('writing PO template file to %s' % self .output_file )
256240 write_po (outfile , catalog , width = self .width ,
257241 no_location = self .no_location ,
258242 omit_header = self .omit_header ,
259243 sort_output = self .sort_output ,
260244 sort_by_file = self .sort_by_file )
261- finally :
262- outfile .close ()
263245
264246 def _get_mappings (self ):
265- mappings = {}
247+ mappings = []
266248
267249 if self .mapping_file :
268- fileobj = open (self .mapping_file , 'U' )
269- try :
250+ with open (self .mapping_file , 'U' ) as fileobj :
270251 method_map , options_map = parse_mapping (fileobj )
271252 for dirname in self .input_dirs :
272- mappings [dirname ] = method_map , options_map
273- finally :
274- fileobj .close ()
253+ mappings .append ((dirname , method_map , options_map ))
275254
276255 elif getattr (self .distribution , 'message_extractors' , None ):
277256 message_extractors = self .distribution .message_extractors
@@ -283,11 +262,11 @@ def _get_mappings(self):
283262 for pattern , method , options in mapping :
284263 method_map .append ((pattern , method ))
285264 options_map [pattern ] = options or {}
286- mappings [ dirname ] = method_map , options_map
265+ mappings . append (( dirname , method_map , options_map ))
287266
288267 else :
289268 for dirname in self .input_dirs :
290- mappings [ dirname ] = DEFAULT_MAPPING , {}
269+ mappings . append (( dirname , DEFAULT_MAPPING , {}))
291270
292271 return mappings
293272
@@ -686,11 +665,8 @@ def extract(self, argv):
686665 keywords .update (parse_keywords (options .keywords ))
687666
688667 if options .mapping_file :
689- fileobj = open (options .mapping_file , 'U' )
690- try :
668+ with open (options .mapping_file , 'U' ) as fileobj :
691669 method_map , options_map = parse_mapping (fileobj )
692- finally :
693- fileobj .close ()
694670 else :
695671 method_map = DEFAULT_MAPPING
696672 options_map = {}
@@ -710,30 +686,15 @@ def extract(self, argv):
710686 copyright_holder = options .copyright_holder ,
711687 charset = options .charset )
712688
713- for dirname in args :
714- if not os .path .isdir (dirname ):
715- parser .error ('%r is not a directory' % dirname )
716-
717- def callback (filename , method , options ):
718- if method == 'ignore' :
719- return
720- filepath = os .path .normpath (os .path .join (dirname , filename ))
721- optstr = ''
722- if options :
723- optstr = ' (%s)' % ', ' .join (['%s="%s"' % (k , v ) for
724- k , v in options .items ()])
725- self .log .info ('extracting messages from %s%s' , filepath ,
726- optstr )
727-
728- extracted = extract_from_dir (dirname , method_map , options_map ,
729- keywords , options .comment_tags ,
730- callback = callback ,
731- strip_comment_tags =
732- options .strip_comment_tags )
733- for filename , lineno , message , comments , context in extracted :
734- filepath = os .path .normpath (os .path .join (dirname , filename ))
735- catalog .add (message , None , [(filepath , lineno )],
736- auto_comments = comments , context = context )
689+ data_iter = ((dirname , method_map , options_map ) for dirname in args )
690+
691+ try :
692+ operations .extract_to_catalog (catalog , data_iter , keywords ,
693+ options .comment_tags ,
694+ options .strip_comment_tags ,
695+ log = self .log )
696+ except operations .ConfigureError as e :
697+ parser .error (e .message )
737698
738699 catalog_charset = catalog .charset
739700 if options .output not in (None , '-' ):
0 commit comments