deskbar-applet r2101 - in trunk: . deskbar/handlers



Author: sebp
Date: Sat Apr 12 13:08:07 2008
New Revision: 2101
URL: http://svn.gnome.org/viewvc/deskbar-applet?rev=2101&view=rev

Log:
Replace all whitespaces in query with spaces. 
Catch exception that might appear during parsing and cleanup correctly in any case. (fixes bug #484667)

Modified:
   trunk/ChangeLog
   trunk/deskbar/handlers/desklicious.py

Modified: trunk/deskbar/handlers/desklicious.py
==============================================================================
--- trunk/deskbar/handlers/desklicious.py	(original)
+++ trunk/deskbar/handlers/desklicious.py	Sat Apr 12 13:08:07 2008
@@ -2,11 +2,18 @@
 from deskbar.defs import VERSION
 from deskbar.handlers.actions.ShowUrlAction import ShowUrlAction
 from gettext import gettext as _
-import deskbar, deskbar.interfaces.Match, deskbar.interfaces.Module, deskbar.core.Utils
+from xml.dom import DOMException
+from xml.parsers.expat import ExpatError
+import deskbar
+import deskbar.core.Utils
+import deskbar.interfaces.Match
+import deskbar.interfaces.Module
 import gconf
 import gtk
 import logging
-import xml.dom.minidom, urllib
+import re
+import urllib
+import xml.dom.minidom
 
 LOGGER = logging.getLogger(__name__)
 
@@ -52,16 +59,11 @@
     def __init__(self):
         deskbar.interfaces.Module.__init__ (self)
         self._delicious = DeliciousTagQueryEngine(self)
+        self._bad_chars_pattern = re.compile("\s")
 
     def query(self, tag):
-        #Hey man, calm down and query once a time :P
-        # TODO: Missing
-        #self.check_query_changed (timeout=QUERY_DELAY)
-        
-        # Yes, the google and yahoo search might take a long time
-        # and of course deliciuos too !!! ... better check if we're still valid    
-        # TODO: Missing
-        #self.check_query_changed ()
+        # Remove bad characters from query
+        tag = self._bad_chars_pattern.sub(' ', tag)
         
         #The queryyyyYyyYy :)
         LOGGER.info( "Asking del.icio.us tags for %s", tag )
@@ -133,39 +135,49 @@
         if self._user == None:
             return []
         
+        posts=[]
         #Get the info from del.icio.us and parse
         url = DEFAULT_QUERY_TAG % (urllib.quote_plus(self._user), urllib.quote_plus(tag))
+        
         LOGGER.debug("Opening URL %s", url)
         try:
-            stream = urllib.urlopen(url, proxies=deskbar.core.Utils.get_proxy())
-        except IOError, msg:
-            LOGGER.error("Could not open URL %s: %s, %s", url, msg[0], msg[1])
-            return []
-        
-        dom = xml.dom.minidom.parse(stream)
-        stream.close()
+            try:
+                stream = urllib.urlopen(url, proxies=deskbar.core.Utils.get_proxy())
+                dom = xml.dom.minidom.parse(stream)
+            except IOError, msg:
+                LOGGER.error("Could not open URL %s: %s, %s", url, msg[0], msg[1])
+            except ExpatError, e:
+                LOGGER.exception(e)
+        finally:
+            stream.close()
         
         #And return the results
-        posts=[]
-        for item in dom.getElementsByTagName("item"):
-            title_node = item.getElementsByTagName("title")[0]
-            url_node = item.getElementsByTagName("link")[0]
-            subject_node = item.getElementsByTagName("dc:subject")[0]
-            creator_node = item.getElementsByTagName("dc:creator")[0]
-            
-            # There might be no tags
-            if (subject_node.hasChildNodes()):
-                tags = subject_node.firstChild.nodeValue.split(" ")
-            else:
-                tags = []
-            
-            posts.append(
-                DeliciousMatch(
-                    name=title_node.firstChild.nodeValue,
-                    url=url_node.firstChild.nodeValue,
-                    tags=tags,
-                    author=creator_node.firstChild.nodeValue,
-                    category="web"))
+        try:
+            try:
+                for item in dom.getElementsByTagName("item"):
+                    title_node = item.getElementsByTagName("title")[0]
+                    url_node = item.getElementsByTagName("link")[0]
+                    subject_node = item.getElementsByTagName("dc:subject")[0]
+                    creator_node = item.getElementsByTagName("dc:creator")[0]
+                    
+                    # There might be no tags
+                    if (subject_node.hasChildNodes()):
+                        tags = subject_node.firstChild.nodeValue.split(" ")
+                    else:
+                        tags = []
+                    
+                    posts.append(
+                        DeliciousMatch(
+                            name=title_node.firstChild.nodeValue,
+                            url=url_node.firstChild.nodeValue,
+                            tags=tags,
+                            author=creator_node.firstChild.nodeValue,
+                            category="web"))
+            except DOMException, e:
+                LOGGER.exception(e)
+        finally:
+            # Cleanup
+            dom.unlink()
         
         return posts
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]