Get blocklist plugin working

This commit is contained in:
Andrew Resch 2009-02-24 01:03:02 +00:00
parent fa77f8403a
commit 9b05ec8b5f
6 changed files with 41 additions and 127 deletions

View File

@ -1,9 +1,8 @@
# #
# blocklist/__init__.py # blocklist/__init__.py
# #
# Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2007-2009 Andrew Resch <andrewresch@gmail.com>
# Copyright (C) 2008 Mark Stahler ('kramed') <markstahler@gmail.com> #
# #
# Deluge is free software. # Deluge is free software.
# #
@ -24,36 +23,13 @@
# Boston, MA 02110-1301, USA. # Boston, MA 02110-1301, USA.
# #
from deluge.log import LOG as log
from deluge.plugins.init import PluginInitBase from deluge.plugins.init import PluginInitBase
class CorePlugin(PluginInitBase): class CorePlugin(PluginInitBase):
def __init__(self, plugin_name): from core import Core as _plugin_cls
# Load the Core portion of the plugin
try:
from core import Core
self.plugin = Core(plugin_name)
except Exception, e:
log.error("Failed to load core plugin %s!", plugin_name)
log.exception(e)
class GtkUIPlugin(PluginInitBase): class GtkUIPlugin(PluginInitBase):
def __init__(self, plugin_name): from gtkui import GtkUI as _plugin_cls
# Load the GtkUI portion of the plugin
try:
from gtkui import GtkUI
self.plugin = GtkUI(plugin_name)
except Exception, e:
log.error("Failed to load gtkui plugin %s!", plugin_name)
log.exception(e)
class WebUIPlugin(PluginInitBase): class WebUIPlugin(PluginInitBase):
def __init__(self, plugin_name): from webui import WebUI as _plugin_cls
# Load the WebUI portion of the plugin
try:
from webui import WebUI
self.plugin = WebUI(plugin_name)
except Exception, e:
log.error("Failed to load webui plugin %s!", plugin_name)
log.exception(e)

View File

@ -1,7 +1,7 @@
# #
# ui.py # common.py
# #
# Copyright (C) 2008 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
# #
# Deluge is free software. # Deluge is free software.
# #
@ -26,15 +26,5 @@
import pkg_resources import pkg_resources
import os.path import os.path
class UI: def get_resource(filename):
def __init__(self, plugin_api, plugin_name): return pkg_resources.resource_filename("blocklist", os.path.join("data", filename))
self.plugin = plugin_api
def enable(self):
pass
def disable(self):
pass
def get_resource(self, filename):
return pkg_resources.resource_filename("blocklist", os.path.join("data", filename))

View File

@ -32,7 +32,7 @@ import time
import shutil import shutil
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.plugins.corepluginbase import CorePluginBase from deluge.plugins.pluginbase import CorePluginBase
import deluge.component as component import deluge.component as component
import deluge.configmanager import deluge.configmanager
from deluge.core.rpcserver import export from deluge.core.rpcserver import export
@ -94,29 +94,29 @@ class Core(CorePluginBase):
pass pass
## Exported RPC methods ### ## Exported RPC methods ###
@export @export()
def download_list(self, _import=False): def download_list(self, _import=False):
"""Download the blocklist specified in the config as url""" """Download the blocklist specified in the config as url"""
self.download_blocklist(_import) self.download_blocklist(_import)
@export @export()
def import_list(self, force=False): def import_list(self, force=False):
"""Import the blocklist from the blocklist.cache, if load is True, then """Import the blocklist from the blocklist.cache, if load is True, then
it will download the blocklist file if needed.""" it will download the blocklist file if needed."""
threading.Thread(target=self.import_blocklist, kwargs={"force": force}).start() threading.Thread(target=self.import_blocklist, kwargs={"force": force}).start()
@export @export()
def get_config(self): def get_config(self):
"""Returns the config dictionary""" """Returns the config dictionary"""
return self.config.config return self.config.config
@export @export()
def set_config(self, config): def set_config(self, config):
"""Sets the config based on values in 'config'""" """Sets the config based on values in 'config'"""
for key in config.keys(): for key in config.keys():
self.config[key] = config[key] self.config[key] = config[key]
@export @export()
def get_status(self): def get_status(self):
"""Returns the status of the plugin.""" """Returns the status of the plugin."""
status = {} status = {}

View File

@ -25,21 +25,23 @@
import gtk import gtk
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.ui.client import aclient as client from deluge.ui.client import client
import deluge.component as component import deluge.component as component
import deluge.common import deluge.common
from deluge.plugins.pluginbase import GtkPluginBase
import common
import ui
from core import FORMATS from core import FORMATS
class GtkUI(ui.UI): class GtkUI(GtkPluginBase):
def enable(self): def enable(self):
log.debug("Blocklist GtkUI enable..") log.debug("Blocklist GtkUI enable..")
self.plugin = component.get("PluginManager")
self.load_preferences_page() self.load_preferences_page()
self.status_item = component.get("StatusBar").add_item( self.status_item = component.get("StatusBar").add_item(
image=self.get_resource("blocklist16.png"), image=common.get_resource("blocklist16.png"),
text="", text="",
callback=self._on_status_item_clicked, callback=self._on_status_item_clicked,
tooltip="Blocked IP Ranges") tooltip="Blocked IP Ranges")
@ -114,7 +116,7 @@ class GtkUI(ui.UI):
self.glade.get_widget("label_url").set_text( self.glade.get_widget("label_url").set_text(
status["file_url"]) status["file_url"])
client.blocklist.get_status(_on_get_status) client.blocklist.get_status().addCallback(_on_get_status)
def _on_show_prefs(self): def _on_show_prefs(self):
def _on_get_config(config): def _on_get_config(config):
@ -132,7 +134,7 @@ class GtkUI(ui.UI):
self.glade.get_widget("chk_import_on_start").set_active( self.glade.get_widget("chk_import_on_start").set_active(
config["load_on_start"]) config["load_on_start"])
client.blocklist.get_config(_on_get_config) client.blocklist.get_config().addCallback(_on_get_config)
def _on_apply_prefs(self): def _on_apply_prefs(self):
config = {} config = {}
@ -141,15 +143,15 @@ class GtkUI(ui.UI):
config["url"] = self.glade.get_widget("entry_url").get_text() config["url"] = self.glade.get_widget("entry_url").get_text()
config["check_after_days"] = self.glade.get_widget("spin_check_days").get_value_as_int() config["check_after_days"] = self.glade.get_widget("spin_check_days").get_value_as_int()
config["load_on_start"] = self.glade.get_widget("chk_import_on_start").get_active() config["load_on_start"] = self.glade.get_widget("chk_import_on_start").get_active()
client.blocklist.set_config(None, config) client.blocklist.set_config(config)
def _on_button_check_download_clicked(self, widget): def _on_button_check_download_clicked(self, widget):
self._on_apply_prefs() self._on_apply_prefs()
client.blocklist.import_list(None, False) client.blocklist.import_list(False)
def _on_button_force_download_clicked(self, widget): def _on_button_force_download_clicked(self, widget):
self._on_apply_prefs() self._on_apply_prefs()
client.blocklist.import_list(None, True) client.blocklist.import_list(True)
def _on_status_item_clicked(self, widget, event): def _on_status_item_clicked(self, widget, event):
component.get("Preferences").show("Blocklist") component.get("Preferences").show("Blocklist")
@ -157,7 +159,7 @@ class GtkUI(ui.UI):
def load_preferences_page(self): def load_preferences_page(self):
"""Initializes the preferences page and adds it to the preferences dialog""" """Initializes the preferences page and adds it to the preferences dialog"""
# Load the preferences page # Load the preferences page
self.glade = gtk.glade.XML(self.get_resource("blocklist_pref.glade")) self.glade = gtk.glade.XML(common.get_resource("blocklist_pref.glade"))
self.progress_bar = self.glade.get_widget("progressbar") self.progress_bar = self.glade.get_widget("progressbar")
self.table_info = self.glade.get_widget("table_info") self.table_info = self.glade.get_widget("table_info")
@ -187,10 +189,10 @@ class GtkUI(ui.UI):
# Set button icons # Set button icons
self.glade.get_widget("image_download").set_from_file( self.glade.get_widget("image_download").set_from_file(
self.get_resource("blocklist_download24.png")) common.get_resource("blocklist_download24.png"))
self.glade.get_widget("image_import").set_from_file( self.glade.get_widget("image_import").set_from_file(
self.get_resource("blocklist_import24.png")) common.get_resource("blocklist_import24.png"))
# Update the preferences page with config values from the core # Update the preferences page with config values from the core
self._on_show_prefs() self._on_show_prefs()

View File

@ -26,13 +26,13 @@
import os import os
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.ui.client import sclient, aclient from deluge.ui.client import client
from deluge import component from deluge import component
import ui from deluge.plugins.pluginbase import WebPluginBase
import deluge.ui.webui.lib.newforms_plus as forms #import deluge.ui.webui.lib.newforms_plus as forms
config_page_manager = component.get("ConfigPageManager") #config_page_manager = component.get("ConfigPageManager")
FORMAT_LIST = [ FORMAT_LIST = [
('gzmule',_("Emule IP list (GZip)")), ('gzmule',_("Emule IP list (GZip)")),
@ -41,68 +41,11 @@ FORMAT_LIST = [
('p2bgz',_("PeerGuardian P2B (GZip)")) ('p2bgz',_("PeerGuardian P2B (GZip)"))
] ]
class BlockListCfgForm(forms.Form): class WebUI(WebPluginBase):
"""
a config form based on django forms.
see webui.lib.newforms_plus, config_forms, etc.
"""
#meta:
title = _("BlockList")
#load/save:
def initial_data(self):
data = sclient.blocklist.get_config()
return data
def save(self, data):
cfg = dict(data)
del cfg["btn_import_now"]
del cfg["btn_force_download"]
sclient.blocklist.set_config(cfg)
if data.btn_import_now:
sclient.blocklist.import_list(data.btn_force_download)
#input fields :
listtype = forms.ChoiceField(FORMAT_LIST)
url = forms.CharField(label=_("Url"))
check_after_days = forms.IntegerField(label=_("Check for every (days)"), min_value=-1, max_value=14)
timeout = forms.IntegerField(label=_("Timeout (seconds)"), min_value=15, max_value=360)
try_times = forms.IntegerField(label=_("Times to attempt download"), min_value=1, max_value=5)
load_on_start = forms.CheckBox(_('Import on daemon startup'))
btn_import_now = forms.CheckBox(_('Import Now'))
btn_force_download = forms.CheckBox(_('Force Download'))
def post_html(self):
"show blocklist status"
status = sclient.blocklist.get_status()
if status["state"] == "Downloading":
txt = _("Downloading %.2f%%") % (status["file_progress"] * 100)
elif status["state"] == "Importing":
txt = _("Importing %s") % str(status["num_blocked"])
elif status["state"] == "Idle":
txt = _("Blocked Ranges: %s") % str(status["num_blocked"])
else:
txt = _("Inactive")
return """
<h3>Status</h3>
<div class="info">
%s<br><form method="GET"><input type="submit" value="%s"></form>
</div>
""" % (txt, _("Refresh status"))
class WebUI(ui.UI):
def __init__(self, plugin_api, plugin_name):
log.debug("Calling UI init")
# Call UI constructor
ui.UI.__init__(self, plugin_api, plugin_name)
log.debug("Blocklist WebUI plugin initalized..")
def enable(self): def enable(self):
config_page_manager.register('plugins','blocklist',BlockListCfgForm) #config_page_manager.register('plugins','blocklist',BlockListCfgForm)
pass
def disable(self): def disable(self):
config_page_manager.deregister('blocklist') #config_page_manager.deregister('blocklist')
pass

View File

@ -38,13 +38,16 @@ class CorePluginBase(PluginBase):
# Register RPC methods # Register RPC methods
component.get("RPCServer").register_object(self, plugin_name.lower()) component.get("RPCServer").register_object(self, plugin_name.lower())
log.debug("CorePlugin initialized..") log.debug("CorePlugin initialized..")
component.start("CorePlugin." + plugin_name)
class GtkPluginBase(PluginBase): class GtkPluginBase(PluginBase):
def __init__(self, plugin_name): def __init__(self, plugin_name):
component.Component.__init__(self, "GtkPlugin." + plugin_name) component.Component.__init__(self, "GtkPlugin." + plugin_name)
log.debug("GtkPlugin initialized..") log.debug("GtkPlugin initialized..")
component.start("GtkPlugin." + plugin_name)
class WebPluginBase(PluginBase): class WebPluginBase(PluginBase):
def __init__(self, plugin_name): def __init__(self, plugin_name):
component.Component.__init__(self, "WebPlugin." + plugin_name) component.Component.__init__(self, "WebPlugin." + plugin_name)
log.debug("WebPlugin initialized..") log.debug("WebPlugin initialized..")
component.start("WebPlugin." + plugin_name)