Skip to content

Commit

Permalink
Rewriting to move a lot of stuff into the new App class
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Sugden committed Dec 7, 2009
1 parent 620d688 commit 0350e1a
Showing 1 changed file with 54 additions and 58 deletions.
112 changes: 54 additions & 58 deletions wrtc.py
Expand Up @@ -30,22 +30,25 @@ def make_hash(tdata):
return sha1(bencode(bdecode(tdata)['info'])).hexdigest().upper()

class SettingsManager():
def __init__(self, main_window, defaults={'rtorrent url': 'http://localhost/RPC2', 'remote root': '/'}, config_path=None, load=True):
def __init__(self, main_window, defaults={'rtorrent url': 'http://localhost/RPC2', 'remote root': '/'}, config_path=None):
self.main_window = main_window
self.settings = ConfigParser(defaults)
if load:
if not config_path:
self.config_path = self.get_default_config_path()
self.settings.read(self.config_path)
if not config_path:
config_path = self.get_default_config_path()
if not os.path.is_file(config_path):
self.show_dialog()
else:
self.settings.read(config_path)
self.config_path = config_path


def get_default_config_path(self):
if os.name == 'nt':
config_path = os.path.expanduser("~\AppData\Local\wrtc.rc")
return os.path.expanduser("~\AppData\Local\wrtc.rc")
else:
config_path = os.path.expanduser("~/.config/wrtc.rc")
return config_path
return os.path.expanduser("~/.config/wrtc.rc")

def show_dialog(self, evt):
def show_dialog(self, evt=None):
self.dlg = wx.Dialog(self.main_window, title="Settings")
sizer = wx.FlexGridSizer(4,2,0,10)
sizer.SetFlexibleDirection(wx.HORIZONTAL)
Expand Down Expand Up @@ -89,15 +92,20 @@ def save(self, evt):

class wrtcApp(wx.App):
def __init__(self, *args, **kwargs):
self.settings_manager = SettingsManager(self)
self.daemon = XMLRPCDaemon(self.settings_manager.settings.get("DEFAULT", "rTorrent URL"))
self.daemon.start()
wx.App.__init__(self, *args, **kwargs)
self.refresher_thread = UpdateScheduler(self)
self.refresher_thread.start()
self.Bind(wx.EVT_ACTIVATE_APP, self.activate)

def OnInit(self):
self.frame = MainWindow(None, wx.ID_ANY, NAME_OF_THIS_APP+" - wxPython rTorrent client")
self.frame.Show()
import sys
if len(sys.argv) > 1 and os.path.is_file(sys.argv[1]):
self.frame.load_torrent(sys.argv[1])
self.load_torrent(sys.argv[1])
return True

def raise_frame(self):
Expand All @@ -112,33 +120,50 @@ def activate(self, evt):
evt.Skip()

def MacOpenFile(self, filename):
self.frame.load_torrent(filename=filename)
self.load_torrent(filename=filename)

def MacReopenApp(self):
self.raise_frame()

def load_torrent(self,e=None,filename=None):
dlg = LoadTorrentDialog(self)
if filename:
dlg.filepath.SetValue(filename)
if dlg.ShowModal() == wx.ID_OK:
self.send_torrent(dlg)
dlg.Destroy()

def send_torrent(self, dlg):
start = dlg.start_immediate.GetValue()
dest = dlg.browser.GetPyData(dlg.browser.GetSelection())['path']
if dlg.filepath.GetValue() != '':
torrent_file = open(dlg.filepath.GetValue(),'rb')
elif dlg.url.GetValue() != '':
import urllib2
torrent_file = urllib2.urlopen(dlg.url.GetValue())
torrent_data = torrent_file.read()
torrent_file.close()
infohash = make_hash(torrent_data)
torrent_data = xmlrpcdaemon.Binary(torrent_data)
def dest_callback(rv):
def start_callback(rv):
if start:
self.daemon.jobs.append(('d.start', infohash))
self.job_queue.append(('d.set_directory', (infohash, dest),
start_callback))
self.daemon.jobs.append(('load_raw', torrent_data, dest_callback))

class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600,600))
self.settings_manager = SettingsManager(self)
self.daemon = XMLRPCDaemon(self.settings_manager.settings.get("DEFAULT", "rTorrent URL"))
self.job_queue = self.daemon.jobs
self.daemon.start()
self.create_interface()
self.refresher_thread = UpdateScheduler(self.notebook)
self.refresher_thread.start()
self.Bind(wx.EVT_CLOSE, self.on_exit)

def create_interface(self):
self.menu_bar = wx.MenuBar()

self.file_menu = wx.Menu()
self.file_menu.Append(wx.ID_OPEN, "Add &Torrent")
self.Bind(wx.EVT_MENU, self.load_torrent, id=wx.ID_OPEN)
self.Bind(wx.EVT_MENU, wx.GetApp().load_torrent, id=wx.ID_OPEN)
self.file_menu.Append(wx.ID_PREFERENCES, "&Preferences")
self.Bind(wx.EVT_MENU, self.settings_manager.show_dialog, id=wx.ID_PREFERENCES)
self.Bind(wx.EVT_MENU, wx.GetApp().settings_manager.show_dialog, id=wx.ID_PREFERENCES)
self.file_menu.Append(wx.ID_EXIT, "&Quit")
self.Bind(wx.EVT_MENU, self.on_exit, id=wx.ID_EXIT)
self.menu_bar.Append(self.file_menu, "&File")
Expand All @@ -154,6 +179,7 @@ def create_interface(self):
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.notebook, 1, wx.EXPAND | wx.ALL, 10)
self.SetSizer(main_sizer)
self.Bind(wx.EVT_CLOSE, self.on_exit)
# Icons = {}
# Icons['play'] = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_TOOLBAR)
# Icons['pause'] = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR)
Expand All @@ -165,37 +191,6 @@ def on_about_request(self, evt):
dlg = wx.MessageDialog(self, "wxPython rTorrent client", NAME_OF_THIS_APP, wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()

def load_torrent(self,e=None,filename=None):
dlg = LoadTorrentDialog(self)
if filename:
dlg.filepath.SetValue(filename)
if dlg.ShowModal() == wx.ID_OK:
self.send_torrent(dlg)
dlg.Destroy()

def send_torrent(self, dlg):
start = dlg.start_immediate.GetValue()
dest = dlg.browser.GetPyData(dlg.browser.GetSelection())['path']
if dlg.filepath.GetValue() != '':
torrent_file = open(dlg.filepath.GetValue(),'rb')
elif dlg.url.GetValue() != '':
import urllib2
torrent_file = urllib2.urlopen(dlg.url.GetValue())
torrent_data = torrent_file.read()
torrent_file.close()
infohash = make_hash(torrent_data)
torrent_data = xmlrpcdaemon.Binary(torrent_data)
def dest_callback(rv):
print 'Hit dest_callback', infohash, dest, start
def start_callback(rv):
print 'Hit start_callback', infohash, dest, start
if start:
self.job_queue.append(('d.start', infohash))
print 'adding job to queue'
self.job_queue.append(('d.set_directory', (infohash, dest),
start_callback))
self.job_queue.append(('load_raw', torrent_data, dest_callback))

def on_exit(self,e):
self.daemon.proceed = False
self.refresher_thread.proceed = False
Expand Down Expand Up @@ -224,7 +219,7 @@ def page_changed(self, evt=None):
page = self.GetPage(evt.GetSelection())
else:
page = self.GetCurrentPage()
queue = self.GetTopLevelParent().job_queue
queue = wx.GetApp().daemon.jobs
queue.clear()
queue.append(('download_list', page.title, page.set_list ))

Expand Down Expand Up @@ -304,6 +299,7 @@ def __init__(self, parent, title="default"):
else:
self.setResizeColumn(0)
self.joblist = MultiQueue()
self.joblist.put(0, ("download_list", self.title, self.set_list))
self.joblist.put(5, ("download_list", self.title, self.set_list))

def __repr__(self):
Expand Down Expand Up @@ -362,10 +358,10 @@ def on_erase(self, e):
class UpdateScheduler(threading.Thread):
''' This thread reads the joblist for the current view,
and queues up jobs at an appropriate frequency '''
def __init__(self, notebook):
def __init__(self, app):
threading.Thread.__init__(self)
self.notebook = notebook
self.remote_queue = notebook.GetTopLevelParent().job_queue
self.notebook = app.frame.notebook
self.remote_queue = app.daemon.jobs
self.proceed = True

def run(self):
Expand Down Expand Up @@ -434,5 +430,5 @@ def on_browse(self,e):
dlg.Destroy()

if __name__ == "__main__":
app = wrtcApp()
app = wrtcApp(False)
app.MainLoop()

0 comments on commit 0350e1a

Please sign in to comment.