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

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Tue May 7 00:57:37 CEST 2013


Revision: 21015
          http://gar.svn.sourceforge.net/gar/?rev=21015&view=rev
Author:   wahwah
Date:     2013-05-06 22:57:37 +0000 (Mon, 06 May 2013)
Log Message:
-----------
pkgdb: Uniform way of connecting to the database

It was inconsistent across the two web apps. It's now moved to a common place,
and connecting is done only at application startup, and in each method.

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

Added Paths:
-----------
    csw/mgar/gar/v2/lib/web/web_lib.py

Modified: csw/mgar/gar/v2/lib/web/pkgdb_web.py
===================================================================
--- csw/mgar/gar/v2/lib/web/pkgdb_web.py	2013-05-06 17:53:56 UTC (rev 21014)
+++ csw/mgar/gar/v2/lib/web/pkgdb_web.py	2013-05-06 22:57:37 UTC (rev 21015)
@@ -20,8 +20,6 @@
 import datetime
 from sqlobject import sqlbuilder
 
-connected_to_db = False
-
 urls_html = (
   r'/', 'index',
   r'/srv4/', 'Srv4List',
@@ -72,18 +70,6 @@
     return json.JSONEncoder.default(self, obj)
 
 
-def ConnectToDatabase():
-  """Connect to the database only if necessary.
-
-  One problem with this approach might be that if the connection is lost, the
-  script will never try to reconnect (unless it's done by the ORM).
-  """
-  global connected_to_db
-  if not connected_to_db:
-    configuration.SetUpSqlobjectConnection()
-    connected_to_db = True
-
-
 class index(object):
   def GET(self):
     return render.index()
@@ -421,7 +407,6 @@
 
   def GET(self, catrel_name, arch_name, osrel_name, catalogname):
     """Get a srv4 reference by catalog ane catalogname."""
-    configuration.SetUpSqlobjectConnection()
     try:
       sqo_osrel, sqo_arch, sqo_catrel = models.GetSqoTriad(
           osrel_name, arch_name, catrel_name)
@@ -524,7 +509,7 @@
 
 
 def app_wrapper():
-  ConnectToDatabase()
+  web_lib.ConnectToDatabase()
   app = web.application(urls, globals())
   logging.basicConfig(level=logging.DEBUG)
   return app.wsgifunc()

Modified: csw/mgar/gar/v2/lib/web/releases_web.py
===================================================================
--- csw/mgar/gar/v2/lib/web/releases_web.py	2013-05-06 17:53:56 UTC (rev 21014)
+++ csw/mgar/gar/v2/lib/web/releases_web.py	2013-05-06 22:57:37 UTC (rev 21015)
@@ -11,7 +11,6 @@
 import sqlobject
 import cjson
 from lib.python import models
-from lib.python import configuration
 from lib.python import checkpkg_lib
 from lib.python import package_stats
 from lib.python import opencsw
@@ -43,10 +42,6 @@
     "beanie",
 ])
 
-def ConnectToDatabase():
-  configuration.SetUpSqlobjectConnection()
-
-
 class Index(object):
   def GET(self):
     return "It works!\n"
@@ -54,7 +49,6 @@
 class Srv4List(object):
   def POST(self):
     messages = []
-    configuration.SetUpSqlobjectConnection()
     x = web.input(srv4_file={})
     web.header(
         'Content-type',
@@ -104,7 +98,6 @@
 class Srv4Detail(object):
   def GET(self, md5_sum):
     """Allows to verify whether a given srv4 file exists."""
-    configuration.SetUpSqlobjectConnection()
     srv4 = None
     try:
       srv4 = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne()
@@ -142,7 +135,6 @@
 class Srv4CatalogAssignment(object):
   def GET(self, catrel_name, arch_name, osrel_name):
     """See if that package is in that catalog."""
-    configuration.SetUpSqlobjectConnection()
     sqo_osrel, sqo_arch, sqo_catrel = models.GetSqoTriad(
         osrel_name, arch_name, catrel_name)
     srv4 = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne()
@@ -167,7 +159,6 @@
     is to add the 'Content-Length' header.  However, it sometimes still gets
     stuck and I don't know why.
     """
-    configuration.SetUpSqlobjectConnection()
     if catrel_name not in CAN_UPLOAD_TO_CATALOGS:
       # Updates via web are allowed only for the unstable catalog.
       # We should return an error message instead.
@@ -239,7 +230,6 @@
       raise web.notacceptable(data=response)
 
   def DELETE(self, catrel_name, arch_name, osrel_name, md5_sum):
-    configuration.SetUpSqlobjectConnection()
     try:
       if osrel_name not in common_constants.OS_RELS:
         self.ReturnError(
@@ -270,9 +260,14 @@
 
 web.webapi.internalerror = web.debugerror
 
-app = web.application(urls, globals())
-# main = app.wsgifunc()
-application = app.wsgifunc()
+def app_wrapper():
+  web_lib.ConnectToDatabase()
+  app = web.application(urls, globals())
+  logging.basicConfig(level=logging.DEBUG)
+  return app.wsgifunc()
+
+application = app_wrapper()
+
 from paste.exceptions.errormiddleware import ErrorMiddleware
 application = ErrorMiddleware(application, debug=True)
 

Added: csw/mgar/gar/v2/lib/web/web_lib.py
===================================================================
--- csw/mgar/gar/v2/lib/web/web_lib.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/web/web_lib.py	2013-05-06 22:57:37 UTC (rev 21015)
@@ -0,0 +1,14 @@
+# A common library for web apps.
+
+connected_to_db = False
+
+def ConnectToDatabase():
+  """Connect to the database only if necessary.
+
+  One problem with this approach might be that if the connection is lost, the
+  script will never try to reconnect (unless it's done by the ORM).
+  """
+  global connected_to_db
+  if not connected_to_db:
+    configuration.SetUpSqlobjectConnection()
+    connected_to_db = True

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