-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·348 lines (295 loc) · 13.5 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import wx
import wx.lib.platebtn as platebtn
import wx.lib.dialogs
import thread
import pleco
import sys, os
import shutil
import tempfile
import time
class GroupBox(wx.StaticBox):
def __init__(self, parent, boxcaption):
wx.StaticBox.__init__(self, parent, wx.ID_ANY, boxcaption)
self.sizer = wx.StaticBoxSizer(self, wx.HORIZONTAL)
checkboxcaptionlist = ["Company", "Financials", "Prices", "Extra Info"]
self.checkboxes={}
for caption in checkboxcaptionlist:
self.checkboxes[caption] = wx.CheckBox(parent, wx.ID_ANY, caption)
self.sizer.Add(self.checkboxes[caption], 1, wx.EXPAND)
self.selectallbutton = platebtn.PlateButton(parent, wx.ID_ANY," SELECT ALL ",
style=platebtn.PB_STYLE_DEFAULT|platebtn.PB_STYLE_TOGGLE)
self.sizer.Add(self.selectallbutton,0, wx.CENTER)
self.selectallbutton.Bind(wx.EVT_TOGGLEBUTTON, self.SelectAll)
def SelectAll(self, evt):
button = evt.GetEventObject()
ispressed = button.IsPressed()
if ispressed:
#If the button is pressed now to select all checkboxes
for caption, checkbox in self.checkboxes.items():
checkbox.SetValue(True)
button.SetLabel("SELECT NONE")
else:
#If the button is pressed now to deselect all checkboxes
for caption, checkbox in self.checkboxes.items():
checkbox.SetValue(False)
button.SetLabel(" SELECT ALL ")
def GetSelection(self):
selectionlist = []
for caption, checkbox in self.checkboxes.items():
if checkbox.GetValue() == True:
selectionlist.append(caption)
return selectionlist
def Disable(self):
self.Enable(False)
def Enable(self, enable=True):
wx.StaticBox.Enable(self, enable)
if enable == False: #Disable:
for checkbox in self.checkboxes.values()+[self.selectallbutton]:
checkbox.Disable()
else:
for checkbox in self.checkboxes.values()+[self.selectallbutton]:
checkbox.Enable()
class PlecoFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, size = (600, 350),
title="Pleco Stock Data Scraper")
self.panel = PlecoPanel(self)
self.statusbar = wx.StatusBar(self)
self.SetStatusBar(self.statusbar)
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, evt):
if evt.CanVeto() and (self.panel.updating or self.panel.exporting):
dlg = wx.MessageDialog(self, 'There is an ongoning update or export process. Are you sure?',
'Close Application',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION
)
if dlg.ShowModal() == wx.ID_YES:
evt.Skip()
dlg.Destroy()
else:
evt.Skip()
class Redirect:
def __init__(self, writefunction):
self.writefunction = writefunction
def write(self, string):
if string == "\n": return
self.writefunction(string)
def flush(self):
pass
class PlecoPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
self.log = []
self.controls = set()
self.parent = parent
self.updating = False
self.exporting = False
sys.stdout = Redirect(self.StandardOutput)
sys.stderr = Redirect(self.StandardErr)
self.stockexchangelist = ["TSX", "NYSE", "NASDAQ", "HKG"]
self.stockboxes = {}
for stock in self.stockexchangelist:
self.stockboxes[stock] = GroupBox(self, stock)
sizer.Add(self.stockboxes[stock].sizer, 1, wx.EXPAND)
self.controls.add( self.stockboxes[stock] )
horsizer1 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(horsizer1, 1, wx.CENTER)
self.updatebutton = wx.Button(self, label="UPDATE")
self.showlogbutton = wx.Button(self, label="SHOW LOG")
self.emptybutton = wx.Button(self, label="EMPTY CACHE")
self.exportbutton = wx.Button(self, label="EXPORT TO EXCEL")
self.controls.add( self.updatebutton )
self.controls.add( self.showlogbutton )
self.controls.add( self.emptybutton )
self.controls.add( self.exportbutton )
horsizer1.Add(self.updatebutton, 1, wx.CENTER)
horsizer1.Add(self.showlogbutton, 1, wx.CENTER)
horsizer1.Add(self.emptybutton, 1, wx.CENTER)
horsizer1.Add(self.exportbutton, 1, wx.CENTER)
self.Bind(wx.EVT_BUTTON, self.UpdateButtonClicked, self.updatebutton)
self.Bind(wx.EVT_BUTTON, self.ShowLogClicked, self.showlogbutton)
self.Bind(wx.EVT_BUTTON, self.EmptyCache, self.emptybutton)
self.Bind(wx.EVT_BUTTON, self.ExportToExcel, self.exportbutton)
def EmptyCache(self, evt):
dlg = wx.MessageDialog(self,
u'Saved web pages will be deleted. Are you sure?',
u'Confirm Delete Cache Contents',
wx.YES_NO | wx.ICON_QUESTION | wx.NO_DEFAULT)
yesno = dlg.ShowModal()
dlg.Destroy()
if yesno == wx.ID_YES:
pleco.PageCache().EmptyCache()
dlg = wx.MessageDialog(self, 'Cache has been emptied successfully!',
'Success',
wx.OK | wx.ICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy()
def ExportToExcel(self, evt):
def GenerateSheet(self, excelfile, arguments):
pleco.SetXlsFilename(excelfile)
pleco.run(arguments)
SaveSheet(excelfile)
#export completed successfully:
self.exporting = False
self.EnableAllControls()
self.exportbutton.SetLabel("EXPORT TO EXCEL")
def SaveSheet(excelfile):
wildcard = "Excel File (*.xls)|*.xls|"\
"All files (*.*)|*.*"
dlg = wx.FileDialog( self, message="Save excel file as ...",
defaultDir=os.getcwd(), defaultFile="pleco.xls", wildcard=wildcard,
style=wx.SAVE)
if dlg.ShowModal() == wx.ID_OK:
savefilename = dlg.GetPath()
try: #check if the selected file already exists
os.stat(savefilename)
fileexists=True
except:
fileexists=False
finally:
if fileexists:
dlg = wx.MessageDialog(self,
u'File you have selected already exists. Do you want to replace it?',
u'Confirm Save',
wx.YES_NO | wx.ICON_QUESTION | wx.NO_DEFAULT)
yesno = dlg.ShowModal()
dlg.Destroy()
if yesno != wx.ID_YES:
return SaveSheet(excelfile)
else:
shutil.copyfile(excelfile, savefilename)
ShowSuccess()
else:
shutil.copyfile(excelfile, savefilename)
ShowSuccess()
def ShowSuccess():
dlg = wx.MessageDialog(self, 'Export to excel completed succesfully!',
'Success',
wx.OK | wx.ICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy()
def ExportExcel(self):
dlg = wx.MultiChoiceDialog( self,
"Select stock exchanges to be exported",
"Export To Excel", self.stockexchangelist)
dlg.SetSelections(range(0,len(self.stockexchangelist)))#by default select all
if (dlg.ShowModal() == wx.ID_OK):
self.exporting = True
self.exportbutton.SetLabel("CANCEL EXPORT")
self.DisableAllControlsExcept((self.exportbutton, self.showlogbutton))
selections = dlg.GetSelections()
mylist=[]
for i in selections:
mylist.append(self.stockexchangelist[i])
excelfile = tempfile.NamedTemporaryFile(suffix=".xls").name
arguments = [None,"--excelexport@"+",".join(mylist)]
thread.start_new_thread(GenerateSheet,(self, excelfile,arguments))
else:
pass
dlg.Destroy()
if not self.exporting:
ExportExcel(self)
else:
dlg = wx.MessageDialog(self, 'Are you sure? If yes, the application will close.',
'Cancel Export?',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION
)
if dlg.ShowModal() == wx.ID_YES:
print >> sys.stderr, "Export terminated upon user request."
self.exporting = False
self.exportbutton.SetLabel("EXPORT TO EXCEL")
self.EnableAllControls()
self.parent.Close()
else:
dlg.Destroy()
def StandardOutput(self, text):
self.log.append(text)
self.SetStatusText(text)
def StandardErr(self, text):
self.StandardOutput(text)
def SetStatusText(self, text):
self.parent.SetStatusText(text,0)
def GetCommandlineArguments(self):
argumentlist = [None]
for stockexchange in self.stockexchangelist:
selection = self.stockboxes[stockexchange].GetSelection()
if "Company" in selection:
argumentlist.append("--companies@%s"%stockexchange)
if "Financials" in selection:
argumentlist.append("--financials@%s"%stockexchange)
if "Prices" in selection:
argumentlist.append("--prices@%s"%stockexchange)
if "Extra Info" in selection:
argumentlist.append("--extra@%s"%stockexchange)
return argumentlist
def DisableAllControlsExcept(self,excludedcontrols):
controlstobedisabled = self.controls - set(excludedcontrols)
for control in controlstobedisabled:
control.Disable()
def EnableAllControls(self):
for control in self.controls:
control.Enable()
def UpdateButtonClicked(self, evt):
def Update(self, updatebutton):
self.updating = True
try:
pleco.run(self.GetCommandlineArguments())
#Updating Completed Successfully:
self.updating = False
except StandardError, e:
#Update failed
self.updating = False
print >> sys.stderr, e
print >> sys.stderr, 'An error occurred! Update did not complete successfully.'
dlg = wx.MessageDialog(self, 'An error occurred! Update did not'\
' complete successfully and application will close.'\
' If problem persists please consult to programmer'\
' with application id number = 1127 via skype account: emre062011.\n\nLast log entries:\n%s'%"\n".join(self.log[-10:]),
'Failure',
wx.OK | wx.ICON_INFORMATION
)
if dlg.ShowModal() == wx.ID_OK:
self.parent.Close()
dlg.Destroy()
return
updatebutton.SetLabel("Update")
self.EnableAllControls()
print >> sys.stdout, 'Update completed succesfully!'
dlg = wx.MessageDialog(self, 'Update completed succesfully!',
'Success',
wx.OK | wx.ICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy()
if not self.updating:
updatebutton = evt.GetEventObject()
thread.start_new_thread(Update,(self, updatebutton))
updatebutton.SetLabel("CANCEL UPDATE")
self.DisableAllControlsExcept((self.updatebutton, self.showlogbutton))
else:
dlg = wx.MessageDialog(self, 'Are you sure? If yes, the application will close.',
'Cancel Update?',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION
)
if dlg.ShowModal() == wx.ID_YES:
print >> sys.stderr, "Update terminated upon user request."
self.updating = False
evt.GetEventObject().SetLabel("Update")
self.EnableAllControls()
self.parent.Close()
else:
dlg.Destroy()
def ShowLogClicked(self, evt):
msg = "\n".join(self.log)
dlg = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "Pleco output log")
dlg.ShowModal()
dlg.Destroy()
if __name__ == "__main__":
app = wx.App(False)
frame = PlecoFrame(None)
frame.Show()
app.MainLoop()