From e43ebf8f0bd358622c6f03846a02a4c197dec3e2 Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Sat, 8 Aug 2015 17:11:52 +0200 Subject: [PATCH] Logging, verbosity & rootdir creation --- persoconf/main.py | 60 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/persoconf/main.py b/persoconf/main.py index 57d50b7..f9bb9ba 100755 --- a/persoconf/main.py +++ b/persoconf/main.py @@ -1,15 +1,46 @@ #!/usr/bin/env python3 import argparse +import logging +import os, os.path +# FUNCTIONS +######################################## +def yes_no_question(question="Yes or no?", default=False): + prompt="" + if(default is True): + prompt = " [Y/n]" + else: + prompt = " [y/N]" + + answer = input(question + prompt).lower() + + if(len(answer) == 0): + return default + else: + answer = answer[0] + + if(default is True): + if answer in ['n','0']: + return False + else: + if answer in ['y','o','1']: + return True + + return default + +# ARGPARSE +######################################## # Argument parsing using argparse parser = argparse.ArgumentParser() -parser.add_argument( "--dir", type=str, help="The persoconf directory to use", default="~/.config/persoconf/" ) +parser.add_argument( "--rootdir", type=str, help="The persoconf directory to use", default="~/.config/persoconf/" ) +parser.add_argument( '--verbose', help='Spout out more logs', action="store_true") -subparsers = parser.add_subparsers( dest="command", title="commands", description="Valid commands", help="sub-command help" ) + +subparsers = parser.add_subparsers( dest="command", title="commands", description="Valid commands" ) # Help -parser_help = subparsers.add_parser("help", help="Getting some help") +parser_help = subparsers.add_parser("help", help="Print this help") # Package parser_package = subparsers.add_parser("package", help="Package a persoconf repository") @@ -19,9 +50,30 @@ parser_package.add_argument( "apps", type=str, help="The apps to package. Use al parser_backup = subparsers.add_parser("backup", help="Backup an apps configuration in the persoconf directory with the configuration in use now") parser_backup.add_argument( 'apps', help='The apps to backup', type=str, nargs="*", default=["all"]) - args = parser.parse_args() +# SCRIPT +######################################## +# Let's adjust the verbosity level +if(args.verbose): + logging.bascConfig(level=logging.DEBUG) + +# Let's check that the rootdir exists +if(not os.path.isdir(args.rootdir)): + + if(os.path.exists(args.rootdir)): + logging.error("'" + str(args.rootdir) + "' exists but is a file") + + logging.warning("'" + str(args.rootdir) + "' does not exist") + + if not yes_no_question( "Do you want to create the persoconf directory '%s'?" % str(args.rootdir) ) : + logging.info("The persoconf directory was not created") + exit() + + # Create the directory + logging.info("Creating persoconf directory...") + os.mkdir(args.dir) + print(args)