From 91d8ad33aef61bba82de72fc6753b154bcc3a3e5 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sun, 4 Nov 2007 22:33:55 +0000 Subject: [PATCH] add error management to blocklist plugin --- ChangeLog | 4 +++ TODO | 1 - plugins/BlocklistImport/__init__.py | 16 +++++---- plugins/BlocklistImport/peerguardian.py | 8 +++-- plugins/BlocklistImport/text.py | 48 +++++++++++++++++-------- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index fa66593a4..4fab4f35c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Deluge 0.5.7 (xx November 2007) + * Blocklist plugin will now display errors, instead of just crashing on a bad + list + Deluge 0.5.6.2 (31 October 2007) * Set default piece size to 256-KiB in TorrentCreator plugin and add 2048KiB as a size option. diff --git a/TODO b/TODO index 1de76dbfb..e74ee58ec 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ for 0.5.7 1. manual recheck 2. preference for .torrent location - 3. have blocklist detect 7zip files and popup a warning instead of crashing 4. add auto-pickup folder 5. remap filenames diff --git a/plugins/BlocklistImport/__init__.py b/plugins/BlocklistImport/__init__.py index b1b216ab1..a9c4d97bc 100644 --- a/plugins/BlocklistImport/__init__.py +++ b/plugins/BlocklistImport/__init__.py @@ -88,8 +88,8 @@ class BlocklistImport: filename, headers = urllib.urlretrieve(self.config.get('url'), filename=self.blockfile, reporthook=self._download_update) - except IOError, (errno, strerr): - err = ui.GTKError(_("Couldn't download URL") + ": %s"%strerr) + except IOError, e: + err = ui.GTKError(_("Couldn't download URL") + ": %s"%e) self.gtkprog.stop() return @@ -101,13 +101,16 @@ class BlocklistImport: try: reader = readers[ltype][1](self.blockfile) - except IOError, (errno, strerr): - err = ui.GTKError(_("Couldn't open blocklist file") + ": %s"%strerr) + except IOError, e: + err = ui.GTKError(_("Couldn't open blocklist file") + ": %s"%e) self.gtkprog.stop() return print "Starting import" - ips = reader.next() + try: + ips = reader.next() + except: + ui.GTKError(_("Wrong file type or corrupted blocklist file.")) curr = 0 try: while ips and not self.cancelled: @@ -119,8 +122,7 @@ class BlocklistImport: else: self.gtkprog.import_prog() - except FormatException, (ex): - err = ui.GTKError(_("Format error in blocklist") + ": %s"%ex) + except: self.gtkprog.stop() reader.close() return diff --git a/plugins/BlocklistImport/peerguardian.py b/plugins/BlocklistImport/peerguardian.py index 8f7c887dd..94e1fec44 100644 --- a/plugins/BlocklistImport/peerguardian.py +++ b/plugins/BlocklistImport/peerguardian.py @@ -6,6 +6,7 @@ from exceptions import Exception from struct import unpack import gzip, socket +import ui class PGException(Exception): pass @@ -17,8 +18,11 @@ class PGReader: def __init__(self, filename): print "PGReader loading",filename - # FIXME: Catch and convert exception? - self.fd = gzip.open(filename, "rb") + try: + self.fd = gzip.open(filename, "rb") + except IOError, e: + ui.GTKError(_("We were expecting a gzip file, but didn't get that, \ +or possibly the file is corrupt. Please edit your Blocklist preferences")) # 4 bytes, should be 0xffffffff buf = self.fd.read(4) diff --git a/plugins/BlocklistImport/text.py b/plugins/BlocklistImport/text.py index 10c9c685f..81a5fa3d9 100644 --- a/plugins/BlocklistImport/text.py +++ b/plugins/BlocklistImport/text.py @@ -9,7 +9,7 @@ import re, gzip, os from socket import inet_aton from struct import unpack from zipfile import ZipFile - +import ui class TextException(Exception): pass @@ -34,13 +34,17 @@ class TextBase: match = self.re.search(txt) if not match: - raise FormatException(_("Couldn't match on line") + " %d: %s (%s)"%(self.count,txt,txt)) + ui.GTKError(_("Wrong file type or corrupted blocklist file.")) - g = match.groups() - start = ".".join(g[0:4]) - end = ".".join(g[4:8]) + try: + g = match.groups() + except AttributeError: + pass + else: + start = ".".join(g[0:4]) + end = ".".join(g[4:8]) - return (start, end) + return (start, end) def close(self): self.fd.close() @@ -60,7 +64,10 @@ class TextReader(PGTextReader): def __init__(self, filename): print "TextReader loading",filename - PGTextReader.__init__(self, open(filename, 'r')) + try: + PGTextReader.__init__(self, open(filename, 'r')) + except: + ui.GTKError(_("Wrong file type or corrupted blocklist file.")) # Reads Emule style blocklists (aka nipfilter) @@ -75,7 +82,10 @@ class GZMuleReader(MuleReader): def __init__(self, filename): print "GZMuleReader loading",filename - MuleReader.__init__(self, gzip.open(filename, "r")) + try: + MuleReader.__init__(self, gzip.open(filename, "r")) + except: + ui.GTKError(_("Wrong file type or corrupted blocklist file.")) # Reads zip files from SafePeer style files @@ -83,10 +93,13 @@ class PGZip(TextBase): def __init__(self, filename): # Open zip and extract first file - self.zfd = ZipFile(filename, 'r') - self.files = self.zfd.namelist() - - self.opennext() + try: + self.zfd = ZipFile(filename, 'r') + except: + ui.GTKError(_("Wrong file type or corrupted blocklist file.")) + else: + self.files = self.zfd.namelist() + self.opennext() def opennext(self): self.tmp = os.tmpfile() @@ -112,14 +125,19 @@ class PGZip(TextBase): return False return ret - except FormatException, (e): - print "Got format exception for zipfile:",e + except FormatException, e: + ui.GTKError(_("Got format exception for zipfile:",e)) # Just skip if len(self.files) > 0: self.opennext() return self.next() else: return False + except AttributeError: + pass def close(self): - self.zfd.close() + try: + self.zfd.close() + except AttributeError: + pass