#!/bin/env python
"""vsyspy: A Python api wrapper for VSYS network.
usage: vsyspy [--version] [--debug] [--config=<ph>] [--help]
           <command> [<args>...]

Options:
    -d, --debug           Show debugging info.
    -h, --help            Show this help screen.
    -v, --version         Show vsyspy version.
"""

__copyright__ = "Copyright (C) 2019 Icerm"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import logging
from docopt import docopt
from vsyspy import version

LOGGER = logging.getLogger("VSYSPY")


def log_dict(dictionary, indent=0):
    """Helps logging dictionary with indent.
    """
    prefix = ''.join('\t' for i in range(indent))
    return prefix + prefix.join(
        dictionary.__repr__().splitlines(True)
        )


def parse_command_args(doc, command, args, logger=LOGGER):
    """Processes Vsyspy commands with docopt.
    """
    logger.debug("Entering Vsyspy command: %s", command)
    command_args = docopt(doc, argv=[command] + args)
    logger.debug("Running %s command with arguments:\n%s", command,
                 log_dict(command_args, 1))
    return command_args


if __name__ == "__main__":

    # parse command line input
    ARGUMENTS = docopt(__doc__,
                       version="\n".join(
                           ["vsyspy " + version.VERSION_TEXT + "\n",
                            __copyright__,
                            __license__,
                            ]),
                       options_first=True)

    # set logging level
    if ARGUMENTS['--debug']:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    LOGGER.info("This is Vsyspy, version %s.", version.VERSION_TEXT)
    LOGGER.debug("Received global arguments:\n%s", log_dict(ARGUMENTS, 1))

    # TODO: read config file and handle commands

# vim: ft=python
