config.py 713 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 配置管理器
  5. Configuration Manager
  6. """
  7. from configparser import ConfigParser
  8. import os
  9. class ConfigHandler:
  10. def __init__(self, config_file=None):
  11. self.config = ConfigParser()
  12. if os.path.exists(config_file):
  13. self.config.read(config_file, encoding='utf-8')
  14. # @staticmethod
  15. def get(self, section, option, default=None):
  16. try:
  17. value = self.config.get(section, option)
  18. if "#" in value:
  19. value = value.split('#')[0].strip()
  20. except Exception:
  21. value = default
  22. return value
  23. # 全局配置实例
  24. config_handler = ConfigHandler("./config/config.ini")