Author: johnnyg

Revision: 5729

Log:
        Readers raise ReaderParseError when parsing b0rks.
on_import_failure, now handles parse errors.

Diff:
Modified: trunk/deluge/plugins/blocklist/blocklist/common.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/common.py  2009-09-13 13:51:00 UTC 
(rev 5728)
+++ trunk/deluge/plugins/blocklist/blocklist/common.py  2009-09-13 14:45:23 UTC 
(rev 5729)
@@ -39,3 +39,13 @@
 
 def get_resource(filename):
     return pkg_resources.resource_filename("blocklist", os.path.join("data", 
filename))
+
+def raiseError(error):
+    def safer(func):
+        def new(self, *args, **kwargs):
+            try:
+                return func(self, *args, **kwargs)
+            except:
+                raise error
+        return new
+    return safer

Modified: trunk/deluge/plugins/blocklist/blocklist/core.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/core.py    2009-09-13 13:51:00 UTC 
(rev 5728)
+++ trunk/deluge/plugins/blocklist/blocklist/core.py    2009-09-13 14:45:23 UTC 
(rev 5729)
@@ -49,6 +49,7 @@
 from deluge.core.rpcserver import export
 from deluge.httpdownloader import download_file
 from detect import detect_compression, detect_format, create_reader, 
UnknownFormatError
+from readers import ReaderParseError
 
 # TODO: check return values for deferred callbacks
 # TODO: review class attributes for redundancy
@@ -111,7 +112,7 @@
         pass
 
     ## Exported RPC methods ###
-    @export()
+    @export
     def check_import(self, force=False):
         """
         Imports latest blocklist specified by blocklist url.
@@ -126,6 +127,7 @@
         self.force_download = force
         self.use_cache = False
         self.failed_attempts = 0
+        self.auto_detected = False
 
         # Start callback chain
         d = self.download_list()
@@ -135,12 +137,12 @@
 
         return d
 
-    @export()
+    @export
     def get_config(self):
         """Returns the config dictionary"""
         return self.config.config
 
-    @export()
+    @export
     def set_config(self, config):
         """
         Sets the config based on values in 'config'
@@ -151,7 +153,7 @@
         for key in config.keys():
             self.config[key] = config[key]
 
-    @export()
+    @export
     def get_status(self):
         """Returns the status of the plugin."""
         status = {}
@@ -284,6 +286,7 @@
 
         if not self.reader:
             self.auto_detect(blocklist)
+            self.auto_detected = True
 
         d = 
threads.deferToThread(self.reader(blocklist).read(on_read_ip_range))
         d.addCallback(on_finish_read)
@@ -306,17 +309,27 @@
 
     def on_import_error(self, f):
         """Recovers from import error"""
-        # TODO: catch invalid / corrupt list error
-        #       and reset self.reader
-        d = None
+        d = f
         self.is_importing = False
+        try_again = False
         blocklist = deluge.configmanager.get_config_dir("blocklist.cache")
-        # If we have a backup and we haven't already used it
-        if os.path.exists(blocklist) and not self.use_cache:
+
+        if f.check(ReaderParseError) and not self.auto_detected:
+            # Invalid / corrupt list, let's detect it
+            log.warning("Invalid / corrupt blocklist")
+            self.reader = None
+            try_again = True
+        elif os.path.exists(blocklist) and not self.use_cache:
+           # If we have a backup and we haven't already used it
             e = f.trap(Exception)
             log.warning("Error reading blocklist: ", e)
+            self.use_cache = True
+            try_again = True
+
+        if try_again:
             d = self.import_list()
             d.addCallbacks(self.on_import_complete, self.on_import_error)
+
         return d
 
     def auto_detect(self, blocklist):

Modified: trunk/deluge/plugins/blocklist/blocklist/readers.py
===================================================================
--- trunk/deluge/plugins/blocklist/blocklist/readers.py 2009-09-13 13:51:00 UTC 
(rev 5728)
+++ trunk/deluge/plugins/blocklist/blocklist/readers.py 2009-09-13 14:45:23 UTC 
(rev 5729)
@@ -34,8 +34,10 @@
 #
 
 from deluge.log import LOG as log
+from common import raiseError
 
-# TODO: Create exception classes to be raised
+class ReaderParseError(Exception):
+    pass
 
 class BaseReader(object):
     """Base reader for blocklist files"""
@@ -85,11 +87,13 @@
 
 class EmuleReader(BaseReader):
     """Blocklist reader for emule style blocklists"""
+    @raiseError(ReaderParseError)
     def parse(self, line):
         return line.strip().split(" , ")[0].split(" - ")
 
 class SafePeerReader(BaseReader):
     """Blocklist reader for SafePeer style blocklists"""
+    @raiseError(ReaderParseError)
     def parse(self, line):
         return line.strip().split(":")[1].split("-")
 



--~--~---------~--~----~------------~-------~--~----~
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