Better logging

master
Lertsenem 2015-08-09 15:03:34 +02:00
parent 03ae84e81e
commit 50f0990f3e
1 changed files with 34 additions and 12 deletions

View File

@ -34,7 +34,7 @@ def yes_no_question(question="Yes or no?", default=False):
# Argument parsing using argparse
parser = argparse.ArgumentParser()
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")
parser.add_argument( '--verbose', "-v", help='Spout out more logs', action="store_true")
subparsers = parser.add_subparsers( dest="command", title="commands") #, description="Valid commands" )
@ -47,36 +47,58 @@ parser_package = subparsers.add_parser("package", help="Package a persoconf repo
parser_package.add_argument( "apps", type=str, help="The apps to package. Use all to make a global package.", nargs="*", default=["all"] )
# Backup
parser_backup = subparsers.add_parser("backup", help="Backup an apps configuration in the persoconf directory with the configuration in use now")
parser_backup = subparsers.add_parser("backup", help="Backup an app 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"])
# Add
parser_add = subparsers.add_parser("add", help="Add an app to the persoconf directory, or add a config file to an existing app")
parser_add.add_argument( 'app', help="The app to add, or the app to add a conf file to", type=str)
parser_add.add_argument( 'conf', help="The conf file or directory to add to the app", nargs="?", type=str)
parser.add_argument( '--confname', help='A special name to save the conf file/directory in the persoconf directory', type=str)
args = parser.parse_args()
# LOGGING
########################################
log = logging.getLogger("persoconf")
log.setLevel(logging.DEBUG)
# Console handler
log_handler_console = logging.StreamHandler()
log_handler_console.setLevel(logging.WARNING)
# Let's adjust the console handler to the verbosity level required
if(args.verbose):
log_handler_console.setLevel(logging.DEBUG)
log_formatter_console = logging.Formatter("%(name)s:%(levelname)s: %(message)s")
log_handler_console.setFormatter(log_formatter_console)
log.addHandler(log_handler_console)
# SCRIPT
########################################
# Let's adjust the verbosity level
if(args.verbose):
logging.bascConfig(level=logging.DEBUG)
# If we were asked for help, let's give some
if args.command in [ "help", None ]:
parser.print_help()
exit()
# Let's check that the rootdir exists
if(not os.path.isdir(args.rootdir)):
if not os.path.isdir(args.rootdir):
if(os.path.exists(args.rootdir)):
logging.error("'" + str(args.rootdir) + "' exists but is a file")
if os.path.exists(args.rootdir):
log.error("'%s' exists but is a file" % str(args.rootdir))
logging.warning("'" + str(args.rootdir) + "' does not exist")
log.warning("'%s' does not exist" % str(args.rootdir))
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")
log.info("The persoconf directory was not created")
exit()
# Create the directory
logging.info("Creating persoconf directory...")
log.info("Creating persoconf directory...")
os.mkdir(args.dir)
print(args)