view web/hgbook/dbutil.py @ 794:41bb6ec2ca27

Propagate c36a6f534b99 Fix named branching book section. 'hg update -C branchname' is no longer required to jump from one named branch to another one; this can also be done simply using 'hg update branchname'.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 18 Jun 2009 16:23:58 +0900
parents 3b062018273a
children
line wrap: on
line source

import MySQLdb as mysql
import sys

def connect():
    try:
        import secrets
    except ImportError:
        print >> sys.stderr, 'Decrypt secrets.py.gpg or create a new copy!'
        sys.exit(1)

    if secrets.DATABASE_ENGINE != 'mysql':
        print >> sys.stderr, ('You are using a %s database' %
                              secrets.DATABASE_ENGINE)
        sys.exit(1)

    kwargs = {
        'charset': 'utf8',
        'use_unicode': True,
        }
    if secrets.DATABASE_USER:
        kwargs['user'] = secrets.DATABASE_USER
    if secrets.DATABASE_NAME:
        kwargs['db'] = secrets.DATABASE_NAME
    if secrets.DATABASE_PASSWORD:
        kwargs['passwd'] = secrets.DATABASE_PASSWORD
    if secrets.DATABASE_HOST.startswith('/'):
        kwargs['unix_socket'] = secrets.DATABASE_HOST
    elif secrets.DATABASE_HOST:
        kwargs['host'] = secrets.DATABASE_HOST
    if secrets.DATABASE_PORT:
        kwargs['port'] = int(secrets.DATABASE_PORT)
    return mysql.connect(**kwargs)