config.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from os import path
  2. import sys
  3. from importlib import util
  4. def resource_path(relative_path, persistent=False):
  5. """
  6. Get the resource path
  7. """
  8. base_path = path.abspath(".")
  9. total_path = path.join(base_path, relative_path)
  10. if persistent or path.exists(total_path):
  11. return total_path
  12. else:
  13. try:
  14. base_path = sys._MEIPASS
  15. return path.join(base_path, relative_path)
  16. except Exception:
  17. return total_path
  18. def load_external_config(config_path):
  19. """
  20. Load the external config file
  21. """
  22. config = None
  23. if path.exists(config_path):
  24. spec = util.spec_from_file_location("config", config_path)
  25. config = util.module_from_spec(spec)
  26. spec.loader.exec_module(config)
  27. else:
  28. import config
  29. return config
  30. def get_config():
  31. """
  32. Get the config
  33. """
  34. user_config_path = resource_path("user_config.py")
  35. default_config_path = resource_path("config.py")
  36. config = (
  37. load_external_config(user_config_path)
  38. if path.exists(user_config_path)
  39. else load_external_config(default_config_path)
  40. )
  41. return config