From 0d76c92ebf0b4b4188b961e956ed187fc996b043 Mon Sep 17 00:00:00 2001 From: Vuolter Date: Sun, 12 May 2013 14:51:14 +0200 Subject: Version 0.3: rewrited code and added new feature "Sleep mode" --- module/plugins/hooks/DeleteFinished.py | 69 +++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/module/plugins/hooks/DeleteFinished.py b/module/plugins/hooks/DeleteFinished.py index be83d6ced..d5b4d3345 100644 --- a/module/plugins/hooks/DeleteFinished.py +++ b/module/plugins/hooks/DeleteFinished.py @@ -23,25 +23,66 @@ from time import time class DeleteFinished(Hook): __name__ = "DeleteFinished" - __version__ = "0.2" + __version__ = "0.3" __description__ = "Automatically delete finished packages from queue" __config__ = [ ("activated", "bool", "Activated", "False"), - ("interval", "int", "Delete every (hours)", "72") + ("interval", "int", "Delete every (hours)", "72"), + ("sleep", "bool", "Sleep mode (useful if interval is small)", "False") ] __author_name__ = ("Walter Purcaro") __author_mail__ = ("vuolter@gmail.com") - #: event_map don't load periodical anyway def periodical(self): - now = time() - deletetime = self.getConfig("interval") * 3600 + self.info["lastdelete"] - if now >= deletetime: - self.core.api.deleteFinished() - self.logDebug("called self.core.api.deleteFinished()") - self.info["lastdelete"] = now - - def setup(self): - now = time() - self.info = {"lastdelete": now} - self.interval = 3600 + # self.logDebug("self.periodical") + self.core.api.deleteFinished() + self.logDebug("called self.core.api.deleteFinished") + if self.getConfig("sleep"): + self.interval = 0 + + def addEvent(self, event, handler): + if event in self.manager.events: + if handler not in self.manager.events[event]: + self.manager.events[event].append(handler) + # self.logDebug("self.addEvent: " + event + ": added handler") + else: + # self.logDebug("self.addEvent: " + event + ": NOT added handler") + return False + else: + self.manager.events[event] = [handler] + # self.logDebug("self.addEvent: " + event + ": added event and handler") + return True + + def removeEvent(self, event, handler): + if event in self.manager.events and handler in self.manager.events[event]: + self.manager.events[event].remove(handler) + # self.logDebug("self.removeEvent: " + event + ": removed handler") + return True + else: + # self.logDebug("self.removeEvent: " + event + ": NOT removed handler") + return False + + def enPeriodical(self): + # self.logDebug("self.enPeriodical") + if not self.interval: + self.interval = self.getConfig("interval") * 3600 + + def configEvents(self, plugin, name, value): + # self.logDebug("self.configEvents") + self.enPeriodical() + if self.getConfig("sleep"): + self.addEvent("packageFinished", self.enPeriodical) + else: + self.removeEvent("packageFinished", self.enPeriodical) + + def unload(self): + # self.logDebug("self.unload") + self.removeEvent("pluginConfigChanged", self.configEvents) + self.removeEvent("packageFinished", self.enPeriodical) + self.interval = 0 + + def coreReady(self): + # self.logDebug("self.coreReady") + self.interval = 0 + self.addEvent("pluginConfigChanged", self.configEvents) + self.configEvents(None, None, None) -- cgit v1.2.3 From 8d8ff424c48345c8b2ad222cda58cd78a7de5282 Mon Sep 17 00:00:00 2001 From: Vuolter Date: Sun, 12 May 2013 21:36:56 +0200 Subject: Version 0.5: finished rewrite --- module/plugins/hooks/DeleteFinished.py | 42 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/module/plugins/hooks/DeleteFinished.py b/module/plugins/hooks/DeleteFinished.py index d5b4d3345..e0df69eef 100644 --- a/module/plugins/hooks/DeleteFinished.py +++ b/module/plugins/hooks/DeleteFinished.py @@ -23,22 +23,27 @@ from time import time class DeleteFinished(Hook): __name__ = "DeleteFinished" - __version__ = "0.3" + __version__ = "0.5" __description__ = "Automatically delete finished packages from queue" __config__ = [ ("activated", "bool", "Activated", "False"), - ("interval", "int", "Delete every (hours)", "72"), - ("sleep", "bool", "Sleep mode (useful if interval is small)", "False") + ("interval", "int", "Delete every (hours)", "72") ] __author_name__ = ("Walter Purcaro") __author_mail__ = ("vuolter@gmail.com") + def wakeup(self, pypack): + # self.logDebug("self.wakeup") + self.removeEvent("packageFinished", self.wakeup) + self.info["sleep"] = False + def periodical(self): # self.logDebug("self.periodical") - self.core.api.deleteFinished() - self.logDebug("called self.core.api.deleteFinished") - if self.getConfig("sleep"): - self.interval = 0 + if not self.info["sleep"]: + self.core.api.deleteFinished() + self.logDebug("called self.core.api.deleteFinished") + self.info["sleep"] = True + self.addEvent("packageFinished", self.wakeup) def addEvent(self, event, handler): if event in self.manager.events: @@ -62,27 +67,20 @@ class DeleteFinished(Hook): # self.logDebug("self.removeEvent: " + event + ": NOT removed handler") return False - def enPeriodical(self): - # self.logDebug("self.enPeriodical") - if not self.interval: - self.interval = self.getConfig("interval") * 3600 - - def configEvents(self, plugin, name, value): + def configEvents(self, plugin=None, name=None, value=None): # self.logDebug("self.configEvents") - self.enPeriodical() - if self.getConfig("sleep"): - self.addEvent("packageFinished", self.enPeriodical) - else: - self.removeEvent("packageFinished", self.enPeriodical) + interval = self.getConfig("interval") * 3600 + if interval != self.interval: + self.interval = interval def unload(self): # self.logDebug("self.unload") self.removeEvent("pluginConfigChanged", self.configEvents) - self.removeEvent("packageFinished", self.enPeriodical) - self.interval = 0 + self.removeEvent("packageFinished", self.wakeup) def coreReady(self): # self.logDebug("self.coreReady") - self.interval = 0 + self.info = {"sleep": True} self.addEvent("pluginConfigChanged", self.configEvents) - self.configEvents(None, None, None) + self.configEvents() + self.addEvent("packageFinished", self.wakeup) -- cgit v1.2.3