From 2e3f84c8e402563a9c11850ad07d95f59cd43be3 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sun, 6 Nov 2022 08:03:42 -0500 Subject: feat: window manager and file manager --- src/helpers/managers.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/helpers/managers.py (limited to 'src/helpers/managers.py') diff --git a/src/helpers/managers.py b/src/helpers/managers.py new file mode 100644 index 0000000..518762c --- /dev/null +++ b/src/helpers/managers.py @@ -0,0 +1,50 @@ +import json +import os + + +class PreferenceManager: + def __init__(self, default_prefs, preferences_file): + self.DEFAULT_PREFS, self.preferences = default_prefs, default_prefs + self.preferences_file = preferences_file + self.load() + + def load(self): + if os.path.exists(self.preferences_file): + with open(self.preferences_file, "r") as f: + self.preferences = json.load(f) + + def save(self): + with open(self.preferences_file, "w") as f: + json.dump(self.preferences, f) + + def get(self, key): + return self.preferences[key] + + def set(self, key, value): + self.preferences[key] = value + + def reset(self): + self.preferences = self.DEFAULT_PREFS + self.save() + + +class FileManager: + # keeps track of all open files and their contents + def __init__(self): + self.files = {} + + def open_file(self, path): + # open a file and store it in the files dict + with open(path, "r") as f: + contents = f.read() + self.files[path] = contents + + def save_file(self, path, contents): + # save a file + self.files[path] = contents + with open(path, "w") as f: + f.write(contents) + + def close_file(self, path): + # close a file + del self.files[path] -- cgit v1.2.3