-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddress-cli.py
More file actions
173 lines (147 loc) · 5.19 KB
/
address-cli.py
File metadata and controls
173 lines (147 loc) · 5.19 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
#!/usr/bin/env python3
"""
Simplified address application application
This is the version just uses standard serialisation to text files
"""
import os, sys, getopt, json
FILE_PATH = "data/contacts.json"
def open_addressbook(filepath):
"""
Function to open the address book file specified
Returns the addressbook or None
"""
path_exists = os.path.exists(filepath)
addressbook = None
if path_exists:
with open(filepath, 'r') as infile:
addressbook = json.load(infile)
return addressbook
def list_contacts(filepath):
"""
Function to list contact details in the addressbook
Returns
"""
addressbook = open_addressbook(filepath)
if addressbook:
for contact in addressbook['contacts']:
print('Name: ' + contact['name'], 'Telephone: ' + contact['phone'], 'Address: ' + contact['address'], sep='\t', end='\n' )
else:
print('No address book found!')
def retrieve_contact(filepath, name):
"""
Function to retirve specified contact details in the addressbook
Returns a contact or None
"""
contact_details = None
addressbook = open_addressbook(filepath)
print('Finding ', name)
if addressbook:
for contact in addressbook['contacts']:
if contact['name'] == name:
contact_details = contact
break
else:
print('No address book found!')
print('Name: ' + contact_details['name'], 'Telephone: ' + contact_details['phone'],
'Address: ' + contact_details['address'], sep='\t', end='\n' )
return contact_details
def add_contact(filepath):
"""
Function to add contact details to the addressbook
It take user input and creates a contact that is added
Returns
"""
new_contact = {}
fullname = input('Please enter the contact name: ')
print(fullname)
# Have we got a name to add?
if fullname:
new_contact['name'] = fullname
new_contact['phone'] = str(input('Please enter their phone number: '))
new_contact['address'] = input('Please enter their address: ')
# Try to open the current addressbook
addressbook = open_addressbook(filepath)
if addressbook is None:
# If there was no addressbook create one
print('Creating new address book')
contacts = []
addressbook = {'filetype':'Addressbook','application':'address-cli.py', 'contacts': contacts}
print(new_contact)
with open(filepath, 'w') as outfile:
# Write the output file with the new contact
addressbook['contacts'].append(new_contact)
json.dump(addressbook, outfile)
def remove_contact(filepath, name):
"""
Function to remove specified contact details from the addressbook
Returns
"""
# Have we got a name to remove?
if name:
# Try to open the current addressbook and find the contact index
addressbook = open_addressbook(filepath)
contact = retrieve_contact(filepath, name)
if contact:
with open(filepath, 'w') as outfile:
# If contact was found delete it
addressbook['contacts'].remove(contact)
json.dump(addressbook, outfile)
print('Contact: ', contact, ' removed.\n')
def help():
"""
Function to display the command line help
Returns
"""
cmd_help = 'address-cli.py -h -i <input file> -l -a -r -f <contact to find>"'
print(cmd_help)
def process_options(argv, inputfile):
"""
Function to determine what option were spefied
only the last action is used. Setting the file name always gets handled.
Returns options that are selected including the last action, file path and contact name
"""
action = ''
contact = ''
options = {}
try:
opts, args = getopt.getopt(argv,"hi:lar:f:",["help","ifile=","list","add","remove","find="])
except getopt.GetoptError:
help()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
help()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-l", "--list"):
action = 'list'
elif opt in ("-a", "--add"):
action = 'add'
elif opt in ("-r", "--remove"):
action = 'remove'
contact = arg
elif opt in ("-f", "--find"):
action = 'find'
contact = arg
if action == '':
#If there are no actions display the help and quit the program
help()
sys.exit(2)
options['filepath'] = inputfile
options['action'] = action
options['contact'] = contact
return options
def main(argv):
options = process_options(argv, FILE_PATH)
#Process the actions
if options['action'] == 'list':
list_contacts(options['filepath'])
elif options['action'] == 'add':
add_contact(options['filepath'])
elif options['action'] == 'remove':
remove_contact(options['filepath'], options['contact'])
elif options['action'] == 'find':
retrieve_contact(options['filepath'], options['contact'])
if __name__ == "__main__":
main(sys.argv[1:])