-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_db.py
55 lines (45 loc) · 1.21 KB
/
utils_db.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
import shelve
import sys
import json
import time
# args
db_name = sys.argv[1]
action = sys.argv[2]
# shelve db
db = shelve.open(db_name.split(".")[0], writeback=True)
if not 'count' in db:
db['count'] = 0
db_keys = list(db.keys())
db_keys.sort()
# Read DB
if action == "print":
for k in db_keys:
print(k, db[k])
# Remove entry from DB
elif action == "remove":
entry = sys.argv[3]
del db[entry]
# Add entries to DB
elif action == "insert":
f = open(sys.argv[3],'r')
collections = json.load(f)
f.close()
for c in collections:
if "collection" not in c:
continue
count = str(db['count'])
print("count = "+count)
signature = collections[c]
new_id = "document"+ count + "_" + str(signature["x"]) + "_" + str(signature["y"]) + "_" + signature["comment"] + "_" + signature["map"]
# signature = signature[list(signature.keys())[0]]
# print(signature)
print(new_id)
db[new_id] = {
"document": new_id,
"signature": signature}
# db["document"+count] = new_doc
db["count"] += 1
# time.sleep(1)
db_keys.sort()
print(list(db.keys()))
db.close()