-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
67 lines (52 loc) · 1.38 KB
/
database.py
File metadata and controls
67 lines (52 loc) · 1.38 KB
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
import json
import os
BASEDBPATH = 'data'
ACCOUNTFILE = 'account'
class BaseDB():
filepath = ''
def __init__(self):
self.set_path()
self.filepath = '/'.join((BASEDBPATH, self.filepath))
def set_path(self):
pass
def find_all(self):
return self.read()
def insert(self, item):
self.write(item)
def read(self):
raw = ''
if not os.path.exists(self.filepath):
return []
with open(self.filepath, 'r+') as f:
raw = f.readline()
if len(raw) > 0:
data = json.loads(raw)
else:
data = []
return data
def write(self, item):
data = self.read()
if isinstance(item, list):
data = data + item
else:
data.append(item)
with open(self.filepath, 'w+') as f:
f.write(json.dumps(data))
return True
def clear(self):
with open(self.filepath, 'w+') as f:
f.write('')
def hash_insert(self, item):
exists = False
for i in self.find_all():
if item['hash'] == i['hash']:
exists = True
break
if not exists:
self.write(item)
class AccountDB(BaseDB):
def set_path(self):
self.filepath = ACCOUNTFILE
def find_one(self):
ac = self.read()
return ac[0]