-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddr.py
executable file
·193 lines (152 loc) · 5.9 KB
/
addr.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
#!/usr/bin/python3
# addr.py
# This is a simple application to keep track of contact information from the terminal
import os
import re
import shutil
import sys
DEFAULT_PATH = os.path.join(os.path.expanduser('~'), '.addr_book')
DEFAULT_BACKUP_PATH = os.path.join(os.path.expanduser('~'), '.addr_book_backup')
class Contact:
def __init__(self, name='Unkown', homep='Unkown', cellp='Unkown', workp='Unkown',
emailp='Unkown', emailw='Unkown'):
self.name = str(name)
self.homep = str(homep)
self.cellp = str(cellp)
self.workp = str(workp)
self.emailp = str(emailp)
self.emailw = str(emailw)
def __itter__(self):
return self
def __str__(self):
return 'name:{}, homep:{}, cellp:{}, workp:{}, emailp:{}, emailw:{}'.format(
self.name, self.homep, self.cellp, self.workp, self.emailp, self.emailw)
def display(self):
print('''{0}
phone:
(h) {1}
(c) {2}
(w) {3}
email:
(p) {4}
(w) {5}\n'''.format(self.name, self.homep, self.cellp, self.workp,
self.emailp, self.emailw))
def add_contact(contact_line):
book = open_book()
book.append(build_contact(contact_line))
write_book(book)
def build_contact(line):
copy = line
name = re.search(r'(?<=name:)[a-zA-Z\s]*', line) #contact name
homep = re.search(r'(?<=homep:)[0-9\.\-\(\)x]*', line) #contact home phone
cellp = re.search(r'(?<=cellp:)[0-9\.\-\(\)x]*', line) #contact cell phone
workp = re.search(r'(?<=workp:)[0-9\.\-\(\)x]*', line) #contact work phone
emailp = re.search(r'(?<=emailp:)[a-zA-Z0-9\@\._]*', line) #contact personal email
emailw = re.search(r'(?<=emailw:)[a-zA-Z0-9\@\._]*', line) #contact work email
temp_contact = Contact()
if name is not None:
temp_contact.name = str(name.group(0))
if homep is not None:
temp_contact.homep = str(homep.group(0))
if cellp is not None:
temp_contact.cellp = str(cellp.group(0))
if workp is not None:
temp_contact.workp = str(workp.group(0))
if emailp is not None:
temp_contact.emailp = str(emailp.group(0))
if emailw is not None:
temp_contact.emailw = str(emailw.group(0))
return temp_contact
def delete_contact(name):
book = open_book()
for contact in book:
if contact.name == name:
book.remove(contact)
write_book(book)
def get_help():
print('''This is a simple application to keep track of contact information from the terminal
format:
addr function [keys] [values]
funtions:
add - create new contact
delete - delete an existing contact
print - search and print saved contact information
update - update an existing contact
contact keys (all string values):
name - first and last name
cellp - cell phone number
homep - home phone number
workp - work phone number
workx - work phone extention
email - email address
examples:
addr add {name:'John Doe', cellp:'555-555-5555', email:'[email protected]'}
- creates a new contact
addr delete 'John Doe'
- deletes John Doe's contact information
addr print 'John Doe'
- prints John Doe's information
addr update 'John Doe' 'homep:222-222-2222'
- updates an existing contact named 'John Doe' ''')
sys.exit()
def open_book():
if not os.path.isfile(DEFAULT_PATH):
open(DEFAULT_PATH, 'w').close() # creates an addr_book file if one does not exist
with open(DEFAULT_PATH) as f:
contacts = [build_contact(line) for line in f]
return contacts
def print_contact(query):
book = open_book()
if query == '*':
[contact.display() for contact in book]
else:
for contact in book:
if contact.name == query:
contact.display()
def update_contact(name, key, value):
book = open_book()
for contact in book:
print('{}'.format(contact.name))
if contact.name == name:
if key == 'name':
contact.name = str(value)
elif key == 'homep':
contact.homep = str(value)
elif key == 'cellp':
contact.cellp = str(value)
elif key == 'workp':
contact.workp = str(value)
elif key == 'emailp':
contact.emailp = str(value)
elif key == 'emailw':
contact.emailw = str(value)
else:
print('The key you privided "{}", is not a valid key.'.format((str(key))))
get_help()
print('Contact updated, writing changes to address book')
write_book(book)
print('Done, exiting.')
del book, temp
sys.exit()
def write_book(contacts):
shutil.copy2(DEFAULT_PATH, DEFAULT_BACKUP_PATH) # move original to backup file with metadata
with open(DEFAULT_PATH, 'w') as book:
for entry in contacts:
book.write('{}\n'.format(str(entry)))
sys.exit()
def main():
if 3 > len(sys.argv):
get_help()
if sys.argv[1] == 'add':
add_contact(str(sys.argv[2]))
elif sys.argv[1] == 'delete':
delete_contact(str(sys.argv[2]))
elif sys.argv[1] == 'update':
update_contact(str(sys.argv[2]), str(sys.argv[3]), str(sys.argv[4]))
elif sys.argv[1] == 'print':
print_contact(str(sys.argv[2]))
sys.exit()
print('There seems to have been some sort of issue, you were never supposed to make it this far')
# Statement to call the main function
if __name__ == '__main__':
main()