util.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import os
  18. import sys
  19. import toml
  20. import platform
  21. def get_os():
  22. if sys.platform.startswith('java'):
  23. os_name = platform.java_ver()[3][0]
  24. if os_name.startswith('Windows'):
  25. system = 'win32'
  26. elif os_name.startswith('Mac'):
  27. system = 'macOS'
  28. else:
  29. system = 'linux'
  30. else:
  31. system = sys.platform
  32. return system
  33. def user_config_dir(appname, system):
  34. if system == "win32":
  35. path = windows_dir(appname)
  36. elif system == 'macOS':
  37. path = os.path.expanduser('~/Library/Preferences/')
  38. path = os.path.join(path, appname)
  39. else:
  40. path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
  41. path = os.path.join(path, appname)
  42. return path
  43. def windows_dir(appname):
  44. appauthor = appname
  45. const = "CSIDL_APPDATA"
  46. path = os.path.normpath(_get_win_folder(const))
  47. path = os.path.join(path, appname)
  48. return path
  49. def spawn_config(path):
  50. file_exists = os.path.exists(path)
  51. if file_exists:
  52. with open(path) as f:
  53. cfg = toml.load(f)
  54. return cfg
  55. else:
  56. with open('dnet_config.toml') as f:
  57. cfg = toml.load(f)
  58. with open(path, 'w') as f:
  59. toml.dump(cfg, f)
  60. print(f"Config file created in {path}. Please review it and try again.")
  61. sys.exit(0)