/ bin / dnet / src / util.py
util.py
 1  # This file is part of DarkFi (https://dark.fi)
 2  #
 3  # Copyright (C) 2020-2025 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  
18  import os
19  import sys
20  import toml
21  import platform
22  
23  def get_os():
24     if sys.platform.startswith('java'):
25        os_name = platform.java_ver()[3][0]
26        if os_name.startswith('Windows'): 
27            system = 'win32'
28        elif os_name.startswith('Mac'):
29            system = 'macOS'
30        else: 
31            system = 'linux'
32     else:
33          system = sys.platform
34     return system
35  
36  def user_config_dir(appname, system):
37     if system == "win32":
38         path = windows_dir(appname)
39     elif system == 'macOS':
40         path = os.path.expanduser('~/Library/Preferences/')
41         path = os.path.join(path, appname)
42     else:
43         path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
44         path = os.path.join(path, appname)
45     return path
46  
47  def windows_dir(appname):
48     appauthor = appname
49     const = "CSIDL_APPDATA"
50     path = os.path.normpath(_get_win_folder(const))
51     path = os.path.join(path, appname)
52     return path
53  
54  def spawn_config(path):
55      file_exists = os.path.exists(path)
56      if file_exists:
57          with open(path) as f:
58              cfg = toml.load(f)
59              return cfg
60      else:
61          with open('dnet_config.toml') as f:
62              cfg = toml.load(f)
63          with open(path, 'w') as f:
64              toml.dump(cfg, f)
65          print(f"Config file created in {path}. Please review it and try again.")
66          sys.exit(0)
67