Mercurial > hgbook
annotate web/hgbook/dbutil.py @ 737:9038da8b2d22
Merge with http://hg.serpentine.com/mercurial/book
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Mon, 16 Mar 2009 17:23:05 +0800 |
parents | ad304b606163 |
children | 3b062018273a |
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 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 def connect(): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 try: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 import secrets |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 except ImportError: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 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
|
8 sys.exit(1) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 if secrets.DATABASE_ENGINE != 'mysql': |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 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
|
12 secrets.DATABASE_ENGINE) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 sys.exit(1) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 kwargs = { |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 'charset': 'utf8', |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 'use_unicode': True, |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 } |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 if secrets.DATABASE_USER: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 kwargs['user'] = secrets.DATABASE_USER |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 if secrets.DATABASE_NAME: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 kwargs['db'] = secrets.DATABASE_NAME |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 if secrets.DATABASE_PASSWORD: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 kwargs['passwd'] = secrets.DATABASE_PASSWORD |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 if secrets.DATABASE_HOST.startswith('/'): |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 kwargs['unix_socket'] = secrets.DATABASE_HOST |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 elif secrets.DATABASE_HOST: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 kwargs['host'] = secrets.DATABASE_HOST |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 if secrets.DATABASE_PORT: |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 kwargs['port'] = int(secrets.DATABASE_PORT) |
ad304b606163
Initial cut at web comment system import
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 return mysql.connect(**kwargs) |