Mercurial > babel > old > mirror
comparison babel/messages/frontend.py @ 234:541b6d630575
Use logging module for output from CLI frontend.
author | cmlenz |
---|---|
date | Mon, 30 Jul 2007 20:28:43 +0000 |
parents | 451aac9888f5 |
children | bf9579c4b0ee |
comparison
equal
deleted
inserted
replaced
233:451aac9888f5 | 234:541b6d630575 |
---|---|
17 from ConfigParser import RawConfigParser | 17 from ConfigParser import RawConfigParser |
18 from datetime import datetime | 18 from datetime import datetime |
19 from distutils import log | 19 from distutils import log |
20 from distutils.cmd import Command | 20 from distutils.cmd import Command |
21 from distutils.errors import DistutilsOptionError, DistutilsSetupError | 21 from distutils.errors import DistutilsOptionError, DistutilsSetupError |
22 import logging | |
22 from optparse import OptionParser | 23 from optparse import OptionParser |
23 import os | 24 import os |
24 import re | 25 import re |
25 import shutil | 26 import shutil |
26 from StringIO import StringIO | 27 from StringIO import StringIO |
293 filepath = os.path.normpath(os.path.join(dirname, filename)) | 294 filepath = os.path.normpath(os.path.join(dirname, filename)) |
294 optstr = '' | 295 optstr = '' |
295 if options: | 296 if options: |
296 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for | 297 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for |
297 k, v in options.items()]) | 298 k, v in options.items()]) |
298 log.info('extracting messages from %s%s' | 299 log.info('extracting messages from %s%s', filepath, optstr) |
299 % (filepath, optstr)) | |
300 | 300 |
301 extracted = extract_from_dir(dirname, method_map, options_map, | 301 extracted = extract_from_dir(dirname, method_map, options_map, |
302 keywords=self._keywords, | 302 keywords=self._keywords, |
303 comment_tags=self._add_comments, | 303 comment_tags=self._add_comments, |
304 callback=callback) | 304 callback=callback) |
601 self.parser.disable_interspersed_args() | 601 self.parser.disable_interspersed_args() |
602 self.parser.print_help = self._help | 602 self.parser.print_help = self._help |
603 self.parser.add_option('--list-locales', dest='list_locales', | 603 self.parser.add_option('--list-locales', dest='list_locales', |
604 action='store_true', | 604 action='store_true', |
605 help="print all known locales and exit") | 605 help="print all known locales and exit") |
606 self.parser.set_defaults(list_locales=False) | 606 self.parser.add_option('-v', '--verbose', action='store_const', |
607 dest='loglevel', const=logging.DEBUG, | |
608 help='print as much as possible') | |
609 self.parser.add_option('-q', '--quiet', action='store_const', | |
610 dest='loglevel', const=logging.ERROR, | |
611 help='print as little as possible') | |
612 self.parser.set_defaults(list_locales=False, loglevel=logging.INFO) | |
607 | 613 |
608 options, args = self.parser.parse_args(argv[1:]) | 614 options, args = self.parser.parse_args(argv[1:]) |
615 | |
616 # Configure logging | |
617 self.log = logging.getLogger('babel') | |
618 self.log.setLevel(options.loglevel) | |
619 handler = logging.StreamHandler() | |
620 handler.setLevel(options.loglevel) | |
621 formatter = logging.Formatter('%(message)s') | |
622 handler.setFormatter(formatter) | |
623 self.log.addHandler(handler) | |
609 | 624 |
610 if options.list_locales: | 625 if options.list_locales: |
611 identifiers = localedata.list() | 626 identifiers = localedata.list() |
612 longest = max([len(identifier) for identifier in identifiers]) | 627 longest = max([len(identifier) for identifier in identifiers]) |
613 format = u'%%-%ds %%s' % (longest + 1) | 628 format = u'%%-%ds %%s' % (longest + 1) |
714 if options.statistics: | 729 if options.statistics: |
715 translated = 0 | 730 translated = 0 |
716 for message in list(catalog)[1:]: | 731 for message in list(catalog)[1:]: |
717 if message.string: | 732 if message.string: |
718 translated +=1 | 733 translated +=1 |
719 print "%d of %d messages (%d%%) translated in %r" % ( | 734 self.log.info("%d of %d messages (%d%%) translated in %r", |
720 translated, len(catalog), translated * 100 // len(catalog), | 735 translated, len(catalog), |
721 po_file | 736 translated * 100 // len(catalog), po_file) |
722 ) | |
723 | 737 |
724 if catalog.fuzzy and not options.use_fuzzy: | 738 if catalog.fuzzy and not options.use_fuzzy: |
725 print 'catalog %r is marked as fuzzy, skipping' % (po_file) | 739 self.log.warn('catalog %r is marked as fuzzy, skipping', |
740 po_file) | |
726 continue | 741 continue |
727 | 742 |
728 for message, errors in catalog.check(): | 743 for message, errors in catalog.check(): |
729 for error in errors: | 744 for error in errors: |
730 print 'error: %s:%d: %s' % (po_file, message.lineno, error) | 745 self.log.error('error: %s:%d: %s', po_file, message.lineno, |
731 | 746 error) |
732 print 'compiling catalog %r to %r' % (po_file, mo_file) | 747 |
748 self.log.info('compiling catalog %r to %r', po_file, mo_file) | |
733 | 749 |
734 outfile = open(mo_file, 'w') | 750 outfile = open(mo_file, 'w') |
735 try: | 751 try: |
736 write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy) | 752 write_mo(outfile, catalog, use_fuzzy=options.use_fuzzy) |
737 finally: | 753 finally: |
838 charset=options.charset) | 854 charset=options.charset) |
839 | 855 |
840 for dirname in args: | 856 for dirname in args: |
841 if not os.path.isdir(dirname): | 857 if not os.path.isdir(dirname): |
842 parser.error('%r is not a directory' % dirname) | 858 parser.error('%r is not a directory' % dirname) |
859 | |
860 def callback(filename, method, options): | |
861 if method == 'ignore': | |
862 return | |
863 filepath = os.path.normpath(os.path.join(dirname, filename)) | |
864 optstr = '' | |
865 if options: | |
866 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for | |
867 k, v in options.items()]) | |
868 self.log.info('extracting messages from %s%s', filepath, | |
869 optstr) | |
870 | |
843 extracted = extract_from_dir(dirname, method_map, options_map, | 871 extracted = extract_from_dir(dirname, method_map, options_map, |
844 keywords, options.comment_tags) | 872 keywords, options.comment_tags, |
873 callback=callback) | |
845 for filename, lineno, message, comments in extracted: | 874 for filename, lineno, message, comments in extracted: |
846 filepath = os.path.normpath(os.path.join(dirname, filename)) | 875 filepath = os.path.normpath(os.path.join(dirname, filename)) |
847 catalog.add(message, None, [(filepath, lineno)], | 876 catalog.add(message, None, [(filepath, lineno)], |
848 auto_comments=comments) | 877 auto_comments=comments) |
849 | 878 |
879 if options.output not in (None, '-'): | |
880 self.log.info('writing PO template file to %s' % options.output) | |
850 write_po(outfile, catalog, width=options.width, | 881 write_po(outfile, catalog, width=options.width, |
851 no_location=options.no_location, | 882 no_location=options.no_location, |
852 omit_header=options.omit_header, | 883 omit_header=options.omit_header, |
853 sort_output=options.sort_output, | 884 sort_output=options.sort_output, |
854 sort_by_file=options.sort_by_file) | 885 sort_by_file=options.sort_by_file) |
907 infile.close() | 938 infile.close() |
908 | 939 |
909 catalog.locale = locale | 940 catalog.locale = locale |
910 catalog.revision_date = datetime.now(LOCALTZ) | 941 catalog.revision_date = datetime.now(LOCALTZ) |
911 | 942 |
912 print 'creating catalog %r based on %r' % (options.output_file, | 943 self.log.info('creating catalog %r based on %r', options.output_file, |
913 options.input_file) | 944 options.input_file) |
914 | 945 |
915 outfile = open(options.output_file, 'w') | 946 outfile = open(options.output_file, 'w') |
916 try: | 947 try: |
917 write_po(outfile, catalog) | 948 write_po(outfile, catalog) |
918 finally: | 949 finally: |
991 | 1022 |
992 if not po_files: | 1023 if not po_files: |
993 parser.error('no message catalogs found') | 1024 parser.error('no message catalogs found') |
994 | 1025 |
995 for locale, filename in po_files: | 1026 for locale, filename in po_files: |
996 print 'updating catalog %r based on %r' % (filename, | 1027 self.log.info('updating catalog %r based on %r', filename, |
997 options.input_file) | 1028 options.input_file) |
998 infile = open(filename, 'U') | 1029 infile = open(filename, 'U') |
999 try: | 1030 try: |
1000 catalog = read_po(infile, locale=locale, domain=domain) | 1031 catalog = read_po(infile, locale=locale, domain=domain) |
1001 finally: | 1032 finally: |
1002 infile.close() | 1033 infile.close() |