Aggiornare 'crapset/crapset.py'

This commit is contained in:
Valentino Orlandi 2022-06-16 20:44:09 +00:00
parent 8c9111b609
commit e195faeef1
1 changed files with 181 additions and 55 deletions

View File

@ -1,11 +1,13 @@
from sys import argv
from time import sleep
from os.path import abspath
from crappy import aux
form crappy.crapup import UpSet
form crappy.craplog import LogSet
form crappy.craplview import ViewSet
from crappy.crapup import UpSet
from crappy.crapset import SetSet
from crappy.craplog import LogSet
from crappy.crapview import ViewSet
class Crapset():
@ -15,18 +17,46 @@ class Crapset():
crappath = abspath(__file__)
crappath = crappath[:crappath.rfind('/')]
self.confpath = "%s/crapconf" %(crappath[:crappath.rfind('/')])
self.file_path = "%s/crapset.conf" %(confpath)
# initialize crapset's variables
self.use_configs = True
self.file_path = "%s/crapset.crapconf" %(self.confpath)
#
################################################################
# START OF THE EDITABLE SECTION
#
# HIERARCHY FOR APPLYING SETTINGS:
# - HARDCODED VARIABLES (THESE)
# - CONFIGURATIONS FILE
# - COMMAND LINE ARGUMENTS
# THE ELEMENTS ON TOP ARE REPLACED BY THE ONES WHICH FOLLOW THEM,
# IF HARDCODED VARIABLES ARE SET TO DO SO
#
# READ THE CONFIGURATIONS FILE AND LOAD THE SETTING
# [ ]
# IF SET TO 'False' MEANS THAT THE SAVED CONFIGS WILL BE IGNORED
self.use_configs = True
#
# USE COMMAND LINE ARGUMENTS
# [ ]
# IF SET TO 'False' MEANS THAT EVERY ARGUMENT WILL BE IGNORED
self.use_arguments = True
self.less_output = False
self.more_output = False
self.use_colors = True
# True when something have been changed
self.upset_changed = False
self.setset_changed = False
self.logset_changed = False
self.viewset_changed = False
#
# REDUCE THE OUTPUT ON SCREEN
# [ -l / --less ]
self.less_output = False
#
# PRINT MORE INFORMATIONS ON SCREEN
# [ -m / --more ]
self.more_output = False
#
# USE COLORS WHEN PRINTING TEXT ON SCREEN
# CAN BE DISABLED PASSING [ --no-colors ]
self.use_colors = True
#
# END OF THE EDITABLE SECTION
################################################################
#
#
# DO NOT MODIFY THE FOLLOWING CONTENT
#
# initialize text messages
self.initMessages()
# read configs if not unset
@ -36,10 +66,10 @@ class Crapset():
if self.use_arguments is True:
self.parseArguments( args )
# load the objects
self.upset = UpSet()
self.setset = SetSet()
self.logset = LogSet()
self.viewset = ViewSet()
self.upset = UpSet( self )
self.setset = SetSet( self )
self.logset = LogSet( self )
self.viewset = ViewSet( self )
# read the configurations
self.upset.readConfigs( self )
self.setset.readConfigs( self )
@ -62,13 +92,13 @@ class Crapset():
configs.append(f)
# check the length
if len(configs) != 5:
print("\n{err}Error{white}[{grey}crapset.conf{white}]{red}>{default} invalid number of lines: {rose}%s{default}"\
print("\n{err}Error{white}[{grey}crapset.crapconf{white}]{red}>{default} invalid number of lines: {rose}%s{default}"\
.format(**self.text_colors)\
%( len(configs) ))
if self.less_output is False:
print("""
if you have manually edited the configurations file, please un-do the changes
else, please report this issue""")
if you have manually edited the configurations file, please un-do the changes
else, please report this issue""")
print("\n{err}CRAPSET ABORTED{default}\n"\
.format(**self.text_colors))
exit()
@ -92,17 +122,17 @@ class Crapset():
if arg == "":
continue
# elB4RTO
elif arg == "-elbarto-":
elif arg in ["elB4RTO","elbarto","-elbarto-"]:
print("\n%s\n" %( self.MSG_elbarto ))
exit()
# help
elif arg in ["help", "-h", "--help"]:
print("\n%s\nHelp can be found using {cyan}h{default} {italic}or{default} {cyan}help{default} while running Crapset\n"\
print("\n%s\n\nHelp can be found using {cyan}h{default} {italic}or{default} {cyan}help{default} while running Crapset\n"\
.format(**self.text_colors)\
%( self.MSG_craplogo ))
exit()
elif arg == "--examples":
print("\n%s\nHelp can be found using {cyan}h{default} {italic}or{default} {cyan}help{default} while running Crapset\n"\
print("\n%s\n\nHelp can be found using {cyan}h{default} {italic}or{default} {cyan}help{default} while running Crapset\n"\
.format(**self.text_colors)\
%( self.MSG_craplogo ))
exit()
@ -131,16 +161,14 @@ class Crapset():
else:
self.text_colors = aux.no_colors()
self.MSG_elbarto = aux.elbarto()
self.MSG_help = aux.help( self.text_colors )
self.MSG_examples = aux.examples( self.text_colors )
self.MSG_craplogo = aux.craplogo()
self.MSG_crapset = aux.crapup( self.text_colors )
self.MSG_fin = aux.fin( self.text_colors )
self.TXT_fin = "{orange}F{grass}I{cyan}N{default}".format(**self.text_colors)
self.TXT_crapset = "{red}c{orange}r{grass}a{cyan}p{white}SET{default}".format(**self.text_colors)
self.TXT_crapup = "{red}c{orange}r{grass}a{cyan}p{white}UP{default}".format(**self.text_colors)
self.TXT_craplog = "{red}C{orange}R{grass}A{cyan}P{white}LOG{default}".format(**self.text_colors)
self.TXT_crapview = "{red}C{orange}R{grass}A{cyan}P{white}VIEW{default}".format(**self.text_colors)
self.TXT_craplog = "{red}c{orange}r{grass}a{cyan}p{white}LOG{default}".format(**self.text_colors)
self.TXT_crapview = "{red}c{orange}r{grass}a{cyan}p{white}VIEW{default}".format(**self.text_colors)
def welcomeMessage(self):
@ -172,7 +200,9 @@ class Crapset():
"""
Print a warning message
"""
print("\n{warn}Warning{white}[{grey}%s{white}]{red}>{default} %s{default}"\
if self.less_output is False:
print()
print("{warn}Warning{white}[{grey}%s{white}]{red}>{default} %s{default}"\
.format(**self.text_colors)\
%( err_key, message ))
@ -181,7 +211,9 @@ class Crapset():
"""
Print an error message
"""
print("\n{err}Error{white}[{grey}%s{white}]{red}>{default} %s{default}"\
if self.less_output is False:
print()
print("{err}Error{white}[{grey}%s{white}]{red}>{default} %s{default}"\
.format(**self.text_colors)\
%( err_key, message ))
@ -202,14 +234,81 @@ class Crapset():
Save all the changes to all the tools
"""
# crapup
if self.upset_changed is True:
if self.upset.unsaved_changes is True:
self.upset.writeConfigs( self )
# crapset
if self.setset.unsaved_changes is True:
self.setset.writeConfigs( self )
# craploh
if self.logset.unsaved_changes is True:
self.logset.writeConfigs( self )
# crapview
if self.viewset.unsaved_changes is True:
self.viewset.writeConfigs( self )
def checkUnsavedChanges(self):
"""
Check if there is any unsaved modification before to quit
"""
unsaved = []
if self.upset.unsaved_changes is True:
unsaved.append("crapup")
if self.setset.unsaved_changes is True:
unsaved.append("crapset")
if self.logset.unsaved_changes is True:
unsaved.append("craplog")
if self.viewset.unsaved_changes is True:
unsaved.append("crapview")
if len(unsaved) > 0:
while True:
self.printWarning("unsaved","{yellow}There are unsaved modifications to:{default}"\
.format(**self.text_colors))
if self.more_output is True:
print()
space = "\n "
if self.less_output is True:
space = ""
else:
print(" ",end="")
for tool in unsaved:
print(" {rose}%s{default}%s"\
.format(**self.text_colors)\
%( tool, space ), end="", flush=True)
if self.more_output is True:
sleep(0.5)
if self.less_output is False:
print()
choice = input("What do you want to do? {white}[{green}save{grey}/{rose}quit{white}] :{default} "\
.format(**self.text_colors)).strip().lower()
if choice in ["q","quit","exit"]:
break
elif choice in ["s","save","w","write"]:
self.saveConfigs()
break
else:
# leave this normal yellow, it's secondary and doesn't need real attention
if self.less_output is False:
print()
print("{yellow}Warning{white}[{grey}choice{white}]{yellow}>{default} not a valid choice: {bold}%s{default}"\
.format(**self.text_colors)\
%(choice))
if self.less_output is False:
print()
sleep(1)
def main(self):
"""
Run Crapset
"""
space = ""
morespace = ""
if self.less_output is False:
space = "\n"
if self.more_output is True:
morespace = "\n"
loop = True
redirect = None
while loop is True:
@ -217,11 +316,13 @@ class Crapset():
user_input = redirect
redirect = None
else:
space = ""
if self.less_output is False:
print("{grey}(Enter {white}help{default}{grey} to view a help message)"\
print("{grey}(Enter {white}help{default}{grey} to view a help message){default}"\
.format(**self.text_colors))
user_input = input("{bold}Which {cyan}tool{default}{bold} do you want to configure{default}? {paradise}:{default} "\
.format(**self.text_colors)).lower().strip()
space = "\n"
user_input = input("{bold}Which {cyan}tool{default}{bold} do you want to configure{default}?%s {paradise}:{default} "\
.format(**self.text_colors)%(space)).lower().strip()
if user_input.startswith('-'):
self.printWarning("input","dashes {grey}[{default}{bold}-{default}{grey}]{default} are not required"\
.format(**self.text_colors))
@ -231,56 +332,81 @@ class Crapset():
print()
elif user_input in ["q","quit","exit","bye"]:
loop = False
elif user_input in ["h","help","help me"]:
if self.less_output is False:
print()
print("""Available choices
{grey}[{paradise}h{grey}/{white}help{grey}]{default} {italic}view this help message{default}
{grey}[{paradise}q{grey}/{white}quit{grey}]{default} {italic}quit Crapset{default}
{grey}[{paradise}s{grey}/{white}save{grey}]{default} {italic}save the changes to all of the configurations{default}
{grey}[{paradise}craplog{grey}]{default} {italic}edit Craplog's configuration file{default}
{grey}[{paradise}crapview{grey}]{default} {italic}edit Crapview's configuration file{default}
{grey}[{paradise}crapset{grey}]{default} {italic}edit Crapset's configuration file{default}
{grey}[{paradise}crapup{grey}]{default} {italic}edit Crapup's configuration file{default}\
""".format(**self.text_colors))
elif user_input in ["s","save"]:
elif user_input in ["s","w","save","write"]:
self.saveConfigs()
elif user_input in ["h","help","help me","show help"]:
print("""%s{cyan}Available choices{default}%s
{grey}[{paradise}h{white}/{paradise}help{grey}]{default} {italic}view this help message{default}
{grey}[{paradise}q{white}/{paradise}quit{grey}]{default} {italic}quit Crapset{default}
{grey}[{paradise}s{white}/{paradise}save{grey}]{default} {italic}save the changes to all of the configurations{default}%s
{grey}[{paradise}craplog{grey}]{default} {italic}edit Craplog's configuration file{default}
{grey}[{paradise}crapview{grey}]{default} {italic}edit Crapview's configuration file{default}
{grey}[{paradise}crapset{grey}]{default} {italic}edit Crapset's configuration file{default}
{grey}[{paradise}crapup{grey}]{default} {italic}edit Crapup's configuration file{default}%s\
""".format(**self.text_colors) %( space,morespace,space,space ))
elif user_input in ["e","ex","eg","example","examples","show example","show examples"]:
print("""%s{cyan}Examples{default}%s
{italic}Save any the modification to any of the tools{default}%s\n{bold} : save{default}%s
{italic}Start editing Craplog's configurations{default}%s\n{bold} : craplog{default}%s\
""".format(**self.text_colors) %(space,morespace,morespace,morespace,morespace,space))
elif user_input in ["log","craplog"]:
if self.less_output is False:
print()
loop, redirect = self.logset.run( self )
elif user_input in ["view","crapview"]:
if self.less_output is False:
print()
loop, redirect = self.viewset.run( self )
elif user_input in ["set","crapset"]:
if self.less_output is False:
print()
loop, redirect = self.setset.run( self )
elif user_input in ["up","crapup"]:
if self.less_output is False:
print()
loop, redirect = self.upset.run( self )
else:
# leave this normal yellow, it's secondary and doesn't need real attention
print("\n{yellow}Warning{white}[{grey}choice{white}]{yellow}>{default} not a valid choice: {bold}%s{default}"\
.format(**self.text_colors))
if self.less_output is False:
print()
print("{yellow}Warning{white}[{grey}choice{white}]{yellow}>{default} not a valid choice: {bold}%s{default}"\
.format(**self.text_colors)\
%( user_input ))
if self.less_output is False:
print()
sleep(1)
if __name__ == "__main__":
failed = False
crapset = Crapset( argv )
crapset.welcomeMessage()
crapset.main()
try:
crapset.welcomeMessage()
crapset.main()
crapset.exitMessage()
pass
#crapset.welcomeMessage()
#crapset.main()
#crapset.exitMessage()
except (KeyboardInterrupt):
failed = True
crapset.printError("failed","{yellow}keyboard interruption".format(**crapset.text_colors))
except:
failed = True
crapset.printError("failed","{rose}an error occured".format(**crapset.text_colors))
finally:
if failed is True:
# failing succesfully
crapset.exitAborted()
crapset.checkUnsavedChanges()
crapset.exitMessage()
del crapset