-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
100 lines (85 loc) · 2.95 KB
/
storage.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
#!/usr/bin/env python3
import json
from pprint import pprint
from util import Util
import datetime
class StorageDecorators():
def must_be_valid_database(f):
def wrapper(*args, **kw):
_self = args[0]
if _self.db is not None:
return f(*args, **kw)
else:
raise
return(wrapper)
class Storage():
def __init__(self):
self.db = None
self.FILEPATH = 'spamtrapdb.json'
self.INITIALSTRUCTURE = {'spamtrap': {'config': {},
'fromstats': {},
'status': {},
},
}
self.openDatabase()
def openDatabase(self):
try:
with open(self.FILEPATH) as f:
self.db = json.loads(f.read())
print("[INFO:Storage] Database opened")
except:
print("[WARN:Storage] Initializing new database")
self.db = self.INITIALSTRUCTURE
@StorageDecorators.must_be_valid_database
def saveDatabase(self):
try:
with open(self.FILEPATH, 'w') as f:
f.write(json.dumps(self.db))
print("[INFO:Storage] Database closed")
except:
print('[ERROR:Storage] Cannot write to database')
@StorageDecorators.must_be_valid_database
def closeDatabase(self):
self.db = None
@StorageDecorators.must_be_valid_database
def increment_from(self, _from=None):
if _from is None:
return
fs = self.db['spamtrap']['fromstats']
if _from in fs:
fs[_from] += 1
else:
fs[_from] = 1
self.saveDatabase()
@StorageDecorators.must_be_valid_database
def updateExecutionTime(self):
if 'status' not in self.db['spamtrap']:
self.db['spamtrap']['status'] = {}
fs = self.db['spamtrap']['status']
fs['last_execution'] = str(datetime.datetime.now())
self.saveDatabase()
@StorageDecorators.must_be_valid_database
def updateTrappedTime(self):
if 'status' not in self.db['spamtrap']:
self.db['spamtrap']['status'] = {}
fs = self.db['spamtrap']['status']
fs['last_trapped'] = str(datetime.datetime.now())
self.saveDatabase()
@StorageDecorators.must_be_valid_database
# @StorageDecorators.must_have_contents_in('fromstats')
def getSendersDict(self):
if 'fromstats' not in self.db['spamtrap']:
return(None)
if isinstance(self.db['spamtrap']['fromstats'], dict):
r = []
for x in self.db['spamtrap']['fromstats']:
try:
r.append(Util.extract_address(x))
except:
continue
return(r)
return(None)
if __name__ == '__main__':
db = Storage()
db.updateExecutionTime()
db.saveDatabase()