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
--- /dev/null
+#!/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()
+++ /dev/null
-#!/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()
__all__ = [
'IPV4_IN_IPV6_PREFIX',
'datetime_to_timestamp',
+ 'read_cfg',
'clean_ais_charset',
'clean_alnum',
'clean_alnum_unicode',
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 = ''
--- /dev/null
+# postgres database configuration for AIS
+host=localhost
+dbname=ais
+user=ais
+password=topsecret