Refactoring

This commit is contained in:
Spitap
2023-03-12 10:22:40 +01:00
parent 4cd3937fdd
commit 52bccf3393

View File

@@ -386,32 +386,31 @@ def update_config(path, apply_update=True):
if value != new_config.get(section, option, raw=True): if value != new_config.get(section, option, raw=True):
update = True update = True
new_config.set(section, option, value) new_config.set(section, option, value)
if apply_update:
if update:
# Backing up old config file
date = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
dest = f"{os.path.splitext(path)[0]}_{date}.old"
shutil.copy(path, dest)
if update and apply_update: # Overwritting old config file
# Backing up old config file with open(path, "w") as configfile:
date = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") new_config.write(configfile)
dest = f"{os.path.splitext(path)[0]}_{date}.old"
shutil.copy(path, dest)
# Overwritting old config file # Set file owner to running u+g, and set config file permission to 600
with open(path, "w") as configfile: current_username = getpass.getuser()
new_config.write(configfile) current_user = pwd.getpwnam(current_username)
os.chown(dest, current_user[2], current_user[3])
os.chmod(dest, stat.S_IRUSR | stat.S_IWUSR)
# Set file owner to running u+g, and set config file permission to 600 return dest
current_username = getpass.getuser() return None
current_user = pwd.getpwnam(current_username) else:
os.chown(dest, current_user[2], current_user[3]) if update:
os.chmod(dest, stat.S_IRUSR | stat.S_IWUSR) # Simply check if current config file is outdated
return True
return dest
elif update and not apply_update:
# Simply check if current config file is outdated
return True
elif not update and not apply_update:
return False return False
return None
def gen_config(dest, interactive=False): def gen_config(dest, interactive=False):
"""Create config file from dict template""" """Create config file from dict template"""