dabo Commit
Revision 7234
Date: 2012-08-25 13:18:19 -0700 (Sat, 25 Aug 2012)
Author: Jacekk
Trac: http://trac.dabodev.com/changeset/7234

Changed:
U   trunk/dabo/db/dConnectInfo.py
U   trunk/dabo/lib/connParser.py

Log:
Added support for custom connection parameters passed in .cnxml files, e.g.:
<dialect type="int">1</dialect>
Only builtin types are supported.

Diff:
Modified: trunk/dabo/db/dConnectInfo.py
===================================================================
--- trunk/dabo/db/dConnectInfo.py       2012-08-23 19:22:02 UTC (rev 7233)
+++ trunk/dabo/db/dConnectInfo.py       2012-08-25 20:18:19 UTC (rev 7234)
@@ -59,6 +59,7 @@
        def setConnInfo(self, connInfo, nm=""):
                # Run through the connDict, and set the appropriate properties. 
If it isn't
                # a valid property name, raise TypeError.
+               self._customParameters = {}
                props = ["Name", "DbType", "Host", "User", "Password", 
"Database",
                                "PlainTextPassword", "Port", "RemoteHost", 
"KeepAliveInterval"]
                lprops = [p.lower() for p in props]
@@ -70,10 +71,11 @@
                        if propidx is not None:
                                setattr(self, props[propidx], v)
                        else:
-                               raise TypeError("Property '%s' invalid." % k)
+                               self._customParameters[k] = v
 
 
        def getConnection(self, **kwargs):
+               kwargs.update(self.CustomParameters)
                return self._backendObject.getConnection(self, **kwargs)
 
 
@@ -123,6 +125,13 @@
                self._cryptoProvider = val
 
 
+       def _getCustomParameters(self):
+               try:
+                       return self._customParameters.copy()
+               except AttributeError:
+                       return {}
+
+
        def _getDbType(self):
                try:
                        return self._dbType
@@ -159,6 +168,9 @@
                                elif nm == "web":
                                        import dbWeb
                                        self._backendObject = dbWeb.Web()
+                               elif nm == "odbc":
+                                       import dbODBC
+                                       self._backendObject = dbODBC.ODBC()
                                else:
                                        raise ValueError("Invalid database 
type: %s." % nm)
                        except ImportError:
@@ -214,6 +226,7 @@
        def _setPlainPassword(self, val):
                self._password = self.encrypt(val)
 
+
        def _getPort(self):
                return self._port
 
@@ -243,6 +256,9 @@
                        _("""Reference to the object that provides 
cryptographic services if run
                        outside of an application.  (varies)"""))
 
+       CustomParameters = property(_getCustomParameters, None, None,
+                       _("""Additional parameters passed to backend object 
connect method. (dict)"""))
+
        DbType = property(_getDbType, _setDbType, None,
                        _("Name of the backend database type.  (str)"))
 

Modified: trunk/dabo/lib/connParser.py
===================================================================
--- trunk/dabo/lib/connParser.py        2012-08-23 19:22:02 UTC (rev 7233)
+++ trunk/dabo/lib/connParser.py        2012-08-25 20:18:19 UTC (rev 7234)
@@ -6,9 +6,10 @@
 from xmltodict import escQuote
 import dabo
 import dabo.lib.utils as utils
+from dabo.dLocalize import _
 
 # Tuple containing all file-based database types.
-FILE_DATABASES = ("sqlite", )
+FILE_DATABASES = ("sqlite",)
 
 
 
@@ -35,15 +36,29 @@
                if name == "connection":
                        for att in attrs.keys():
                                if att == "dbtype":
-                                       self.currDict["dbtype"] = 
attrs.getValue("dbtype")
+                                       dbType = 
attrs.getValue("dbtype").split(":")
+                                       self.currDict["dbtype"] = dbType[0]
+                                       if len(dbType) > 1:
+                                               self.currDict["driver"] = 
dbType[1]
+               self.attributes = attrs
 
 
        def characters(self, content):
-               if self.element:
+               if self.element and self.element not in ("connectiondefs", 
"connection"):
                        if self.element in self.currDict:
                                self.currDict[self.element] += content
+                       else:
+                               # We can now define custom connection 
parameters, example:
+                               # <dialect type="int">1</dialect>
+                               # It's an extended connection information, we 
log it.
+                               dabo.log.info(_(u"Extended database connection 
parameter loaded: %s = %s") % \
+                                               (self.element, content))
+                               atype = self.attributes.get("type", None)
+                               if not atype:
+                                       # Set default type to 'str'.
+                                       atype = "str"
+                               self.currDict[self.element] = 
globals()["__builtins__"][atype](content)
 
-
        def endElement(self, name):
                if name == "connection":
                        if self.currDict:
@@ -56,6 +71,7 @@
                                self.connDict[nm] = self.currDict.copy()
                                self.currDict = self.blankConn.copy()
                self.element = None
+               self.attributes = None
 
 
        def getConnectionDict(self):
@@ -88,7 +104,7 @@
                dbtype = data.get("dbtype", "")
                if dbtype.lower() in FILE_DATABASES:
                        for key, val in data.items():
-                               if key=="database":
+                               if key == "database":
                                        osp = os.path
                                        relpath = utils.resolvePath(val, 
basePath, abspath=False)
                                        pth = 
pth.decode(dabo.fileSystemEncoding)



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to