-
Notifications
You must be signed in to change notification settings - Fork 1
/
persist.py
105 lines (79 loc) · 2.47 KB
/
persist.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
import json
import os
import entity
import hashlib
import uuid
"""
Because this is so light-weight, and subject to change, things will be stored
in a specific directory as JSON. This way, entries can be hand-modified if
this program changes, and requires no external packages.
"""
def initializeProfiles():
if not os.path.exists("./profiles/"):
os.mkdir("profiles")
def profileExists(username):
return os.path.exists("./profiles/" + username + ".json")
def hashPassword(password, salt=None):
if salt is None:
salt = uuid.uuid4().hex
return salt, hashlib.sha512(password + salt).hexdigest()
def validate(username, password):
if not profileExists(username):
return False
f = open("./profiles/" + username + ".json")
j = f.read().strip()
f.close()
data = json.loads(j)
hcode = data["hcode"]
salt = data["salt"]
salt, hcode_attempt = hashPassword(password, salt=salt)
return hcode == hcode_attempt
def saveEntity(e):
if profileExists(e.name):
os.remove("./profiles/" + e.name + ".json")
f = open("./profiles/" + e.name + ".json", "w")
data = {}
data["name"] = e.name
data["hcode"] = e.hcode
data["salt"] = e.salt
tally_data = {}
for key in e.tallies:
if key in e.tallies_persist:
tally_data[key] = e.tallies[key]
data["tallies"] = tally_data
bag_data = {}
for key in e.bags:
if key in e.bags_persist:
bag_data[key] = e.bags[key]
data["bags"] = bag_data
data["facade"] = e.facade
data["dm"] = e.dm
data["spectator"] = e.spectator
data["languages"] = e.languages
data["aliases"] = e.aliases
data["settings"] = e.settings
data["aspects"] = e.aspects
f.write(json.dumps(data))
f.close()
def loadEntity(username):
if not profileExists(username):
raise IOError("Cannot load non-existing json for profile " + entity.name)
f = open("./profiles/" + username + ".json")
j = f.read().strip()
f.close()
data = json.loads(j)
e = entity.Entity(name=username)
e.tallies = data["tallies"]
e.tallies_persist = e.tallies.keys()
e.bags = data["bags"]
e.bags_persist = e.bags.keys()
e.hcode = data["hcode"]
e.salt = data["salt"]
e.dm = data["dm"]
e.spectator = data["spectator"]
e.facade = data["facade"]
e.languages = data["languages"]
e.aliases = data["aliases"]
e.settings = data["settings"]
e.aspects = data["aspects"]
return e