Put database configuration file in dedicated config file
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 11 Aug 2010 16:11:50 +0000 (16:11 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 11 Aug 2010 16:11:50 +0000 (16:11 +0000)
INSTALL
bin/db.py [new file with mode: 0644]
bin/db.py.SAMPLE [deleted file]
bin/ntools.py
database.SAMPLE [new file with mode: 0644]

diff --git a/INSTALL b/INSTALL
index 308fa5a5aecea315d8fbf04a69b0dec02e2424a3..de75a50cf963cac57b99af5d7aa14def27e66aeb 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -36,6 +36,10 @@ create your file in /etc/ais/config
 chmod 660 /etc/ais/config
 see config.SAMPLE
 
+create your file in /etc/ais/database
+chmod 660 /etc/ais/database
+see database.SAMPLE
+
 = Postgres setup
 ================
 createuser ais
diff --git a/bin/db.py b/bin/db.py
new file mode 100644 (file)
index 0000000..d5be16a
--- /dev/null
+++ b/bin/db.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+import psycopg2, psycopg2.extensions
+
+from ais.ntools import read_cfg
+
+DATABASE_CONFIG_FILE = '/etc/ais/database'
+DUMP_SQL_QUERIES = False
+
+def get_connect_str():
+    cfg = read_cfg(DATABASE_CONFIG_FILE)
+
+    dbname = cfg.get('dbname', None)
+    assert dbname, u'You must define a database name'
+    connectstr = u'dbname=' + dbname
+    
+    host = cfg.get('host', None)
+    if host:
+        connectstr += u' host=' + host
+    user = cfg.get('user', None)
+    if user:
+        connectstr += u' user=' + user
+    password = cfg.get('password', None)
+    if password:
+        connectstr += u' password=' + password
+    return connectstr
+    
+__db = None
+def get_common_db():
+    global __db
+    if not __db:
+        psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
+        __db=psycopg2.connect(get_connect_str())
+        __db.set_client_encoding('UTF8')
+    return __db
+
+__cursor = None
+def get_common_cursor():
+    global __cursor
+    if not __cursor:
+        __cursor = get_common_db().cursor()
+    return __cursor
+
+def sql_setdebug(b):
+    global DUMP_SQL_QUERIES
+    DUMP_SQL_QUERIES = b
+
+def sqlexec(sql, *args, **kargs):
+    cursor = get_common_cursor()
+    if DUMP_SQL_QUERIES:
+        print cursor.mogrify(sql.encode('utf8'), *args, **kargs)
+    cursor.execute(sql, *args, **kargs)
+
+def dbcommit():
+    get_common_db().commit()
diff --git a/bin/db.py.SAMPLE b/bin/db.py.SAMPLE
deleted file mode 100644 (file)
index 4df57ac..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env python
-# -*- encoding: utf-8 -*-
-
-import psycopg2, psycopg2.extensions
-
-DUMP_SQL_QUERIES = False
-
-__db = None
-def get_common_db():
-    global __db
-    if not __db:
-        psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
-        __db=psycopg2.connect("dbname=ais host=localhost user=FIXME password=FIXME")
-        __db.set_client_encoding('UTF8')
-    return __db
-
-__cursor = None
-def get_common_cursor():
-    global __cursor
-    if not __cursor:
-        __cursor = get_common_db().cursor()
-    return __cursor
-
-def sql_setdebug(b):
-    global DUMP_SQL_QUERIES
-    DUMP_SQL_QUERIES = b
-
-def sqlexec(sql, *args, **kargs):
-    cursor = get_common_cursor()
-    if DUMP_SQL_QUERIES:
-        print cursor.mogrify(sql.encode('utf8'), *args, **kargs)
-    cursor.execute(sql, *args, **kargs)
-
-def dbcommit():
-    get_common_db().commit()
index 1233b7bde22bc2b26a3e89b6cf9b64e3c0c9fc3e..c8d8def6b51247387264c77e032eced78fa123da 100644 (file)
@@ -3,6 +3,7 @@
 __all__ = [
     'IPV4_IN_IPV6_PREFIX',
     'datetime_to_timestamp',
+    'read_cfg',
     'clean_ais_charset',
     'clean_alnum',
     'clean_alnum_unicode',
@@ -23,6 +24,26 @@ IPV4_IN_IPV6_PREFIX = '::ffff:'
 def datetime_to_timestamp(dt):
     return calendar.timegm(dt.utctimetuple())
 
+def read_cfg(filename):
+    '''
+    Function that reads a file in the form
+    key=value
+    and returns the resulting dictionary
+    '''
+    cfg = {}
+    for line in file(filename).readlines():
+        line = line.rstrip('\r\n\0')
+        line = unicode(line, 'utf-8')
+        if line.startswith(u'#'):
+            continue # skip comments
+        spl = line.split(u'=', 1)
+        if len(spl) == 2:
+            cfg[spl[0]] = spl[1]
+        else:
+            cfg[spl[0]] = None
+    return cfg
+
+
 def clean_ais_charset(txt):
     assert isinstance(txt, str)
     result = ''
diff --git a/database.SAMPLE b/database.SAMPLE
new file mode 100644 (file)
index 0000000..e76953c
--- /dev/null
@@ -0,0 +1,5 @@
+# postgres database configuration for AIS
+host=localhost
+dbname=ais
+user=ais
+password=topsecret