Added decompresser and reader dic...
Content-type: text/plain

Author: johnnyg

Revision: 5490

Log:
        Updated imports.
Added decompresser and reader dicts.
Make import_list actually import list into libtorrent session.
Begin auto-detection of blocklists.

Diff:
Modified: trunk/deluge/plugins/blocklist/blocklist/core.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/core.py    2009-07-15 03:06:58 UTC 
(rev 5489)
+++ trunk/deluge/plugins/blocklist/blocklist/core.py    2009-07-15 14:00:57 UTC 
(rev 5490)
@@ -39,7 +39,7 @@
 import shutil
 
 from twisted.internet.task import LoopingCall
-from twisted.internet import reactor, threads, defer
+from twisted.internet import threads, defer
 from twisted.web import error
 
 from deluge.log import LOG as log
@@ -48,7 +48,20 @@
 import deluge.configmanager
 from deluge.core.rpcserver import export
 from deluge.httpdownloader import download_file
+from decompressers import Zipped, GZipped, BZipped2
+from readers import EmuleReader, SafePeerReader, PeerGuardianReader
+from detect import detect_compression, detect_format, UnknownFormatError
 
+try:
+    import deluge.libtorrent as lt
+except ImportError:
+    import libtorrent as lt
+    if not (lt.version_major == 0 and lt.version_minor == 14):
+        raise ImportError("This version of Deluge requires libtorrent 0.14!")
+
+# TODO: check return values for deferred callbacks
+# TODO: review class attributes for redundancy
+
 DEFAULT_PREFS = {
     "url": "http://deluge-torrent.org/blocklist/nipfilter.dat.gz";,
     "load_on_start": False,
@@ -61,6 +74,23 @@
     "try_times": 3,
 }
 
+DECOMPRESSERS = {
+    "Zip" : Zipped,
+    "GZip" : GZipped,
+    "BZip2" : BZipped2
+}
+
+READERS = {
+    "Emule" : EmuleReader,
+    "SafePeer" : SafePeerReader,
+    "PeerGuardian" : PeerGuardianReader
+}
+
+# Libtorrent IP filter constants
+START = 0
+END = 1
+BLOCK = 1
+
 class Core(CorePluginBase):
     def enable(self):
         log.debug('Blocklist: Plugin enabled..')
@@ -71,13 +101,15 @@
         self.up_to_date = False
         self.num_blocked = 0
         self.file_progress = 0.0
-        self.reader = None
 
         self.core = component.get("Core")
+        self.config = deluge.configmanager.ConfigManager("blocklist.conf", 
DEFAULT_PREFS)
 
+        self.reader = READERS.get(self.config["list_type"])
+        if self.config["list_compression"]:
+            self.reader = 
DECOMPRESSERS.get(self.config["list_compression"])(self.reader)
+
         update_now = False
-
-        self.config = deluge.configmanager.ConfigManager("blocklist.conf", 
DEFAULT_PREFS)
         if self.config["load_on_start"]:
             if self.config["last_update"]:
                 now = datetime.datetime.now()
@@ -231,23 +263,35 @@
     def import_list(self, force=False):
         """Imports the downloaded blocklist into the session"""
         def on_read_ip_range(ip_range):
-            # TODO: add to lt session
+            self.blocklist.add_rule(ip_range[START], ip_range[END], BLOCK)
             self.num_blocked += 1
 
-        if self.use_cache and self.has_imported:
+        def on_finish_read(result):
+            # Add blocklist to session
+            self.core.session.set_ip_filter(self.blocklist)
+
+        # TODO: double check logic
+        if self.up_to_date and self.has_imported:
             log.debug("Latest blocklist is already imported")
-            return True
+            return defer.succeed(None)
 
         self.is_importing = True
         self.num_blocked = 0
+        self.blocklist = lt.ip_filter()
 
+        if self.use_cache:
+            blocklist = deluge.configmanager.get_config_dir("blocklist.cache")
+        else:
+            blocklist = 
deluge.configmanager.get_config_dir("blocklist.download")
+
         if not self.reader:
-            # TODO: auto-detect reader
-            pass
+            self.auto_detect(blocklist)
 
-        #return threads.deferToThread(self.reader.read(on_read_ip_range))
-        return defer.succeed(None)
+        d = 
threads.deferToThread(self.reader(blocklist).read(on_read_ip_range))
+        d.addCallback(on_finish_read)
 
+        return d
+
     def on_import_complete(self, result):
         """Runs any import clean up functions"""
         d = None
@@ -264,6 +308,8 @@
 
     def on_import_error(self, f):
         """Recovers from import error"""
+        # TODO: catch invalid / corrupt list error
+        #       and reset self.reader
         d = None
         self.is_importing = False
         blocklist = deluge.configmanager.get_config_dir("blocklist.cache")
@@ -274,3 +320,10 @@
             d = self.import_list()
             d.addCallbacks(self.on_import_complete, self.on_import_error)
         return d
+
+    def auto_detect(self, blocklist):
+        self.config["list_compression"] = detect_compression(blocklist)
+        self.config["list_type"] = detect_format(blocklist)
+        if not self.config["list_type"]:
+            self.config["list_compression"] = ""
+            raise UnknownFormatError

Added: trunk/deluge/plugins/blocklist/blocklist/detect.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/detect.py                          
(rev 0)
+++ trunk/deluge/plugins/blocklist/blocklist/detect.py  2009-07-15 14:00:57 UTC 
(rev 5490)
@@ -0,0 +1,53 @@
+#
+# detect.py
+#
+# Copyright (C) 2009 John Garland <[email protected]>
+#
+# Deluge is free software.
+#
+# You may redistribute it and/or modify it under the terms of the
+# GNU General Public License, as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option)
+# any later version.
+#
+# deluge is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with deluge.    If not, write to:
+#      The Free Software Foundation, Inc.,
+#      51 Franklin Street, Fifth Floor
+#      Boston, MA  02110-1301, USA.
+#
+#    In addition, as a special exception, the copyright holders give
+#    permission to link the code of portions of this program with the OpenSSL
+#    library.
+#    You must obey the GNU General Public License in all respects for all of
+#    the code used other than OpenSSL. If you modify file(s) with this
+#    exception, you may extend this exception to your version of the file(s),
+#    but you are not obligated to do so. If you do not wish to do so, delete
+#    this exception statement from your version. If you delete this exception
+#    statement from all source files in the program, then also delete it here.
+#
+#
+
+COMPRESSION_TYPES = {
+    "PK" : "zip",
+    "\x1f\x8b" : "gzip",
+    "BZ" : "bzip2"
+}
+
+class UnknownFormatError(Exception):
+    pass
+
+def detect_compression(filename):
+    f = open(filename, "rb")
+    magic_number = f.read(2)
+    f.close()
+    return COMPRESSION_TYPES.get(magic_number, "")
+
+def detect_format(filename):
+    # TODO: implement this function
+    return ""

Modified: trunk/deluge/plugins/blocklist/blocklist/readers.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/readers.py 2009-07-15 03:06:58 UTC 
(rev 5489)
+++ trunk/deluge/plugins/blocklist/blocklist/readers.py 2009-07-15 14:00:57 UTC 
(rev 5490)
@@ -35,6 +35,8 @@
 
 from deluge.log import LOG as log
 
+# TODO: Create exception classes to be raised
+
 class BaseReader(object):
     """Base reader for blocklist files"""
     def __init__(self, file):



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to