import logging
from importlib import import_module
import pkgutil
import sys

import coloredlogs
import click
import ipdb
import requests_cache   # @todo - replace with cache control

from regparser import commands
from regparser.commands.dependency_resolver import DependencyResolver
from regparser.index import dependency

logger = logging.getLogger(__name__)


@click.group()
@click.option('--debug/--no-debug', default=False)
def cli(debug):
    log_level = logging.INFO
    requests_cache.install_cache('fr_cache', expire_after=60*60*24*3)  # 3 days
    if debug:
        log_level = logging.DEBUG
        sys.excepthook = lambda t, v, tb: ipdb.post_mortem(tb)
    coloredlogs.install(level=log_level)


for _, command_name, _ in pkgutil.iter_modules(commands.__path__):
    module = import_module('regparser.commands.{}'.format(command_name))
    if hasattr(module, command_name):
        subcommand = getattr(module, command_name)
        cli.add_command(subcommand)


def run_or_resolve(cmd, prev_dependency=None):
    """Wrapper around a click command or group, providing exception handling for
    dependency errors. When a dependency is missing, this will try to resolve
    that dependency and then retry running cli(). When retrying, the
    `prev_dependency` parameter indirectly tells us if we've progressed, due
    to the dependency changing"""
    try:
        cmd()
    except dependency.Missing, e:
        resolvers = [resolver(e.dependency)
                     for resolver in DependencyResolver.__subclasses__()]
        resolvers = [r for r in resolvers if r.has_resolution()]
        if e.dependency == prev_dependency or len(resolvers) != 1:
            raise e
        else:
            logger.info("Attempting to resolve dependency: %s", e.dependency)
            resolvers[0].resolution()
            run_or_resolve(cmd, e.dependency)


def main(prev_dependency=None):
    run_or_resolve(cli, prev_dependency=prev_dependency)


if __name__ == '__main__':
    main()

@@ -44,16 +44,26 @@
 _module%0A
+import os%0A
 import p
@@ -69,16 +69,16 @@
 pkgutil%0A
-
 import s
@@ -365,16 +365,75 @@
 name__)%0A
+DEFAULT_LOG_FORMAT = %22%25(asctime)s %25(name)-40s %25(message)s%22%0A
 %0A%0A@click
@@ -752,16 +752,25 @@
 install(
+%0A        
 level=lo
@@ -776,16 +776,85 @@
 og_level
+,%0A        fmt=os.getenv(%22COLOREDLOGS_LOG_FORMAT%22, DEFAULT_LOG_FORMAT)
 )%0A%0A%0Afor 

Set a default log format that can be overridden