[csw-devel] SF.net SVN: gar:[16907] csw/mgar/gar/v2/lib/web/pkgdb_web.py

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Thu Jan 26 09:56:09 CET 2012


Revision: 16907
          http://gar.svn.sourceforge.net/gar/?rev=16907&view=rev
Author:   wahwah
Date:     2012-01-26 08:56:08 +0000 (Thu, 26 Jan 2012)
Log Message:
-----------
pkgdb-web: Add a function for unicode rescue

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/web/pkgdb_web.py

Modified: csw/mgar/gar/v2/lib/web/pkgdb_web.py
===================================================================
--- csw/mgar/gar/v2/lib/web/pkgdb_web.py	2012-01-25 02:41:08 UTC (rev 16906)
+++ csw/mgar/gar/v2/lib/web/pkgdb_web.py	2012-01-26 08:56:08 UTC (rev 16907)
@@ -318,6 +318,18 @@
 
 class RestSrv4FullStats(object):
 
+  def GetUnicodeOrNone(self, s):
+    """Tries to decode UTF-8"""
+    if s is None:
+      return None
+    if type(s) != unicode:
+      try:
+        s = unicode(s, 'utf-8')
+      except UnicodeDecodeError, e:
+        s = s.decode("utf-8", "ignore")
+        s = s + u" (bad unicode detected)"
+    return s
+
   def GET(self, md5_sum):
     ConnectToDatabase()
     class PkgStatsEncoder(json.JSONEncoder):
@@ -336,14 +348,8 @@
       # There was a problem with bad utf-8 in the VENDOR field.
       # This is a workaround.
       if "VENDOR" in data_structure["pkginfo"]:
-        vendor = data_structure["pkginfo"]["VENDOR"]
-        if type(vendor) != unicode:
-          try:
-            vendor = unicode(vendor, 'utf-8')
-          except UnicodeDecodeError, e:
-            vendor = vendor.decode("utf-8", "ignore")
-          data_structure["pkginfo"]["VENDOR"] = (
-              vendor + " (bad unicode detected)")
+        data_structure["pkginfo"]["VENDOR"] = self.GetUnicodeOrNone(
+            data_structure["pkginfo"]["VENDOR"])
       # The end of the hack.
       return json.dumps(data_structure, cls=PkgStatsEncoder)
     except sqlobject.main.SQLObjectNotFound, e:

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list