From 4d5e01abefc4f4e27f22b084d400c0b205ce9bb7 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sat, 12 Jul 2014 21:41:48 +0100 Subject: [PATCH] [#1290] [Execute] Add TorrentRemoved event --- .../Execute/deluge/plugins/execute/core.py | 44 +++++++++++++------ .../deluge/plugins/execute/data/execute.js | 6 ++- .../Execute/deluge/plugins/execute/gtkui.py | 5 ++- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/deluge/plugins/Execute/deluge/plugins/execute/core.py b/deluge/plugins/Execute/deluge/plugins/execute/core.py index dbd1b1a12..2fdfe5d0c 100644 --- a/deluge/plugins/Execute/deluge/plugins/execute/core.py +++ b/deluge/plugins/Execute/deluge/plugins/execute/core.py @@ -58,9 +58,11 @@ EXECUTE_COMMAND = 2 EVENT_MAP = { "complete": "TorrentFinishedEvent", - "added": "TorrentAddedEvent" + "added": "TorrentAddedEvent", + "removed": "TorrentRemovedEvent" } + class ExecuteCommandAddedEvent(DelugeEvent): """ Emitted when a new command is added. @@ -68,6 +70,7 @@ class ExecuteCommandAddedEvent(DelugeEvent): def __init__(self, command_id, event, command): self._args = [command_id, event, command] + class ExecuteCommandRemovedEvent(DelugeEvent): """ Emitted when a command is removed. @@ -75,11 +78,13 @@ class ExecuteCommandRemovedEvent(DelugeEvent): def __init__(self, command_id): self._args = [command_id] + class Core(CorePluginBase): def enable(self): self.config = ConfigManager("execute.conf", DEFAULT_CONFIG) event_manager = component.get("EventManager") self.registered_events = {} + self.preremoved_cache = {} # Go through the commands list and register event handlers for command in self.config["commands"]: @@ -93,43 +98,56 @@ class Core(CorePluginBase): return event_handler event_handler = create_event_handler(event) event_manager.register_event_handler(EVENT_MAP[event], event_handler) + if event == "removed": + event_manager.register_event_handler("PreTorrentRemovedEvent", self.on_preremoved) self.registered_events[event] = event_handler log.debug("Execute core plugin enabled!") - def execute_commands(self, torrent_id, event, *arg): + def on_preremoved(self, torrent_id): + # Get and store the torrent info before it is removed torrent = component.get("TorrentManager").torrents[torrent_id] info = torrent.get_status(["name", "download_location"]) + self.preremoved_cache[torrent_id] = [utf8_encoded(torrent_id), utf8_encoded(info["name"]), + utf8_encoded(info["download_location"])] - # Grab the torrent name and download location - # getProcessOutputAndValue requires args to be str - torrent_id = utf8_encoded(torrent_id) - torrent_name = utf8_encoded(info["name"]) + def execute_commands(self, torrent_id, event, *arg): if event == "added" and arg[0]: # No futher action as from_state (arg[0]) is True return + elif event == "removed": + torrent_id, torrent_name, download_location = self.preremoved_cache.pop(torrent_id) else: + torrent = component.get("TorrentManager").torrents[torrent_id] + info = torrent.get_status(["name", "download_location"]) + # Grab the torrent name and download location + # getProcessOutputAndValue requires args to be str + torrent_id = utf8_encoded(torrent_id) + torrent_name = utf8_encoded(info["name"]) download_location = utf8_encoded(info["download_location"]) - log.debug("[execute] Running commands for %s", event) + log.debug("Running commands for %s", event) def log_error(result, command): (stdout, stderr, exit_code) = result if exit_code: - log.warn("[execute] command '%s' failed with exit code %d", command, exit_code) + log.warn("Command '%s' failed with exit code %d", command, exit_code) if stdout: - log.warn("[execute] stdout: %s", stdout) + log.warn("stdout: %s", stdout) if stderr: - log.warn("[execute] stderr: %s", stderr) + log.warn("stderr: %s", stderr) # Go through and execute all the commands for command in self.config["commands"]: if command[EXECUTE_EVENT] == event: command = os.path.expandvars(command[EXECUTE_COMMAND]) command = os.path.expanduser(command) - log.debug("[execute] running %s", command) - d = getProcessOutputAndValue(command, (torrent_id, torrent_name, download_location), env=os.environ) - d.addCallback(log_error, command) + if os.path.isfile(command) and os.access(command, os.X_OK): + log.debug("Running %s", command) + d = getProcessOutputAndValue(command, (torrent_id, torrent_name, download_location), env=os.environ) + d.addCallback(log_error, command) + else: + log.error("Execute script not found or not executable") def disable(self): self.config.save() diff --git a/deluge/plugins/Execute/deluge/plugins/execute/data/execute.js b/deluge/plugins/Execute/deluge/plugins/execute/data/execute.js index 4d58f2b22..4e8a0c401 100644 --- a/deluge/plugins/Execute/deluge/plugins/execute/data/execute.js +++ b/deluge/plugins/Execute/deluge/plugins/execute/data/execute.js @@ -54,7 +54,8 @@ Deluge.ux.ExecuteWindowBase = Ext.extend(Ext.Window, { fields: ['id', 'text'], data: [ ['complete', _('Torrent Complete')], - ['added', _('Torrent Added')] + ['added', _('Torrent Added')], + ['removed', _('Torrent Removed')] ] }), name: 'event', @@ -152,7 +153,8 @@ Deluge.ux.preferences.ExecutePage = Ext.extend(Ext.Panel, { Deluge.ux.preferences.ExecutePage.superclass.initComponent.call(this); var event_map = this.event_map = { 'complete': _('Torrent Complete'), - 'added': _('Torrent Added') + 'added': _('Torrent Added'), + 'removed': _('Torrent Removed') } this.list = new Ext.list.ListView({ diff --git a/deluge/plugins/Execute/deluge/plugins/execute/gtkui.py b/deluge/plugins/Execute/deluge/plugins/execute/gtkui.py index 8b9e69c6d..1245869ff 100644 --- a/deluge/plugins/Execute/deluge/plugins/execute/gtkui.py +++ b/deluge/plugins/Execute/deluge/plugins/execute/gtkui.py @@ -52,10 +52,11 @@ EXECUTE_COMMAND = 2 EVENT_MAP = { "complete": _("Torrent Complete"), - "added": _("Torrent Added") + "added": _("Torrent Added"), + "removed": _("Torrent Removed") } -EVENTS = ["complete", "added"] +EVENTS = ["complete", "added", "removed"] class ExecutePreferences(object): def __init__(self, plugin):