import os.path, sys # load libs needed for loading the icon from .exe resource if sys.platform == 'win32': import win32api, win32con, StringIO, binascii from wxPython.wx import * from wx.grid import * import BitTornado.bencode ID_ABOUT=101 ID_OPEN=102 ID_SAVE=103 ID_SAVEAS=104 ID_EXIT=110 ID_BUTTON_PRIO_HIGH=201 ID_BUTTON_PRIO_LOW=202 ID_BUTTON_PRIO_LOWEST=203 ID_BUTTON_PRIO_NOT=204 ID_BUTTON_CCMDL=205 def niceFileSize (fsize): descriptions = ('bytes', 'KiB', 'MiB', 'GiB') for desc in descriptions: newfsize = fsize / 1024 if (newfsize < 1): return '%.2f %s' % (0.0 + fsize, desc) fsize = 0.0 + newfsize class MainWindow(wxFrame): def __init__(self,parent,id,filename='noname.txt'): self.filename=filename self.dirname="." wxFrame.__init__(self,parent,-4, "BitTornado-helper", size = (800,400), style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE) # from resource within exe if sys.platform == 'win32': ICON_HEADER = binascii.unhexlify('00 00 01 00 01 00 20 20 00 00 00 00 00 00 A8 08 00 00 16 00 00 00'.replace(' ', '')) # load icon that has an id of 1 icon_data = win32api.LoadResource(0, win32con.RT_ICON, 1) stream = StringIO.StringIO(ICON_HEADER + icon_data) img = wxImageFromStream(stream, wxBITMAP_TYPE_ICO) bmp = img.ConvertToBitmap() # desperate attempt to get an empty icon icon = wxEmptyIcon() icon.CopyFromBitmap(bmp) # set title bar icon to use this one self.SetIcon(icon) # wxPython 2.4 """ _icon = wxEmptyIcon() _icon.CopyFromBitmap(wxBitmap("favicon.ico", wxBITMAP_TYPE_ANY)) self.SetIcon(_icon) """ # wxPython 2.5 and up """ _icon = Icon('favicon.ico', wx.BITMAP_TYPE_ICO) self.SetIcon(_icon) """ self.sizer_btn = wxBoxSizer(wxHORIZONTAL) self.sizer_output = wxBoxSizer(wxHORIZONTAL) self.btnPrioHigh = wxButton(self, ID_BUTTON_PRIO_HIGH, "Set high Prio") EVT_BUTTON(self, ID_BUTTON_PRIO_HIGH, self.OnPrioHighClick) self.sizer_btn.Add(self.btnPrioHigh,1,wxEXPAND) self.btnPrioNormal = wxButton(self, ID_BUTTON_PRIO_LOW, "Set normal Prio") EVT_BUTTON(self, ID_BUTTON_PRIO_LOW, self.OnPrioNormalClick) self.sizer_btn.Add(self.btnPrioNormal,1,wxEXPAND) self.btnPrioLowest = wxButton(self, ID_BUTTON_PRIO_LOWEST, "Set lowest Prio") EVT_BUTTON(self, ID_BUTTON_PRIO_LOWEST, self.OnPrioLowestClick) self.sizer_btn.Add(self.btnPrioLowest,1,wxEXPAND) self.btnPrioNot = wxButton(self, ID_BUTTON_PRIO_NOT, "don't download") EVT_BUTTON(self, ID_BUTTON_PRIO_NOT, self.OnPrioNotClick) self.sizer_btn.Add(self.btnPrioNot,1,wxEXPAND) self.btnCreateCmdLine = wxButton(self, ID_BUTTON_CCMDL, "create commandline") EVT_BUTTON(self, ID_BUTTON_CCMDL, self.OnCreateCmdLine) self.sizer_output.Add(self.btnCreateCmdLine,1,wxEXPAND) self.textfield = wxTextCtrl(self, 1, style=wxTE_MULTILINE) self.sizer_output.Add(self.textfield,1,wxEXPAND) # Create a wxGrid object self.grid = Grid(self, -1) #wxGrid(self, -1, wxPoint( 0, 0 ), wxSize( 400, 300 ) ); # setup selected rows tracker self.currentSelection = [] EVT_GRID_RANGE_SELECT( self.grid, self._OnSelectedRange ) EVT_GRID_SELECT_CELL( self.grid, self._OnSelectedCell ) #Then we call CreateGrid to set the dimensions of the grid #(100 rows and 10 columns in this example) self.grid.CreateGrid( 0, 3 ); self.grid.SetColLabelValue(0, "Priority"); self.grid.SetColLabelValue(1, "Filename"); self.grid.SetColLabelValue(2, "Filesize"); self.sb = self.CreateStatusBar(3) # A Statusbar in the bottom of the window self.sb.SetStatusWidths([-1, 150, 150]) self.sb.SetStatusText("Files: ", 1) self.sb.SetStatusText("Total: ", 2) # LAYOUT # Use some sizers to see layout options self.sizer=wxBoxSizer(wxVERTICAL) self.sizer.Add(self.grid,1,wxEXPAND) self.sizer.Add(self.sizer_btn,0,wxEXPAND) self.sizer.Add(self.sizer_output,0,wxEXPAND) #Layout sizers self.SetSizer(self.sizer) self.SetAutoLayout(1) #self.sizer.Fit(self) # Setting up the menu. filemenu= wxMenu() filemenu.Append(ID_ABOUT, "&About"," Information about this program") filemenu.Append(ID_OPEN,"&Open"," Open a new file ") #filemenu.Append(ID_SAVE,"&Save"," Save the current file") #filemenu.Append(ID_SAVEAS,"Save &As"," Save the file under a different name") filemenu.AppendSeparator() filemenu.Append(ID_EXIT,"E&xit"," Terminate the program") # Creating the menubar. menuBar = wxMenuBar() menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. EVT_MENU(self, ID_ABOUT, self.OnAbout) # attach the menu-event ID_ABOUT to the method self.OnAbout EVT_MENU(self, ID_EXIT, self.OnExit) # attach the menu-event ID_EXIT to the method self.OnExit EVT_MENU(self, ID_OPEN, self.OnOpen) self.Show(true) def _OnSelectedRange( self, event ): """Internal update to the selection tracking list""" if event.Selecting(): # adding to the list... for index in range( event.GetTopRow(), event.GetBottomRow()+1): if index not in self.currentSelection: self.currentSelection.append( index ) else: # removal from list for index in range( event.GetTopRow(), event.GetBottomRow()+1): while index in self.currentSelection: self.currentSelection.remove( index ) #self.ConfigureForSelection() event.Skip() def _OnSelectedCell( self, event ): """Internal update to the selection tracking list""" self.currentSelection = [ event.GetRow() ] #self.ConfigureForSelection() event.Skip() def OnAbout(self,e): d= wxMessageDialog( self, " BitTornado --priority commandline generator\n\n created by HTD in 2004 using wxPython","About bt-helper", wxOK) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished. def OnExit(self,e): self.Close(true) # Close the frame. def OnOpen(self,e): """ Open a file""" dlg = wxFileDialog(self, "Choose a file", self.dirname, "", "Bittorrent Files|*.torrent|All files|*.*", wxOPEN) if dlg.ShowModal() == wxID_OK: self.filename=dlg.GetFilename() self.dirname=dlg.GetDirectory() else: # pressed "Cancel" or sth. else return fullpath = os.path.join(self.dirname,self.filename) if not os.path.exists(fullpath): print "file '%s' not found!" % (fullpath) return # clear gridview if (self.grid.GetNumberRows() > 0): self.grid.DeleteRows(0, self.grid.GetNumberRows()) self.grid.SetSelectionMode(Grid.wxGridSelectRows) self.readDotTorrent(fullpath) self.grid.ClearSelection() dlg.Destroy() # --- BUTTONS def OnCreateCmdLine(self, event): cmdline = 'btdownloadcurses.py --max_upload_rate 8 --priority ' prios = '' for row in range (0, self.grid.GetNumberRows()): prios += self.grid.GetCellValue(row, 0) + ',' cmdline += prios[:-1] + " '" + self.filename + "'" self.textfield.SetValue (cmdline); def OnPrioNotClick(self, event): for row in self.currentSelection: self.grid.SetCellValue (row, 0, '-1') def OnPrioHighClick(self, event): for row in self.currentSelection: self.grid.SetCellValue (row, 0, '0') def OnPrioNormalClick(self, event): for row in self.currentSelection: self.grid.SetCellValue (row, 0, '1') def OnPrioLowestClick(self, event): for row in self.currentSelection: self.grid.SetCellValue (row, 0, '2') #print " Click on object with Id %d\n" % event.GetId() def readDotTorrent (self, metainfo_name): metainfo_file = open(metainfo_name, 'rb') metainfo = BitTornado.bencode.bdecode(metainfo_file.read()) # print metainfo info = metainfo['info'] retval = '' total_torrent_size = 0 if info.has_key('length'): # let's assume we just have a file retval = 'file name.....: %s' % info['name'] total_torrent_size = info['length'] self.appendFile (info['name'], info['length']) else: # let's assume we have a directory structure retval = "directory name: %s\n" % info['name'] retval = retval + 'files.........: ' for file in info['files']: path = '' for item in file['path']: if (path != ''): path = path + "/" path = path + item retval = retval + " %s (%d)\n" % (path, file['length']) total_torrent_size += file['length'] self.appendFile (path, file['length']) self.grid.AutoSizeColumn(1) self.sb.SetStatusText("Files: %d" % self.grid.GetNumberRows(), 1) self.sb.SetStatusText("Total: " + niceFileSize(total_torrent_size), 2) return retval def appendFile (self, filename, filesize): self.grid.AppendRows (1) curRow = self.grid.GetNumberRows() -1 self.grid.SetCellValue (curRow, 0, "1") self.grid.SetCellValue (curRow, 1, filename) self.grid.SetCellAlignment (curRow, 2, wxALIGN_RIGHT, wxALIGN_CENTRE) self.grid.SetCellValue (curRow, 2, niceFileSize(filesize)) app = wxPySimpleApp() frame = MainWindow(None, -1) frame.Show(1) app.MainLoop()