-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_handler.py
293 lines (251 loc) · 7.88 KB
/
data_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
__author__ = 'ozsvarmarton'
from os import system
from time import sleep
import event_reg
import donor_reg
import sort_by_order
import delete_donor
import search_in_files
import csv
import modify_donor_data
import donor_csv_writer
# import menu
header = """
-----------------------------------------------------------------------
--- Welcome to the coolest donor and donation event managing system ---
-----------------------------------------------------------------------
"""
event_file = "DATA/donations.csv"
# list of donation events:
our_events = {}
# data-reader and writer functions:
def read():
with open(event_file, "r", newline='') as file_obj:
new_events = {}
reader = csv.reader(file_obj)
# assert header.index("id") == 0
next(reader)
for row in reader:
index = int(row[0])
data = row[1:]
new_events[index] = event_reg.Donation.from_lists(data)
global our_events
our_events = new_events
def write():
with open(event_file, "w", newline='') as file_obj:
global our_events
writer = csv.writer(file_obj)
writer.writerow(["id"] + event_reg.Donation.header)
for ind, event in our_events.items():
writer.writerow([str(ind)] + event.to_lists())
# adder functions:
def add_event():
global our_events
err_msg = "Adding donation event unsuccessful.\nReason:\n%s"
try:
new_event = event_reg.Donation.from_user()
new_event.input_successful_donation()
new_event.evaluate_event()
ind = 1
while ind in our_events.keys():
ind += 1
our_events[ind] = new_event
write()
except event_reg.UserInterrupt as interruption:
print(err_msg % str(interruption))
sleep(1.5)
except FileNotFoundError:
our_events.pop()
print(err_msg % "File cannot be written.")
sleep(1.5)
def add_donor():
data = donor_reg.main()
if data:
donor_csv_writer.store_donor(data)
# remover functions:
def remove_event_at(index: int):
global our_events
clone_events = our_events.copy()
our_events.pop(index)
try:
write()
except FileNotFoundError as err:
our_events = clone_events
raise err
def remove_event():
err_msg = "Deleting donation event unsuccessful.\nReason:\n%s"
s_index = ""
p_index = 0
while True:
system("cls")
s_index = input("Which item would you like to remove? (type 'q' for returning to main menu) :")
if s_index == 'q':
return
try:
p_index = int(s_index)
remove_event_at(p_index)
print("Deleting donation event successful.")
sleep(1.5)
break
except ValueError:
print(err_msg % "Input cannot be parsed as an integer!")
except IndexError:
print(err_msg % "No donation event with such an index in database.")
except FileNotFoundError:
print(err_msg % "Modifications cannot be saved to file.")
sleep(1.5)
def remove_donor():
delete_donor.delete()
# lister functions:
def list_events():
global our_events
err_msg = "Error while listing donors\n%s"
try:
sort_by_order.sorting_donation_by_order()
except ValueError as lister_error:
system("cls")
print(err_msg % "ValueError\n%" % str(lister_error))
except IndexError as lister_error:
system("cls")
print(err_msg % "IndexError" % str(lister_error))
def list_donors():
err_msg = "Error while listing donors\n%s"
try:
sort_by_order.sorting_donor_by_order()
except FileNotFoundError as lister_error:
system("cls")
print(err_msg % "File cannot be found\n%s" % str(lister_error))
except ValueError as lister_error:
system("cls")
print(err_msg % "ValueError\n%" % str(lister_error))
except IndexError as lister_error:
system("cls")
print(err_msg % "IndexError" % str(lister_error))
finally:
sleep(1.5)
# searcher functions:
def search_in_donors():
err_msg = "Error while searching in donors\n%s"
try:
search_in_files.search_in_donors()
except FileNotFoundError as searcher_error:
system("cls")
print(err_msg % "File cannot be found\n%s" % str(searcher_error))
except ValueError as searcher_error:
system("cls")
print(err_msg % "ValueError\n%" % str(searcher_error))
except IndexError as searcher_error:
system("cls")
print(err_msg % "IndexError" % str(searcher_error))
finally:
sleep(1.5)
def search_in_events():
err_msg = "Error while searching in donors\n%s"
try:
search_in_files.search_in_events()
except FileNotFoundError as searcher_error:
system("cls")
print(err_msg % "File cannot be found\n%s" % str(searcher_error))
except ValueError as searcher_error:
system("cls")
print(err_msg % "ValueError\n%" % str(searcher_error))
except IndexError as searcher_error:
system("cls")
print(err_msg % "IndexError" % str(searcher_error))
finally:
sleep(1.5)
# changer:
def modify_event(event):
system('cls')
print(header)
print("Donation to modify:")
print(event)
event = event_reg.Donation.from_user()
event.input_successful_donation()
event.evaluate_event()
return event
def modify():
global our_events
system('cls')
print(header)
inp = input("Please, enter the id of Donor or Donation you want to modify:")
if inp.isdigit():
i = int(inp)
if i in range(len(our_events)):
try:
events = our_events.copy()
our_events[i] = modify_event(our_events[i])
write()
print("Donation event data successfully modified!")
sleep(1.5)
except event_reg.UserInterrupt:
print("Input interrupted by user")
sleep(1)
except FileNotFoundError:
our_events = events
print("File cannot be written")
sleep(2)
else:
print("Id number is not found in list of Donations!")
sleep(1.5)
elif donor_reg.validate_identifier(inp):
if donor_reg.is_id_unique(inp):
print("The given ID is not in data-base!")
else:
modify_donor_data.modify_donor(inp)
sleep(1.5)
else:
print("Input cannot be parsed as an id of either a Donor or an Donation event.")
sleep(1.5)
# initializer (on start-up we need to read the files):
def initialize():
try:
read()
except FileNotFoundError:
system("cls")
print(header)
print("Error: File cannot be read. Database stays empty in memory.")
sleep(1)
except ValueError:
system("cls")
print(header)
print("Error: File cannot be read. Database stays empty in memory.")
sleep(1)
except IndexError:
system("cls")
print(header)
print("Error: File cannot be read. Database stays empty in memory.")
sleep(1)
# demo for stand-alone running:
def demo():
# print("Adding 2 events:")
# print("First...")
# add_event()
# print("Second...")
# add_event()
# print("Done.")
# print("Removing 2nd element...")
# remove_event()
# print("Done.")
# print("Adding another element...")
# add_event()
# print("Done.")
# print("Listing donors...")
# list_donors()
# print("Adding new donor...")
# donor_reg.main()
# print("Done.")
# print("Listing donors.")
# list_donors()
# print("Deleting one donor...")
# remove_donor()
# print("Done.")
# print("Listing donors again.")
# list_donors()
# print("Demo terminates.")
modify()
def shutdown():
pass
# in main mode running demo:
if __name__ == '__main__':
demo()