comparison babel/messages/frontend.py @ 523:81e35b223dd6

shorten the __init__() method of !CommandLineInterface by extracting the logging configuration into another method
author fschwarz
date Sat, 05 Mar 2011 14:53:03 +0000
parents 36856816299a
children 85e1beadacb0
comparison
equal deleted inserted replaced
522:918569a5a855 523:81e35b223dd6
624 help='print as little as possible') 624 help='print as little as possible')
625 self.parser.set_defaults(list_locales=False, loglevel=logging.INFO) 625 self.parser.set_defaults(list_locales=False, loglevel=logging.INFO)
626 626
627 options, args = self.parser.parse_args(argv[1:]) 627 options, args = self.parser.parse_args(argv[1:])
628 628
629 # Configure logging 629 self._configure_logging(options.loglevel)
630 self.log = logging.getLogger('babel')
631 self.log.setLevel(options.loglevel)
632 # Don't add a new handler for every instance initialization (#227), this
633 # would cause duplicated output when the CommandLineInterface as an
634 # normal Python class.
635 if self.log.handlers:
636 handler = self.log.handlers[0]
637 else:
638 handler = logging.StreamHandler()
639 self.log.addHandler(handler)
640 handler.setLevel(options.loglevel)
641 formatter = logging.Formatter('%(message)s')
642 handler.setFormatter(formatter)
643
644 if options.list_locales: 630 if options.list_locales:
645 identifiers = localedata.list() 631 identifiers = localedata.list()
646 longest = max([len(identifier) for identifier in identifiers]) 632 longest = max([len(identifier) for identifier in identifiers])
647 identifiers.sort() 633 identifiers.sort()
648 format = u'%%-%ds %%s' % (longest + 1) 634 format = u'%%-%ds %%s' % (longest + 1)
661 cmdname = args[0] 647 cmdname = args[0]
662 if cmdname not in self.commands: 648 if cmdname not in self.commands:
663 self.parser.error('unknown command "%s"' % cmdname) 649 self.parser.error('unknown command "%s"' % cmdname)
664 650
665 return getattr(self, cmdname)(args[1:]) 651 return getattr(self, cmdname)(args[1:])
652
653 def _configure_logging(self, loglevel):
654 self.log = logging.getLogger('babel')
655 self.log.setLevel(loglevel)
656 # Don't add a new handler for every instance initialization (#227), this
657 # would cause duplicated output when the CommandLineInterface as an
658 # normal Python class.
659 if self.log.handlers:
660 handler = self.log.handlers[0]
661 else:
662 handler = logging.StreamHandler()
663 self.log.addHandler(handler)
664 handler.setLevel(loglevel)
665 formatter = logging.Formatter('%(message)s')
666 handler.setFormatter(formatter)
666 667
667 def _help(self): 668 def _help(self):
668 print self.parser.format_help() 669 print self.parser.format_help()
669 print "commands:" 670 print "commands:"
670 longest = max([len(command) for command in self.commands]) 671 longest = max([len(command) for command in self.commands])
Copyright (C) 2012-2017 Edgewall Software