#!/usr/bin/env python
#coding=utf-8

import sys
import configparser


class Config(object):
    config = None

    def __init__(self):
        try:
            # 读取配置文件
            config = configparser.ConfigParser()
            config.read('/data/quantization/config.cfg', encoding='utf-8')
            self.config = config
        except IOError as e:
            print('cant open file -> ~/data/quantization/config.cfg', e)
            sys.exit(1)

    def get(self, section, position):
        return self.config.get(section, position)

    def read(self, section, position, filter={}):
        mongo_client = pymongo.MongoClient(self.get('mongodb', 'host'), int(self.get('mongodb', 'port')))
        db = mongo_client[self.get('mongodb', 'db')]
        db.authenticate(self.get('mongodb', 'user'), self.get('mongodb', 'password'))

        s = db[section]
        result = s.find_one(filter)
        return result[position]


config = Config()
