changeset 207:c6dd9b0c5009

Make logging start right away in `witch`. Added coloring to console output.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 17 Feb 2014 08:34:14 -0800
parents 64c64df08d2a
children 1e3275ff5dfc
files requirements.txt wikked/commands/base.py wikked/witch.py wk.py
diffstat 4 files changed, 82 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/requirements.txt	Fri Feb 14 00:00:04 2014 -0800
+++ b/requirements.txt	Mon Feb 17 08:34:14 2014 -0800
@@ -1,20 +1,36 @@
 Flask==0.10.1
+Flask-Bcrypt==0.5.2
 Flask-Login==0.1.3
 Flask-SQLAlchemy==1.0
 Flask-Script==0.5.1
-Flask-Testing==0.4
+Genshi==0.6
+GitPython==0.3.2.RC1
 Jinja2==2.6
 Markdown==2.2.1
+PyMeta==0.5.0
 PyYAML==3.10
 Pygments==1.5
 SQLAlchemy==0.8.3
 Werkzeug==0.8.3
-Whoosh==2.4.1
+Whoosh==2.5.5
 amqp==1.3.3
+anyjson==0.3.3
 argparse==1.2.1
+async==0.6.1
+billiard==3.3.0.8
 celery==3.1.5
-nose==1.2.1
+colorama==0.2.7
+elasticsearch==0.4.3
+gitdb==0.5.4
+itsdangerous==0.22
+kombu==3.0.6
+misaka==1.0.2
+py-bcrypt==0.2
 pybars==0.0.4
-python-hglib==1.0
+python-creole==1.0.6
+python-hglib==unknown
+pytz==2013.8
+smmap==0.8.2
 twill==0.9
+urllib3==1.7.1
 wsgiref==0.1.2
--- a/wikked/commands/base.py	Fri Feb 14 00:00:04 2014 -0800
+++ b/wikked/commands/base.py	Mon Feb 17 08:34:14 2014 -0800
@@ -28,12 +28,3 @@
             result = 0
         return result
 
-
-# Import the commands.
-# (this creates a PyLint warning but it's OK)
-# pylint: disable=unused-import
-import wikked.commands.manage
-import wikked.commands.query
-import wikked.commands.users
-import wikked.commands.web
-
--- a/wikked/witch.py	Fri Feb 14 00:00:04 2014 -0800
+++ b/wikked/witch.py	Mon Feb 17 08:34:14 2014 -0800
@@ -1,5 +1,7 @@
+import sys
 import logging
 import argparse
+import colorama
 from wikked.commands.base import command_classes
 from wikked.utils import find_wiki_root
 from wikked.wiki import Wiki, WikiParameters
@@ -8,6 +10,26 @@
 logger = logging.getLogger(__name__)
 
 
+class ColoredFormatter(logging.Formatter):
+    COLORS = {
+            'DEBUG': colorama.Fore.BLACK + colorama.Style.BRIGHT,
+            'INFO': '',
+            'WARNING': colorama.Fore.YELLOW,
+            'ERROR': colorama.Fore.RED,
+            'CRITICAL': colorama.Back.RED + colorama.Fore.WHITE
+            }
+
+    def __init__(self, fmt=None, datefmt=None):
+        logging.Formatter.__init__(self, fmt, datefmt)
+
+    def format(self, record):
+        color = self.COLORS.get(record.levelname)
+        res = logging.Formatter.format(self, record)
+        if color:
+            res = color + res + colorama.Style.RESET_ALL
+        return res
+
+
 class WitchContext(object):
     def __init__(self, params, wiki, args):
         self.params = params
@@ -16,6 +38,31 @@
 
 
 def main():
+    # Setup logging first, even before arg parsing, so we really get
+    # all the messages.
+    arg_log = False
+    arg_debug = False
+    arg_quiet = False
+    for i, arg in enumerate(sys.argv[1:]):
+        if not arg.startswith('--'):
+            break
+        elif arg == '--debug':
+            arg_debug = True
+        elif arg == '--quet':
+            arg_quiet = True
+        elif arg == '--log':
+            arg_log = sys.argv[i+1]
+    if arg_debug and arg_quiet:
+        raise Exception("You can't specify both --debug and --quiet.")
+    root_logger = logging.getLogger()
+    if arg_quiet:
+        root_logger.setLevel(logging.WARNING)
+    elif arg_debug:
+        root_logger.setLevel(logging.DEBUG)
+    if arg_log:
+        from logging.handlers import FileHandler
+        root_logger.addHandler(FileHandler(arg_log))
+
     # Setup the parser.
     parser = argparse.ArgumentParser(
             description="Wikked command line utility")
@@ -30,6 +77,14 @@
     parser.add_argument('--log',
             help="Send log messages to the specified file.")
 
+    # Import the commands.
+    # (this creates a PyLint warning but it's OK)
+    # pylint: disable=unused-import
+    import wikked.commands.manage
+    import wikked.commands.query
+    import wikked.commands.users
+    import wikked.commands.web
+
     # Setup the command parsers.
     subparsers = parser.add_subparsers()
     commands = map(lambda cls: cls(), command_classes)
@@ -42,18 +97,6 @@
     # Parse!
     result = parser.parse_args()
 
-    # Setup logging.
-    root_logger = logging.getLogger()
-    if result.debug and result.quiet:
-        raise Exception("You can't specify both --debug and --quiet.")
-    if result.quiet:
-        root_logger.setLevel(logging.WARNING)
-    elif result.debug:
-        root_logger.setLevel(logging.DEBUG)
-    if result.log:
-        from logging.handlers import FileHandler
-        root_logger.addHandler(FileHandler(result.log))
-
     # Create the wiki.
     root = find_wiki_root(result.root)
     params = WikiParameters(root)
--- a/wk.py	Fri Feb 14 00:00:04 2014 -0800
+++ b/wk.py	Mon Feb 17 08:34:14 2014 -0800
@@ -1,11 +1,15 @@
 #!/usr/local/bin/python
 import logging
-from wikked.witch import main
+import colorama
+from wikked.witch import ColoredFormatter, main
 
 
 # Configure logging.
-logging.basicConfig(level=logging.DEBUG,
-        format="[%(levelname)s]: %(message)s")
+colorama.init()
+root_logger = logging.getLogger()
+handler = logging.StreamHandler()
+handler.setFormatter(ColoredFormatter('%(message)s'))
+root_logger.addHandler(handler)
 
 
 if __name__ == "__main__":