-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_management.py
356 lines (308 loc) · 14.9 KB
/
storage_management.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 26 13:42:55 2023
@author: DELL
"""
import discord, copy
def get_guild(client, guild_id) :
guild = [guild for guild in client.guilds if guild.id == guild_id] or None # returns None if no matches.
return guild
def decrypt_data(row_message_object, split_delim) :
row = row_message_object.content
message_id = row_message_object.id
row_sample = row.split(split_delim)
row_data = {row_sample[k] : row_sample[(k + 1)] for k in range(0, len(row_sample), 2)}
row_info = {'row_data' : row_data, 'message_id' : message_id}
return row_info
class UniformBranch() :
# Keys are the same for every row.
def __init__(self, perm_db, channel) :
self.channel = channel
self.data = []
self.name = self.channel.name
self.perm_db = perm_db
self.split_delim = ' [:|=|:] '
self.setted_up = False
return
async def setup(self) :
await self.refresh()
self.setted_up = True
return
async def refresh(self) :
self.data = []
async for row_message_object in self.channel.history(limit = None) :
# row = row_message_object.content
# message_id = row_message_object.id
# row_sample = row.split(self.split_delim)
# row_data = {row_sample[k] : row_sample[(k + 1)] for k in range(0, len(row_sample), 2)}
row_info = decrypt_data(row_message_object, self.split_delim)
self.data.append(row_info)
return
async def store(self, values) :
# Values should be a dictionary with {key : value} format for storage.
storage_str = self.split_delim.join([k + self.split_delim + values[k] for k in values])
message = await self.channel.send(storage_str)
row_info = {'row_data' : copy.deepcopy(values), 'message_id' : message.id}
self.data.append(row_info)
return row_info
async def modify(self, search_values, modif_values) :
filtered_data = await self.fetch_results(search_values)
for info in filtered_data :
try :
new_values = copy.deepcopy(info['row_data'])
msg_id = info['message_id']
index = self.data.index(info)
for k in modif_values :
new_values[k] = modif_values[k]
self.data[index]['row_data'][k] = copy.deepcopy(modif_values[k])
continue
message = await self.channel.fetch_message(msg_id)
storage_str = self.split_delim.join([k + self.split_delim + new_values[k] for k in new_values])
await message.edit(content = storage_str)
continue
except :
print('Row has been deleted/doesn\'t exist anymore or couldn\'t be retrieved[Try again, since the internal cache might not have been refreshed.]')
return None
return
async def delete(self, search_values) :
filtered_data = await self.fetch_results(search_values)
for info in filtered_data :
message = await self.channel.fetch_message(info['message_id'])
await message.delete()
self.data = [copy.deepcopy(k) for k in self.data.copy() if k['message_id'] != info['message_id']].copy()
continue
return
# async def store(self, key, value) :
# await self.channel.send('{key}{delim}{value}'.format(key = key, value = value, delim = self.split_delim))
# return
async def fetch_all(self) :
return self.data.copy()
async def fetch_results(self, search_values) :
# ## OLD CODE
# # sample = [row async for row in self.channel.history(limit = None) if row.author == self.perm_db.client and row.content.startswith('{key}{delim}'.format(key = key, delim = self.split_delim))]
# # EXPLANATION OF ARGS -:
# # SEARCH_VALUES KWARGS WILL TAKE IN KEYS TO SEARCH FOR AS ARGUMENTS.
# data = []
# async for row_message_object in self.channel.history(limit = lim) :
# row = row_message_object.content
# row_sample = row.split(self.split_delim)
# row_data = {row_sample[k] : row_sample[(k + 1)] for k in range(0, len(row_sample), 2)}
# flag = False
# for value in search_values :
# if value not in list(row_data.keys()) : break
# if row_data[value] == search_values[value] :
# flag = True
# continue
# else :
# flag = False
# break
# pass
# if flag :
# message_id = row_message_object.id
# row_info = {'row_data' : row_data, 'message_id' : message_id}
# data.append(row_info)
# continue
# new code, once done, comment the old one
filtered_data = []
for row_info in self.data :
row_data = row_info['row_data']
flag = False
for value in search_values :
if value not in list(row_data.keys()) : break
if row_data[value] == search_values[value] :
flag = True
continue
else :
flag = False
break
pass
if flag :
filtered_data.append(row_info)
continue
return filtered_data
def fetch_row(self, msg_id) :
return [k for k in self.data if k['message_id'] == msg_id][0]
# async def fetch_results(self, key) :
# data = [row async for row in self.channel.history(limit = None) if row.author == self.perm_db.client and row.content.startswith('{key}{delim}'.format(key = key, delim = self.split_delim))]
# return data
pass
class NonUniformBranch() :
# Keys may differ for every row.
def __init__(self, perm_db, channel) :
self.channel = channel
self.data = []
self.name = self.channel.name
self.perm_db = perm_db
self.split_delim = ' [:|=|:] '
self.setted_up = False
return
async def setup(self) :
await self.refresh()
self.setted_up = True
return
async def refresh(self) :
self.data = []
async for row_message_object in self.channel.history(limit = None) :
# row = row_message_object.content
# message_id = row_message_object.id
# row_sample = row.split(self.split_delim)
# row_data = {row_sample[k] : row_sample[(k + 1)] for k in range(0, len(row_sample), 2)}
row_info = decrypt_data(row_message_object, self.split_delim)
self.data.append(row_info)
return
async def store(self, values) :
# Values should be a dictionary with {key : value} format for storage.
storage_str = self.split_delim.join([k + self.split_delim + values[k] for k in values])
message = await self.channel.send(storage_str)
row_info = {'row_data' : copy.deepcopy(values), 'message_id' : message.id}
self.data.append(row_info)
return row_info
async def modify(self, search_values, modif_values) :
filtered_data = await self.fetch_results(search_values)
for info in filtered_data :
try :
new_values = copy.deepcopy(info['row_data'])
msg_id = info['message_id']
index = self.data.index(info)
for k in modif_values :
new_values[k] = modif_values[k]
self.data[index]['row_data'][k] = copy.deepcopy(modif_values[k])
continue
message = await self.channel.fetch_message(msg_id)
storage_str = self.split_delim.join([k + self.split_delim + new_values[k] for k in new_values])
await message.edit(content = storage_str)
continue
except :
print('Row has been deleted/doesn\'t exist anymore or couldn\'t be retrieved[Try again, since the internal cache might not have been refreshed.]')
return None
return
async def delete(self, search_values) :
filtered_data = await self.fetch_results(search_values)
for info in filtered_data :
message = await self.channel.fetch_message(info['message_id'])
await message.delete()
self.data = [copy.deepcopy(k) for k in self.data.copy() if k['message_id'] != info['message_id']].copy()
continue
return
# async def store(self, key, value) :
# await self.channel.send('{key}{delim}{value}'.format(key = key, value = value, delim = self.split_delim))
# return
async def fetch_all(self) :
return self.data.copy()
async def fetch_results(self, search_values) :
# ## OLD CODE
# # sample = [row async for row in self.channel.history(limit = None) if row.author == self.perm_db.client and row.content.startswith('{key}{delim}'.format(key = key, delim = self.split_delim))]
# # EXPLANATION OF ARGS -:
# # SEARCH_VALUES KWARGS WILL TAKE IN KEYS TO SEARCH FOR AS ARGUMENTS.
# data = []
# async for row_message_object in self.channel.history(limit = lim) :
# row = row_message_object.content
# row_sample = row.split(self.split_delim)
# row_data = {row_sample[k] : row_sample[(k + 1)] for k in range(0, len(row_sample), 2)}
# flag = False
# for value in search_values :
# if value not in list(row_data.keys()) : break
# if row_data[value] == search_values[value] :
# flag = True
# continue
# else :
# flag = False
# break
# pass
# if flag :
# message_id = row_message_object.id
# row_info = {'row_data' : row_data, 'message_id' : message_id}
# data.append(row_info)
# continue
# new code, once done, comment the old one
filtered_data = []
for row_info in self.data :
row_data = row_info['row_data']
flag = False
for value in search_values :
if value not in list(row_data.keys()) : break
if row_data[value] == search_values[value] :
flag = True
continue
else :
flag = False
break
pass
if flag :
filtered_data.append(row_info)
continue
return filtered_data
def fetch_row(self, msg_id) :
return [k for k in self.data if k['message_id'] == msg_id][0]
# async def fetch_results(self, key) :
# data = [row async for row in self.channel.history(limit = None) if row.author == self.perm_db.client and row.content.startswith('{key}{delim}'.format(key = key, delim = self.split_delim))]
# return data
pass
class PermanentDatabase() :
def __init__(self, client, serv_id, dump_category = 'BOWBOTDB') :
self.client = client
self.serv_id = serv_id
if self.serv_id not in [serv.id for serv in self.client.guilds] :
print('ERROR: The client is not a part of the guild(the id of which has been given.)')
return
self.server = self.client.get_guild(self.serv_id)
self.dump_category = dump_category
# print(self.server.name, self.server.categories)
try :
self.storage_category = [category for category in self.server.categories if category.name == self.dump_category][0]
self.load_info()
except Exception as e :
print('STORAGE CATEGORY INITIALIZATION DIDNT WORK[{0}]'.format(e))
self.branches, self.branch_names = [], []
return
def set_serv_id(self, serv_id) : self.serv_id = serv_id
async def set_dump_category(self, dump_category) :
# dump_category is a STRING.
self.dump_category = dump_category
if len([category for category in self.server.categories if category.name == self.dump_category]) == 0 :
try :
print('creating category')
self.storage_category = await self.server.create_category_channel(name = self.dump_category)
except Exception as e :
print('failed to create category[{0}]'.format(e))
else :
self.storage_category = [category for category in self.server.categories if category.name == self.dump_category][0]
return
def load_info(self) :
self.branches = [Branch(self, channel) for channel in self.storage_category.channels]
self.branch_names = [branch.name for branch in self.branches]
pass
async def get_branch(self, name) :
branch = [branch for branch in self.branches if branch.name == name][0]
if not branch.setted_up : await branch.setup()
return branch
async def create_branch(self, name) :
self.load_info()
if name not in self.branch_names :
if self.dump_category not in [category.name for category in self.server.categories] :
self.storage_category = await self.server.create_category_channel(self.dump_category)
channel = await self.server.create_text_channel(name, category = self.storage_category)
branch = Branch(self, channel)
self.load_info()
await branch.setup()
return branch
else :
print('The branch requested for creation already exists.' + name)
def print_info(self) :
print('Perm DB \n\nServer ID:{server_id}\nBranches:{branches}\n\n'.format(server_id = self.serv_id, branches = self.branch_names))
return
pass
class GlobalDatabase(PermanentDatabase) :
def __init__(self, client) :
super().__init__(client = client, serv_id = 1058974341670375535, dump_category = 'GLOBALBOWBOTDB')
# self.client = client
# self.serv_id = 1058974341670375535
# self.server = self.client.get_guild(self.serv_id)
# print(self.server, self.server.name)
# self.dump_category = 'BOWBOTDB'
# self.storage_category = [category for category in self.server.categories if category.name == self.dump_category][0]
# self.load_info()
return
# ...
pass
# ...create temporary database class.