# Path: servi/exceptions.py
# class ForceError(Exception):
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             'Use -f / --force to override.\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# Path: servi/config.py
# def set_master_dir(set_dir_to):
#     """
#     sets c.MASTER_DIR
#         by finding the first ancestor(default)
#         to set_dir_to (if supplied - only for servi init)
#     """
#     global MASTER_DIR
# 
#     MASTER_DIR = set_dir_to
#     # TODO - remove this function
# 
# def load_user_config():
#     """
#     Reads and processes Servifile.yml, adding all variables to this modules
#     globals()
# 
#     For Servifile_Globals, Servifile:
#         Step 1: Render the file as a Jinja2 template
#                     (with custom function: lookup('env', envvar) )
#         Step 2: Load as a yaml doc
#         Step 3: Add to this module's globals()
# 
#     Note - Servifile.yml can essentially update or override Servifile_globals
#       but it CAN NOT delete a key from Servifile_globals
#       ie: combined >= servifile_globals
# 
#     """
# 
#     # Global config
#     global_config = {}
#     if not os.path.exists(SERVIFILE_GLOBAL_FULL):
#         warn('No global servifile found in {0}'.format(SERVIFILE_GLOBAL_FULL))
#     else:
#         with open(SERVIFILE_GLOBAL_FULL) as f:
#             servi_raw = f.read()
#             global_config = process_config(servi_raw)
# 
#     if MASTER_DIR:
#         user_config = getconfig(
#             SERVIFILE, TEMPLATE, MASTER, TMPL_DIR_SITE, MASTER_DIR)
#     else:
#         warn('Getting config but MASTER_DIR is empty.')
#         user_config = {}
# 
#     combined_config = deep_update(global_config, user_config)
#     for k, v in combined_config.items():
#         globals()[k] = v
# 
#     return global_config, user_config, combined_config


from glob import glob
from importlib import import_module
from logging import debug, info, warning as warn, error
from contextlib import contextmanager
from servi.exceptions import ForceError, ServiError
from servi.config import set_master_dir, load_user_config
import argparse
import io
import os
import logging
import servi.config as c
    import servi.commands  # required for import_module()

def arg_to_log_level(arglevel):
    return (5 - arglevel)*10


def log_level_to_arg(level):
    return 5 - int(level/10)


def process_and_run_command_line(command_line=None):
        servi_parser, sub_parsers = setup_parsers()

        commands = load_plugins(sub_parsers)

        args, extra_args = parse_args(commands, servi_parser, command_line)

        log_level = arg_to_log_level(args.verbose)
        retval = False
        with use_log_level(log_level):
            if args.template_dir:
                c.TMPL_DIR_SITE = args.template_dir
                warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
                    c.TMPL_DIR_SITE))

            if not args.command:
                error('\n***** Error ******\nNo command on command line.\n')
                servi_parser.print_help()
                raise ServiError('No command line')

            if args.command not in commands:

# Path: servi/exceptions.py
# class ForceError(Exception):
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             'Use -f / --force to override.\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# class ServiError(Exception):
#     # Expected errrors. Handle gracefully (ie: no stack dump)
#     def __init__(self, msg=None):
#         self.msg = msg
# 
#     def __str__(self):
#         return (
#             '\n***** ERROR *****\n'
#             '{0}\n'
#             '\n**Servi Aborting**'.format(self.msg))
# 
# Path: servi/config.py
# def set_master_dir(set_dir_to):
#     """
#     sets c.MASTER_DIR
#         by finding the first ancestor(default)
#         to set_dir_to (if supplied - only for servi init)
#     """
#     global MASTER_DIR
# 
#     MASTER_DIR = set_dir_to
#     # TODO - remove this function
# 
# def load_user_config():
#     """
#     Reads and processes Servifile.yml, adding all variables to this modules
#     globals()
# 
#     For Servifile_Globals, Servifile:
#         Step 1: Render the file as a Jinja2 template
#                     (with custom function: lookup('env', envvar) )
#         Step 2: Load as a yaml doc
#         Step 3: Add to this module's globals()
# 
#     Note - Servifile.yml can essentially update or override Servifile_globals
#       but it CAN NOT delete a key from Servifile_globals
#       ie: combined >= servifile_globals
# 
#     """
# 
#     # Global config
#     global_config = {}
#     if not os.path.exists(SERVIFILE_GLOBAL_FULL):
#         warn('No global servifile found in {0}'.format(SERVIFILE_GLOBAL_FULL))
#     else:
#         with open(SERVIFILE_GLOBAL_FULL) as f:
#             servi_raw = f.read()
#             global_config = process_config(servi_raw)
# 
#     if MASTER_DIR:
#         user_config = getconfig(
#             SERVIFILE, TEMPLATE, MASTER, TMPL_DIR_SITE, MASTER_DIR)
#     else:
#         warn('Getting config but MASTER_DIR is empty.')
#         user_config = {}
# 
#     combined_config = deep_update(global_config, user_config)
#     for k, v in combined_config.items():
#         globals()[k] = v
# 
#     return global_config, user_config, combined_config


# Path: servi/command.py
from glob import glob
from importlib import import_module
from logging import debug, info, warning as warn, error
from contextlib import contextmanager
from servi.exceptions import ForceError, ServiError
from servi.config import set_master_dir, load_user_config
import argparse
import io
import os
import logging
import servi.config as c
    import servi.commands  # required for import_module()


def arg_to_log_level(arglevel):
    return (5 - arglevel)*10


def log_level_to_arg(level):
    return 5 - int(level/10)


def process_and_run_command_line(command_line=None):
        servi_parser, sub_parsers = setup_parsers()

        commands = load_plugins(sub_parsers)

        args, extra_args = parse_args(commands, servi_parser, command_line)

        log_level = arg_to_log_level(args.verbose)
        retval = False
        with use_log_level(log_level):
            if args.template_dir:
                c.TMPL_DIR_SITE = args.template_dir
                warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
                    c.TMPL_DIR_SITE))

            if not args.command:
                error('\n***** Error ******\nNo command on command line.\n')
                servi_parser.print_help()
                raise ServiError('No command line')

            if args.command not in commands:

                raise ServiError('Unknown command: {0}'.format(args.command))