Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label as a fourth argument to scripts called by Execute plugin #102

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions deluge/plugins/Execute/deluge/plugins/execute/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import os
import time
from collections import namedtuple

from twisted.internet.utils import getProcessOutputAndValue

Expand All @@ -37,6 +38,8 @@
"removed": "TorrentRemovedEvent"
}

ScriptArgs = namedtuple("ScriptArgs", ["torrent_id", "torrent_name", "download_location", "label"])


class ExecuteCommandAddedEvent(DelugeEvent):
"""
Expand All @@ -57,6 +60,7 @@ def __init__(self, command_id):
class Core(CorePluginBase):
def enable(self):
self.config = ConfigManager("execute.conf", DEFAULT_CONFIG)
self.plugin = component.get("CorePluginManager")
event_manager = component.get("EventManager")
self.registered_events = {}
self.preremoved_cache = {}
Expand All @@ -79,27 +83,28 @@ def event_handler(torrent_id, *arg):

log.debug("Execute core plugin enabled!")

def create_script_args(self, torrent_id):
# getProcessOutputAndValue requires args to be str
torrent = component.get("TorrentManager").torrents[torrent_id]
torrent_info = torrent.get_status(["name", "download_location"])
plugin_info = self.plugin.get_status(torrent_id, ["label"])
return ScriptArgs(utf8_encoded(torrent_id),
utf8_encoded(torrent_info["name"]),
utf8_encoded(torrent_info["download_location"]),
utf8_encoded(plugin_info.get("label", "")))

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"])]
self.preremoved_cache[torrent_id] = self.create_script_args(torrent_id)

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)
script_args = 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"])
script_args = self.create_script_args(torrent_id)

log.debug("Running commands for %s", event)

Expand All @@ -119,7 +124,7 @@ def log_error(result, command):
command = os.path.expanduser(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 = getProcessOutputAndValue(command, script_args, env=os.environ)
d.addCallback(log_error, command)
else:
log.error("Execute script not found or not executable")
Expand Down
2 changes: 1 addition & 1 deletion deluge/plugins/Execute/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__plugin_name__ = "Execute"
__author__ = "Damien Churchill"
__author_email__ = "[email protected]"
__version__ = "1.2"
__version__ = "1.3"
__url__ = "http://deluge-torrent.org"
__license__ = "GPLv3"
__description__ = "Plugin to execute a command upon an event"
Expand Down