-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
368 lines (300 loc) · 12.3 KB
/
handler.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
#!/usr/bin/env python3
backend validated data processing
progression_tracker_OOP_V2/handler.py
"""
import os
import uuid
from decorators import timer
from queries import QueryData
from validator import DataValidator
from cryptohandler import CryptoHandler
from python_datalogger import DataLogger
# TODO: Data entry method and get user stats unauthorized bin file generated.
class DataHandler:
"""
Main class for handling user credentials, progress data, data processing, data reading and data writing.
"""
salt_path = f"{os.getcwd()}/user_keys/"
data_path = f"{os.getcwd()}/user_data/"
progress_values = {
1: "Progress",
2: "Trailing",
3: "Retriever",
4: "Exclude"
}
def __init__(self, name: str, pass_marks: int = None, defer_marks: int = None, fail_marks: int = None,
password: str = "root", database: str = "local"):
self.__name = name
self.__password = password
self.__uid = None
self.__pass_marks = pass_marks
self.__defer_marks = defer_marks
self.__fail_marks = fail_marks
self.__outcome = "undetermined"
self.__db_con = QueryData(choice=database)
self.__authentication = False
self.__user_init = False
self.__data_file = None
self.__salt_file = None
self.logger = DataLogger(name="HandleLogger", propagate=False)
if self.__pass_marks is None or self.__defer_marks is None or self.__fail_marks is None:
self.__validity = False
else:
student = DataValidator(self.__name, self.__pass_marks, self.__defer_marks, self.__fail_marks)
if student.validity:
self.__validity = True
else:
self.__validity = False
self.init_env()
self.init_user()
@property
def validity(self) -> bool:
return self.__validity
@property
def authentication(self):
return self.__authentication
@classmethod
@timer
def init_env(cls) -> None:
"""creates necessary directories for user data files"""
path_list = [DataHandler.salt_path, DataHandler.data_path]
cls_logger = DataLogger(name="ClassDataHandler", propagate=False)
for paths in path_list:
try:
os.mkdir(paths)
cls_logger.log_info(f"[{paths}] created.")
except FileExistsError:
cls_logger.log_info(f"Existing path found: [{paths}]")
def init_user(self) -> None:
"""initiates and coordinates the methods assigning uid and password validation for new users"""
self.assign_uid()
self.check_password()
if self.__authentication or not self.check_user_availability():
self.__user_init = True
def check_user_availability(self) -> bool:
"""
search the database for existing users and sort new users and existing users
:return: boolean value depending on user's existence
"""
result = False
temp_user_data = (self.__name,)
sample_data = self.__db_con.execute(func=QueryData.read_user_data_fields(
table_name='user_data',
columns='name'
),
output=True
)
for value in sample_data:
if value == temp_user_data:
result = True
else:
pass
return result
def check_password(self) -> None:
"""check and validate the passwords of existing users."""
if self.check_user_availability():
self.logger.log_info(f"[{self.__name}] existing user found.")
sample_data = self.__db_con.execute(func=QueryData.read_user_specific_field(
column='password',
table_name='user_data',
filter_expression=f"name='{self.__name}'"
),
output=True
)
if self.__password == sample_data[0][0]:
self.__authentication = True
self.logger.log_info(f"User [{self.__name}] authentication successful.")
else:
self.logger.log_warning(f"User [{self.__name}] authentication unsuccessful.")
else:
pass
def set_file_paths(self) -> None:
"""assign the paths for user data and bin files"""
self.__data_file = f"{DataHandler.data_path}{self.__uid}.bin"
self.__salt_file = f"{DataHandler.salt_path}{self.__uid}.bin"
def assign_uid(self) -> None:
"""assigns a universally unique id for each new user.
if the user already exists retrieve the existing uuid from the database"""
if not self.check_user_availability():
self.__uid = uuid.uuid4()
self.set_file_paths()
self.assign_cryptodata()
data_entry = [f"'{self.__uid}'", f"'{self.__name}'", f"'{self.__password}'"]
self.__db_con.execute(func=QueryData.create_row_query(
table_name='user_data',
data_list=data_entry
))
else:
sample_data = self.__db_con.execute(func=QueryData.read_user_specific_field(
column='uid',
table_name='user_data',
filter_expression=f"name='{self.__name}'"
),
output=True
)
if len(sample_data) > 0:
self.__uid = sample_data[0][0]
self.set_file_paths()
def check_salt_data(self) -> bool:
"""
check for existing salt bin files
:return: boolean value depending on file's existence
"""
current_files = os.listdir(DataHandler.salt_path)
salt_file = f"{self.__uid}.bin"
if salt_file in current_files:
return True
else:
return False
def assign_cryptodata(self) -> None:
"""assign necessary cryptographic data for new users"""
if not self.check_salt_data():
user = CryptoHandler(availability=False, password=self.__password)
user.assign_salt()
user.salt_to_bin(path=self.__salt_file)
self.logger.log_info(f"[{self.__name}] - cryptodata assigned.")
else:
pass
def progression_outcome(self) -> None:
"""calculate the user outcome based on validated marks"""
progress, trailing, retriever, exclude = 0, 0, 0, 0
if self.__pass_marks >= 100:
if self.__pass_marks == 120:
self.__outcome = DataHandler.progress_values[1]
progress += 1
else:
self.__outcome = DataHandler.progress_values[2]
trailing += 1
elif self.__fail_marks >= 80:
self.__outcome = DataHandler.progress_values[4]
exclude += 1
else:
self.__outcome = DataHandler.progress_values[3]
retriever += 1
self.update_progression_stats(progress, trailing, retriever, exclude)
self.logger.log_info("Progression stats updated.")
def data_entry(self) -> list:
"""
if validated, enters a new data set of user progress to the database
:return: list of user progression data
"""
if self.__user_init and self.__validity:
self.progression_outcome()
data_entry = [f"'{self.__uid}'", f"'{self.__pass_marks}'",
f"'{self.__defer_marks}'", f"'{self.__fail_marks}'", f"'{self.__outcome}'"]
self.__db_con.execute(func=QueryData.create_row_query(
table_name='user_progression',
data_list=data_entry
))
return [self.__name, self.__pass_marks, self.__defer_marks, self.__fail_marks, self.__outcome]
else:
return [self.__validity]
def update_progression_stats(self, progress: int = 0, trailing: int = 0,
retriever: int = 0, exclude: int = 0) -> None:
"""
update the database according to new user progression stats.
:param progress: progress count
:param trailing: trailing count
:param retriever: retriever count
:param exclude: exclude count
"""
sample_data = self.__db_con.execute(func=QueryData.read_all_queries(
table_name='user_stats',
),
output=True
)
if len(sample_data) == 0:
filter_value = 0
else:
filter_value = sample_data[0][0]
progress += sample_data[0][0]
trailing += sample_data[0][1]
retriever += sample_data[0][2]
exclude += sample_data[0][3]
progress_list = [progress, trailing, retriever, exclude]
self.__db_con.execute(func=QueryData.delete_rows(
table_name='user_stats',
filter_expression=f"progress='{filter_value}'"
))
self.__db_con.execute(func=QueryData.create_row_query(
table_name='user_stats',
data_list=progress_list
))
def dump_to_bin(self, data_str: str = None) -> None:
"""
Outputs a json file with given data.
:param data_str: A string containing updated student progress data.
:return: None
"""
if data_str is None:
data_str = self.generate_str()
dump_user = CryptoHandler(availability=False, password=self.__password, logger_name="CryptoHandleLogger(ENC)")
self.assign_cryptodata()
dump_user.get_salt(path=self.__salt_file)
self.logger.log_info("Key requested to dump data.")
dump_user.generate_key()
encrypted_data = dump_user.encrypt(data=data_str)
dump_user.write_to_bin(file_name=self.__data_file, data=encrypted_data)
self.logger.log_info(f"[{self.__name}] - Data bin encrypted.")
def load_from_bin(self) -> str:
"""
Loads a data string from a given bin file and returns it.
:return: data_str: A dictionary containing student progress data.
"""
try:
load_user = CryptoHandler(availability=True, password=self.__password,
logger_name="CryptoHandleLogger(DEC)")
if self.check_salt_data():
load_user.get_salt(path=self.__salt_file)
else:
raise Exception(f"{self.__salt_file} does not exists.")
self.logger.log_info("Key requested to load data.")
load_user.generate_key()
encrypted_data = load_user.read_from_bin(file_name=self.__data_file)
decrypted_data = load_user.decrypt(data=encrypted_data)
self.logger.log_info(f"[{self.__name}] - Data bin decrypted.")
return decrypted_data.decode()
except FileNotFoundError as error:
self.logger.log_error(str(error))
def generate_str(self) -> str:
"""
generated a string of user progression data from the database
:return: string of progression data
"""
sample_data = self.__db_con.execute(func=QueryData.read_user_specific_field(
column="pass_marks, defer_marks, fail_marks, outcome",
table_name='user_progression',
filter_expression=f"uid='{self.__uid}'"
),
output=True
)
user_progress = f"""
Progress Data
--------------
Name: {self.__name}
UID: {self.__uid}
Data File: {self.__data_file}\n
"""
for data in sample_data:
user_progress = user_progress + f"\n [pass:{data[0]} | defer:{data[1]} | fail:{data[2]} | " \
f"outcome:{data[3]}]"
self.logger.log_info("progress data string generated from database.")
return user_progress
def get_output(self) -> str:
"""
if user is authenticated, coordinates the methods of data sorting, encrypting,
writing, retrieving and decrypting
:return: string of retrieved data
"""
if self.__authentication:
self.dump_to_bin(self.generate_str())
data = self.load_from_bin()
return data
def get_user_data(self):
return f"""
name: {self.__name}
password: {self.__password}
"""
def __repr__(self) -> str:
return f"{self.__name}, {self.__pass_marks}, {self.__defer_marks}, {self.__fail_marks}, {self.__outcome}"