-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomerwork12_new + CLI bot.py
More file actions
396 lines (337 loc) · 13.2 KB
/
homerwork12_new + CLI bot.py
File metadata and controls
396 lines (337 loc) · 13.2 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from collections import UserDict
from datetime import datetime
from datetime import date
import math
import re
import json
class AddressBook(UserDict):
def __init__(self, name):
super().__init__()
self.name = name
self.current_page = 0
self.records_on_the_page = 2
def add_record(self,record):
self.data [record.name.value] = record
def __iter__(self):
return self
def __next__(self):
if self.current_page < int(math.ceil(len(self.data)/self.records_on_the_page)) :
keys = list(self.data.keys())
r_list = []
for i in range(self.current_page*self.records_on_the_page ,min([(self.current_page+1)*self.records_on_the_page,len(self.data)])):
a_dict = {}
a_dict["Name"] = keys[i]
a_dict["Phones"]= [x.value for x in self.data[keys[i]].phones]
if type(self.data[keys[i]].birthday)!=type(""):
a_dict["Birthday"]= str(self.data[keys[i]].birthday.value)
r_list.append(a_dict)
self.current_page+=1
return r_list
else:
self.current_page = 0
raise StopIteration
def delete(self, name):
if name in self.data.keys():
print("Input 'Y' to delete the record for contact: ", name)
if input() in ["Y", "y"]:
self.data.pop(name)
print("Record deleted")
else:
print("Delete of the record cancelled")
def dump(self, file):
with open(file, 'w+') as write_file:
dump_dict ={self.name:{}}
store_records_on_the_page = self.records_on_the_page
self.records_on_the_page = 1
id =1
for page in self:
dump_dict[self.name]["RecID"+str(id)]= page[0]
id+=1
json.dump(dump_dict, write_file)
self.records_on_the_page = store_records_on_the_page
print("Data exported to the file")
def load(self, file):
with open(file, 'r') as read_file:
data = json.load(read_file)
self.name= list(data.keys())[0]
for name in list(data[self.name].keys()):
record = data[self.name][name]
rec = Record(record["Name"])
if "Phones" in record.keys():
for phone in record["Phones"]:
rec.add_phone(Phone(phone))
if "Birthday" in record.keys():
lst = record["Birthday"].split("-")
birthday = Birthday(lst[2]+"."+lst[1]+"."+lst[0])
rec.add_birthday(birthday)
self.add_record(rec)
print ("Data have been loaded from file")
def find(self, request):
result_lst = []
for name in self.data.keys():
search_list = [name]
search_list.extend([phone.value for phone in self.data[name].phones])
for field in search_list:
if request[0]=="+":
request = request[1:]
if re.search(request.upper(),field.upper())!=None:
result_lst.append(name)
break
return result_lst
class Record:
def __init__(self, name):
self.phones = list()
self.birthday = ""
self.name = Name(name)
def add_phone(self,phone):
if phone.value not in [ph.value for ph in self.phones]:
self.phones.append(phone)
def del_phone(self,phone):
self.phones = list(filter(lambda x: x.value!=phone, self.phones))
def edit_phone(self,phone, new_phone):
status = ""
if phone in [x.value for x in self.phones]:
self.del_phone(phone)
self.add_phone(Phone(new_phone))
else:
status = "Can't find the number "+phone+" in the phone list"
return status
def add_birthday(self,birthday):
self.birthday = birthday
def days_to_birthday(self):
date1 = datetime(datetime.now().timetuple().tm_yday, self.birthday.value.timetuple().tm_mon, self.birthday.value.timetuple().tm_mday)
delta = date1.timetuple().tm_yday - datetime.now().timetuple().tm_yday
if delta > 0:
return str(delta)
else:
date1 = datetime(datetime.now().timetuple().tm_year+1, self.birthday.value.timetuple().tm_mon, self.birthday.value.timetuple().tm_mday)
date2 = datetime(datetime.now().timetuple().tm_year, datetime.now().timetuple().tm_mon, datetime.now().timetuple().tm_mday)
delta = date1 - date2
return str(delta.days)
class Field:
def __init__(self, value):
self.value = value
def __str__(self):
print(f"{self.__dict__}")
@property
def value(self):
return self.__value
@value.setter
def value(self, value_):
if len(value_) > 0:
self.__value = value_
class Name(Field):
def __init__(self, name):
self.__value = name
@property
def value(self):
return self.__value
@value.setter
def set_value(self, value):
if len(value) > 0:
self.__value = value
class Phone(Field):
def __init__(self, phone):
self.value = phone
@property
def value(self):
return self.__value
@value.setter
def value(self, phone):
if re.search('\+\d{12}', phone) != None:
self.__value = phone
else:
raise ValueError("Phone should be in the next format: '+XXXXXXXXXXXX' (12 digits)")
class Birthday(Field):
def __init__(self, birthday):
self.value = birthday
@property
def value(self):
return self.__value
@value.setter
def value(self, birthday):
if re.search('\d{2}\.\d{2}\.\d{4}', birthday) != None:
self.__value = datetime.strptime(birthday, '%d.%m.%Y').date()
else:
return False
##################################################
# CLI BOT section #
##################################################
exit_command = ["good bye", "close", "exit"]
def format_phone_number(func):
def inner(phone):
result=func(phone)
if len(result) == 12:
result='+'+result
else: result='+38'+result
return result
return inner
@format_phone_number
def sanitize_phone_number(phone):
new_phone = (
phone.strip()
.removeprefix("+")
.replace("(", "")
.replace(")", "")
.replace("-", "")
.replace(" ", "")
)
return new_phone
def is_correct_input_add(data):
name = ""
phone = ""
message =""
is_correct_input = re.search("[a-zA-Zа-яА-Я1-9]{1,100} \+?[{0,1}\d \-\(\)]*$",data)
if is_correct_input!=None:
match_ = re.search(" \+?[{0,1}\d \-\(\)]*$", data)
if match_!=None:
phone = sanitize_phone_number(data[match_.start():match_.end()].strip())
name = data[:match_.start()].strip().rstrip()
if len(phone)!=13:
message = "Incorrect telephone number. Use (+)(Country code)(XXX Operator code) XXX XX XX"
else:
message = "Incorrect command format"
return (name, phone, message)
def is_correct_input_change(data):
name = ""
phone_old = ""
phone_new = ""
message =""
is_correct_input = re.search("[a-zA-Zа-яА-Я1-9]{1,100} \+?[{0,1}\d\-\(\)]* \+?[{0,1}\d\-\(\)]*$",data)
if is_correct_input!=None:
phone_old, phone_new = data.split(" ")[-2:]
name = data[0: len(data)-len(phone_old)-len(phone_new)-2]
phone_old = sanitize_phone_number(phone_old)
phone_new = sanitize_phone_number(phone_new)
if len(phone_new)!=13 or len(phone_old)!=13:
message = "Incorrect telephone number. Use (+)(Country code)-(XXX Operator code)-XXX-XX-XX"
else:
message = "Incorrect command format"
return (name, phone_old, phone_new, message)
def hello_(data):
return "How can I help You?"
def add_(data):
name, phone, message = is_correct_input_add(data)
if message == "":
print('Name ', name, ' phone ', phone,' message', message)
if name in a.data.keys() and phone in [ph.value for ph in a.data[name].phones]:
print ("Contact is already exist with exactly the same number")
elif name not in a.data.keys() and phone in a.find(phone):
print ("Another contact has this number")
elif name in a.data.keys() and phone not in a.find(phone):
a.data[name].add_phone(Phone(phone))
print ("Contact phone list successfully appended")
else:
r = Record(name)
p = Phone(phone)
r.add_phone(p)
a.add_record(r)
print ("Contact successfully added to phone_book")
else:
print (message)
print ("Please use next format for add comand: ", exec_command["add"][1])
return "Please choose command"
def change_(data):
name, phone_old, phone_new, message = is_correct_input_change(data)
if message == "":
if name in a.data.keys():
message = a.data[name].edit_phone(phone_old, phone_new)
if message == "":
print("Contact successfully changed")
else:
print(message)
else:
print("Contact not in your phone book")
else:
print (message)
print ("Please use next format for add comand: ", exec_command["change"][1])
return "Please choose command"
def find_(data):
name = data.strip().rstrip()
res_lst = a.find(data)
if res_lst == []:
print("Couldn't find ", name, " in the phone book")
else:
print("Found next contacts:")
for contact in res_lst:
print(contact)
return "Please choose command"
def show_all(data):
adress_book = a
for page in adress_book:
for record in page:
print("Name:", record["Name"])
print("Phone list:")
for phone in record["Phones"]:
print(phone)
if "Birthday" in record.keys():
print ("Birthday: ", record["Birthday"])
input("Press enter to continue")
return "Please choose command"
def help_(command):
print("List of available commands: ")
for key in exec_command.keys():
print (exec_command[key][1])
return "Please choose command"
def birthday_(data):
cmnd_lst = data.split(" ")
birthday = cmnd_lst[-1]
name = data[0:len(data) - len(birthday)-1]
if a.find(name)!=[]:
b=Birthday(birthday)
if b == False:
return "Birthday should be in the next format: 'dd.mm.yyyy'"
else:
a.data[name].add_birthday(b)
return "Birthday setted successfully"
def delete_(data):
res = re.search(" \+?[\d\-\(\)]*$",data)
name = data.strip().split(" ")[0]
if name in a.data.keys():
if res == None:
for record in a.find(name):
print(a.find(name))
a.delete(record)
else:
phone = data.strip().split(" ")[1]
phone = sanitize_phone_number(phone)
if phone in [ph.value for ph in a.data[name].phones]:
a.data[name].delete_phone(phone)
print ("Phone deleted succesfully")
else:
print( "Couldn't find this phone number for the "+name)
else:
print ("Couldn't find user "+name)
return "Please choose command"
def save_(data):
a.dump("Work telephones.json")
return "Please choose command"
exec_command = {
"hello": [hello_, "hello", 0],
"add": [add_, "add [name] [phone]", 2],
"change": [change_, "change [name] [phone_old] [phone_new]", 2],
"find": [find_, "phone or name", 1],
"show all": [show_all, "show all", 0],
"help": [help_, "help",0],
"birthday": [birthday_, "birthday [name] [date of birthday dd.mm.yyyy]",1],
"delete": [delete_, "delete [name] [phone (optional)]", 2],
"save": [save_, "save", 0]
}
def handler(command, data):
return exec_command[command][0](data.replace(command+" ",""))
def parser(input_str):
for token in exec_command.keys():
if token in input_str:
return handler(token, input_str.replace(token+" ", ""))
return "Input error, please type 'help' for commands description"
def listener():
command = ""
communication_str = "CLI phone book bot looking for your command"
while (command) not in exit_command:
print(communication_str+": ")
command = input()
communication_str = parser(command)
a = AddressBook("Work telephones")
a.load("Work telephones.json")
listener()
a.dump("Work telephones.json")