Attachment 'DownloadProgress.py'

Download

   1 #!/usr/bin/python
   2 
   3 import wx, wx.py
   4 import wx.lib.scrolledpanel as scrolledpanel
   5 import urllib2, cStringIO, time, random
   6 
   7 appopts = {}
   8 
   9 class MainFrame(wx.Frame):
  10     pass
  11 
  12 class ProgressPanelContainer(scrolledpanel.ScrolledPanel):
  13     def __init__(self, *args, **kwargs):
  14 	scrolledpanel.ScrolledPanel.__init__(self, *args, **kwargs)
  15 	self.sizer = wx.BoxSizer(wx.VERTICAL)
  16 	self.SetSizer(self.sizer)
  17 	self.SetAutoLayout(True)
  18 	self.SetupScrolling()
  19 
  20 class ProgressPanel(wx.Panel):
  21     def __init__(self, *args, **kwargs):
  22 	wx.Panel.__init__(self, *args, **kwargs)
  23 
  24 	# variables
  25 	self.downloading = False
  26 	self.sizer = wx.GridBagSizer(3, 6)
  27 	self.SetSizer(self.sizer)
  28 	self.SetAutoLayout(True)
  29 
  30 	# progress bar
  31 	self.progress = wx.Gauge(self)
  32 	self.sizer.Add(self.progress, (0,0), (1,6), flag=wx.EXPAND)
  33 
  34 	# download label
  35 	self.download_label = wx.StaticText(self, label="Downloading:", style=wx.ALIGN_RIGHT)
  36 	self.sizer.Add(self.download_label, (1,0), (1,2), flag=wx.EXPAND)
  37 
  38 	# url area
  39 	self.url_label = wx.StaticText(self, label="None")
  40 	self.sizer.Add(self.url_label, (1,2), (1,2), flag= wx.EXPAND)
  41 
  42 	# view image button
  43 	self.view_image_btn = wx.Button(self, label="View &Image")
  44 	self.view_image_btn.Disable()
  45 	self.sizer.Add(self.view_image_btn, (2,0))
  46 
  47 	# stop button
  48 	self.stop_btn = wx.Button(self, label="&Stop")
  49 	self.sizer.Add(self.stop_btn, (2,1))
  50 
  51 	# X button
  52 	self.x_btn = wx.Button(self, label="&Download Porn!")
  53 	self.sizer.Add(self.x_btn, (2,2), flag=wx.EXPAND)
  54 
  55 	# size label
  56 	self.size_lbl = wx.StaticText(self, label="Size = 0")
  57 	self.sizer.Add(self.size_lbl, (2,3), flag=wx.EXPAND)
  58 
  59 	# set events
  60 	self.stop_btn.Bind(wx.EVT_BUTTON, self.OnClickStop)
  61 	self.x_btn.Bind(wx.EVT_BUTTON, self.OnClickX)
  62 
  63         # set growable row and column
  64         self.sizer.AddGrowableRow(0)
  65         self.sizer.AddGrowableCol(2)
  66 
  67     def OnClickStop(self, event):
  68 	if self.stop_btn.GetLabel() == "Stop":
  69 	    self.stop_btn.SetLabel("&Resume")
  70 	else:
  71 	    self.stop_btn.SetLabel("&Stop")
  72 
  73     def Slurp(self, *arg, **kwargs):
  74         """Slurp up the image in the background via Future Calls"""
  75 	try:
  76 	    if self.downloading:
  77 		data = self.socket.read(4096)
  78 		amount_read = len(data)
  79 		if data:
  80 		    self.data.write(data)
  81 		    self.amount += amount_read
  82 		    self.progress.SetValue(int((float(self.amount) / self.content_length) * 100))
  83 		    self.future_call.Restart(10)
  84 		else:
  85                     self.data.seek(0)
  86                     self._clean()
  87 		    image = wx.ImageFromStream(self.data)
  88 		    size = (image.GetWidth(), image.GetHeight())
  89 		    bmp = wx.BitmapFromImage(image)
  90 		    frame = wx.Frame(self, title="Yowza!", size=size)
  91 		    wx.StaticBitmap(frame, bitmap=bmp)
  92 		    frame.Show(True)
  93 	except IOError, e:
  94             self._clean()
  95             self.Complain(e)
  96 
  97     def _clean(self):
  98         self.progress.SetValue(0)
  99         self.downloading = False
 100         self.amount = 0
 101         self.size_lbl.SetLabel("Size = 0")
 102         self.url_label.SetLabel("None")
 103 
 104     def Complain(self, message):
 105         dlg = wx.MessageDialog(self, style=wx.OK,
 106                                message="I can'a do it captin'!\n%s" % str(message),
 107                                caption="Trouble!")
 108         dlg.ShowModal()
 109         dlg.Destroy()
 110 
 111 	
 112     def OnClickX(self, event):
 113 	"""Starts a not-really-but-sort-of asynchronous network download"""
 114 	if self.downloading: return
 115 	else:
 116 	    date = time.strftime("%y%m%d", (6,6,random.randint(1,30),1,1,1,1,1,1))
 117             url = 'http://playboy.com/sex/day/%s/imx/motd.jpg' % date
 118             self.url_label.SetLabel(url)
 119             try: self.socket = urllib2.urlopen(url)
 120             except IOError, e:
 121                 self._clean()
 122                 self.Complain(e)
 123                 return
 124 	    self.downloading = True
 125 	    info = self.socket.info()
 126 	    self.content_length = int(info['content-length'])
 127 	    self.size_lbl.SetLabel("Size = %d" % self.content_length)
 128 	    self.data = cStringIO.StringIO()
 129 	    self.amount = 0
 130 	    self.future_call = wx.FutureCall(20, self.Slurp, "test")
 131 
 132 
 133 def main():
 134     app = wx.App()
 135     frame = wx.Frame(None, size=(729, 260),
 136                      title="Gallery Downloader")
 137     notebook = wx.Notebook(frame)
 138 
 139     shell = wx.py.shell.Shell(notebook, size=(700,100))
 140     notebook.AddPage(shell, "Interactive Prompt")
 141     
 142     progress = ProgressPanelContainer(notebook)
 143     notebook.AddPage(progress, "Progress")
 144     progress_panel1 = ProgressPanel(progress)
 145     progress.sizer.Add(progress_panel1, flag=wx.EXPAND)
 146 
 147     global appopts
 148     appopts['frame'] = frame
 149     appopts['progress1'] = progress_panel1
 150     frame.CenterOnScreen()
 151     frame.Show(True)
 152     shell.SetFocus()
 153     app.MainLoop()
 154     app.Exit()
 155 
 156 if __name__ == '__main__':
 157     main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:03, 4.6 KB) [[attachment:DownloadProgress.py]]
  • [get | view] (2009-10-04 09:16:03, 8.6 KB) [[attachment:Sam2.jpg]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.