settings.py 887 B

123456789101112131415161718192021222324252627
  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. elif os.environ.get('APP_ENV') == 'production':
  21. config.readfp(open('production.ini'))
  22. print(f"get config of {os.environ.get('APP_ENV')}")
  23. print(config.get('DATABASE', 'host'))