[csw-devel] SF.net SVN: gar:[16995] csw/mgar/gar/v2/lib

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Sun Feb 5 01:58:42 CET 2012


Revision: 16995
          http://gar.svn.sourceforge.net/gar/?rev=16995&view=rev
Author:   wahwah
Date:     2012-02-05 00:58:42 +0000 (Sun, 05 Feb 2012)
Log Message:
-----------
pkgdb-web: Sanitize pkg stats at earlier stage

Don't sanitize just before pickling, but before returning the data structure.

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

Modified: csw/mgar/gar/v2/lib/python/models.py
===================================================================
--- csw/mgar/gar/v2/lib/python/models.py	2012-02-05 00:58:08 UTC (rev 16994)
+++ csw/mgar/gar/v2/lib/python/models.py	2012-02-05 00:58:42 UTC (rev 16995)
@@ -222,9 +222,34 @@
         u"Package: %s-%s, %s"
         % (self.catalogname, self.version_string, self.arch.name))
 
+  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 GetStatsStruct(self):
     if not self._cached_pkgstats:
       self._cached_pkgstats = cPickle.loads(str(self.data_obj.pickle))
+      # There was a problem with bad utf-8 in the VENDOR field.
+      # This is a workaround.
+      if "VENDOR" in self._cached_pkgstats["pkginfo"]:
+        self._cached_pkgstats["pkginfo"]["VENDOR"] = self.GetUnicodeOrNone(
+            self._cached_pkgstats["pkginfo"]["VENDOR"])
+      # The end of the hack.
+      #
+      # One more workaround
+      for d in self._cached_pkgstats["pkgmap"]:
+        if "path" in d:
+          d["path"] = self.GetUnicodeOrNone(d["path"])
+          d["line"] = self.GetUnicodeOrNone(d["line"])
+      # End of the workaround
     return self._cached_pkgstats
 
   def _GetBuildSource(self):

Modified: csw/mgar/gar/v2/lib/web/pkgdb_web.py
===================================================================
--- csw/mgar/gar/v2/lib/web/pkgdb_web.py	2012-02-05 00:58:08 UTC (rev 16994)
+++ csw/mgar/gar/v2/lib/web/pkgdb_web.py	2012-02-05 00:58:42 UTC (rev 16995)
@@ -49,6 +49,19 @@
 render = web.template.render('/home/maciej/src/opencsw-git/gar/v2/'
                              'lib/web/templates/')
 
+
+class PkgStatsEncoder(json.JSONEncoder):
+  """Maps frozensets to lists."""
+  def default(self, obj):
+    if isinstance(obj, frozenset):
+      # Python 2.6 doesn't have the dictionary comprehension
+      # return {x: None for x in obj}
+      return list(obj)
+    if isinstance(obj, datetime.datetime):
+      return obj.isoformat()
+    return json.JSONEncoder.default(self, obj)
+
+
 def ConnectToDatabase():
   """Connect to the database only if necessary.
 
@@ -328,46 +341,12 @@
 
 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):
-      def default(self, obj):
-        if isinstance(obj, frozenset):
-          # Python 2.6 doesn't have the dictionary comprehension
-          # return {x: None for x in obj}
-          return list(obj)
-        if isinstance(obj, datetime.datetime):
-          return obj.isoformat()
-        return json.JSONEncoder.default(self, obj)
     try:
       pkg = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne()
       data_structure = pkg.GetStatsStruct()
       web.header('Content-type', 'application/x-vnd.opencsw.pkg;type=pkg-stats')
-      # There was a problem with bad utf-8 in the VENDOR field.
-      # This is a workaround.
-      if "VENDOR" in data_structure["pkginfo"]:
-        data_structure["pkginfo"]["VENDOR"] = self.GetUnicodeOrNone(
-            data_structure["pkginfo"]["VENDOR"])
-      # The end of the hack.
-      #
-      # One more workaround
-      for d in data_structure["pkgmap"]:
-        if "path" in d:
-          d["path"] = self.GetUnicodeOrNone(d["path"])
-          d["line"] = self.GetUnicodeOrNone(d["line"])
-      # End of the workaround
       return json.dumps(data_structure, cls=PkgStatsEncoder)
     except sqlobject.main.SQLObjectNotFound, e:
       raise web.notfound()

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