[Zope3-checkins]
SVN: Zope3/branches/jim-adapter/src/zope/app/rdb/__init__.py
Merge from trunk so that we don't lose this.
Brian Sutherland
jinty at web.de
Mon Apr 10 19:25:54 EDT 2006
Log message for revision 66824:
Merge from trunk so that we don't lose this.
------------------------------------------------------------------------
r66635 | hdima | 2006-04-07 13:19:19 +0200 (Fri, 07 Apr 2006) | 2 lines
Simplified implementation of the parseDSN method
Changed:
U Zope3/branches/jim-adapter/src/zope/app/rdb/__init__.py
-=-
Modified: Zope3/branches/jim-adapter/src/zope/app/rdb/__init__.py
===================================================================
--- Zope3/branches/jim-adapter/src/zope/app/rdb/__init__.py 2006-04-10 22:21:23 UTC (rev 66823)
+++ Zope3/branches/jim-adapter/src/zope/app/rdb/__init__.py 2006-04-10 23:25:54 UTC (rev 66824)
@@ -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