Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sqlite to dburl2dict utility function #209

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 18 additions & 7 deletions web/db.py
Expand Up @@ -1143,20 +1143,31 @@ def dburl2dict(url):
{'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'}
>>> dburl2dict('sqlite:///mygreatdb.db')
{'db': 'mygreatdb.db', 'dbn': 'sqlite'}
>>> dburl2dict('sqlite:////absolute/path/mygreatdb.db')
{'db': '/absolute/path/mygreatdb.db', 'dbn': 'sqlite'}
"""
dbn, rest = url.split('://', 1)
user, rest = rest.split(':', 1)
pw, rest = rest.split('@', 1)
if ':' in rest:
host, rest = rest.split(':', 1)
port, rest = rest.split('/', 1)
if rest.startswith('/'):
_, rest = rest.split('/', 1)
user = pw = host = port = ''
else:
host, rest = rest.split('/', 1)
port = None
user, rest = rest.split(':', 1)
pw, 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

uq = urllib.unquote
out = dict(dbn=dbn, user=uq(user), pw=uq(pw), host=uq(host), db=uq(db))
if not user: out.pop('user')
if not pw: out.pop('pw')
if not host: out.pop('host')
if port: out['port'] = port
return out

Expand Down