util.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import sys
  3. import toml
  4. import platform
  5. def get_os():
  6. if sys.platform.startswith('java'):
  7. os_name = platform.java_ver()[3][0]
  8. if os_name.startswith('Windows'):
  9. system = 'win32'
  10. elif os_name.startswith('Mac'):
  11. system = 'macOS'
  12. else:
  13. system = 'linux'
  14. else:
  15. system = sys.platform
  16. return system
  17. def user_config_dir(appname, system):
  18. if system == "win32":
  19. path = windows_dir(appname)
  20. elif system == 'macOS':
  21. path = os.path.expanduser('~/Library/Preferences/')
  22. path = os.path.join(path, appname)
  23. else:
  24. path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
  25. path = os.path.join(path, appname)
  26. return path
  27. def windows_dir(appname):
  28. appauthor = appname
  29. const = "CSIDL_APPDATA"
  30. path = os.path.normpath(_get_win_folder(const))
  31. path = os.path.join(path, appname)
  32. return path
  33. def spawn_config(path):
  34. file_exists = os.path.exists(path)
  35. if file_exists:
  36. with open(path) as f:
  37. cfg = toml.load(f)
  38. return cfg
  39. else:
  40. with open('config.toml') as f:
  41. cfg = toml.load(f)
  42. with open(path, 'w') as f:
  43. toml.dump(cfg, f)
  44. print(f"Config file created in {path}. Please review it and try again.")
  45. sys.exit(0)