Logging, verbosity & rootdir creation

master
Lertsenem 2015-08-08 17:11:52 +02:00
parent 76ace3c9da
commit e43ebf8f0b
1 changed files with 56 additions and 4 deletions

View File

@ -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)