Edgewall Software

Ticket #91: extract_file.2.patch

File extract_file.2.patch, 4.4 KB (added by dackze+babel@…, 4 years ago)

Patch that implements module support (and logs each extraction)

  • babel/messages/frontend.py

     
    3333from babel import Locale, localedata 
    3434from babel.core import UnknownLocaleError 
    3535from babel.messages.catalog import Catalog 
    36 from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \ 
    37                                    DEFAULT_MAPPING 
     36from babel.messages.extract import extract_from_dir, extract_from_file, \ 
     37                                   DEFAULT_KEYWORDS, DEFAULT_MAPPING 
    3838from babel.messages.mofile import write_mo 
    3939from babel.messages.pofile import read_po, write_po 
    4040from babel.messages.plurals import PLURALS 
     
    770770 
    771771        :param argv: the command arguments 
    772772        """ 
    773         parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...'), 
     773        parser = OptionParser(usage=self.usage % ('extract', 
     774                              'dir1/module1 <dir2/module2> ...'), 
    774775                              description=self.commands['extract']) 
    775776        parser.add_option('--charset', dest='charset', 
    776777                          help='charset to use in the output (default ' 
     
    865866                              charset=options.charset) 
    866867 
    867868            for dirname in args: 
    868                 if not os.path.isdir(dirname): 
    869                     parser.error('%r is not a directory' % dirname) 
     869                if os.path.isdir(dirname): 
     870                    def callback(filename, method, options): 
     871                        if method == 'ignore': 
     872                            return 
     873                        filepath = os.path.normpath(os.path.join(dirname, 
     874                                                    filename)) 
     875                        optstr = '' 
     876                        if options: 
     877                            optstr = ' (%s)' % ', '.join( 
     878                                ['%s="%s"' % (k, v) for k, v 
     879                                in options.items()]) 
     880                        self.log.info('extracting messages from %s%s', 
     881                                      filepath, optstr) 
    870882 
    871                 def callback(filename, method, options): 
    872                     if method == 'ignore': 
    873                         return 
    874                     filepath = os.path.normpath(os.path.join(dirname, filename)) 
    875                     optstr = '' 
    876                     if options: 
    877                         optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for 
    878                                                       k, v in options.items()]) 
    879                     self.log.info('extracting messages from %s%s', filepath, 
    880                                   optstr) 
     883                    extracted = extract_from_dir(dirname, method_map, 
     884                                                 options_map, keywords, 
     885                                                 options.comment_tags, 
     886                                                 callback=callback) 
     887                    for filename, lineno, message, comments in extracted: 
     888                        filepath = os.path.normpath(os.path.join(dirname, 
     889                            filename)) 
     890                        catalog.add(message, None, [(filepath, lineno)], 
     891                                    auto_comments=comments) 
     892                else: 
     893                    extracted = extract_from_file( 
     894                        'python', dirname, keywords, options.comment_tags) 
     895                    filepath = os.path.normpath(dirname) 
     896                    self.log.info('extracting messages from %s', filepath) 
     897                    for lineno, message, comments in extracted: 
     898                        catalog.add(message, None, [(filepath, lineno)], 
     899                                    auto_comments=comments) 
    881900 
    882                 extracted = extract_from_dir(dirname, method_map, options_map, 
    883                                              keywords, options.comment_tags, 
    884                                              callback=callback) 
    885                 for filename, lineno, message, comments in extracted: 
    886                     filepath = os.path.normpath(os.path.join(dirname, filename)) 
    887                     catalog.add(message, None, [(filepath, lineno)], 
    888                                 auto_comments=comments) 
    889  
    890901            if options.output not in (None, '-'): 
    891902                self.log.info('writing PO template file to %s' % options.output) 
    892903            write_po(outfile, catalog, width=options.width,