const.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. #
  3. import os
  4. from dotenv import load_dotenv
  5. from .conf import ConfigManager
  6. __all__ = ['BASE_DIR', 'PROJECT_DIR', 'VERSION', 'CONFIG']
  7. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  8. LOG_DIR = os.path.join('/', 'opt', 'maxkb', 'logs')
  9. PROJECT_DIR = os.path.dirname(BASE_DIR)
  10. VERSION = '2.0.0'
  11. # load environment variables from .env file
  12. load_dotenv()
  13. def _find_config_root():
  14. """Auto-detect config directory across platforms."""
  15. # 1. Explicit env var takes priority
  16. if os.getenv('MAXKB_CONFIG') is not None:
  17. return PROJECT_DIR
  18. config_names = ('config.yml', 'config.yaml', 'config_example.yml')
  19. # 2. Project root directory
  20. for name in config_names:
  21. if os.path.isfile(os.path.join(PROJECT_DIR, name)):
  22. return PROJECT_DIR
  23. # 3. conf/ subdirectory under project root
  24. conf_dir = os.path.join(PROJECT_DIR, 'conf')
  25. if os.path.isdir(conf_dir):
  26. for name in config_names:
  27. if os.path.isfile(os.path.join(conf_dir, name)):
  28. return conf_dir
  29. # 4. Fallback to /opt/maxkb/conf (Linux convention)
  30. return os.path.abspath('/opt/maxkb/conf')
  31. CONFIG = ConfigManager.load_user_config(root_path=_find_config_root())