From b67120122f77805800208a5e48f7cf39782dcd2f Mon Sep 17 00:00:00 2001 From: Vikraman Choudhury Date: Mon, 11 Jul 2011 19:44:32 +0530 Subject: make db configurable --- .gitignore | 1 + server/app.py | 2 +- server/config.py | 11 +++++++---- server/db.cfg.example | 6 ++++++ server/dbconfig.py | 24 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 server/db.cfg.example create mode 100644 server/dbconfig.py diff --git a/.gitignore b/.gitignore index a253c69..7ac969b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.*~ *.pyc *.swp +server/db.cfg diff --git a/server/app.py b/server/app.py index 3d31d73..e957cde 100755 --- a/server/app.py +++ b/server/app.py @@ -33,7 +33,7 @@ urls = ( r'/host/(.+)', 'Host' ) -app = web.application(urls, globals()) +app = web.application(urls, globals(), autoreload=True) app.notfound = config.notfound app.internalerror = config.internalerror diff --git a/server/config.py b/server/config.py index a7b0f26..98326ae 100644 --- a/server/config.py +++ b/server/config.py @@ -2,15 +2,18 @@ import os import sys import web +from dbconfig import DBConfig +rootdir = os.path.abspath(os.path.dirname(__file__)) + '/' + +dbconfig = DBConfig(rootdir + 'db.cfg').get_config() db = web.database( dbn='mysql', - user='gentoostats', - pw='poicyurp3ZaddajGhaf', - db='gentoostats' + db=dbconfig['DB'], + user=dbconfig['USER'], + pw=dbconfig['PASS'] ) -rootdir = os.path.abspath(os.path.dirname(__file__)) + '/' render = web.template.render(rootdir + 'templates/', base='layout') def notfound(): diff --git a/server/db.cfg.example b/server/db.cfg.example new file mode 100644 index 0000000..1857e2f --- /dev/null +++ b/server/db.cfg.example @@ -0,0 +1,6 @@ + +[MYSQL] + +DB = gentoostats +USER = gentoo +PASS = gentoo diff --git a/server/dbconfig.py b/server/dbconfig.py new file mode 100644 index 0000000..e5eb42c --- /dev/null +++ b/server/dbconfig.py @@ -0,0 +1,24 @@ + +import sys +import ConfigParser + +class DBConfig(object): + + def __init__(self, configfile): + self.config = ConfigParser.ConfigParser() + if len(self.config.read(configfile)) == 0: + sys.stderr.write('Cannot read ' + configfile) + sys.exit(1) + + def get_config(self): + ret = dict() + try: + ret['DB'] = self.config.get('MYSQL', 'DB') + ret['USER'] = self.config.get('MYSQL', 'USER') + ret['PASS'] = self.config.get('MYSQL', 'PASS') + + except ConfigParser.NoSectionError, ConfigParser.NoOptionError: + sys.stderr.write('Invalid db config') + sys.exit(1) + + return ret -- cgit v1.2.3-65-gdbad