-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDGStorage.py
812 lines (773 loc) · 26.5 KB
/
DGStorage.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#!/usr/bin/env python3
__author__ = 'DGideas'
#Document:https://github.com/DGideas/DGStorage/wiki
class DGStorage:
def __init__(self, conf = {}):
import os
import sys
self.DGSTORAGE_VERSION = '2.4'
self.DGSTORAGE_CHARSET = 'utf8'
self.DGSTORAGE_SINGLECOLLECTIONLIMIT = 1024
self.DGSTORAGE_SEARCHRANGE = 3
self.DGSTORAGE_SEARCHINDEXLIMIT = 32
self.DGSTORAGE_SEARCHCACHELIMIT = 32
self.DGSTORAGE_PROPCACHELIMIT = 32
self.DGSTORAGE_SAFETY = True
self.DGSTORAGE_Name = None
self.DGSTORAGE_TimeStamp = ''
self.CollectionCache = []
self.LastCollection = ''
self.SearchCache = []
try:
os.chdir(os.path.dirname(sys.argv[0]));
except FileNotFoundError:
pass
except OSError:
pass
def create(self, name):
import codecs
import uuid
import urllib.parse
import os
self.DGSTORAGE_Name = str(name)
if self.DGSTORAGE_SAFETY == True:
self.DGSTORAGE_Name = urllib.parse.quote_plus(self.DGSTORAGE_Name);
try:
os.mkdir(self.DGSTORAGE_Name)
except FileExistsError:
return False
else:
self.DGSTORAGE_Name = self.DGSTORAGE_Name
self.DGSTORAGE_Name = name
with codecs.open(self.DGSTORAGE_Name + '/conf.dgb', 'a', self.DGSTORAGE_CHARSET) as conf:
conf.write(str(uuid.uuid1()) + '\n')
conf.write('Version:' + self.DGSTORAGE_VERSION)
os.mkdir(self.DGSTORAGE_Name + '/index')
with codecs.open(self.DGSTORAGE_Name + '/index/index.dgi', 'a', self.DGSTORAGE_CHARSET) as index:
pass
os.mkdir(self.DGSTORAGE_Name+'/cache')
os.mkdir(self.DGSTORAGE_Name+'/cache/search')
os.mkdir(self.DGSTORAGE_Name+'/cache/prop')
os.mkdir(self.DGSTORAGE_Name+'/index/BTree')
self.uptmp()
return True
def select(self, name):
import urllib.parse
import os
self.DGSTORAGE_Name = str(name)
if self.DGSTORAGE_SAFETY == True:
self.DGSTORAGE_Name = urllib.parse.quote_plus(self.DGSTORAGE_Name)
try:
os.mkdir(self.DGSTORAGE_Name)
except FileExistsError:
self.DGSTORAGE_Name = name
with open(self.DGSTORAGE_Name + '/conf.dgb') as conf:
correctVersion = False;
for line in conf:
if line.find('Version:2') != -1:
correctVersion = True
if correctVersion == False:
return False
with open(self.DGSTORAGE_Name + '/index/index.dgi') as index:
for line in index:
line = line.replace('\n','')
if line != '':
self.CollectionCache.append(str(line))
with open(self.DGSTORAGE_Name + '/cache/time.dgb') as cacheTimeStamp:
self.DGSTORAGE_TimeStamp = CacheTimeStamp.read()
return len(self.CollectionCache)
else:
os.rmdir(self.DGSTORAGE_Name)
return False
def append(self, content):
import uuid
return self.add(str(uuid.uuid1()), content, {"method":"append"})
def add(self, key, content, prop = {}, insertuid = None, rawProp = None):
import codecs
import uuid
import urllib.parse
key = str(key).replace('\n', '')
key = urllib.parse.quote_plus(str(key))
operationCollection = ''
if key=='':
return False
if len(self.CollectionCache) == 0:
if (self.createcoll(0)):
operationCollection = 0
else:
return False
else:
if self.LastCollection != '':
with open(self.DGSTORAGE_Name + '/' + str(self.LastCollection) + '/index/index.dgi') as collIndex:
i = 0
for line in collIndex:
if line != '':
i += 1
if i < self.DGSTORAGE_SINGLECOLLECTIONLIMIT:
operationCollection = self.LastCollection
else:
operationCollection = self.findavailablecoll(True)
else:
operationCollection = self.findavailablecoll(True)
self.LastCollection = operationCollection
uid = ''
with codecs.open(self.DGSTORAGE_Name + '/' + str(operationCollection) + '/index/index.dgi', 'a', self.DGSTORAGE_CHARSET) as collIndex:
collIndexR = open(self.DGSTORAGE_Name + '/' + str(operationCollection) + '/index/index.dgi');
i = 0
for line in collIndexR:
if line != '' and line != '\n':
i += 1
collIndexR.close()
if insertuid == None:
uid = uuid.uuid1()
else:
uid = insertuid
if i==0:
collIndex.write(str(uid) + ',' + str(key))
else:
collIndex.write('\n' + str(uid) + ',' + str(key))
with codecs.open(self.DGSTORAGE_Name + '/' + str(operationCollection) + '/' + str(uid) + '.dgs', 'a', self.DGSTORAGE_CHARSET) as storage:
storage.write(str(content))
if len(prop) != 0:
with codecs.open(self.DGSTORAGE_Name + '/' + str(operationCollection) + '/' + str(uid) + '.dgp', 'a', self.DGSTORAGE_CHARSET) as storageProp:
for propItem in prop:
propItem = urllib.parse.quote_plus(str(propItem))
prop[propItem] = urllib.parse.quote_plus(str(prop[propItem]))
storageProp.write(str(propItem) + ':' + str(prop[propItem]) + '\n')
if insertuid != None and rawProp != None:
with codecs.open(self.DGSTORAGE_Name + '/' + str(operationCollection) + '/' + str(uid) + '.dgp', 'a', self.DGSTORAGE_CHARSET) as storageProp:
storageProp.write(rawProp)
self.uptmp()
return uid
def index(self, key):
return self.get(key)
def count(self,key):
return len(self.get(key))
def get(self, key, limit = -1, skip = 0):
return self.finditemviakey(key, limit, skip)
def fetch(self, limit = 5, skip = 0):
return self.finditemviakey('$all', limit, skip)
def uid(self, uid):
return self.finditemviauid(uid)
def search(self, keyword, cache = False):
import codecs
res = []
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs', 'r', 'utf8') as storage:
if storage.read().find(str(keyword)) != -1:
res.append(self.finditemviauid(split[0], str(collection)))
return res
def pervious(self, uid):
pervious = ''
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n','')
if line != ''
split = line.split(',')
if split[0] == uid:
if pervious != '':
return pervious
else:
with open(self.DGSTORAGE_Name + '/' + str(self.CollectionCache[-1]) + '/index/index.dgi') as lastColl:
lastUid = ''
for line in lastColl:
line = line.replace("\n", "")
if line != '':
lastUid = line
return line.split(",")[0]
else:
pervious = split[0]
return False
def following(self, uid):
follow = ''
find = False
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[0] == uid:
find = True
else:
if find == True:
return split[0]
with open(self.DGSTORAGE_Name + '/' + str(self.CollectionCache[0]) + '/index/index.dgi') as firstColl:
for line in firstColl:
if line != '':
return line.split(",")[0]
def sort(self, propItem, order = "ASC", limit = 5, skip = 0):
import urllib.parse
import os
propItem = str(propItem)
propItem = urllib.parse.quote_plus(propItem)
sortArray = []
res = []
if skip < 0:
skip = 0
if limit == 0:
return res
elif limit < 0 or limit == None:
limit = -1
try:
open(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgb')
except:
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(",")
prop = self.getprop(split[0], collection)
try:
prop[propItem]
except:
pass
else:
sortArray.append({"uid":split[0], "propValue":(prop[propItem])})
if order == "WORD":
srt = sorted(sortArray, key = lambda x:str(x["propValue"]))
elif order == "ASC":
srt=sorted(sortArray, key = lambda x:float(x["propValue"]))
elif order == "DESC":
srt = sorted(sortArray, key = lambda x:float(x["propValue"]), reverse=True)
else:
return False
for element in srt:
res.append(element)
try:
open(self.DGSTORAGE_Name + '/cache/prop/index.dgi')
except:
pass
else:
with open(self.DGSTORAGE_Name + '/cache/prop/index.dgi') as cacheIndex:
count = 0
for line in cacheIndex:
line = line.replace('\n', '')
if line != '':
count += 1
if count >= self.DGSTORAGE_PROPCACHELIMIT:
if limit == -1:
return res[skip:]
else:
return res[skip:skip + limit]
with open(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgb', 'a') as cacheTimeStamp:
cacheTimeStamp.write(self.DGSTORAGE_TimeStamp)
with open(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgc', 'a') as cacheObject:
for element in res:
cacheObject.write(element["uid"] + ',' + element["propValue"] + '\n')
with open(self.DGSTORAGE_Name + '/cache/prop/index.dgi', 'a') as cacheIndex:
cacheIndex.write(propItem + '_' + order)
if limit==-1:
return res[skip:]
else:
return res[skip:skip + limit]
else:
with open(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgb') as cacheTimeStamp:
if cacheTimeStamp.read() != self.DGSTORAGE_TimeStamp:
os.remove(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgb')
os.remove(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgc')
return self.sort(propItem, order, limit, skip)
with open(self.DGSTORAGE_Name + '/cache/prop/' + propItem + '_' + order + '.dgc') as cacheObject:
for line in cacheObject:
line = line.replace("\n", "")
if line != '':
split = line.split(",")
res.append({"uid":split[0], "propValue":split[1]})
if limit==-1:
return res[skip:]
else:
return res[skip:skip + limit]
def query(self, sql):
import codecs
import uuid
operation = []
operation[0] = [] #DEV:[operation_id[operaction[suboperation(select_item)]]];
operationStack = 0
suboperationStack = 0
select = []
word = ""
selectword = ""
mode = "NORM" #NORM,QUOTE,SELECT
for letter in sql:
pass
def sql(self, sql):
return self.query(sql)
def put(self, uid, content):
import codecs
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
findStatus = False
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split=line.split(',')
if split[0] == uid:
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgs', 'w', self.DGSTORAGE_CHARSET) as storage:
storage.write(content)
findStatus = True
if findStatus == True:
break
if findStatus == True:
break
else:
continue
if findStatus == True:
self.uptmp()
return True
else:
return False
def setprop(self, uid, propItem, propValue):
import codecs
import urllib.parse
propItem = urllib.parse.quote_plus(str(propItem))
propValue = urllib.parse.quote_plus(str(propValue))
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[0] == uid:
try:
open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp')
except:
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp', 'a', self.DGSTORAGE_CHARSET) as storageProp:
storageProp.write(propItem+':' + propValue)
return True
else:
propList = {};
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp', 'r', self.DGSTORAGE_CHARSET) as storageProp:
for line in storageProp:
line = line.replace('\n', '')
if line != '':
split = line.split(':')
if split[0] != propItem:
propList[split[0]] = split[1]
propList[propItem] = propValue
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp', 'w', self.DGSTORAGE_CHARSET) as storageProp:
for propElement in propList:
storageProp.write(propElement + ':' + propList[propElement] + '\n')
return True
return False
def removeprop(self, uid, propItem):
import codecs
import urllib.parse
import os
propItem = urllib.parse.quote_plus(str(propItem))
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[0] == uid:
try:
open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp')
except:
return False
else:
propList = {}
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp', 'r', self.DGSTORAGE_CHARSET) as storageProp:
for line in storageProp:
line = line.replace('\n', '')
if line != '':
split = line.split(':')
if split[0] != propItem:
propList[split[0]] = split[1]
if(len(propList) > 0):
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp', 'w', self.DGSTORAGE_CHARSET) as storageProp:
for propElement in propList:
storageProp.write(propElement + ':' + propList[propElement] + '\n')
return True
else:
os.remove(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp')
return True
return False
def remove(self, uid):
import os
import codecs
findStatus = False
for line in self.CollectionCache:
line = line.replace('\n', '')
itemList = []
with open(self.DGSTORAGE_Name + '/' + str(line) + '/index/index.dgi') as collIndex:
for row in collIndex:
row = row.replace('\n', '')
split = row.split(',')
if split[0] == uid:
os.remove(self.DGSTORAGE_Name + '/' + str(line) + '/' + str(uid) + '.dgs')
try:
os.remove(self.DGSTORAGE_Name + '/' + str(line) + '/' + str(uid) + '.dgp')
except FileNotFoundError:
pass
findStatus = True
else:
itemList.append(row)
if findStatus == True:
with codecs.open(self.DGSTORAGE_Name + '/' + str(line) + '/index/index.dgi', 'w', self.DGSTORAGE_CHARSET) as collIndex:
string = ''
for item in itemList:
string = str(string) + str(item) + '\n'
collIndex.write(string)
i = 0
with open(self.DGSTORAGE_Name + '/' + str(line) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
i += 1
if i == 0:
self.removecoll(str(line))
break
if findStatus == False:
return False
self.uptmp()
return True
def zip(self, zipName = "DGStorage"):
import codecs
import urllib.parse
if zipName == '' or zipName == None:
return False
zipName = str(zipName).replace('.dgz', '')
zipName = urllib.parse.quote_plus(str(zipName))
zip = codecs.open(zipName + '.dgz', 'a', self.DGSTORAGE_CHARSET)
zip.write(self.DGSTORAGE_Name + '\n')
with codecs.open(self.DGSTORAGE_Name + '/conf.dgb', 'r', self.DGSTORAGE_CHARSET) as conf:
zip.write(urllib.parse.quote_plus(conf.read()) + '\n')
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
zip.write(split[0] + ',' + split[1] + ',')
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs', 'r', 'utf8') as storage:
zip.write(urllib.parse.quote_plus(storage.read()) + ',')
try:
open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgp')
except:
zip.write('\n')
else:
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgp', 'r', 'utf8') as storageProp:
zip.write(urllib.parse.quote_plus(storageProp.read()) + '\n')
zip.close()
return True
def unzip(self, zipName = "DGStorage"):
import codecs
import urllib.parse
import os
import uuid
if zipName == '' or zipName == None:
return False
zipName = str(zipName).replace('.dgz', '')
zipName = urllib.parse.quote_plus(zipName)
try:
open(zipName + '.dgz')
except:
return False
else:
zip = codecs.open(zipName + '.dgz', 'r', self.DGSTORAGE_CHARSET)
i = 0
for line in zip:
line = line.replace('\n', '')
if i == 0:
self.create(line)
self.select(line)
i += 1
elif i == 1:
os.remove(self.DGSTORAGE_Name + '/conf.dgb')
with codecs.open(self.DGSTORAGE_Name + '/conf.dgb', 'a', self.DGSTORAGE_CHARSET) as conf:
conf.write(urllib.parse.unquote_plus(line))
i += 1
else:
split = line.split(',')
split[1] = urllib.parse.unquote_plus(split[1])
split[2] = urllib.parse.unquote_plus(split[2])
split[3] = urllib.parse.unquote_plus(split[3])
if split[3] != '' and split[3] != '\n':
self.add(split[1], split[2], {}, split[0], split[3])
else:
self.add(split[1], split[2], {}, split[0])
return True
#Private
def clche(self, where = ''):
if where == '':
self.CollectionCache = []
def createcoll(self, coll):
import codecs
import os
try:
os.mkdir(self.DGSTORAGE_Name + '/' + str(coll))
except FileExistsError:
return False
else:
os.mkdir(self.DGSTORAGE_Name + '/' + str(coll) + '/index')
with codecs.open(self.DGSTORAGE_Name+'/' + str(coll) + '/index/index.dgi' , 'a' , self.DGSTORAGE_CHARSET) as dgc:
pass
self.CollectionCache.append(str(coll))
with open(self.DGSTORAGE_Name + '/index/index.dgi', 'a') as index:
index.write(str(coll) + '\n')
return True
def removecoll(self, coll):
import codecs
import os
os.remove(self.DGSTORAGE_Name + '/' + str(coll)+'/index/index.dgi')
os.rmdir(self.DGSTORAGE_Name + '/' + str(coll)+'/index')
os.rmdir(self.DGSTORAGE_Name + '/' + str(coll))
self.CollectionCache.remove(str(coll))
collCache = []
with open(self.DGSTORAGE_Name + '/index/index.dgi') as index:
for line in index:
line = str(line.replace('\n', ''))
if line != str(coll):
collCache.append(line)
with codecs.open(self.DGSTORAGE_Name + '/index/index.dgi', 'w', self.DGSTORAGE_CHARSET) as index:
if len(collCache) != 0:
for collection in collCache:
index.write(str(collection) + '\n')
return True
def findavailablecoll(self, createNewColl = False):
searchRange = self.DGSTORAGE_SEARCHRANGE
if searchRange != '' or searchRange != None:
searchRange = -1 - int(searchRange)
for collection in self.CollectionCache[:searchRange:-1]:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
i = 0
for line in collIndex:
if line != '':
i += 1
if i < self.DGSTORAGE_SINGLECOLLECTIONLIMIT:
return collection
else:
continue
if createNewColl==True:
self.createcoll(int(self.LastCollection) + 1)
return int(self.LastCollection) + 1
else:
return False
def finditemviakey(self, key, limit, skip):
limit = int(limit)
skip = int(skip)
if skip < 0:
skip = 0
res = []
if limit == 0:
return res
elif limit < 0 or limit == None:
limit = -1
if key != '$all':
import urllib.parse
key = str(urllib.parse.quote_plus(str(key)))
s = 0
i = 1
res = []
if key == '$all':
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
if s >= skip:
if i <= limit and limit != -1:
line = line.replace('\n', '');
if line != '':
split = line.split(',');
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs') as storage:
prop = self.getprop(split[0], collection)
res.append({"uid":str(split[0]), "key":str(split[1]), "content":str(storage.read()), "prop":prop})
i += 1
elif limit == -1:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs') as storage:
prop = self.getprop(split[0], collection)
res.append({"uid":str(split[0]), "key":str(split[1]), "content":str(storage.read()), "prop":prop})
i += 1
else:
break
else:
s+=1
else:
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
if s >= skip:
if i <= limit and limit != -1:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[1] == key:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs') as storage:
prop = self.getprop(split[0], collection)
res.append({"uid":str(split[0]), "key":str(split[1]), "content":str(storage.read()), "prop":prop})
i += 1
elif limit == -1:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[1] == key:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs') as storage:
prop = self.getprop(split[0], collection)
res.append({"uid":str(split[0]), "key":str(split[1]), "content":str(storage.read()), "prop":prop})
i += 1
else:
break
else:
s += 1
return res
def finditemviauid(self, uid, coll = None):
res = {}
if coll == None:
for collection in self.CollectionCache:
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[0] == str(uid):
with open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(split[0]) + '.dgs') as storage:
res["uid"] = str(split[0])
res["key"] = str(split[1])
res["content"] = str(storage.read())
res["prop"] = self.getprop(split[0],collection)
return res
return res
else:
with open(self.DGSTORAGE_Name + '/' + str(coll) + '/index/index.dgi') as collIndex:
for line in collIndex:
line = line.replace('\n', '')
if line != '':
split = line.split(',')
if split[0] == str(uid):
with open(self.DGSTORAGE_Name + '/' + str(coll) + '/' + str(split[0]) + '.dgs') as storage:
res["uid"] = str(split[0])
res["key"] = str(split[1])
res["content"] = str(storage.read())
res["prop"] = self.getprop(split[0],coll)
return res
return res
def getprop(self, uid, coll = None):
import codecs
import urllib.parse
res = {}
if coll == None:
for collection in CollectionCache:
try:
open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp')
except FileNotFoundError:
return res
else:
with codecs.open(self.DGSTORAGE_Name + '/' + str(collection) + '/' + str(uid) + '.dgp') as f:
for line in f:
line = line.replace('\n', '')
if line != '':
split = line.split(':')
if len(split) >= 2:
split[0] = urllib.parse.unquote_plus(str(split[0]))
split[1] = urllib.parse.unquote_plus(str(split[1]))
res[split[0]] = split[1]
return res
else:
try:
open(self.DGSTORAGE_Name + '/' + str(coll) + '/' + str(uid) + '.dgp')
except FileNotFoundError:
return res
else:
with codecs.open(self.DGSTORAGE_Name + '/' + str(coll) + '/' + str(uid) + '.dgp', 'r', self.DGSTORAGE_CHARSET) as f:
for line in f:
line = line.replace('\n', '')
if line != '':
split = line.split(':')
if len(split) >= 2:
split[0] = urllib.parse.unquote_plus(str(split[0]))
split[1] = urllib.parse.unquote_plus(str(split[1]))
res[split[0]] = split[1]
return res
def uptmp(self):
import uuid
with open(self.DGSTORAGE_Name + '/cache/time.dgb', 'w') as timeStamp:
sts = str(uuid.uuid1())
timeStamp.write(sts)
self.DGSTORAGE_TimeStamp = sts
return True
class DGStorageShell(DGStorage):
def shellAdd(self, key, inFileLocation):
import codecs
with codecs.open(inFileLocation, 'r', self.DGSTORAGE_CHARSET) as f:
string = ''
for line in f:
line = line.replace('\n', '')
string = str(string) + str(line)
self.add(key, string)
def shellGet(self, key, outFileLocation):
import codecs
import urllib.parse
res = self.get(key)
f = codecs.open(outFileLocation, 'w', self.DGSTORAGE_CHARSET)
string = ''
for item in res:
item['content'] = urllib.parse.quote_plus(item['content'])
string = str(string) + str(item['uid']) + ',' + str(item['content']) + ',' + str(item['prop']) + '\n'
f.write(string)
f.close()
def shellFetch(self, limit, skip, outFileLocation):
import codecs
import urllib.parse
res = self.fetch(limit, skip)
f = codecs.open(outFileLocation, 'w', self.DGSTORAGE_CHARSET)
string = ''
for item in res:
item['content'] = urllib.parse.quote_plus(item['content'])
string = str(string) + str(item['uid']) + ',' + str(item['content']) + ',' + str(item['prop']) + '\n'
f.write(string)
f.close()
if __name__ == '__main__':
import sys
try:
sys.argv[1]
except IndexError:
pass
else:
if sys.argv[1] == 'add':
try:
sys.argv[4]
except IndexError:
pass
else:
shellHandle = DGStorageShell()
shellHandle.select(str(sys.argv[2]))
if sys.argv[4].find('/') == -1:
shellHandle.shellAdd(str(sys.argv[3]), '../' + str(sys.argv[4]))
if sys.argv[1] == 'get':
try:
sys.argv[4]
except IndexError:
pass
else:
shellHandle = DGStorageShell();
shellHandle.select(str(sys.argv[2]))
if sys.argv[4].find('/') == -1:
shellHandle.shellGet(str(sys.argv[3]), '../' + str(sys.argv[4]))
if sys.argv[1] == 'fetch':
try:
sys.argv[5]
except IndexError:
pass
else:
shellHandle = DGStorageShell();
shellHandle.select(str(sys.argv[2]))
if sys.argv[5].find('/') == -1:
shellHandle.shellFetch(str(sys.argv[3]), str(sys.argv[4]), '../' + str(sys.argv[5]))
if sys.argv[1] == 'unzip':
try:
sys.argv[2]
except IndexError:
pass
else:
shell=DGStorage()
shell.unzip(str(sys.argv[2]))