/
interface.py
executable file
·477 lines (399 loc) · 12.4 KB
/
interface.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/python3
'''
Archived Contact Engine
Copyright (C) 2022 Nathan Weinberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
import csv
import yaml
import vobject
import argparse
import database as db
import mysql.connector
from mysql.connector import errorcode
def format_header(print_flag):
""" prints header
"""
# additional settings
lastname_ljust = 20
firstname_ljust = 20
mobile_ljust = 15
# w/ affiliation
if print_flag == 1:
affiliation_ljust = 15
header = "\n{} {} {} {}".format("First Name".ljust(firstname_ljust), "Last Name".ljust(lastname_ljust), "Mobile".ljust(mobile_ljust), "Affiliation".ljust(affiliation_ljust))
# w/o affiliation
elif print_flag == 2:
header = "\n{} {} {}".format("First Name".ljust(firstname_ljust), "Last Name".ljust(lastname_ljust), "Mobile".ljust(mobile_ljust))
# No other case should be found
else:
raise ValueError("Error in flag settings value: Value {} was found".format(print_flag))
print(header)
print('-' * len(header))
return None
def format_line(line, print_flag):
""" formats and prints line
"""
# additional settings
lastname_ljust = 20
firstname_ljust = 20
mobile_ljust = 15
firstname = line[0].ljust(firstname_ljust)
lastname = line[1].ljust(lastname_ljust)
mobile = line[2].ljust(mobile_ljust)
# w/ affiliation
if print_flag == 1:
affiliation = line[3].ljust(mobile_ljust)
print("{} {} {} {}".format(firstname, lastname, mobile, affiliation))
# w/o affiliation
elif print_flag == 2:
print("{} {} {}".format(firstname, lastname, mobile))
# No other case should be found
else:
raise ValueError("Error in flag settings value: Value {} was found".format(print_flag))
return None
def print_entire_library():
""" prints entire library
"""
people = db.getAllPeople(conn)
if len(people) == 0:
print("No people exist.")
return None
# print block
format_header(1)
peepLen = len(people)
for i in range(peepLen):
format_line(people[i], 1)
print("\n{} entries".format(peepLen))
return None
def search_by_affilation():
""" prints users associated with particular affiliation
"""
affiliations = db.getAllAffiliations(conn)
if len(affiliations) == 0:
print("No affiliations exist.")
return None
# Prints list of affiliations
affLen = range(len(affiliations))
for i in affLen:
print("(" + str(i + 1) + ") " + affiliations[i][0])
print()
# Returns user-selected affiliation
while True:
try:
choice = int(input("Select the affiliation you wish to search for: "))
if (choice - 1) in affLen:
affiliation = affiliations[choice - 1][0]
break
else:
print('Invalid choice. Please try again.')
except KeyboardInterrupt:
print()
return None
except ValueError:
print('Invalid input. Please try again.')
# get relevant data from database
aid = db.getAffiliationId(conn, affiliation)
people = db.getPeopleFromAid(conn, aid)
# print block
format_header(2)
peepLen = len(people)
for i in range(peepLen):
format_line(people[i], 2)
print("\n{} entries".format(peepLen))
return None
def create_entry(first, last, mobile, affiliation):
""" manual entry creation
"""
# remove apostrophes from fields as they cause issues with SQL
first = first.replace("'", "")
last = last.replace("'", "")
mobile = mobile.replace("'", "")
affiliation = affiliation.replace("'", "")
# further scrub mobile input
if mobile[:2] == "+1":
mobile = mobile[2:]
if ("(" or ")" or "-") not in mobile:
mobile = "(" + mobile[0:3] + ")" + mobile[3:6] + "-" + mobile[6:]
# check for first time occurrence of affiliation
aid = db.getAffiliationId(conn, affiliation)
if not aid:
db.addAffiliation(conn, affiliation)
aid = db.getAffiliationId(conn, affiliation)
try:
db.addPerson(conn, first, last, mobile, aid)
except Exception as e:
print("Error inserting into database: {}".format(e))
else:
print("Person {} {} added to database".format(first, last))
return None
def delete_entry(person):
""" manual entry deletion
"""
# immediately exit if no people exist in db
people = db.getAllPeople(conn)
if len(people) == 0:
print("No people exist.")
return None
# delete all or get uid of specified user
if person == "all":
db.deleteAllPeople(conn)
db.deleteAllAffiliations(conn)
print("All entries deleted from database")
return None
else:
uid = db.getPersonID(conn, person)
# person not found in database
if uid == -1:
print("Person not found in database")
# multiple people found with search terms
elif uid == -2:
print("Multiple people found with search term. Please be more specific.")
else:
aid = db.getPersonAffiliation(conn, uid)
try:
db.deletePerson(conn, uid)
except Exception as e:
print("Error deleting from database: {}".format(e))
else:
print("Person {} deleted from database".format(person))
# check if affiliation is defunct
people = db.getPeopleFromAid(conn, aid)
if people == []:
affName = db.getAffiliationName(conn, aid)
db.deleteAffiliation(conn, aid)
print("Affiliation {} deleted from database".format(affName))
return None
def import_csv(filename):
""" imports contacts from CSV file
"""
# append file extension if not included
if filename[-4:] != ".csv":
filename += ".csv"
try:
with open(filename, "r") as csv_file:
for line in csv_file:
line = line[:-1]
line = line.split(',')
create_entry(line[0], line[1], line[2], line[3])
except Exception as e:
print("Error processing CSV file: {}".format(e))
return None
else:
print("Successfully imported from CSV")
return None
def export_csv(filename):
""" exports contacts to CSV file
"""
# append file extension if not included
if filename[-4:] != ".csv":
filename += ".csv"
try:
with open(filename, "w", newline='') as csv_file:
people = db.getAllPeople(conn)
people_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for person in people:
people_writer.writerow([person[0], person[1], person[2], person[3]])
except Exception as e:
print("Error processing CSV file: {}".format(e))
return None
else:
print("Successfully exported to CSV")
return None
def import_vcf(filename):
""" imports contacts from VCF file
"""
# append file extension if not included
if filename[-4:] != ".vcf":
filename += ".vcf"
try:
with open(filename, "r") as vcf_file:
for entry in vobject.readComponents(vcf_file):
name = entry.fn.value
name = name.split(" ")
if len(name) == 2:
first = name[0]
last = name[1]
else:
first = name[0]
last = None
mobile = entry.tel.value
try:
affiliation = entry.org.value[0]
except:
affiliation = None
create_entry(first, last, mobile, affiliation)
except Exception as e:
print("Error processing VCF file: {}".format(e))
return None
else:
print("Successfully imported from VCF")
return None
def export_vcf(filename):
""" exports contacts to VCF file
"""
# append file extension if not included
if filename[-4:] != ".vcf":
filename += ".vcf"
try:
with open(filename, "a", newline='') as vcf_file:
people = db.getAllPeople(conn)
for person in people:
new_person = vobject.vCard()
new_person.add('n')
new_person.n.value = vobject.vcard.Name(family=person[1], given=person[0])
new_person.add('fn')
new_person.fn.value = person[0] + " " + person[1]
new_person.add('tel')
new_person.tel.value = person[2]
new_person.add('org')
new_person.org.value = [person[3]]
vcf_file.write(new_person.serialize())
except Exception as e:
print("Error processing VCF file: {}".format(e))
return None
else:
print("Successfully exported to VCF")
return None
def main():
while True:
print("\n-------------\n ACE \n-------------")
print("(1) Show entire library")
print("(2) Find affiliation")
print("(3) Write a new entry")
print("(4) Delete an entry")
print("(5) Import from CSV")
print("(6) Import from VCF")
print("(7) Export to CSV")
print("(8) Export to VCF")
print("(0) Exit Program\n")
# ensure choice is type int
try:
choice = int(input("Make a selection: "))
except ValueError:
print("\nPlease input a number.")
except KeyboardInterrupt:
break
else:
# CLI Formatting
print()
# print whole library
if choice == 1:
print_entire_library()
# sort by available affiliations
elif choice == 2:
search_by_affilation()
# write new entry
elif choice == 3:
try:
first = input("Please enter first name: ")
last = input("Please enter last name: ")
mobile = input("Please enter mobile number: ")
affiliation = input("Please enter affiliation: ")
except KeyboardInterrupt:
print("\nEntry Creation Aborted")
else:
create_entry(first, last, mobile, affiliation)
# delete entry
elif choice == 4:
try:
person = input('Please enter the name of the person you wish to delete (type "all" for all): ')
except KeyboardInterrupt:
print("\nEntry Deletion Aborted")
else:
delete_entry(person)
# import from csv
elif choice == 5:
print("NOTE: CSV must be in format <first_name>,<last_name>,<mobile>,<affiliation>")
try:
filename = input("Please input CSV file name to import from (must be in current directory): ")
except KeyboardInterrupt:
print("\nCSV Import Aborted")
else:
import_csv(filename)
# import from vcf
elif choice == 6:
try:
filename = input("Please input VCF file name to import from (must be in current directory): ")
except KeyboardInterrupt:
print("\nVCF Import Aborted")
else:
import_vcf(filename)
# export to csv
elif choice == 7:
print("NOTE: CSV will be in format <first_name>,<last_name>,<mobile>,<affiliation>")
try:
filename = input("Please input CSV file name to export to (will be in current directory): ")
except KeyboardInterrupt:
print("\nCSV Export Aborted")
else:
export_csv(filename)
# export to vcf
elif choice == 8:
try:
filename = input("Please input VCF file name to export to (will be in current directory): ")
except KeyboardInterrupt:
print("\nVCF Export Aborted")
else:
export_vcf(filename)
# exit
elif choice == 0:
break
# invalid choice
else:
print('\nInvalid choice. Please try again.')
print("Goodbye!")
conn.close()
sys.exit()
if __name__ == "__main__":
# argument parsing
parser = argparse.ArgumentParser(description='A simple application designed to display and manipulate contact data')
parser.add_argument("--config", default="config.yaml", type=str, help='Configuration YAML file to use')
parser.add_argument("--test", default=False, action='store_true', help='Flag to use MySQL test credentials')
args = parser.parse_args()
config_file = args.config
test = args.test
# load configuration data
try:
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
except Exception as e:
print("Error loading configuration data: ", e)
sys.exit()
# connect to database
try:
if test:
conn = mysql.connector.connect(
user=config['mysql_test']['user'],
password=config['mysql_test']['password'],
host=config['mysql_test']['host'],
database=config['mysql_test']['database']
)
else:
conn = mysql.connector.connect(
user=config['mysql']['user'],
password=config['mysql']['password'],
host=config['mysql']['host'],
database=config['mysql']['database']
)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
sys.exit()
# license boilerplate
print("Archived Contact Engine Copyright (C) 2022 Nathan Weinberg\nThis program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\nThis is free software, and you are welcome to redistribute it\nunder certain conditions; type `show c' for details.")
main()