Skip to content
Snippets Groups Projects
Commit acb6085c authored by fuchsi*'s avatar fuchsi* Committed by rahix
Browse files

ecg-app: write setting to and load from file to persistent across launches

parent 5168edff
No related branches found
No related tags found
No related merge requests found
......@@ -7,30 +7,72 @@ class Settings(simple_menu.Menu):
color_1 = color.CAMPGREEN
color_2 = color.CAMPGREEN_DARK
options = {}
selected_options = {}
def __init__(self):
super().__init__([("return", False)])
self.config_path = "/".join(__file__.split("/")[0:-1])
def on_select(self, value, index):
if index == 0:
self.exit()
else:
self.options[value[0]] = next(value[1])
self.selected_options[value[0]] = next(value[1])
self.write_to_file()
def entry2name(self, value):
if value[0] == "return":
return value[0]
else:
return "{}: {}".format(value[0], self.options[value[0]][0])
return "{}: {}".format(value[0], self.selected_options[value[0]][0])
def add_option(self, option):
self.entries.append(option)
self.options[option[0]] = next(option[1])
self.selected_options[option[0]] = next(option[1])
def get_option(self, name):
return self.options[name][1]
return self.selected_options[name][1]
def load_from_file(self):
config_path = "/".join(__file__.split("/")[0:-1])
try:
f = open("{}/config.cfg".format(config_path), "r")
for line in f:
parts = [x.strip() for x in line.split(":")]
if parts[0] in self.selected_options:
# find corresponding entry from menu to get access to the corresponding itertools.cycle
option_cycle = next(x for x in self.entries if x[0] == parts[0])[1]
if self.selected_options[parts[0]][0] != parts[1]:
previous = self.selected_options[parts[0]][0]
self.selected_options[parts[0]] = next(option_cycle)
while self.selected_options[parts[0]][0] not in {
parts[1],
previous,
}:
self.selected_options[parts[0]] = next(option_cycle)
if self.selected_options[parts[0]][0] == previous:
print(
"Settings: unknown option '{}' for key '{}'".format(
parts[1], parts[0]
)
)
else:
print("Settings: unknown key '{}'".format(parts[0]))
f.close()
except OSError:
print("Settings could not be loaded from file. Maybe it did not exist yet?")
def write_to_file(self):
config_path = "/".join(__file__.split("/")[0:-1])
try:
f = open("{}/config.cfg".format(config_path), "w")
for option_name in self.selected_options:
f.write(
"{}:{}\n".format(option_name, self.selected_options[option_name][0])
)
f.close()
except OSError as e:
print("Settings could not be written to file! Error: {}".format(e))
def ecg_settings():
......@@ -66,4 +108,6 @@ def ecg_settings():
)
)
config.load_from_file()
return config
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment