-
Notifications
You must be signed in to change notification settings - Fork 0
/
water_interface.py
118 lines (93 loc) · 3.79 KB
/
water_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
#!/usr/bin/env python
# coding: utf-8
"""
water_interface is gui front end for water_scraper.
"""
import os
import threading
from Tkinter import *
from water_scraper import WaterScraper
class WaterThread(threading.Thread):
"""Created to allow GUI responsiveness while running the download.
WaterThread will run a water_scraper operation in it's own thread to allow
the WaterInterface GUI to be responsive while the operation is taking place.
"""
def __init__(self, water_interface):
"""Intitalise the object with an instance of WaterInterface.
When it is time to run, we'll pass the parameters from the WaterInterface
to an instance of WaterScraper."""
self._water_interface = water_interface
threading.Thread.__init__(self)
def run(self):
"""Use the current data in our instance of WaterInterface to make
an instance of a WaterScraper and let it run it's downloads.
"""
scraper = WaterScraper(self._water_interface.source, self._water_interface.dest)
scraper.scrape(self._water_interface.status_callback)
class WaterInterface(object):
"""A GUI interface for WaterScraper."""
def __init__(self, master):
"""Start an instance of WaterInterface
Keword arguments:
master -- A reference to an instance of Tk().
"""
frame = Frame(master)
self.__master = master
master.resizable(FALSE,FALSE)
master.title("Steve's Water App")
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
master.bind('<Return>', self.go)
Label(frame, text='Source (.csv)').grid(column=1, row=1, sticky=(W, E))
self.__source = Entry(frame, width=40)
self.__source.grid(column=2, row=1, sticky=(W, E))
Label(frame, text='Destination (folder)').grid(column=1, row=2, sticky=(W,E))
self.__dest = Entry(frame, width=40)
self.__dest.grid(column=2, row=2, sticky=(W,E))
Button(frame, text="Go", command=self.go).grid(column=2, row=3, sticky=(W,E))
Label(frame, text='Log').grid(column=1, row=4, sticky=(W,E))
self.__logging_window = Text(frame, width=40)
self.__logging_window.config(state=DISABLED)
self.__logging_window.grid(column=2, row=4, sticky=(W,E))
for child in frame.winfo_children():
child.grid_configure()
def go(self, event = None):
"""Start the downloading operation.
Also does a little checking of the input parameters.
Keyword arguments:
event -- Passed via Tkinter. Not used.
"""
source = self.__source.get().strip()
dest = self.__dest.get().strip()
if len(source) <= 0 or not os.path.isfile(source):
self.log('Invalid Source')
return
if len(dest) <= 0:
self.log('Invalid Destination')
return
scraper = WaterScraper(source, dest)
water_thread = WaterThread(self)
water_thread.start()
def log(self, data):
"""Simply posts the data to the long window.
Keyword arguments:
data -- Expect this to be a string. Logged to the log text box.
"""
self.__logging_window.config(state=NORMAL)
self.__logging_window.insert(END, data + '\n')
self.__logging_window.config(state=DISABLED)
def get_source(self):
return self.__source.get().strip()
source = property(get_source)
def get_dest(self):
return self.__dest.get().strip()
dest = property(get_dest)
def get_status_callback(self):
return self.log
status_callback = property(get_status_callback)
if __name__ == "__main__":
root = Tk()
app = WaterInterface(root)
root.mainloop()
print 'Done'