]> scripts.mit.edu Git - wizard.git/blob - lib/wizard/command/_base.py
2ba6e2f83700a6e9a33d68816e2528da70211ea4
[wizard.git] / lib / wizard / command / _base.py
1 import logging
2 import sys
3 import optparse
4
5 import wizard
6
7 class Error(wizard.Error):
8     """Base error class for all command errors"""
9     pass
10
11 def makeLogger(options):
12     logger = logging.getLogger("main")
13     logger.setLevel(logging.INFO)
14     stdout = logging.StreamHandler(sys.stdout)
15     logger.addHandler(stdout)
16     if options.verbose:
17         logger.verbose = True
18     else:
19         if not options.debug: stdout.setLevel(logging.ERROR)
20     if options.debug: logger.setLevel(logging.DEBUG)
21     return logger
22
23 class NullLogHandler(logging.Handler):
24     """Log handler that doesn't do anything"""
25     def emit(self, record):
26         pass
27
28 class WizardOptionParser(optparse.OptionParser):
29     """Configures some default user-level options"""
30     def __init__(self, *args, **kwargs):
31         optparse.OptionParser.__init__(self, *args, **kwargs)
32         self.add_option("-v", "--verbose", dest="verbose", action="store_true",
33                 default=False, help="Turns on verbose output")
34         self.add_option("--debug", dest="debug", action="store_true",
35                 default=False, help="Turns on debugging output")
36     def parse_all(self, argv, logger):
37         options, numeric_args = self.parse_args(argv)
38         return options, numeric_args, logger and logger or makeLogger(options)