* Store config file in a sensible location.
authorUrban Wallasch <urban.wallasch@freenet.de>
Tue, 15 Jun 2021 19:21:30 +0000 (21:21 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Tue, 15 Jun 2021 19:21:30 +0000 (21:21 +0200)
jiten-pai.py

index 0ce9806d2bb2dcabf1b859e26f7da8a9c0a8ddea..fed4f3980adafd0106cebc205b790bd792fcc083 100755 (executable)
@@ -80,22 +80,56 @@ cfg = {
     'hl_col': 'blue',
     'history': [],
     'max_hist': 12,
+    'cfgfile': None,
 }
 
 def _save_cfg():
     try:
-        with open(_JITENPAI_CFG, 'w') as cfgfile:
+        with open(cfg['cfgfile'], 'w') as cfgfile:
+            cfg.pop('cfgfile', None)
             json.dump(cfg, cfgfile, indent=2)
+            return
     except Exception as e:
-        eprint(_JITENPAI_CFG, str(e))
+        eprint(cfg['cfgfile'], str(e))
+        cfg.pop('cfgfile', None)
+    cdirs = []
+    if os.environ.get('APPDATA'):
+        cdirs.append(os.environ.get('APPDATA'))
+    if os.environ.get('XDG_CONFIG_HOME'):
+        cdirs.append(os.environ.get('XDG_CONFIG_HOME'))
+    if os.environ.get('HOME'):
+        cdirs.append(os.path.join(os.environ.get('HOME'), '.config'))
+        cdirs.append(os.environ.get('HOME'))
+    cdirs.append(os.path.dirname(os.path.realpath(__file__)))
+    for d in cdirs:
+        cf = os.path.join(d, _JITENPAI_CFG)
+        try:
+            with open(cf, 'w') as cfgfile:
+                json.dump(cfg, cfgfile, indent=2)
+                return
+        except Exception as e:
+            eprint(cf, str(e))
 
 def _load_cfg():
-    try:
-        with open(_JITENPAI_CFG, 'r') as cfgfile:
-            cfg.update(json.load(cfgfile))
-    except Exception as e:
-        eprint(_JITENPAI_CFG, str(e))
-    #eprint('cfg =', json.dumps(cfg, indent=2))
+    cdirs = []
+    if os.environ.get('APPDATA'):
+        cdirs.append(os.environ.get('APPDATA'))
+    if os.environ.get('XDG_CONFIG_HOME'):
+        cdirs.append(os.environ.get('XDG_CONFIG_HOME'))
+    if os.environ.get('HOME'):
+        cdirs.append(os.path.join(os.environ.get('HOME'), '.config'))
+        cdirs.append(os.environ.get('HOME'))
+    cdirs.append(os.path.dirname(os.path.realpath(__file__)))
+    for d in cdirs:
+        cf = os.path.join(d, _JITENPAI_CFG)
+        try:
+            with open(cf, 'r') as cfgfile:
+                cfg.update(json.load(cfgfile))
+                cfg['cfgfile'] = cf
+                return
+        except Exception as e:
+            eprint(cf, str(e))
+            pass
 
 
 ############################################################