Quellcode durchsuchen

dnet: move config directory to platform specific config dir

lunar-mining vor 2 Jahren
Ursprung
Commit
9f1b19d17b
2 geänderte Dateien mit 68 neuen und 14 gelöschten Zeilen
  1. 18 14
      bin/dnet/main.py
  2. 50 0
      bin/dnet/util.py

+ 18 - 14
bin/dnet/main.py

@@ -15,20 +15,29 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-import sys, toml, json, urwid, asyncio, logging
+import sys, toml, json, urwid, asyncio, logging, util
 
+from os.path import exists, join
+from pathlib import Path
 from model import Model
 from rpc import JsonRpc
 from view import View
 
-
 class Dnetview:
 
     def __init__(self):
         self.ev = asyncio.new_event_loop()
         asyncio.set_event_loop(self.ev)
         self.queue = asyncio.Queue()
-        self.config = self.get_config()
+
+        os = util.get_os()
+        config_path = util.user_config_dir('darkfi', os)
+
+        suffix = '.toml'
+        filename = 'dnet_config'
+        path = join(config_path, filename + suffix)
+        self.config = util.spawn_config(path)
+
         self.model = Model()
         self.view = View(self.model)
 
@@ -42,15 +51,15 @@ class Dnetview:
         while True:
             try:
                 await rpc.start(host, port)
-                logging.debug(f"Started {name} RPC on port {port}")
+                logging.debug(f'Started {name} RPC on port {port}')
                 break
             except Exception as e:
                 info[name] = {}
                 await self.queue.put(info)
                 continue
     
-        if type == "NORMAL":
-            data = await rpc._make_request("p2p.get_info", [])
+        if type == 'NORMAL':
+            data = await rpc._make_request('p2p.get_info', [])
             info[name] = data
 
             await self.queue.put(info)
@@ -70,18 +79,13 @@ class Dnetview:
 
             await rpc.dnet_switch(False)
     
-        if type == "LILITH":
-            data = await rpc._make_request("spawns", [])
+        if type == 'LILITH':
+            data = await rpc._make_request('spawns', [])
             info[name] = data
             await self.queue.put(info)
 
         await rpc.stop()
 
-    def get_config(self):
-        with open("config.toml") as f:
-            cfg = toml.load(f)
-            return cfg
-    
     async def start_connect_slots(self, nodes):
         tasks = []
         async with asyncio.TaskGroup() as tg:
@@ -115,7 +119,7 @@ class Dnetview:
         logging.basicConfig(filename='dnet.log',
                             encoding='utf-8',
                             level=logging.DEBUG)
-        nodes = self.config.get("nodes")
+        nodes = self.config.get('nodes')
 
         loop = urwid.MainLoop(self.view.ui, self.view.palette,
                               unhandled_input=self.unhandled_input,

+ 50 - 0
bin/dnet/util.py

@@ -0,0 +1,50 @@
+import os
+import sys
+import toml
+import platform
+
+def get_os():
+   if sys.platform.startswith('java'):
+      os_name = platform.java_ver()[3][0]
+      if os_name.startswith('Windows'): 
+          system = 'win32'
+      elif os_name.startswith('Mac'):
+          system = 'macOS'
+      else: 
+          system = 'linux'
+   else:
+        system = sys.platform
+   return system
+
+def user_config_dir(appname, system):
+   if system == "win32":
+       path = windows_dir(appname)
+   elif system == 'macOS':
+       path = os.path.expanduser('~/Library/Preferences/')
+       path = os.path.join(path, appname)
+   else:
+       path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
+       path = os.path.join(path, appname)
+   return path
+
+def windows_dir(appname):
+   appauthor = appname
+   const = "CSIDL_APPDATA"
+   path = os.path.normpath(_get_win_folder(const))
+   path = os.path.join(path, appname)
+   return path
+
+def spawn_config(path):
+    file_exists = os.path.exists(path)
+    if file_exists:
+        with open(path) as f:
+            cfg = toml.load(f)
+            return cfg
+    else:
+        with open('config.toml') as f:
+            cfg = toml.load(f)
+        with open(path, 'w') as f:
+            toml.dump(cfg, f)
+        print(f"Config file created in {path}. Please review it and try again.")
+        sys.exit(0)
+