From 736ce8d7507767e4a3c15ba48f1617dd232f058d Mon Sep 17 00:00:00 2001 From: Aaron Swartz Date: Mon, 10 Sep 2012 11:13:37 -0400 Subject: [PATCH] some improvements to dburl2dict --- web/db.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/web/db.py b/web/db.py index 4201f767..e14cf044 100644 --- a/web/db.py +++ b/web/db.py @@ -11,7 +11,7 @@ "database", 'DB', ] -import time, os +import time, os, urllib try: import datetime except ImportError: @@ -1139,16 +1139,27 @@ def dburl2dict(url): Takes a URL to a database and parses it into an equivalent dictionary. >>> dburl2dict('postgres://james:day@serverfarm.example.net:5432/mygreatdb') - {'host': 'serverfarm.example.net', 'pw': 'day', 'dbn': 'postgres', 'db': 'mygreatdb', 'port': '5432', 'user': 'james'} - + {'pw': 'day', 'dbn': 'postgres', 'db': 'mygreatdb', 'host': 'serverfarm.example.net', 'user': 'james', 'port': '5432'} + >>> dburl2dict('postgres://james:day@serverfarm.example.net/mygreatdb') + {'user': 'james', 'host': 'serverfarm.example.net', 'db': 'mygreatdb', 'pw': 'day', 'dbn': 'postgres'} + >>> dburl2dict('postgres://james:d%40y@serverfarm.example.net/mygreatdb') + {'user': 'james', 'host': 'serverfarm.example.net', 'db': 'mygreatdb', 'pw': 'd@y', 'dbn': 'postgres'} """ dbn, rest = url.split('://', 1) user, rest = rest.split(':', 1) pw, rest = rest.split('@', 1) - host, rest = rest.split(':', 1) - port, rest = rest.split('/', 1) + if ':' in rest: + host, rest = rest.split(':', 1) + port, rest = rest.split('/', 1) + else: + host, rest = rest.split('/', 1) + port = None db = rest - return dict(dbn=dbn, user=user, pw=pw, host=host, port=port, db=db) + + uq = urllib.unquote + out = dict(dbn=dbn, user=uq(user), pw=uq(pw), host=uq(host), db=uq(db)) + if port: out['port'] = port + return out _databases = {} def database(dburl=None, **params):