Merge pull request #7 from aglebov/remove-repeater

Use threading.Timer instead of the custom-written Repeater class
This commit is contained in:
Correl Roush 2013-04-11 18:33:04 -07:00
commit 9434434e57
2 changed files with 20 additions and 51 deletions

View file

@ -3,10 +3,10 @@
import sys import sys
import base64 import base64
import threading
import xbmc import xbmc
import xbmcgui import xbmcgui
from basictypes.bytes import Bytes from basictypes.bytes import Bytes
from repeater import Repeater
import search import search
_ = sys.modules[ "__main__" ].__language__ _ = sys.modules[ "__main__" ].__language__
@ -19,11 +19,13 @@ KEY_MENU_ID = 92
EXIT_SCRIPT = ( 6, 10, 247, 275, 61467, 216, 257, 61448, ) EXIT_SCRIPT = ( 6, 10, 247, 275, 61467, 216, 257, 61448, )
CANCEL_DIALOG = EXIT_SCRIPT + ( 216, 257, 61448, ) CANCEL_DIALOG = EXIT_SCRIPT + ( 216, 257, 61448, )
UPDATE_INTERVAL = 1.0
class TransmissionGUI(xbmcgui.WindowXMLDialog): class TransmissionGUI(xbmcgui.WindowXMLDialog):
def __init__(self, strXMLname, strFallbackPath, strDefaultName, bforeFallback=0): def __init__(self, strXMLname, strFallbackPath, strDefaultName, bforeFallback=0):
self.list = {} self.list = {}
self.torrents = {} self.torrents = {}
self.repeater = None self.timer = None
def onInit(self): def onInit(self):
p = xbmcgui.DialogProgress() p = xbmcgui.DialogProgress()
p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission' p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission'
@ -63,8 +65,8 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
return False return False
p.close() p.close()
self.updateTorrents() self.updateTorrents()
self.repeater = Repeater(1.0, self.updateTorrents) self.timer = threading.Timer(UPDATE_INTERVAL, self.updateTorrents)
self.repeater.start() self.timer.start()
def updateTorrents(self): def updateTorrents(self):
list = self.getControl(120) list = self.getControl(120)
torrents = self.transmission.info() torrents = self.transmission.info()
@ -103,6 +105,9 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
list.addItem(item) list.addItem(item)
list.setEnabled(bool(torrents)) list.setEnabled(bool(torrents))
# Update again, after an interval
self.timer = threading.Timer(UPDATE_INTERVAL, self.updateTorrents)
self.timer.start()
def onClick(self, controlID): def onClick(self, controlID):
list = self.getControl(120) list = self.getControl(120)
if (controlID == 111): if (controlID == 111):
@ -190,8 +195,8 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
if ( action.getButtonCode() in CANCEL_DIALOG ) or (action.getId() == KEY_MENU_ID): if ( action.getButtonCode() in CANCEL_DIALOG ) or (action.getId() == KEY_MENU_ID):
self.close() self.close()
def close(self): def close(self):
if self.repeater: if self.timer:
self.repeater.stop() self.timer.cancel()
super(TransmissionGUI, self).close() super(TransmissionGUI, self).close()
@ -200,11 +205,12 @@ class TorrentInfoGUI(xbmcgui.WindowXMLDialog):
self.transmission = None self.transmission = None
self.torrent_id = None self.torrent_id = None
self.list = {} self.list = {}
self.repeater = Repeater(1.0, self.updateTorrent) self.timer = None
def setTorrent(self, transmission, t_id): def setTorrent(self, transmission, t_id):
self.transmission = transmission self.transmission = transmission
self.torrent_id = t_id self.torrent_id = t_id
self.repeater.start() self.timer = threading.Timer(UPDATE_INTERVAL, self.updateTorrent)
self.timer.start()
def updateTorrent(self): def updateTorrent(self):
pbar = self.getControl(219) pbar = self.getControl(219)
list = self.getControl(220) list = self.getControl(220)
@ -235,10 +241,15 @@ class TorrentInfoGUI(xbmcgui.WindowXMLDialog):
# Update existing list item # Update existing list item
l = self.list[i] l = self.list[i]
l.setProperty('Progress', '[%3d%%]' % (file['completed'] * 100 / file['size'])) l.setProperty('Progress', '[%3d%%]' % (file['completed'] * 100 / file['size']))
# Update again, after an interval
self.timer = threading.Timer(UPDATE_INTERVAL, self.updateTorrent)
self.timer.start()
def onInit(self): def onInit(self):
self.updateTorrent() self.updateTorrent()
def close(self): def close(self):
self.repeater.stop() if self.timer:
self.timer.cancel()
super(TorrentInfoGUI, self).close() super(TorrentInfoGUI, self).close()
def onAction(self, action): def onAction(self, action):
if (action.getButtonCode() in CANCEL_DIALOG) or (action.getId() == KEY_MENU_ID): if (action.getButtonCode() in CANCEL_DIALOG) or (action.getId() == KEY_MENU_ID):

View file

@ -1,42 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2010 Correl J. Roush
import threading
import time
class Repeater:
def __init__(self, interval, action, arguments = []):
self.interval = interval
self.action = action
self.arguments = arguments
self.event = None
def start(self):
if self.event:
return
self.event = threading.Event()
self.thread = threading.Thread(target=Repeater.repeat, args=(self.event, self.interval, self.action, self.arguments))
self.thread.start()
def stop(self):
if not self.event:
return
self.event.set()
self.thread.join()
self.event = None
def repeat(cls, event, interval, action, arguments = []):
while True:
event.wait(interval)
if event.isSet():
break;
action(*arguments)
repeat = classmethod(repeat)
if __name__ == '__main__':
def foo(a, b):
print a, b
r = Repeater(1.0, foo, ['foo', 'bar'])
r.start()
time.sleep(10)
r.stop()