-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettingsManager.py
55 lines (44 loc) · 1.77 KB
/
settingsManager.py
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
52
53
54
55
# -*- coding: utf-8 -*-
import json
from lbc_utils import *
from pushbulletJsonEncoder import *
SETTINGS_FILE_PATH = "./UserSettings.json"
TAG_SEARCH_GLOBAL = "Search"
TAG_SEARCH_QUERY = "Query"
TAG_SEARCH_REGION = "Region"
TAG_PUSHBULLET_ACCOUNTS = "Pushbullet accounts"
class settingsManager():
def __init__(self):
# Holds the contents of the settings file when opened on app launch
self._data = {}
try:
with open(SETTINGS_FILE_PATH) as settingsFile:
self._data = json.load(settingsFile)
except FileNotFoundError:
pass # self._data is already initialized
def saveSettings(self, searchTerm, searchRegion, listOfPushbulletAccounts):
globalSettings = {
TAG_SEARCH_GLOBAL: {
TAG_SEARCH_QUERY: searchTerm,
TAG_SEARCH_REGION: searchRegion
},
TAG_PUSHBULLET_ACCOUNTS: listOfPushbulletAccounts
}
with open(SETTINGS_FILE_PATH, mode="w") as settingsFile:
settingsFile.write(
json.dumps(globalSettings, cls=PushbulletJSONEncoder, indent=4, sort_keys=True))
def getSearchSettings(self):
try:
searchSettings = self._data[TAG_SEARCH_GLOBAL]
return searchSettings[TAG_SEARCH_QUERY], searchSettings[TAG_SEARCH_REGION]
except KeyError:
return "", "" # Settings file does not exist yet. Doesn't matter
def getPushbulletAccounts(self):
accounts = []
try:
for element in self._data[TAG_PUSHBULLET_ACCOUNTS]:
accounts += [Pushbullet(JSON=element)]
except KeyError:
print("No accounts found in settings file")
pass # No accounts available in settings file
return accounts