-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.py
52 lines (47 loc) · 1.71 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
from Machine import Machine
from MachineManager import MachineManager
from UserManager import UserManager
from User import User
import time, datetime
class Storage:
def __init__(self, filePath):
self.filePath = filePath
def saveToStorage(self, manager):
f= open(self.filePath, "w+")
for i in manager.getList():
help = i.makeForStorage()
f.write(help)
f.close()
def readMachineFromStorage(self, manager):
rf = open(self.filePath, "r")
if rf.mode == 'r':
contents =rf.read()
info = contents.split("|")
# print(info)
# print(int(len(info)/4))
for i in range(int(len(info)/4)):
user = int(info[i*4])
time = info[i*4 + 1]
if time != "0":
time = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")
s = info[i*4 + 2]
if s == "True":
hasNotified = True
else:
hasNotified = False
machType = info[i*4 + 3]
newMachine = Machine(machType)
newMachine.use(user, time, hasNotified)
manager.addEntity(newMachine)
def readUserFromStorage(self, manager):
rf = open(self.filePath, "r")
if rf.mode == 'r':
contents =rf.read()
info = contents.split("|")
for i in range(int(len(info)/3)):
username = info[i*3]
userid = int(info[i*3 + 1])
time = int(info[i*3 + 2])
newUser = User(username, userid)
newUser.setTime(time)
manager.addEntity(newUser)