config.py 622 B

12345678910111213141516171819202122232425262728293031
  1. import yaml
  2. class Config:
  3. def __init__(self, cdict):
  4. attrs = set(self.attrs) # copy and "unfreeze"
  5. for k, v in cdict.items():
  6. attrs.remove(k) # check if the key is allowed, mark it as present
  7. setattr(self, k, v)
  8. if len(attrs) != 0:
  9. raise KeyError('missing required bot config keys: %s' % attrs)
  10. class WebConfig(Config):
  11. attrs = frozenset([
  12. 'port',
  13. 'host',
  14. 'api_port',
  15. 'api_host',
  16. 'debug',
  17. ])
  18. class DBConfig(Config):
  19. attrs = frozenset([
  20. 'host',
  21. 'user',
  22. 'password',
  23. 'database',
  24. ])
  25. __doc = yaml.load(open('config.yaml', 'r'))
  26. web = WebConfig(__doc['web'])
  27. db = DBConfig(__doc['db'])