Split to methods

This commit is contained in:
Dimitris Zlatanidis 2022-12-28 12:52:25 +02:00
parent 207598dd4c
commit 0be7d96936

View file

@ -11,9 +11,11 @@ from slpkg.dialog_box import DialogBox
class FormConfigs:
def __init__(self):
self.orig_configs = None
self.configs = Configs()
self.load_configs = LoadConfigs()
self.dialog = DialogBox()
self.config_file = f'{self.configs.etc_path}/{self.configs.prog_name}.toml'
def edit(self):
""" Read and write the configuration file. """
@ -45,16 +47,21 @@ class FormConfigs:
os.system('clear')
if code == 'ok':
# Read the original config file.
with open(config_file, 'r') as toml_file:
orig_config = toml_file.readlines()
self.write_file(configs, tags)
# Write the new values to the config file.
with open(config_file, 'w') as patch_toml:
for line in orig_config:
for key, value in zip(configs['configs'].keys(), tags):
if line.lstrip().startswith(key):
line = f' {key} = "{value}"\n'
if line.lstrip().startswith(('colors =', 'dialog =')):
line = line.replace('"', '')
patch_toml.write(line)
def read_file(self):
""" Read the original config file. """
with open(self.config_file, 'r') as toml_file:
self.orig_configs = toml_file.readlines()
def write_file(self, configs, tags):
""" Write the new values to the config file. """
self.read_file()
with open(self.config_file, 'w') as patch_toml:
for line in self.orig_configs:
for key, value in zip(configs['configs'].keys(), tags):
if line.lstrip().startswith(key):
line = f' {key} = "{value}"\n'
if line.lstrip().startswith(('colors =', 'dialog =')):
line = line.replace('"', '')
patch_toml.write(line)