settings.py 950 B

1234567891011121314151617181920212223242526272829
  1. import configparser
  2. import os
  3. class DefaultOption(dict):
  4. def __init__(self, config, section, **kv):
  5. self._config = config
  6. self._section = section
  7. dict.__init__(self, **kv)
  8. def items(self):
  9. _items = []
  10. for option in self:
  11. if not self._config.has_option(self._section, option):
  12. _items.append((option, self[option]))
  13. else:
  14. value_in_config = self._config.get(self._section, option)
  15. _items.append((option, value_in_config))
  16. return _items
  17. config = configparser.ConfigParser()
  18. if os.environ.get('APP_ENV', 'development') == 'development':
  19. config.readfp(open('development.ini'))
  20. with open('development.ini') as f:
  21. print(f.read())
  22. elif os.environ.get('APP_ENV') == 'production':
  23. config.readfp(open('production.ini'))
  24. print(f"get config of {os.environ.get('APP_ENV')}")
  25. print(config.get('DATABASE', 'host'))