aboutsummaryrefslogtreecommitdiff
blob: f0a8418d2fec59ac76ff7f6a627e675fcab159c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys
import ConfigParser

class DBConfig(object):

    def __init__(self, configfile):
        """
        Initialie db config from 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):
        """
        Return db config as dict
        """
        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