]> scripts.mit.edu Git - wizard.git/blobdiff - lib/wizard/command/_base.py
Knock off a whole bunch of TODO items.
[wizard.git] / lib / wizard / command / _base.py
index 2ba6e2f83700a6e9a33d68816e2528da70211ea4..930326924dc55c545cce5e875bdc29eb936ed239 100644 (file)
@@ -12,14 +12,40 @@ def makeLogger(options):
     logger = logging.getLogger("main")
     logger.setLevel(logging.INFO)
     stdout = logging.StreamHandler(sys.stdout)
+    stdout.setFormatter(logging.Formatter(" " * int(options.indent) + '%(message)s'))
     logger.addHandler(stdout)
-    if options.verbose:
-        logger.verbose = True
+    if options.log_file:
+        file = logging.FileHandler(options.log_file)
+        logger.addHandler(file)
+    if options.debug:
+        logger.setLevel(logging.DEBUG)
     else:
-        if not options.debug: stdout.setLevel(logging.ERROR)
-    if options.debug: logger.setLevel(logging.DEBUG)
+        stdout.setLevel(logging.WARNING)
+        if options.verbose or options.dry_run:
+            stdout.setLevel(logging.INFO)
+        if options.log_file:
+            file.setLevel(logging.INFO)
     return logger
 
+def makeBaseArgs(options, **grab):
+    """Takes parsed options, and breaks them back into a command
+    line string that we can pass into a subcommand"""
+    args = []
+    grab["log_file"] = "--log-file"
+    grab["debug"] = "--debug"
+    grab["verbose"] = "--verbose"
+    grab["indent"] = "--indent"
+    #grab["log_db"] = "--log-db"
+    for k,flag in grab.items():
+        value = getattr(options, k)
+        if not value and k != "indent": continue
+        args.append(flag)
+        if type(value) is not bool:
+            if k == "indent":
+                value += 4
+            args.append(str(value))
+    return args
+
 class NullLogHandler(logging.Handler):
     """Log handler that doesn't do anything"""
     def emit(self, record):
@@ -33,6 +59,10 @@ class WizardOptionParser(optparse.OptionParser):
                 default=False, help="Turns on verbose output")
         self.add_option("--debug", dest="debug", action="store_true",
                 default=False, help="Turns on debugging output")
+        self.add_option("--log-file", dest="log_file",
+                default=None, help="Logs verbose output to file")
+        self.add_option("--indent", dest="indent",
+                default=0, help="Indents stdout, useful for nested calls")
     def parse_all(self, argv, logger):
         options, numeric_args = self.parse_args(argv)
         return options, numeric_args, logger and logger or makeLogger(options)