aboutsummaryrefslogtreecommitdiff
path: root/src/helpers/managers.py
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-11-06 08:03:42 -0500
committerBobby <[email protected]>2022-11-06 08:03:42 -0500
commit2e3f84c8e402563a9c11850ad07d95f59cd43be3 (patch)
treed2b6f5b2a3ee49cd933447fb61ab2bfd1d6c1c9e /src/helpers/managers.py
parent57858724265b39bc829df8eaa41d7c04aa486734 (diff)
downloadtexty-2e3f84c8e402563a9c11850ad07d95f59cd43be3.tar.xz
texty-2e3f84c8e402563a9c11850ad07d95f59cd43be3.zip
feat: window manager and file manager
Diffstat (limited to 'src/helpers/managers.py')
-rw-r--r--src/helpers/managers.py50
1 files changed, 50 insertions, 0 deletions
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]