Navigation Menu

Skip to content

Commit

Permalink
some improvements to dburl2dict
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsw committed Sep 10, 2012
1 parent 4f0e7c6 commit 736ce8d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions web/db.py
Expand Up @@ -11,7 +11,7 @@
"database", 'DB',
]

import time, os
import time, os, urllib
try:
import datetime
except ImportError:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 736ce8d

Please sign in to comment.