Remove the plugin hooks from the Core.. These are replaced by Events

This commit is contained in:
Andrew Resch 2009-03-15 20:29:51 +00:00
parent 7533c4bb88
commit 930c479718
5 changed files with 28 additions and 91 deletions

View File

@ -246,9 +246,6 @@ class Core(component.Component):
except Exception, e:
log.error("There was an error adding the torrent file %s", filename)
log.exception(e)
else:
# Run the plugin hooks for 'post_torrent_add'
self.pluginmanager.run_post_torrent_add(torrent_id)
@export()
def add_torrent_url(self, url, options):
@ -286,17 +283,11 @@ class Core(component.Component):
torrent_id = self.torrentmanager.add(magnet=uri, options=option)
# Run the plugin hooks for 'post_torrent_add'
self.pluginmanager.run_post_torrent_add(torrent_id)
@export()
def remove_torrent(self, torrent_ids, remove_data):
log.debug("Removing torrent %s from the core.", torrent_ids)
for torrent_id in torrent_ids:
if self.torrentmanager.remove(torrent_id, remove_data):
# Run the plugin hooks for 'post_torrent_remove'
self.pluginmanager.run_post_torrent_remove(torrent_id)
self.torrentmanager.remove(torrent_id, remove_data)
@export()
def get_stats(self):

View File

@ -39,13 +39,6 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase,
def __init__(self, core):
component.Component.__init__(self, "CorePluginManager")
self.core = core
# Set up the hooks dictionary
self.hooks = {
"post_torrent_add": [],
"post_torrent_remove": [],
"post_session_load": []
}
self.status_fields = {}
@ -66,15 +59,22 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase,
def update_plugins(self):
for plugin in self.plugins.keys():
try:
self.plugins[plugin].update()
except AttributeError:
# We don't care if this doesn't work
pass
if hasattr(self.plugins[plugin], "update"):
try:
self.plugins[plugin].update()
except Exception, e:
log.exception(e)
def get_core(self):
"""Returns a reference to the core"""
return self.core
def get_status(self, torrent_id, fields):
"""Return the value of status fields for the selected torrent_id."""
status = {}
for field in fields:
try:
status[field] = self.status_fields[field](torrent_id)
except KeyError:
log.warning("Status field %s is not registered with the\
PluginManager.", field)
return status
def register_status_field(self, field, function):
"""Register a new status field. This can be used in the same way the
@ -89,61 +89,3 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase,
del self.status_fields[field]
except:
log.warning("Unable to deregister status field %s", field)
def get_status(self, torrent_id, fields):
"""Return the value of status fields for the selected torrent_id."""
status = {}
for field in fields:
try:
status[field] = self.status_fields[field](torrent_id)
except KeyError:
log.warning("Status field %s is not registered with the\
PluginManager.", field)
return status
def register_hook(self, hook, function):
"""Register a hook function with the plugin manager"""
try:
self.hooks[hook].append(function)
except KeyError:
log.warning("Plugin attempting to register invalid hook.")
def deregister_hook(self, hook, function):
"""Deregisters a hook function"""
try:
self.hooks[hook].remove(function)
except:
log.warning("Unable to deregister hook %s", hook)
def run_post_torrent_add(self, torrent_id):
"""This hook is run after a torrent has been added to the session."""
log.debug("run_post_torrent_add")
for function in self.hooks["post_torrent_add"]:
function(torrent_id)
def run_post_torrent_remove(self, torrent_id):
"""This hook is run after a torrent has been removed from the session.
"""
log.debug("run_post_torrent_remove")
for function in self.hooks["post_torrent_remove"]:
function(torrent_id)
def run_post_session_load(self):
"""This hook is run after all the torrents have been loaded into the
session from the saved state. It is called prior to resuming the
torrents and they all will have a 'Paused' state."""
log.debug("run_post_session_load")
for function in self.hooks["post_session_load"]:
function()
def get_torrent_list(self):
"""Returns a list of torrent_id's in the current session."""
return component.get("TorrentManager").get_torrent_list()
def block_ip_range(self, range):
"""Blocks the ip range in the core"""
return self.core.export_block_ip_range(range)
def reset_ip_filter(self):
"""Resets the ip filter"""
return self.core.export_reset_ip_filter()

View File

@ -497,8 +497,7 @@ class TorrentManager(component.Component):
log.error("Torrent state file is either corrupt or incompatible!")
break
# Run the post_session_load plugin hooks
self.plugins.run_post_session_load()
component.get("EventManager").emit(SessionStartedEvent())
def save_state(self):
"""Save the state of the TorrentManager to the torrents.state file"""

View File

@ -140,6 +140,13 @@ class NewVersionAvailableEvent(DelugeEvent):
"""
self._args = [new_release]
class SessionStartedEvent(DelugeEvent):
"""
Emitted when a session has started. This typically only happens once when
the daemon is initially started.
"""
pass
class SessionPausedEvent(DelugeEvent):
"""
Emitted when the session has been paused.

View File

@ -88,7 +88,7 @@ class Core(CorePluginBase):
self.plugin.register_status_field("label", self._status_get_label)
#__init__
core = self.plugin.get_core()
core = component.get("Core")
self.config = ConfigManager("label.conf", defaults=CONFIG_DEFAULTS)
self.core_cfg = ConfigManager("core.conf")
#self.set_config_defaults()
@ -99,9 +99,9 @@ class Core(CorePluginBase):
self.torrent_labels = self.config["torrent_labels"]
self.clean_initial_config()
#todo: register to torrent_added event.
self.plugin.register_hook("post_torrent_add", self.post_torrent_add)
self.plugin.register_hook("post_torrent_remove", self.post_torrent_remove)
component.get("EventManager").register_event_handler("TorrentAddedEvent", self.post_torrent_add)
component.get("EventManager").register_event_handler("TorrentRemovedEvent", self.post_torrent_remove)
#register tree:
component.get("FilterManager").register_tree_field("label", self.init_filter_dict)
@ -110,8 +110,6 @@ class Core(CorePluginBase):
def disable(self):
self.plugin.deregister_status_field("label")
self.plugin.deregister_hook("post_torrent_add", self.post_torrent_add)
self.plugin.deregister_hook("post_torrent_remove", self.post_torrent_remove)
component.get("FilterManager").deregister_tree_field("label")
def update(self):