config.py 962 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. import sys
  4. import configparser
  5. class Config(object):
  6. config = None
  7. def __init__(self):
  8. try:
  9. # 读取配置文件
  10. config = configparser.ConfigParser()
  11. config.read('/data/quantization/config.cfg', encoding='utf-8')
  12. self.config = config
  13. except IOError as e:
  14. print('cant open file -> ~/data/quantization/config.cfg', e)
  15. sys.exit(1)
  16. def get(self, section, position):
  17. return self.config.get(section, position)
  18. def read(self, section, position, filter={}):
  19. mongo_client = pymongo.MongoClient(self.get('mongodb', 'host'), int(self.get('mongodb', 'port')))
  20. db = mongo_client[self.get('mongodb', 'db')]
  21. db.authenticate(self.get('mongodb', 'user'), self.get('mongodb', 'password'))
  22. s = db[section]
  23. result = s.find_one(filter)
  24. return result[position]
  25. config = Config()