Mercurial > hgbook
annotate web/hgbook/dbutil.py @ 684:3b062018273a
Add an import.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Thu, 19 Mar 2009 22:40:31 -0700 |
parents | ad304b606163 |
children |
rev | line source |
---|---|
673
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 import MySQLdb as mysql |
684 | 2 import sys |
673
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 def connect(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 try: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 import secrets |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 except ImportError: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 print >> sys.stderr, 'Decrypt secrets.py.gpg or create a new copy!' |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 sys.exit(1) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 if secrets.DATABASE_ENGINE != 'mysql': |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 print >> sys.stderr, ('You are using a %s database' % |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 secrets.DATABASE_ENGINE) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 sys.exit(1) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 kwargs = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 'charset': 'utf8', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 'use_unicode': True, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 if secrets.DATABASE_USER: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 kwargs['user'] = secrets.DATABASE_USER |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 if secrets.DATABASE_NAME: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 kwargs['db'] = secrets.DATABASE_NAME |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 if secrets.DATABASE_PASSWORD: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 kwargs['passwd'] = secrets.DATABASE_PASSWORD |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 if secrets.DATABASE_HOST.startswith('/'): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 kwargs['unix_socket'] = secrets.DATABASE_HOST |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 elif secrets.DATABASE_HOST: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 kwargs['host'] = secrets.DATABASE_HOST |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 if secrets.DATABASE_PORT: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 kwargs['port'] = int(secrets.DATABASE_PORT) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 return mysql.connect(**kwargs) |