/
cache_maintenance.py
247 lines (175 loc) · 6.29 KB
/
cache_maintenance.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
try:
# Used when executing with Python
from database_maintenance import *
except ModuleNotFoundError:
# Used when calling as library
from nisaba.database_maintenance import *
class cache_maintenance(database_maintenance):
##############################
# Navigation #
##############################
def iid_iterator(self,database,iid,function_call):
# Search through Database for a Specific Branch or Child
# Works on Any # - IID,Children - # Database
# Go Through Database Items
for key,value in database.items():
# Check for Passed-Through IID
if value['iid'] == iid:
# Call Passed-Through Function
function_call(database,key)
break
# If Not Found
else:
# Run Recursively
self.iid_iterator(value['children'],iid,function_call)
def database_renumberer(self,database):
if len(database) != 0:
i = 0
else:
i = 1
while i < len(database)+1:
if str(i) in database:
i = i + 1
else:
if str(i+1) in database:
database[str(i)] = database[str(i+1)]
del database[str(i+1)]
else:
break
def default_user_setter(self):
# Set everyone to 0
for key,value in self.database['users'].items():
value['default'] = 0
# Set this user to default (1)
self.database['users'][self.current_user.get()]['default'] = 1
self.database_saver()
def database_alphabetiser(self,database):
# Alphabetise Taxonomy
alpha_taxonomy = []
for item,dictionary in database.items():
alpha_taxonomy.append(dictionary['iid'])
alpha_taxonomy = sorted(alpha_taxonomy)
return alpha_taxonomy
##############################
# Adding #
##############################
def collection_adder(self):
i = -1
loop = 1
while loop == 1:
i = i + 1
loop = str(i) in self.database
self.database[str(i)]= {"schema:editor":["","",""],'items' : {}}
self.database_panels_displayer(self.database_window)
def item_adder(self,type):
i = -1
loop = 1
while loop == 1:
i = i + 1
loop = str(i) in self.database[self.collection_index]['items']
if type == 'i':
self.database[self.collection_index]['items'][str(i)]= {"schema:editor":["","",""],'item_type': 'i','annotations':[],'image_file': 'sample.jpg', 'segments' : {}}
elif type == 't':
self.database[self.collection_index]['items'][str(i)]= {"schema:editor":["","",""],'item_type': 't', 'annotations':[],'transcription': ['','',''], 'segments' : {}}
else:
pass
self.collection_item_list_panel_displayer()
def segment_adder(self):
i = -1
loop = 1
while loop == 1:
i = i + 1
loop = str(i) in self.database[self.collection_index]['items'][self.item_index]['segments']
if self.database[self.collection_index]['items'][self.item_index]['item_type'] == 't':
self.database[self.collection_index]['items'][self.item_index]['segments'][str(i)] = {"schema:editor":["","",""],'start':0,'end':len(self.database[self.collection_index]['items'][self.item_index]['transcription'][0].split()),'annotations':[]}
elif self.database[self.collection_index]['items'][self.item_index]['item_type'] == 'i':
self.database[self.collection_index]['items'][self.item_index]['segments'][str(i)] = {"schema:editor":["","",""],'top':0,'right':50,'bottom':50,'left':0,'annotations':[]}
self.item_panels_displayer('s')
def root_adder(self):
# Add a Taxon Root
# Set Loop Variables
i = -1
loop = 1
while loop:
i = i + 1
loop = str(i) in self.taxonomy
# Create new Root with Default Fields
self.taxonomy[i] = {}
self.taxonomy[i]['iid'] = "_New_Root"
self.taxonomy[i]['name'] = "New Root"
self.taxonomy[i]['type'] = "New Root"
self.taxonomy[i]['definition'] = "New Root"
self.taxonomy[i]['children'] = {}
# Reload Taxonomy Viewer
self.taxonomy_viewer(self.taxonomy_window)
def child_adder(self,database,key):
# Add a Taxon Child
# Set Loop Variables
i = -1
loop = 1
# Ascend Numerically until First Missing Number
while loop:
i = i + 1
loop = str(i) in database[key]['children']
# Create New Child with Default Fields
database[key]['children'][i] = {}
database[key]['children'][i]['iid'] = "New_Item_" + str(i)
database[key]['children'][i]['name'] = "New Item " + str(i)
database[key]['children'][i]['type'] = "New Item " + str(i)
database[key]['children'][i]['definition'] = "New Item " + str(i)
database[key]['children'][i]['children'] = {}
savedata = json.dumps(self.taxonomy, indent=4)
# Save JSON to Disk
with open (self.current_taxonomy, 'w') as file:
file.write(savedata)
# Reload Taxonomy Viewer
self.taxonomy_viewer(self.taxonomy_window)
##############################
# Deleting #
##############################
def user_deleter(self,current_user):
# Delete a User
# Delete User from Cached Database
del self.database['users'][current_user]
# Save Cached Database to Disk
self.database_saver()
# Clear Metadata Window
self.metadata_window.destroy()
def element_deleter(self,database,key):
# Delete a Taxon
# Delete Current Key
del database[key]
# Reload Taxonomy Viewer
self.taxonomy_viewer(self.taxonomy_window)
def unit_deleter(self,database,key,level):
del database[key]
if level == 'c':
self.database_renumberer(database)
self.database_panels_displayer(self.database_window)
elif level == 'i':
self.database_renumberer(database)
self.collection_item_list_panel_displayer()
elif level == 's':
self.database_renumberer(database)
self.item_panels_displayer('s')
##############################
# Saving #
##############################
def metadata_entry_saver(self,current_user):
# Save a User's Metadata
# Update Current User
if current_user in self.database['users']:
# Go through the rest of the entries and save the vaule
for entry in self.entries:
self.database['users'][current_user][entry[0]] = entry[1].get()
else:
# Create new entry in database
self.database['users'][current_user] = {}
# Go through the entries and save the vaule
for entry in self.entries:
self.database['users'][current_user][entry[0]] = entry[1].get()
self.database['users'][current_user]['default'] = 0
# Save Cached Database to Disk
self.database_saver()
# Clear Metadata Window
self.metadata_window.destroy()