-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysmspage.py
172 lines (161 loc) · 6.02 KB
/
pysmspage.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
#!/usr/bin/env python
#pysmspage.py
#
#Copyright 2012 - Patrick F. Wilbur <proj pdub net>
#
#
# Python 2.6+ compatibility:
from __future__ import print_function
try:
input = raw_input
except:
pass
try:
import configparser
except:
pass
import ConfigParser as configparser
# End of Python 2.6+ compatibility code
import os
import platform
import sys
sys.path.append('lib')
from SMSOverSMTP import *
from PersonDatabase import *
class SMSPager:
def __init__(self):
self.db = PersonDB(path_to_person_flatfile='personpagingdatabase.fdb')
self.SMSGateway = None
def page(self,recipient,carrier,recipientName,text):
self.SMSGateway.sendSMS(recipient,carrier,recipientName,text)
def searchPeople(self,query):
return self.db.search(query)
class SMSOverSMTPPager(SMSPager):
def __init__(self):
self.db = PersonDB(path_to_person_flatfile='personpagingdatabase.fdb')
self.SMSGateway = SMSOverSMTPSender()
class PySMSPage:
def __init__(self,smsPagerClass):
self.smsPager = smsPagerClass
def clearScreen(self):
os.system( "cls" if platform.system() == "Windows"
else "clear")
def mainMenu(self, clearScreen=True):
menuItems = {'a': ('add person to database',self.addPersonMenu),
'd': ('delete/remove person from database', self.removePersonMenu),
'p': ('page person',self.pageMenu)}
if clearScreen == True:
self.clearScreen()
while True:
try:
print('')
print('Welcome to PySMSPage!')
print('')
print('At any time, press Ctrl+C to quit.')
print('')
print('[MAIN MENU]')
for key in menuItems.keys():
print(key, menuItems[key][0])
choice = input('Enter selection: ')
if choice in menuItems.keys():
menuItems[choice][1]()
self.clearScreen()
else:
self.clearScreen()
print('Invalid choice. Please try again!')
except EOFError:
pass
self.clearScreen()
print('You are already at the Main Menu. Pressing Ctrl+C will quit.')
print('')
print('')
except KeyboardInterrupt:
pass
print('')
print('')
print('')
print('')
exit(0)
def addPersonMenu(self):
print('')
print('')
print('Adding a new person to database (Press Ctrl+D to cancel and go back):')
print('')
try:
name = input('Enter full name: ')
telephone = input('Enter telephone number: ')
carrier = input('Enter telephone carrier: ')
key = input('Enter paging ID (shorthand): ')
self.smsPager.db.addPerson(key,name,telephone,carrier)
self.smsPager.db.savePersonFlatfile()
except EOFError:
pass
def removePersonMenu(self):
print('')
while True:
try:
print('')
query = input('Query for person deletion (Ctrl+D to go back): ')
except EOFError:
pass
break
if query != '':
print('')
results = self.smsPager.searchPeople(query)
if len(results) != 1:
print("Results for '" + query.lower() + "':")
for result in results:
if len(results) == 1:
print('')
print('You are about to delete the following person from the database:')
print(result,results[result][0],results[result][1])
confirm = input('Are you sure? (Y/n): ')
if confirm == 'Y' or confirm == 'y':
self.smsPager.db.deletePerson(result)
self.smsPager.db.savePersonFlatfile()
break
else:
print(results[result][1],
' ',
results[result][0],
' (' + result + ')')
def pageMenu(self):
while True:
try:
print('')
query = input('Query (Ctrl+D to go back): ')
except EOFError:
pass
break
if query != '':
print('')
results = self.smsPager.searchPeople(query)
if len(results) != 1:
print("Results for '" + query.lower() + "':")
for result in results:
if len(results) == 1:
print('Paging', results[result][0],
'(' + result + ')',
'@',
results[result][1],
'...')
try:
text = input('Enter a page message (Ctrl+D to cancel): ')
except EOFError:
pass
continue
self.smsPager.page(results[result][1],
results[result][2],
results[result][0],
text)
else:
print(results[result][1],
' ',
results[result][0],
' (' + result + ')')
if __name__ == '__main__':
smsPager = SMSOverSMTPPager()
# smsPager.db.PersonDatabase = {'doe1':('Jane Doe','5555551212','AT&T (Cingular)'),'deer1':('John Deer','5555551313','Verizon')}
# smsPager.db.savePersonFlatfile()
p = PySMSPage(smsPager)
p.mainMenu(clearScreen=False)