dj-database-url/dj_database_url.py
Kenneth Reitz 7462e79d51 new style
2012-04-30 14:33:12 -04:00

35 lines
No EOL
846 B
Python

# -*- coding: utf-8 -*-
import os
import urlparse
# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('mysql')
DEFAULT_ENV = 'DATABASE_URL'
def config(env=DEFAULT_ENV):
"""Returns configured DATABASES dictionary."""
config = {}
if env in os.environ:
url = urlparse.urlparse(os.environ[env])
# Update with environment configuration.
config.update({
'NAME': url.path[1:],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port,
})
if url.scheme == 'postgres':
config['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
if url.scheme == 'mysql':
config['ENGINE'] = 'django.db.backends.mysql'
return config