Socialify

Folder ..

Viewing managers.py
51 lines (39 loc) • 1.3 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os


class PreferenceManager:
    def __init__(self, default_prefs, preferences_file, debug=False):
        self.DEFAULT_PREFS, self.preferences = default_prefs, default_prefs
        self.preferences_file = preferences_file
        if not debug:
            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]