Source

vim-logging-tools / summarize_cmdlogs

Full commit
#!/usr/bin/env python2.6
import logging
from optparse import OptionParser

from vim_logging_tools.utils import guess_cmdlogdir
from vim_logging_tools.parser import parse_cmdlogdir
from vim_logging_tools.summarizers import list_summarizers

summarizers = list_summarizers()

# The standard setup for a command-line script...
parser = OptionParser()
parser.add_option("-s", "--summarizer", dest="summarizer",
                  default="most_used_commands", choices=summarizers.keys(),
                  help="Summarizers: %s"%(", ".join(summarizers.keys())))
parser.add_option("-f", "--format", dest="format", default="text",
                  help="The format of the output: text, json, xml")
parser.add_option("-c", "--cmdlogdir", dest="cmdlogdir",
                  help="The cmdlogdir (directory which stores cmdlogs)")
parser.add_option("-q", "--quiet", dest="log_level", action="store_const",
                  const=logging.ERROR, default=logging.INFO)
(options, args) = parser.parse_args()

# Setup the logger...
log_format = "%(levelname)s: %(message)s"
logging.basicConfig(level=options.log_level, format=log_format)

# Figure out how to deal with json...
if options.format == "json":
    try:
        import json
    except ImportError:
        raise Exception("Cannot export as json: json module not found.")
    options.format = "object"

if options.cmdlogdir is None:
    logging.warning("cmdlogdir not specified, trying to guess...")
    options.cmdlogdir = guess_cmdlogdir()

# Do all the summarizin'
logging.info("Summarizing commands...")
summarizer = summarizers[options.summarizer]
entries = ( entry for _, entry in parse_cmdlogdir(options.cmdlogdir) )
result = summarizer.summarize_as(options.format, entries, {})

# And print the results!
if options.format == "object":
    result = json.dumps(result)

print result