deskbar-applet r2100 - in branches/gnome-2-22: . deskbar/handlers



Author: sebp
Date: Sat Apr 12 13:07:37 2008
New Revision: 2100
URL: http://svn.gnome.org/viewvc/deskbar-applet?rev=2100&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:
   branches/gnome-2-22/ChangeLog
   branches/gnome-2-22/deskbar/handlers/desklicious.py

Modified: branches/gnome-2-22/deskbar/handlers/desklicious.py
==============================================================================
--- branches/gnome-2-22/deskbar/handlers/desklicious.py	(original)
+++ branches/gnome-2-22/deskbar/handlers/desklicious.py	Sat Apr 12 13:07:37 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__)
 
@@ -51,17 +58,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 +134,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 "+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()
+        LOGGER.debug("Opening URL %s", url)
+        try:
+            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]