~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to deluge/core/pluginmanager.py

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#    this exception statement from your version. If you delete this exception
32
32
#    statement from all source files in the program, then also delete it here.
33
33
#
34
 
 
35
34
#
36
35
 
37
36
 
38
37
"""PluginManager for Core"""
39
38
 
40
 
import gobject
 
39
from twisted.internet import reactor
 
40
from twisted.internet.task import LoopingCall
41
41
 
 
42
from deluge.event import PluginEnabledEvent, PluginDisabledEvent
42
43
import deluge.pluginmanagerbase
43
44
import deluge.component as component
44
45
from deluge.log import LOG as log
49
50
    functions to access parts of the core."""
50
51
 
51
52
    def __init__(self, core):
52
 
        component.Component.__init__(self, "PluginManager")
53
 
        self.core = core
54
 
        # Set up the hooks dictionary
55
 
        self.hooks = {
56
 
            "post_torrent_add": [],
57
 
            "post_torrent_remove": [],
58
 
            "post_session_load": []
59
 
        }
 
53
        component.Component.__init__(self, "CorePluginManager")
60
54
 
61
55
        self.status_fields = {}
62
56
 
68
62
        # Enable plugins that are enabled in the config
69
63
        self.enable_plugins()
70
64
 
71
 
        # Set update timer to call update() in plugins every second
72
 
        self.update_timer = gobject.timeout_add(1000, self.update_plugins)
73
 
 
74
65
    def stop(self):
75
66
        # Disable all enabled plugins
76
67
        self.disable_plugins()
77
 
        # Stop the update timer
78
 
        gobject.source_remove(self.update_timer)
79
68
 
80
69
    def shutdown(self):
81
70
        self.stop()
82
71
 
83
72
    def update_plugins(self):
84
73
        for plugin in self.plugins.keys():
 
74
            if hasattr(self.plugins[plugin], "update"):
 
75
                try:
 
76
                    self.plugins[plugin].update()
 
77
                except Exception, e:
 
78
                    log.exception(e)
 
79
 
 
80
    def enable_plugin(self, name):
 
81
        if name not in self.plugins:
 
82
            super(PluginManager, self).enable_plugin(name)
 
83
            if name in self.plugins:
 
84
                component.get("EventManager").emit(PluginEnabledEvent(name))
 
85
 
 
86
    def disable_plugin(self, name):
 
87
        if name in self.plugins:
 
88
            super(PluginManager, self).disable_plugin(name)
 
89
            if name not in self.plugins:
 
90
                component.get("EventManager").emit(PluginDisabledEvent(name))
 
91
 
 
92
    def get_status(self, torrent_id, fields):
 
93
        """Return the value of status fields for the selected torrent_id."""
 
94
        status = {}
 
95
        for field in fields:
85
96
            try:
86
 
                self.plugins[plugin].update()
87
 
            except AttributeError:
88
 
                # We don't care if this doesn't work
89
 
                pass
90
 
 
91
 
    def get_core(self):
92
 
        """Returns a reference to the core"""
93
 
        return self.core
 
97
                status[field] = self.status_fields[field](torrent_id)
 
98
            except KeyError:
 
99
                log.warning("Status field %s is not registered with the\
 
100
                    PluginManager.", field)
 
101
        return status
94
102
 
95
103
    def register_status_field(self, field, function):
96
104
        """Register a new status field.  This can be used in the same way the
105
113
            del self.status_fields[field]
106
114
        except:
107
115
            log.warning("Unable to deregister status field %s", field)
108
 
 
109
 
    def get_status(self, torrent_id, fields):
110
 
        """Return the value of status fields for the selected torrent_id."""
111
 
        status = {}
112
 
        for field in fields:
113
 
            try:
114
 
                status[field] = self.status_fields[field](torrent_id)
115
 
            except KeyError:
116
 
                log.warning("Status field %s is not registered with the\
117
 
                    PluginManager.", field)
118
 
        return status
119
 
 
120
 
    def register_hook(self, hook, function):
121
 
        """Register a hook function with the plugin manager"""
122
 
        try:
123
 
            self.hooks[hook].append(function)
124
 
        except KeyError:
125
 
            log.warning("Plugin attempting to register invalid hook.")
126
 
 
127
 
    def deregister_hook(self, hook, function):
128
 
        """Deregisters a hook function"""
129
 
        try:
130
 
            self.hooks[hook].remove(function)
131
 
        except:
132
 
            log.warning("Unable to deregister hook %s", hook)
133
 
 
134
 
    def run_post_torrent_add(self, torrent_id):
135
 
        """This hook is run after a torrent has been added to the session."""
136
 
        log.debug("run_post_torrent_add")
137
 
        for function in self.hooks["post_torrent_add"]:
138
 
            function(torrent_id)
139
 
 
140
 
    def run_post_torrent_remove(self, torrent_id):
141
 
        """This hook is run after a torrent has been removed from the session.
142
 
        """
143
 
        log.debug("run_post_torrent_remove")
144
 
        for function in self.hooks["post_torrent_remove"]:
145
 
            function(torrent_id)
146
 
 
147
 
    def run_post_session_load(self):
148
 
        """This hook is run after all the torrents have been loaded into the
149
 
        session from the saved state.  It is called prior to resuming the
150
 
        torrents and they all will have a 'Paused' state."""
151
 
        log.debug("run_post_session_load")
152
 
        for function in self.hooks["post_session_load"]:
153
 
            function()
154
 
 
155
 
    def get_torrent_list(self):
156
 
        """Returns a list of torrent_id's in the current session."""
157
 
        return component.get("TorrentManager").get_torrent_list()
158
 
 
159
 
    def block_ip_range(self, range):
160
 
        """Blocks the ip range in the core"""
161
 
        return self.core.export_block_ip_range(range)
162
 
 
163
 
    def reset_ip_filter(self):
164
 
        """Resets the ip filter"""
165
 
        return self.core.export_reset_ip_filter()
166