Aesthetics

What on earth was I thinking...
master
Lertsenem 2017-01-02 22:17:12 +01:00
parent 671bd90024
commit 755b079ab6
7 changed files with 121 additions and 118 deletions

View File

@ -6,26 +6,26 @@ import os.path
import utils
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
parser.add_argument( "app" ,
type=str ,
parser.add_argument( "app",
type=str,
help="The app to add, or the app to add a conf " \
"file to" )
parser.add_argument( "conf" ,
nargs="?" ,
type=str ,
parser.add_argument( "conf",
nargs="?",
type=str,
help="The conf file or directory to add to the app" )
parser.add_argument( "--confname" ,
type=str ,
parser.add_argument( "--confname",
type=str,
help="A special name to save the conf " \
"file/directory in the persoconf directory" )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
# Check (and create) the app directory
@ -53,15 +53,15 @@ def run(args, persoconf, logger):
# Load app META file
try:
appmeta = Metafile( json_path = os.path.join( appdir ,
appmeta = Metafile( json_path = os.path.join( appdir,
persoconf.metafile ) )
except FileNotFoundError:
logger.info( "Metafile %s does not exist for app %s"
% (persoconf.metafile, args.app) )
appmeta = Metafile( name=args.app )
appmeta.json_path = os.path.join( persoconf.path ,
args.app ,
appmeta.json_path = os.path.join( persoconf.path,
args.app,
persoconf.metafile )
except MalformedMetafileError:
@ -133,8 +133,8 @@ def run(args, persoconf, logger):
confpath = appdir + "/" + confname
# Copy the file (or directory)
if utils.copy_file_or_directory( logger ,
path_dst=confpath ,
if utils.copy_file_or_directory( logger,
path_dst=confpath,
path_src=args.conf,
overwrite=False ):
logger.info( "New conf file or directory '%s' associated with app '%s'"

View File

@ -6,29 +6,29 @@ import filecmp
from metafile import Metafile, MalformedMetafileError
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
"""Initialize check subcommand"""
parser.add_argument( "app" ,
type=str ,
nargs="?" ,
parser.add_argument( "app",
type=str,
nargs="?",
help="The app to check" )
parser.add_argument( "files" ,
type=str ,
nargs="*" ,
default=[] ,
parser.add_argument( "files",
type=str,
nargs="*",
default=[],
help="The files to check ; default to all " \
"added files" )
parser.add_argument( "-d", "--diff" ,
type=str ,
parser.add_argument( "-d", "--diff",
type=str,
help="Print the diff between the saved and the new " \
"file/directory" )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
logger.debug("Starting 'check' command")
@ -128,7 +128,7 @@ def run(args, persoconf, logger):
logger )
# --------------------------------------
# -----------------------------------------------------------------------------
def _compare_and_log(original_file_path, persoconf_backup_path, logger):
"""Compare a file with its persoconf backup and log the result (at info
level if the files are matching, warning level if they are not)."""

View File

@ -9,32 +9,32 @@ import utils
from metafile import Metafile, MalformedMetafileError
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
parser.add_argument( "app" ,
type=str ,
parser.add_argument( "app",
type=str,
help="The app to delete, or the app to delete a " \
"conf file from" )
parser.add_argument( "files" ,
nargs="*" ,
type=str ,
parser.add_argument( "files",
nargs="*",
type=str,
help="The conf files or directories to delete from " \
"the app" )
parser.add_argument( "--force-yes", "-y" ,
action="store_true" ,
parser.add_argument( "--force-yes", "-y",
action="store_true",
help="Don't ask silly questions" )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
# Does this app really exists ?
try:
appmeta = Metafile( json_path = os.path.join( persoconf.path ,
args.app ,
appmeta = Metafile( json_path = os.path.join( persoconf.path,
args.app,
persoconf.metafile ) )
except FileNotFoundError:
logger.error( "App %s does not seem to exist" % args.app )
@ -80,9 +80,9 @@ def run(args, persoconf, logger):
# 1) It's probably a confname
if f in appmeta.files:
if utils.delete_file_or_dir(
logger = logger ,
logger = logger,
path = os.path.join(persoconf.path,
appmeta.name ,
appmeta.name,
f )
):
# File deleted, let's delete the entry in Metafile
@ -104,7 +104,7 @@ def run(args, persoconf, logger):
if delete_file_or_dir( logger = logger,
path = os.path.join(
persoconf.path,
appmeta.name ,
appmeta.name,
dstdir[absf]
)
):

View File

@ -3,24 +3,24 @@
import os.path
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
parser.add_argument( "apps" ,
type=str ,
nargs="*" ,
default=[] ,
parser.add_argument( "apps",
type=str,
nargs="*",
default=[],
help="The apps to list ; defaults to all " \
"apps" )
parser.add_argument( "--show-files", "-f" ,
action="store_true" ,
parser.add_argument( "--show-files", "-f",
action="store_true",
help="Show the files for each app ; it is " \
"automatically set if you specify the " \
"apps to list." )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
app_list = persoconf.list_apps(logger=logger)

View File

@ -7,30 +7,30 @@ import tarfile
from metafile import Metafile, MalformedMetafileError
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
parser.add_argument( "apps" ,
type=str ,
nargs="*" ,
default=[] ,
parser.add_argument( "apps",
type=str,
nargs="*",
default=[],
help="The apps to package ; defaults to all " \
"apps" )
parser.add_argument( "--pkgtype", "-t" ,
type=str ,
nargs="?" ,
default="tgz" ,
choices = [ "tgz" ] ,
parser.add_argument( "--pkgtype", "-t",
type=str,
nargs="?",
default="tgz",
choices = [ "tgz" ],
help="The type of package to use" )
parser.add_argument( "--pkgname", "-p" ,
type=str ,
nargs="?" ,
default="persoconf." + (time.strftime("%Y%m%d")) ,
parser.add_argument( "--pkgname", "-p",
type=str,
nargs="?",
default="persoconf." + (time.strftime("%Y%m%d")),
help="The name of the package to create" )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
pkgname = args.pkgname
@ -47,8 +47,8 @@ def run(args, persoconf, logger):
try:
appmetas.append(
Metafile(json_path = os.path.join(
persoconf.path ,
app ,
persoconf.path,
app,
persoconf.metafile)
)
)

View File

@ -6,22 +6,22 @@ import os.path
import utils
from metafile import Metafile, MalformedMetafileError
# --------------------------------------
# -----------------------------------------------------------------------------
def init(parser):
parser.add_argument( "app" ,
type=str ,
nargs="?" ,
parser.add_argument( "app",
type=str,
nargs="?",
help="The app to update" )
parser.add_argument( "files" ,
type=str ,
nargs="*" ,
default=[] ,
parser.add_argument( "files",
type=str,
nargs="*",
default=[],
help="The files to update ; default to all " \
"added files" )
# --------------------------------------
# -----------------------------------------------------------------------------
def run(args, persoconf, logger):
# app == None => update all apps
@ -32,8 +32,8 @@ def run(args, persoconf, logger):
else:
try:
appmeta = Metafile(
json_path = os.path.join( persoconf.path ,
args.app ,
json_path = os.path.join( persoconf.path,
args.app,
persoconf.metafile )
)
@ -66,10 +66,10 @@ def run(args, persoconf, logger):
if f in apps_to_update[app].files:
if utils.copy_file_or_directory(
logger ,
logger,
path_dst = os.path.join( persoconf.path,
app ,
f ) ,
app,
f ),
path_src = apps_to_update[app].files[f]["dest"]
):
@ -93,10 +93,10 @@ def run(args, persoconf, logger):
if absf in dstdic:
if utils.copy_file_or_directory(
logger ,
logger,
path_dst = os.path.join( persoconf.path,
app ,
dstdic[absf] ) ,
app,
dstdic[absf] ),
path_src = absf
):
logger.info( "Updated %s from %s"
@ -121,10 +121,10 @@ def run(args, persoconf, logger):
for f in apps_to_update[app].files:
if utils.copy_file_or_directory(
logger ,
logger,
path_dst = os.path.join( persoconf.path,
app ,
f ) ,
app,
f ),
path_src = apps_to_update[app].files[f]["dest"]
):
logger.info( "Updated %s from %s"
@ -132,7 +132,7 @@ def run(args, persoconf, logger):
else:
logger.warning( "Failed to update %s from %s ; ignoring"
% ( f ,
% ( f,
apps_to_update[app].files[f]["dest"] )
)

67
main.py
View File

@ -21,77 +21,86 @@ import commands.list, commands.package, commands.update, commands.add, \
# ARGPARSE
########################################
##############################################################################
# Argument parsing using argparse
# =============================================================================
parser = argparse.ArgumentParser()
parser.add_argument( "--rootdir" ,
type=str ,
default="~/.config/persoconf/" ,
parser.add_argument( "--rootdir",
type=str,
default="~/.config/persoconf/",
help="The persoconf directory to use" )
parser.add_argument( "--metafile" ,
type=str ,
default="META" ,
parser.add_argument( "--metafile",
type=str,
default="META",
help="The name of the metadata files" )
parser.add_argument( "--verbose", "-v" ,
action="store_true" ,
parser.add_argument( "--verbose", "-v",
action="store_true",
help="Spout out more logs" )
subparsers = parser.add_subparsers( dest="command" ,
subparsers = parser.add_subparsers( dest="command",
title="commands" )
# Help
parser_help = subparsers.add_parser( "help" ,
# -----------------------------------------------------------------------------
parser_help = subparsers.add_parser( "help",
help="Print this help" )
# List
parser_list = subparsers.add_parser( "list" ,
# -----------------------------------------------------------------------------
parser_list = subparsers.add_parser( "list",
help="List backuped apps" )
commands.list.init(parser_list)
# Package
parser_package = subparsers.add_parser( "package" ,
# -----------------------------------------------------------------------------
parser_package = subparsers.add_parser( "package",
help="Package a persoconf repository" )
commands.package.init(parser_package)
# Update
parser_update = subparsers.add_parser( "update" ,
# -----------------------------------------------------------------------------
parser_update = subparsers.add_parser( "update",
help="Backup an app configuration in " \
"the persoconf directory with " \
"the configuration in use now" )
commands.update.init(parser_update)
# Add
parser_add = subparsers.add_parser( "add" ,
# -----------------------------------------------------------------------------
parser_add = subparsers.add_parser( "add",
help="Add an app to the persoconf " \
"directory, or add a config file " \
"to an existing app" )
commands.add.init(parser_add)
# Check
parser_check = subparsers.add_parser( "check" ,
# -----------------------------------------------------------------------------
parser_check = subparsers.add_parser( "check",
help="Checks if the backuped and the " \
"original file are different." )
commands.check.init(parser_check)
# Delete
parser_del = subparsers.add_parser( "delete" ,
# -----------------------------------------------------------------------------
parser_del = subparsers.add_parser( "delete",
help="Delete an app from the persoconf " \
"directory, or a config file from " \
"an existing app" )
commands.delete.init(parser_del)
# Parse arguments
# =============================================================================
args = parser.parse_args()
# Transform paths
args.rootdir = os.path.expanduser(args.rootdir)
# LOGGING
########################################
##############################################################################
log = logging.getLogger("persoconf")
log.setLevel(logging.DEBUG)
@ -110,13 +119,14 @@ log_handler_console.setFormatter(log_formatter_console)
log.addHandler(log_handler_console)
# SCRIPT
########################################
##############################################################################
# If we were asked for help, let's give some
if args.command in [ "help", None ]:
parser.print_help()
exit()
# Try to create the Persoconf
# =============================================================================
try:
persoconf = Persoconf(path=args.rootdir, metafile=args.metafile)
@ -151,38 +161,31 @@ except PersoconfRootDoesNotExistError:
exit()
# LIST COMMAND
########################################
# =============================================================================
if args.command == "list":
commands.list.run(args, persoconf, log)
# DELETE COMMAND
########################################
# =============================================================================
if args.command == "delete":
commands.delete.run(args, persoconf, log)
# ADD COMMAND
########################################
# =============================================================================
if args.command == "add":
commands.add.run(args, persoconf, log)
# CHECK COMMAND
########################################
# =============================================================================
if args.command == "check":
commands.check.run(args, persoconf, log)
# UPDATE COMMAND
########################################
# =============================================================================
if args.command == "update":
commands.update.run(args, persoconf, log)
# PACKAGE COMMAND
########################################
# =============================================================================
if args.command == "package":
commands.package.run(args, persoconf, log)
########################################