-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
305 lines (248 loc) · 7.36 KB
/
data.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
- Manages User Preferences and Card History
"""
import json
from pathlib import Path
import userpaths
from schema import And, Optional, Schema, SchemaError, Use
from utils.card import Card
from utils.resource import PATH
# Creating folder in public documents dir
documentsDir = userpaths.get_my_documents()
Path(documentsDir+'\\Cut-It').mkdir(parents=True, exist_ok=True)
BASE = documentsDir+'\\Cut-It\\'
P_PATH = PATH.get(BASE+ 'data.json') # Preferences Storage Path
C_PATH = PATH.get(BASE + 'cards.json') # Card History Storage Path
# Defining Schema for the preferences and shortcuts section of Preferences
PREFS_SCHEMA = Schema({
"Font": And(str, len),
"Zoom": And(Use(int)),
"Primary Highlight Color": And(str, len),
"Secondary Highlight Color": And(str, len),
"Font Size of Normal Text": And(Use(int)),
"Font Size of Minimized Text": And(Use(int)),
"Primary Emphasis Settings": And(list),
"Secondary Emphasis Settings": And(list),
"Tertiary Emphasis Settings": And(list),
"Theme": And(str),
})
SHORTCUTS_SCHEMA = Schema({
"Primary Emphasis": And(str, len),
"Secondary Emphasis": And(str, len),
"Tertiary Emphasis": And(str, len),
"Clear Formatting" : And(str, len),
"Minimize Text": And(str, len),
"Cut a New Card": And(str, len),
"AutoPoll": And(str, len),
"AutoCite": And(str, len),
"AutoPoll + AutoCite": And(str, len),
"Save As PDF": And(str, len),
"Close Window": And(str, len)
})
# Data validator/fixer
def init():
"""
Initializes both Card and Preferences storage
"""
# Preferences
try:
with open(P_PATH, 'r') as f:
data = json.loads(f.read())
data["Card Index"]
data["FirstLoad"]
PREFS_SCHEMA.validate(data["preferences"])
SHORTCUTS_SCHEMA.validate(data["shortcuts"])
except Exception:
with open(P_PATH, 'w') as f:
"""
Em. Settings in the form:
[
(bool) bold,
(bool) italicised,
(bool) underlined,
(str) highlight color || None,
]
"""
data = {
"preferences": {
"Font": "Times New Roman",
"Zoom": 0,
"Primary Highlight Color": "Cyan",
"Secondary Highlight Color": "Yellow",
"Font Size of Normal Text": 8,
"Font Size of Minimized Text": 2,
"Primary Emphasis Settings": [
True,
False,
True,
"Cyan",
12
],
"Secondary Emphasis Settings": [
True,
False,
False,
None,
8
],
"Tertiary Emphasis Settings": [
False,
False,
True,
None,
8
],
"Theme" : "light",
},
"shortcuts": {
"Primary Emphasis": "CTRL+S",
"Secondary Emphasis": "CTRL+SHIFT+S",
"Tertiary Emphasis": "CTRL+ALT+S",
"Clear Formatting" : "CTRL+K",
"Minimize Text": "ALT+M",
"Cut a New Card": "CTRL+N",
"AutoPoll": "CTRL+D",
"AutoCite": "CTRL+SHIFT+D",
"AutoPoll + AutoCite": "CTRL+ALT+D",
"Save As PDF": "CTRL+P",
"Close Window": "CTRL+W"
},
"Card Index": None,
"FirstLoad": True
}
json.dump(data, f)
# Card History
try:
with open(C_PATH, 'r') as f:
json.loads(f.read())['cards']
except Exception:
with open(C_PATH, 'w') as f:
json.dump({"cards":[]}, f)
# First Load Methods
def getFirstLoad() -> bool:
"""
Returns (bool) if this is the first load
"""
with open(P_PATH, 'r') as f:
return json.loads(f.read())["FirstLoad"]
def setFirstLoad(val: bool = False) -> None:
"""
Sets FirstLoad to val
"""
with open(P_PATH, 'r') as f:
data = json.loads(f.read())
data["FirstLoad"] = False
with open(P_PATH, 'w') as f:
json.dump(data, f)
def getIndex() -> int:
"""
Returns (int) current card index
"""
with open(P_PATH, 'r') as f:
return json.loads(f.read())["Card Index"]
def setIndex(idx: int) -> None:
"""
Sets the stored card index
"""
with open(P_PATH, 'r') as f:
data = json.loads(f.read())
data["Card Index"] = idx
with open(P_PATH, 'w') as f:
json.dump(data, f)
"""
Preferences Methods
"""
# Getters and Setters
def getPrefData() -> dict:
"""
Returns a dict of data.json
"""
with open(P_PATH, 'r') as f:
return json.loads(f.read())
def setPrefData(data) -> None:
"""
Writes to data.json
"""
with open(P_PATH, 'w') as f:
json.dump(data, f)
# Functions
def getPref(key: str) -> any:
"""
Returns the value of the inputted preference key
"""
return getPrefData()["preferences"][key]
def setPref(key: str, val: any) -> None:
"""
Sets the value of the inputted preference key to the
inputted value
"""
if isinstance(val, list) and val[3] == "None":
val[3] = None
data = getPrefData()
data["preferences"][key] = val
setPrefData(data)
def getShort(key: str) -> any:
"""
Returns the value of the inputted shortcut key
"""
return getPrefData()["shortcuts"][key]
def setShort(key: str, val: any) -> None:
"""
Sets the value of the inputted shortcut key to the
inputted value
"""
data = getPrefData()
data["shortcuts"][key] = val
setPrefData(data)
"""
Card Methods
"""
# Getters and Setters
def getCardData() -> dict:
"""
Returns a dict of cards.json
"""
with open(C_PATH, 'r') as f:
return json.loads(f.read())
def setCardData(data) -> None:
"""
Writes to cards.json
"""
with open(C_PATH, 'w') as f:
json.dump(data, f)
# Functions
def getCard(idx: int = 0) -> Card:
"""
Returns card at start OR at supplied index
"""
try:
return Card(**getCardData()["cards"][idx])
except IndexError:
return None
def addCard(card: Card, idx: int = None) -> None:
"""
Checks if a card contains information, if so adds it
to the end of cards.json, or if an index is
supplied it will overwrite the card at that pos
"""
data = getCardData()
if isinstance(idx, int):
try:
data["cards"][idx] = card.getDict()
except Exception:
return
else:
data["cards"].append(card.getDict())
setCardData(data)
def deleteCard(idx: int) -> None:
"""
Deletes card at specified index
"""
data = getCardData()
if idx is None:
return
try:
del data["cards"][idx]
except IndexError:
return
setCardData(data)