# -*- coding: utf-8 -*-

import os
import urlparse

# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('postgis')
urlparse.uses_netloc.append('mysql')
urlparse.uses_netloc.append('sqlite')

DEFAULT_ENV = 'DATABASE_URL'

def config(env=DEFAULT_ENV, default=None):
    """Returns configured DATABASE dictionary from DATABASE_URL."""

    config = {}

    s = os.environ.get(env, default)

    if s:
        config = parse(s)

    return config


def parse(url):
    """Parses a database URL."""

    config = {}

    url = urlparse.urlparse(url)

    # 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' or url.scheme == 'postgresql':
        config['ENGINE'] = 'django.db.backends.postgresql_psycopg2'

    if url.scheme == 'postgis':
        config['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

    if url.scheme == 'mysql':
        config['ENGINE'] = 'django.db.backends.mysql'

    if url.scheme == 'sqlite':
        config['ENGINE'] = 'django.db.backends.sqlite3'

    return config

@@ -267,16 +267,333 @@
 E_URL'%0A%0A
+SCHEMES = %7B%0A    'postgres': 'django.db.backends.postgresql_psycopg2',%0A    'postgresql': 'django.db.backends.postgresql_psycopg2',%0A    'postgis': 'django.contrib.gis.db.backends.postgis',%0A    'mysql': 'django.db.backends.mysql',%0A    'mysql2': 'django.db.backends.mysql',%0A    'sqlite': 'django.db.backends.sqlite3'%0A%7D%0A%0A%0A
 def conf
@@ -1154,394 +1154,66 @@
 eme 
-== 'postgres' or url.scheme == 'postgresql':%0A        config%5B'ENGINE'%5D = 'django.db.backends.postgresql_psycopg2'%0A%0A    if url.scheme == 'postgis':%0A        config%5B'ENGINE'%5D = 'django.contrib.gis.db.backends.postgis'%0A%0A    if url.scheme == 'mysql':%0A        config%5B'ENGINE'%5D = 'django.db.backends.mysql'%0A%0A    if url.scheme == 'sqlite':%0A        config%5B'ENGINE'%5D = 'django.db.backends.sqlite3'
+in SCHEMES:%0A        config%5B'ENGINE'%5D = SCHEMES%5Burl.scheme%5D
 %0A%0A  

refactor scheme lookup