-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
262 lines (219 loc) · 8.52 KB
/
main.py
File metadata and controls
262 lines (219 loc) · 8.52 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'''
main driver for a simple social network project
'''
import csv
import logging
import pathlib
import users
import user_status
def init_user_collection():
'''
Creates and returns a new instance of UserCollection
'''
user_instance = users.UserCollection()
# print(instance.database)
return user_instance
def init_status_collection():
'''
Creates and returns a new instance of UserStatusCollection
'''
status_instance = user_status.UserStatusCollection()
return status_instance
def load_users(filename, user_collection:users.UserCollection):
'''
Opens a CSV file with user data and
adds it to an existing instance of
UserCollection
Requirements:
- If a user_id already exists, it
will ignore it and continue to the
next.
- Returns False if there are any errors
(such as empty fields in the source CSV file)
- Otherwise, it returns True.
'''
valid = True
if not pathlib.Path(filename).resolve().is_file():
valid = False
logging.warning("false file path given. Valid returning false.")
print('invalid file. Please try again.')
return valid is False
if valid is True:
with open(filename, 'r',encoding='utf-8') as file:
file_read = csv.reader(file)
file_list = list(file_read)
if len(file_list[0]) != 4:
logging.warning("invalid csv headers. please try again.")
return False
for row in file_list[1:]:
if '' in row:
logging.warning("invalid empty fields. exiting function and returning false.")
return valid is False
check_user_exists = search_user(row[0],user_collection)
if check_user_exists.user_id is None:
user_collection.add_user(*row)
logging.info("file path valid. Returning true")
return valid
def check_path_valid(file):
'''
checks if the called file format works for writing to csv
'''
logging.info("entered check_path_valid for save_users. ")
valid = False
restricted_chars = ['/','?', '|', '\\',]
if not file.endswith(".csv"):
logging.error("incorrect file type. returning false")
print("Incorrect file type. Please input a path to a .csv file.")
# elif file.
else:
valid = True
for item in restricted_chars:
if item in file:
valid = False
break
return valid
def save_users(filename, user_collection: users.UserCollection):
'''
Saves all users in user_collection into
a CSV file
Requirements:
- If there is an existing file, it will
overwrite it.
- Returns False if there are any errors
(such as an invalid filename).
- Otherwise, it returns True.
'''
valid = check_path_valid(filename)
logging.info("check file validity returned %s", valid)
if valid is True:
with open(filename, 'w',newline='',encoding='utf-8') as saved_users:
write_csv = csv.writer(saved_users)
write_csv.writerow(["USER_ID","EMAIL","NAME","LASTNAME"])
for item in user_collection.database.values():
write_csv.writerow([item.user_id,item.email,item.user_name,item.user_last_name])
logging.info("users successfully saved into csv")
return valid
def load_status_updates(filename, status_collection:user_status.UserStatusCollection):
'''
Opens a CSV file with status data and adds it to an existing
instance of UserStatusCollection
Requirements:
- If a status_id already exists, it will ignore it and continue to
the next.
- Returns False if there are any errors(such as empty fields in the
source CSV file)
- Otherwise, it returns True.
'''
valid=True
if not pathlib.Path(filename).resolve().is_file():
logging.error("file does not exist. returning false")
valid = False
if valid is True:
with open(filename, 'r', encoding='utf-8') as file:
file_read = csv.reader(file)
file_list = list(file_read)
for row in file_list[1:]:
if '' in row:
valid = False
break
check_status_exists = search_status(row[0], status_collection)
if check_status_exists.status_id is None:
status_collection.add_status(*row)
logging.info("status successfully loaded")
return valid
def save_status_updates(filename, status_collection:user_status.UserStatusCollection):
'''
Saves all statuses in status_collection into a CSV file
Requirements:
- If there is an existing file, it will overwrite it.
- Returns False if there are any errors(such an invalid filename).
- Otherwise, it returns True.
'''
valid = check_path_valid(filename)
if valid is True:
with open(filename, 'w',newline='',encoding='utf-8') as saved_users:
write_csv = csv.writer(saved_users)
write_csv.writerow(["STATUS_ID","USER_ID","STATUS_TEXT"])
for item in status_collection.database.values():
write_csv.writerow([item.status_id,item.user_id,item.status_text])
return valid
def add_user(user_id, email, user_name, user_last_name, user_collection: users.UserCollection):
'''
Creates a new instance of User and stores it in user_collection
(which is an instance of UserCollection)
Requirements:
- user_id cannot already exist in user_collection.
- Returns False if there are any errors (for example, if
user_collection.add_user() returns False).
- Otherwise, it returns True.
'''
return user_collection.add_user(user_id, email, user_name, user_last_name)
def update_user(user_id, email, user_name, user_last_name, user_collection: users.UserCollection):
'''
Updates the values of an existing user
Requirements:
- Returns False if there any errors.
- Otherwise, it returns True.
# '''
# print("here is user_collection",user_collection.database)
return user_collection.modify_user(user_id,email,user_name,user_last_name)
def delete_user(user_id, user_collection:users.UserCollection):
'''
Deletes a user from user_collection.
Requirements:
- Returns False if there are any errors (such as user_id not found)
- Otherwise, it returns True.
'''
return user_collection.delete_user(user_id)
def search_user(user_id, user_collection:users.UserCollection):
'''
Searches for a user in user_collection(which is an instance of
UserCollection).
Requirements:
- If the user is found, returns the corresponding User instance.
- Otherwise, it returns None.
'''
return user_collection.search_user(user_id)
def add_status(user_id, status_id, status_text, status_collection:user_status.UserStatusCollection):
'''
Creates a new instance of UserStatus and stores it in
user_collection(which is an instance of UserStatusCollection)
Requirements:
- status_id cannot already exist in user_collection.
- Returns False if there are any errors (for example, if
user_collection.add_status() returns False).
- Otherwise, it returns True.
'''
return status_collection.add_status(user_id,status_id,status_text)
def update_status(status_id, user_id, status_text,
status_collection:user_status.UserStatusCollection):
'''
Updates the values of an existing status_id
Requirements:
- Returns False if there any errors.
- Otherwise, it returns True.
'''
return status_collection.modify_status(status_id, user_id, status_text)
def delete_status(status_id, status_collection:user_status.UserStatusCollection):
'''
Deletes a status_id from user_collection.
Requirements:
- Returns False if there are any errors (such as status_id not found)
- Otherwise, it returns True.
'''
return status_collection.delete_status(status_id)
def search_status(status_id, status_collection: user_status.UserStatusCollection):
'''
Searches for a status in status_collection
Requirements:
- If the status is found, returns the corresponding
UserStatus instance.
- Otherwise, it returns None.
'''
return status_collection.search_status(status_id)
# if __name__ == "__main__":
# instance = init_user_collection()
# loaded_status = load_users('test_file_good.csv', instance)
# returned_instance = delete_user('testuser',instance)
# # returned_instance = save_users('abc123.csv', instance)
# print("after delete_user",instance.database)