config.py 613 B

123456789101112131415161718192021222324252627282930
  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. 'cookie_secret',
  15. 'debug',
  16. ])
  17. class DBConfig(Config):
  18. attrs = frozenset([
  19. 'host',
  20. 'user',
  21. 'password',
  22. 'database',
  23. ])
  24. __doc = yaml.load(open('config.yaml', 'r'))
  25. web = WebConfig(__doc['web'])
  26. db = DBConfig(__doc['db'])