first draft refactor

This commit is contained in:
2024-03-29 22:36:07 +01:00
parent ef0efba96a
commit baebb052b8
7 changed files with 209 additions and 167 deletions

View File

@@ -6,7 +6,7 @@ import time
import shutil
class Debug():
class Debug:
_verbose = 0
def __init__(self, verbose=0):
@@ -18,6 +18,9 @@ class Debug():
def add_verbose(self):
self._verbose += 1
def get_verbose(self):
return self._verbose
def stdout(self, out, level=0):
if self._verbose >= level:
print(out)
@@ -29,10 +32,9 @@ class Debug():
def get_config(filename, cfg):
"""reads filename as config, checks for DEST parameter and returns cfgfile object"""
ret = None
try:
ret = Conf(filename)
except:
except ValueError:
cfg.debug.stderr("Error reading config file %s" % filename)
return False
# check for DEST parameter
@@ -50,21 +52,22 @@ def read_skip_comment(fp, commentstring):
"""
for line in fp:
line = line[:-1]
if (commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line !="" and not re.match("^\s+$", line):
if ((commentstring != "" and not re.match("^"+re.escape(commentstring), line)) and line != "" and
not re.match(r"^\s+$", line)):
yield line
def diff(destfile, tempfile, commentstring, debug):
def diff(destfile, tempfile, commentstring, cfg):
"""diff destfile and tempfile, returns True if files differ, False if they are the same"""
debug.stdout("Diffing %s and %s" % (destfile, tempfile), 3)
cfg.debug.stdout("Diffing %s and %s" % (destfile, tempfile), 3)
if not os.path.isfile(destfile):
debug.stdout("Destfile %s does not exist, returning True." % destfile, 3)
cfg.debug.stdout("Destfile %s does not exist, returning True." % destfile, 3)
# destfile does not exist -> copy tempfile over
return True
# if not destfile
if not os.path.isfile(tempfile):
# tempfile does not exist, this should never happen
error("Temporary file %s does not exist, this should not happen." % tempfile)
cfg.debug.stderr("Temporary file %s does not exist, this should not happen." % tempfile)
sys.exit(1)
# if not tempfile
@@ -75,13 +78,13 @@ def diff(destfile, tempfile, commentstring, debug):
if line1 != line2:
fp1.close()
fp2.close()
debug.stdout("%s differs, return true" % destfile, 3)
cfg.debug.stdout("%s differs, return true" % destfile, 3)
return True
# if differ
# for line
fp1.close()
fp2.close()
debug.stdout("%s is the same, return false" % destfile, 3)
cfg.debug.stdout("%s is the same, return false" % destfile, 3)
return False
@@ -92,47 +95,47 @@ def user_config_generated(filename, cfg):
# filename does not exist, so it was not generated by userconfig
return False
if not cfg.check("Main","stamp"):
if not cfg.check("Main", "stamp"):
# no STAMP in userconfig.cfg, so no way to check if file was generated by userconfig
return False
fp = open(filename, "r")
for line in fp:
if re.search(re.escape(cfg.get("Main","stamp")), line):
if re.search(re.escape(cfg.get("Main", "stamp")), line):
return True
return False
def backup_file(filename, debug):
def backup_file(filename, cfg):
"""make backup of filename, returns True if backup is successful, False else"""
if os.path.isfile(filename):
debug.stdout("%s exists, finding backup name." % filename, 3)
cfg.debug.stdout("%s exists, finding backup name." % filename, 3)
backupname = filename+".userconfig."+time.strftime("%F")
testbackupname = backupname
counter = 0
while os.path.isfile(testbackupname):
counter+=1
testbackupname=backupname+"."+str(counter)
debug.stdout("Renaming %s to %s" % (filename, testbackupname), 1)
counter += 1
testbackupname = backupname+"."+str(counter)
cfg.debug.stdout("Renaming %s to %s" % (filename, testbackupname), 1)
os.rename(filename, testbackupname)
return True
else:
debug.stdout("%s does not exist, do not need backup." % filename, 3)
cfg.debug.stdout("%s does not exist, do not need backup." % filename, 3)
return False
def copy_file(sourcefile, destfile, debug):
def copy_file(sourcefile, destfile, cfg):
"""copy sourcefile to destfile, returns True if successful, False else"""
if os.path.isfile(sourcefile):
# sourcefile exists
debug.stdout("Source file %s exists, proceeding with copy." % sourcefile, 3)
cfg.debug.stdout("Source file %s exists, proceeding with copy." % sourcefile, 3)
if not os.path.isfile(destfile) or os.access(destfile, os.W_OK):
debug.stdout("Copying %s to %s" % (sourcefile, destfile), 1)
cfg.debug.stdout("Copying %s to %s" % (sourcefile, destfile), 1)
shutil.copy(sourcefile, destfile)
return True
# destfile is writable
else:
debug.stdout("Destination file %s does not exist or is not writable." % destfile, 3)
cfg.debug.stdout("Destination file %s does not exist or is not writable." % destfile, 3)
return False