[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/rdb/__init__.py Simplified implementation of the parseDSN method

Dmitry Vasiliev dima at hlabs.spb.ru
Fri Apr 7 07:19:20 EDT 2006


Log message for revision 66635:
  Simplified implementation of the parseDSN method
  

Changed:
  U   Zope3/trunk/src/zope/app/rdb/__init__.py

-=-
Modified: Zope3/trunk/src/zope/app/rdb/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/rdb/__init__.py	2006-04-07 11:18:06 UTC (rev 66634)
+++ Zope3/trunk/src/zope/app/rdb/__init__.py	2006-04-07 11:19:19 UTC (rev 66635)
@@ -19,6 +19,7 @@
 
 $Id$
 """
+import re
 import time, random, thread
 from urllib import unquote_plus
 
@@ -189,6 +190,16 @@
 def identity(x):
     return x
 
+_dsnFormat = re.compile(
+    r"dbi://"
+    r"(((?P<username>.*?)(:(?P<password>.*?))?)?"
+    r"(@(?P<host>.*?)(:(?P<port>.*?))?)?/)?"
+    r"(?P<dbname>.*?)(;(?P<raw_params>.*))?"
+    r"$"
+    )
+
+_paramsFormat = re.compile(r"([^=]+)=([^;]*);?")
+
 def parseDSN(dsn):
     """Parses a database connection string.
 
@@ -216,59 +227,24 @@
        dbname       database name
        parameters   a mapping of additional parameters to their values
     """
+
     if not isinstance(dsn, (str, unicode)):
         raise ValueError('The dsn is not a string. It is a %r' % type(dsn))
-    if not dsn.startswith('dbi://'):
+
+    match = _dsnFormat.match(dsn)
+    if match is None:
         raise ValueError('Invalid DSN; must start with "dbi://": %r' % dsn)
 
-    result = {}
+    result = match.groupdict("")
+    raw_params = result.pop("raw_params")
 
-    dsn = dsn[6:]
-    # Get parameters (dict) from DSN
-    raw_params = dsn.split(';')
-    dsn = raw_params[0]
-    raw_params = raw_params[1:]
+    for key, value in result.items():
+        result[key] = unquote_plus(value)
 
-    parameters = [param.split('=') for param in raw_params]
-    parameters = dict([(unquote_plus(key), unquote_plus(value))
-                       for key, value in parameters])
+    params = _paramsFormat.findall(raw_params)
+    result["parameters"] = dict([(unquote_plus(key), unquote_plus(value))
+                                for key, value in params])
 
-    result['parameters'] = parameters
-
-    # Get the dbname from the DSN
-    if dsn.find('/') > 0:
-        dsn, dbname = dsn.split('/')
-    else:
-        dbname = dsn
-        dsn = ''
-
-    result['dbname'] = unquote_plus(dbname)
-
-    # Get host and port from DSN
-    if dsn and dsn.find('@') > 0:
-        dsn, host_port = dsn.split('@')
-        if host_port.find(':') > 0:
-            host, port = host_port.split(':')
-        else:
-            host, port = host_port, ''
-    else:
-        host, port = '', ''
-
-    result['host'] = host
-    result['port'] = port
-
-    # Get username and password from DSN
-    if dsn:
-        if dsn.find(':') > 0:
-            username, password = dsn.split(':', 1)
-        else:
-             username, password = dsn, ''
-    else:
-        username, password = '', ''
-
-    result['username'] = unquote_plus(username)
-    result['password'] = unquote_plus(password)
-
     return result
 
 



More information about the Zope3-Checkins mailing list