new plugin system + ip block plugin...hopefully...

This commit is contained in:
Marcos Pinto 2007-06-12 02:52:53 +00:00
parent 0d2711ab06
commit 4fe3d2769d
9 changed files with 74 additions and 107 deletions

View File

@ -1,5 +1,7 @@
# An example plugin for use with Deluge # An example plugin for use with Deluge
import deluge.common, deluge.pref, gtk, gtk.glade
# This plugin is intended to be used with Deluge's default GTK interface # This plugin is intended to be used with Deluge's default GTK interface
class plugin_Example: # The plugin's class class plugin_Example: # The plugin's class
## Your plugin's contructor should follow this format ## Your plugin's contructor should follow this format
@ -11,14 +13,12 @@ class plugin_Example: # The plugin's class
self.path = path self.path = path
self.core = deluge_core self.core = deluge_core
self.interface = deluge_interface self.interface = deluge_interface
# Classes must be imported as they are needed from within
# the plugin's functions
import common, gtk, gtk.glade, dgtk, pref
# Create an options file and try to load existing Values # Create an options file and try to load existing Values
self.config_file = common.CONFIG_DIR + "/example.conf" self.config_file = deluge.common.CONFIG_DIR + "/example.conf"
self.config = pref.Preferences() self.config = deluge.pref.Preferences()
try: try:
self.config.load_from_file(self.config_file) self.config.load(self.config_file)
except IOError: except IOError:
# File does not exist # File does not exist
pass pass
@ -41,7 +41,7 @@ class plugin_Example: # The plugin's class
## unload is called when the plugin is removed or Deluge is shut down ## unload is called when the plugin is removed or Deluge is shut down
def unload(self): def unload(self):
self.toolbar.remove(self.button) # Remove the button from the toolbar self.toolbar.remove(self.button) # Remove the button from the toolbar
self.config.save_to_file(self.config_file) self.config.save(self.config_file)
## update will be called every UPDATE_INTERVAL (usually about 1 second) ## update will be called every UPDATE_INTERVAL (usually about 1 second)
def update(self): def update(self):
@ -71,7 +71,6 @@ class plugin_Example: # The plugin's class
## This will be called whenever self.button is clicked ## This will be called whenever self.button is clicked
def clicked(self, button): def clicked(self, button):
# Build a dialog from scratch rather than from a glade file # Build a dialog from scratch rather than from a glade file
import gtk
dialog = gtk.Dialog(title="Example Plugin", parent=self.interface.window, dialog = gtk.Dialog(title="Example Plugin", parent=self.interface.window,
buttons=(gtk.STOCK_OK, 0)) buttons=(gtk.STOCK_OK, 0))
dialog.set_icon_from_file(self.path + "/example-plugin.png") dialog.set_icon_from_file(self.path + "/example-plugin.png")
@ -91,11 +90,3 @@ class plugin_Example: # The plugin's class
dialog.hide() dialog.hide()
dialog.destroy() dialog.destroy()
register_plugin("Example Plugin", # The name of the plugin
plugin_Example, # The plugin's class
"Zach Tibbitts", # The author's Name
"0.5.0", # The plugin's version number
"An example plugin", # A description of the plugin
config=True, # If the plugin can be configured
)

View File

@ -50,9 +50,6 @@ class plugin_NetGraph:
self.parentNotebook.remove_page(page) self.parentNotebook.remove_page(page)
break break
def configure(self):
pass
def update(self): def update(self):
import gtk import gtk
session_info = self.core.get_state() session_info = self.core.get_state()
@ -158,12 +155,3 @@ class plugin_NetGraph:
self.ctx.stroke() self.ctx.stroke()
### Register plugin with Deluge
register_plugin("Network Activity Graph", # The name of the plugin
plugin_NetGraph, # The plugin's class
"Alon Zakai, Zach Tibbitts", # Authors
"0.2", # The plugin's version number
"Network Activity Graph plugin\n\nWritten by Kripkenstein", # A description of the plugin
config=False, # If the plugin can be configured
)

View File

@ -7,12 +7,6 @@ class plugin_NetworkHealth:
self.counter = 30 self.counter = 30
self.maxCount = self.counter self.maxCount = self.counter
def config(self):
pass
def unload(self):
pass
def update(self): def update(self):
session_info = self.core.get_state() session_info = self.core.get_state()
if not session_info['has_incoming_connections'] and \ if not session_info['has_incoming_connections'] and \
@ -28,12 +22,3 @@ class plugin_NetworkHealth:
self.counter = self.maxCount self.counter = self.maxCount
self.parent.statusbar_temp_msg = self.parent.statusbar_temp_msg + ' ' + message self.parent.statusbar_temp_msg = self.parent.statusbar_temp_msg + ' ' + message
### Register plugin with Deluge
register_plugin("Network Health Monitor", # The name of the plugin
plugin_NetworkHealth, # The plugin's class
"Alon Zakai, Zach Tibbitts", # Authors
"0.2", # The plugin's version number
"Network Health Monitor plugin\n\nWritten by Kripkenstein", # A description of the plugin
config=False # If the plugin can be configured\
)

View File

@ -91,5 +91,3 @@ class plugin_tcreator:
ret = self.core.create_torrent(self.dest, src_dir, trackers, comments, size, author) ret = self.core.create_torrent(self.dest, src_dir, trackers, comments, size, author)
return ret return ret
register_plugin("Deluge Torrent Creator", plugin_tcreator, "regulate", "0.1", "A torrent creator plugin", config=False)

View File

@ -151,11 +151,3 @@ class plugin_Search:
self.menu_button.set_label("Search " + engine_string) self.menu_button.set_label("Search " + engine_string)
self.se = engine_string self.se = engine_string
register_plugin("Torrent Search",
plugin_Search,
"Zach Tibbitts",
"0.5",
"A searchbar for torrent search engines",
config=True
)

View File

@ -742,3 +742,11 @@ class Manager:
def pe_settings(self, out_enc_policy, in_enc_policy, allowed_enc_level, prefer_rc4): def pe_settings(self, out_enc_policy, in_enc_policy, allowed_enc_level, prefer_rc4):
return deluge_core.pe_settings(out_enc_policy, in_enc_policy, allowed_enc_level, prefer_rc4) return deluge_core.pe_settings(out_enc_policy, in_enc_policy, allowed_enc_level, prefer_rc4)
# Creates/resets the IP filter list
def reset_ip_filter(self):
return deluge_core.reset_IP_filter()
# Adds an IP range (as two dotted quad strings) to the filter
def add_range_to_ip_filter(self, start, end):
return deluge_core.add_range_to_IP_filter(start, end)

View File

@ -1191,43 +1191,33 @@ static PyObject *torrent_create_torrent(PyObject *self, PyObject *args)
} }
static PyObject *torrent_apply_IP_filter(PyObject *self, PyObject *args) static PyObject *torrent_reset_IP_filter(PyObject *self, PyObject *args)
{ {
PyObject *ranges;
if (!PyArg_ParseTuple(args, "O", &ranges))
return NULL;
long num_ranges = PyList_Size(ranges);
// printf("Number of ranges: %ld\r\n", num_ranges);
// Py_INCREF(Py_None); return Py_None;
// Remove existing filter, if there is one // Remove existing filter, if there is one
if (M_the_filter != NULL) if (M_the_filter != NULL)
delete M_the_filter; delete M_the_filter;
M_the_filter = new ip_filter(); M_the_filter = new ip_filter();
address_v4 from, to;
PyObject *curr;
// printf("Can I 10.10.10.10? %d\r\n", the_filter->access(address_v4::from_string("10.10.10.10")));
for (long i = 0; i < num_ranges; i++)
{
curr = PyList_GetItem(ranges, i);
// PyObject_Print(curr, stdout, 0);
from = address_v4::from_string(PyString_AsString(PyList_GetItem(curr, 0)));
to = address_v4::from_string(PyString_AsString(PyList_GetItem(curr, 1)));
// printf("Filtering: %s - %s\r\n", from.to_string().c_str(), to.to_string().c_str());
M_the_filter->add_rule(from, to, ip_filter::blocked);
};
// printf("Can I 10.10.10.10? %d\r\n", the_filter->access(address_v4::from_string("10.10.10.10")));
M_ses->set_ip_filter(*M_the_filter); M_ses->set_ip_filter(*M_the_filter);
// printf("Can I 10.10.10.10? %d\r\n", the_filter->access(address_v4::from_string("10.10.10.10"))); Py_INCREF(Py_None); return Py_None;
}
static PyObject *torrent_add_range_to_IP_filter(PyObject *self, PyObject *args)
{
if (M_the_filter == NULL) {
RAISE_PTR(DelugeError, "No filter defined, use reset_IP_filter");
}
char *start, *end;
if (!PyArg_ParseTuple(args, "ss", &start, &end))
return NULL;
address_v4 inet_start = address_v4::from_string(start);
address_v4 inet_end = address_v4::from_string(end);
M_the_filter->add_rule(inet_start, inet_end, ip_filter::blocked);
Py_INCREF(Py_None); return Py_None; Py_INCREF(Py_None); return Py_None;
} }
@ -1286,7 +1276,8 @@ static PyMethodDef deluge_core_methods[] =
{"stop_DHT", torrent_stop_DHT, METH_VARARGS, "."}, {"stop_DHT", torrent_stop_DHT, METH_VARARGS, "."},
{"get_DHT_info", torrent_get_DHT_info, METH_VARARGS, "."}, {"get_DHT_info", torrent_get_DHT_info, METH_VARARGS, "."},
{"create_torrent", torrent_create_torrent, METH_VARARGS, "."}, {"create_torrent", torrent_create_torrent, METH_VARARGS, "."},
{"apply_IP_filter", torrent_apply_IP_filter, METH_VARARGS, "."}, {"reset_IP_filter", torrent_reset_IP_filter, METH_VARARGS, "."},
{"add_range_to_IP_filter", torrent_add_range_to_IP_filter, METH_VARARGS, "."},
{NULL} {NULL}
}; };

View File

@ -154,11 +154,11 @@ class PluginDlg:
return True return True
name = model.get_value(model.get_iter(path), 0) name = model.get_value(model.get_iter(path), 0)
plugin = self.plugins.get_plugin(name) plugin = self.plugins.get_plugin(name)
author = plugin['author'] author = plugin.plugin_author
version = plugin['version'] version = plugin.plugin_version
config = plugin['config'] description = plugin.plugin_description
description = plugin['description']
if name in self.plugins.get_enabled_plugins(): if name in self.plugins.get_enabled_plugins():
config = self.plugins.configurable_plugin(name)
self.glade.get_widget("plugin_conf").set_sensitive(config) self.glade.get_widget("plugin_conf").set_sensitive(config)
else: else:
self.glade.get_widget("plugin_conf").set_sensitive(False) self.glade.get_widget("plugin_conf").set_sensitive(False)
@ -174,8 +174,8 @@ class PluginDlg:
self.store.set_value(plugin_iter, 1, plugin_value) self.store.set_value(plugin_iter, 1, plugin_value)
if plugin_value: if plugin_value:
self.plugins.enable_plugin(plugin_name) self.plugins.enable_plugin(plugin_name)
self.glade.get_widget("plugin_conf").set_sensitive( config = self.plugins.configurable_plugin(plugin_name)
self.plugins.get_plugin(plugin_name)['config']) self.glade.get_widget("plugin_conf").set_sensitive(config)
else: else:
self.plugins.disable_plugin(plugin_name) self.plugins.disable_plugin(plugin_name)
self.glade.get_widget("plugin_conf").set_sensitive(False) self.glade.get_widget("plugin_conf").set_sensitive(False)

View File

@ -20,7 +20,7 @@
# 51 Franklin Street, Fifth Floor # 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA. # Boston, MA 02110-1301, USA.
import os import os, sys, imp
class PluginManager: class PluginManager:
def __init__(self, deluge_core, deluge_interface): def __init__(self, deluge_core, deluge_interface):
@ -32,15 +32,26 @@ class PluginManager:
def add_plugin_dir(self, directory): def add_plugin_dir(self, directory):
self.plugin_dirs.append(directory) self.plugin_dirs.append(directory)
sys.path.append(directory)
# Scans all defined plugin dirs for Deluge plugins. The resulting
# module object is store with the defined name.
def scan_for_plugins(self): def scan_for_plugins(self):
register_plugin = self.register_plugin
for folder in self.plugin_dirs: for folder in self.plugin_dirs:
plugin_folders = os.listdir(folder) print "Scanning plugin dir",folder
for plugin in plugin_folders: for modname in os.listdir(folder):
if os.path.isfile(os.path.join(folder, plugin, "plugin.py")): path = folder+'/'+modname
self.path = os.path.join(folder, plugin) if '__init__.py' in os.listdir(path):
execfile(os.path.join(folder, plugin, "plugin.py")) # Import the found module. Note that the last
# parameter is important otherwise only the base
# modules (ie. 'plugins') is imported. This appears
# to be by design.
print "Loading module",modname
mod = __import__(modname, globals(), locals(), [''])
if 'deluge_init' in dir(mod):
print "Initialising plugin",modname
mod.deluge_init(path)
self.available_plugins[mod.plugin_name] = mod
def get_available_plugins(self): def get_available_plugins(self):
return self.available_plugins.keys() return self.available_plugins.keys()
@ -49,35 +60,38 @@ class PluginManager:
return self.available_plugins[name] return self.available_plugins[name]
def enable_plugin(self, name): def enable_plugin(self, name):
self.enabled_plugins[name] = self.available_plugins[name]['class']( plugin = self.available_plugins[name]
self.available_plugins[name]['path'], self.core, self.interface) self.enabled_plugins[name] = plugin.enable(self.core, self.interface)
def get_enabled_plugins(self): def get_enabled_plugins(self):
return self.enabled_plugins.keys() return self.enabled_plugins.keys()
def disable_plugin(self, name): def disable_plugin(self, name):
self.enabled_plugins[name].unload() plugin = self.enabled_plugins[name]
if 'unload' in dir(plugin):
plugin.unload()
self.enabled_plugins.pop(name) self.enabled_plugins.pop(name)
def configurable_plugin(self, name):
if name in self.enabled_plugins:
return 'configure' in dir(self.enabled_plugins[name])
else:
return False
def configure_plugin(self, name): def configure_plugin(self, name):
self.enabled_plugins[name].configure() self.enabled_plugins[name].configure()
def update_active_plugins(self): def update_active_plugins(self):
for name in self.enabled_plugins.keys(): for name in self.enabled_plugins.keys():
self.enabled_plugins[name].update() plugin = self.enabled_plugins[name]
if 'update' in dir(plugin):
plugin.update()
def shutdown_all_plugins(self): def shutdown_all_plugins(self):
for name in self.enabled_plugins.keys(): for name in self.enabled_plugins.keys():
self.enabled_plugins[name].unload() self.disable_plugin(name)
self.enabled_plugins.clear() self.enabled_plugins.clear()
def register_plugin(self, name, plugin_class, author, version, description, config=False):
self.available_plugins[name] = {'class': plugin_class,
'author': author,
'version': version,
'description': description,
'config': config,
'path': self.path}
## Few lines of code to test functionality ## Few lines of code to test functionality
if __name__ == "__main__": if __name__ == "__main__":