-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtau_interface.py
executable file
·137 lines (117 loc) · 4.86 KB
/
tau_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
#!/usr/bin/python
"""
Copyright (C) 2015 RJ Russell
Created with the collaborative assistance of::
Jacob Martin, Rachael Johnson, Andrew Wood:
References: Daniel Zappala..BYU Python Tutorial.http://ilab.cs.byu.edu/python/
Python Docs:........................https://www.python.org/
tau_interface.py
This file is responsible for all the user interactions with the client side.
It prompts the user for a recipient and a message. There are built in flags to
assist the user if needed. The flags list the address book or display the help message.
When sending a message the header is appended to the message in the correct format.
The interface loops until the user enters "exit" for a recipient or for a message.
"""
import json
import operator
import os
from code_files.tau_client import TauClient
class TauClientInterface:
def __init__(self):
""" Initializes all variables needed, including reading in the address book from a json file """
self.client = None
self.address_book = json.load(open("code_files/address_book.json"))
self.addresses = self.address_book["Address_Book"]
self.version = "version: 0.2\r\n"
self.sender = "from: chupacabra\r\n"
self.receiver = None
self.address = None
self.message = None
self.time_stamp = None
def choose_receiver(self):
"""
Prompts user to pick a recipient. If a flag is entered
displays the information associated with that flag. If an invalid
recipient is entered, displays message and loops to the prompt.
If "exit" is entered, exits program.
"""
flag = True
while flag:
self.receiver = raw_input("Pick recipient: ")
if self.receiver in ("-address", "-a"):
self.display_addresses()
elif self.receiver in ("-help", "-h"):
self.display_help()
elif self.receiver == 'clear':
self.clear()
elif self.receiver == 'exit':
self.exit_program()
else:
for name, address in self.addresses.items():
if self.receiver == name:
self.address = address
print self.address
flag = False
break
else:
print "Recipient entered was not found."
print "\nPlease try again."
print "(-help(-h) for help menu, -address(-a) to display addresses.\n\n"
def get_message(self):
""" Prompts and reads in message to send from user. If "exit" is entered, exits program. """
self.message = raw_input("Enter message: ")
if self.message == "exit":
self.exit_program()
def append_header(self):
""" Appends header information to message """
self.message = self.version + self.sender + "to: " + self.receiver + "\r\n" + "\r\n" + self.message
def display_addresses(self):
""" Displays address book in sorted order by username """
# Makes copy of address dictionary so the original is not affected.
addresses = self.addresses
addresses = sorted(addresses.items(), key=operator.itemgetter(0))
for name, address in addresses:
print name, "-->", address
print "\n\n"
@staticmethod
def display_help():
""" Displays help message """
print """
-address (-a): display address book
-help (-h): displays the help message
'clear': clears the client screen (only on recipient prompt)
'exit': exits program
('exit' can be entered on recipient/message prompt to exit program.)
Choose a person to send a message to by entering in the user name of the
intended recipient.
"""
@staticmethod
def clear():
""" Clears the screen """
os.system('clear')
def exit_program(self):
print ("Exiting Client..")
self.client.close_client()
exit(-1)
def run_client(self):
"""
Responsible for the logic for this part of the program.
Creates necessary objects, chooses reciever, gets message, appends header
and sends message. If message is successful displays sent message.
Continues to loop until user exits the program.
"""
flag1 = True
while flag1:
self.client = TauClient()
self.choose_receiver()
self.get_message()
if self.message == "exit":
self.exit_program()
else:
self.append_header()
success = self.client.connect_client(self.address, self.message)
if not success == -1:
print self.message
if __name__ == "__main__":
client = TauClientInterface()
client.run_client()