-
Notifications
You must be signed in to change notification settings - Fork 1
/
userInterface.py
213 lines (180 loc) · 6.46 KB
/
userInterface.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
import sys
import httplib2
from Tkinter import *
from urllib import quote, unquote
from xmlutils import *
import resource
class UserInterface:
#root: tkinter root
#frame: main window pane
#home: url to GET home dir
#cwd: url to GET current dir
def __init__(self, tkroot, homedir, http_handle):
self.http_handle = http_handle
self.home = homedir
self.cwd = self.home
self.downloads = "./"
self.uploads = "./"
#set up root window stuff
self.root = tkroot
self.frame = Frame(self.root, width=1000, height=500)
#build top menu bar
menuframe = Frame(self.root)
Button(menuframe, text="Home", command=self.go_home).grid(row=0, column=0)
Button(menuframe, text="Refresh", command=self.refresh).grid(row=0, column=1)
Button(menuframe, text="Close", command=self.root.destroy).grid(row=0, column=2)
Button(menuframe, text="Log Out", command=self.logout).grid(row=0, column=3)
menuframe.pack()
# display current (Home) directory
self.fileframe = Frame(self.root)
self.files = Frame(self.fileframe)
self.refresh()
self.fileframe.pack()
# add buttons to create folder/upload file
bframe = Frame(self.root)
bframe.pack()
b = Button(bframe, text="Create Folder",
command=self.create_folder_dialog,
padx=10, pady=10).grid(row=0, column=0)
b = Button(bframe, text="Upload File",
command=self.upload_file_dialog,
padx=10, pady=10).grid(row=0, column=1)
def go_home(self):
self.cwd = self.home
self.refresh()
def go_here(self, folderName):
def g():
self.cwd = folderName
self.refresh()
return g
def make_request(self,method, body, path):
resp, content = self.http_handle.request(path,
method, body=body,
headers={'content-type':'text/plain'} )
if not resp.status == 200:
self.error_dialog("Server error status: " + str(resp.status) +
"\n" + resp.reason)
return content
def refresh(self):
self.display_directory(
parseResourceList(
self.make_request("GET","",self.cwd)))
#display_directory :: [Resources] -> ()
def display_directory(self, resourceList):
self.files.destroy()
self.files = Frame(self.fileframe)
dirNames = []
fileNames = []
i = 0
for r in resourceList:
Label(self.files, text=r.name).grid(row=i, column=0)
if r.category == "directory":
Button(self.files, text=unichr(8658),
command=self.go_here(r.url + '/')).grid(row=i, column=1)
else:
Button(self.files, text=unichr(8659),
command=self.download_file(r.url)).grid(row=i, column=1)
if not r.name == '..':
Button(self.files, text='X',
command=self.delete_resource(r.url)).grid(row=i, column=2)
i = i + 1
self.files.pack()
def upload_file_dialog(self):
top = Toplevel()
self.popup = top
top.title("Which File to Upload? And where to send it?")
self.srcinput = StringVar()
e = Entry(top, textvariable=self.srcinput)
e.pack()
self.srcinput.set("Name of File to Send")
self.destinput = StringVar()
e = Entry(top, textvariable=self.destinput)
e.pack()
self.destinput.set("Name of file on server")
button = Button(top, text="Upload", command=self.upload_file)
button.pack()
def upload_file(self):
src = self.srcinput.get()
dest = self.destinput.get()
self.popup.destroy()
srcpath = self.uploads + src
desturl = self.cwd + quote(dest)
r = Resource()
r.initFromPath(srcpath)
if hasattr(r , 'category'):
r.addContent(path=srcpath)
xmlstr = buildResourceUpload(r)
self.make_request("PUT", xmlstr, desturl)
self.refresh()
else:
self.error_dialog("File name provided does not exist.")
def error_dialog(self, message):
top = Toplevel()
top.title("ERROR")
Label(top, text="\n " + message + " \n").pack()
b = Button(top, text="OK", command=top.destroy)
b.pack()
def create_folder_dialog(self):
top = Toplevel()
self.popup = top
top.title("What name should the folder have?")
self.destinput = StringVar()
e = Entry(top, textvariable=self.destinput)
e.pack()
self.destinput.set("Name of New Folder")
button = Button(top, text="Create", command=self.create_folder)
button.pack()
def create_folder(self):
r = Resource()
r.name = self.destinput.get()
self.popup.destroy()
r.category = 'directory'
xmlstr = buildResourceUpload(r)
self.make_request("PUT", xmlstr,self.cwd + quote(r.name))
self.refresh()
def download_file(self, name):
def d():
r = parseResourceDownload(self.make_request("GET","",name))
r.putContent(self.downloads + r.name)
return d
def delete_resource(self, name):
def d():
self.make_request("DELETE","", name)
self.refresh()
return d
def logout(self):
self.root.destroy()
self.root = Tk()
login_dialogue(self.root)
self.root.mainloop()
def login_dialogue(root):
top = Toplevel()
top.title("Login:")
username = StringVar()
e = Entry(top, textvariable=username)
e.pack()
username.set("sampleuser")
passwd = StringVar()
e = Entry(top, textvariable=passwd)
e.pack()
passwd.set("samplepw")
host = StringVar()
e = Entry(top, textvariable=host)
e.pack()
host.set("127.0.0.1:8887")
button = Button(top, text="Login", command=login(root, top, username, passwd, host))
button.pack()
def login(root, top, uname, pswd, hst):
def l():
username = uname.get()
passwd = pswd.get()
host = hst.get()
top.destroy()
homedir = 'http://' + host + '/' + username + '/'
h = httplib2.Http(".cache")
h.add_credentials(username, passwd)
app = UserInterface(root, homedir, h)
return l
root = Tk()
login_dialogue(root)
root.mainloop()