config.py 602 B

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