-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileOperations.py
146 lines (123 loc) · 3.88 KB
/
FileOperations.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import Global
from Global import FILE_NAME, USER_STRING_LENGTH
from User import User
def getNumberOfUsers():
return int(os.path.getsize(FILE_NAME)/USER_STRING_LENGTH)
def addUser(user):
f = open(FILE_NAME, 'a')
f.write(user.toString())
f.close()
def addUsers(users):
f = open(FILE_NAME, 'a')
for user in users:
f.write(user.toString())
f.close()
def getUser(index):
f = open(FILE_NAME, 'r')
f.seek(index*USER_STRING_LENGTH)
user = Global.parseUser(f.read(USER_STRING_LENGTH))
f.close()
return user
def getUsers(startIndex=0, count=0):
index = startIndex*USER_STRING_LENGTH
length = count*USER_STRING_LENGTH
f = open(FILE_NAME, 'r')
f.seek(index)
users = []
usersData = f.read(length)
f.close()
while 1:
if len(usersData) < USER_STRING_LENGTH:
return users
user = Global.parseUser(usersData[:USER_STRING_LENGTH])
if user != None:
users.append(Global.parseUser(usersData[:USER_STRING_LENGTH]))
usersData = usersData[USER_STRING_LENGTH:]
def getUserById(userId):
index = (int(userId) - 1) * USER_STRING_LENGTH
if index > os.path.getsize(FILE_NAME) - 1:
return None
f = open(FILE_NAME, 'r')
f.seek(index)
userData = f.read(Global.USER_STRING_LENGTH)
user = Global.parseUser(userData)
f.close()
return user
# userId = str(userId).strip()
# #Number of users in file
# count = int(os.path.getsize(FILE_NAME)/USER_STRING_LENGTH)
#
# f = open(FILE_NAME, 'r')
#
# userData = ''
# i = 0
# for i in range(count):
# f.seek(i*USER_STRING_LENGTH + Global.USER_ID_INDEX)
# if f.read(Global.USER_ID_LENGTH).strip() == userId:
# f.seek(i*USER_STRING_LENGTH)
# userData = f.read(USER_STRING_LENGTH)
# f.close()
# user = Global.parseUser(userData)
# return user
def getUserByName(userName):
userName = str(userName).strip()
#Number of users in file
count = int(os.path.getsize(FILE_NAME)/USER_STRING_LENGTH)
f = open(FILE_NAME, 'r')
userData = ''
i = 0
for i in range(count):
f.seek(i*USER_STRING_LENGTH + Global.USER_NAME_INDEX)
if f.read(Global.USER_NAME_LENGTH).strip() == userName:
f.seek(i*USER_STRING_LENGTH)
userData = f.read(USER_STRING_LENGTH)
f.close()
user = Global.parseUser(userData)
return user
def getUserByPhone(userPhone):
userPhone = str(userPhone).strip()
#Number of users in file
count = int(os.path.getsize(FILE_NAME)/USER_STRING_LENGTH)
f = open(FILE_NAME, 'r')
userData = ''
i = 0
for i in range(count):
f.seek(i*USER_STRING_LENGTH + Global.USER_PHONE_INDEX)
if f.read(Global.USER_PHONE_LENGTH).strip() == userPhone:
f.seek(i*USER_STRING_LENGTH)
userData = f.read(USER_STRING_LENGTH)
f.close()
user = Global.parseUser(userData)
return user
def getUserByEmail(userPhone):
userPhone = str(userPhone).strip()
#Number of users in file
count = getNumberOfUsers()
f = open(FILE_NAME, 'r')
userData = ''
i = 0
for i in range(count):
f.seek(i*USER_STRING_LENGTH + Global.USER_EMAIL_INDEX)
if f.read(Global.USER_EMAIL_LENGTH).strip() == userPhone:
f.seek(i*USER_STRING_LENGTH)
userData = f.read(USER_STRING_LENGTH)
f.close()
user = Global.parseUser(userData)
return user
def getNextId():
#Number of users
count = os.path.getsize(FILE_NAME)/USER_STRING_LENGTH
return int(count+1)
def modifyUserData(user):
index = (int(user.ID) - 1)*USER_STRING_LENGTH
f = open(FILE_NAME, 'r+')
f.seek(index)
f.write(user.toString())
f.close()
def deleteUser(user = User()):
index = (int(user.ID) - 1) * USER_STRING_LENGTH
f = open(FILE_NAME, 'r+')
f.seek(index)
f.write(' '*USER_STRING_LENGTH)
f.close()