From: Urban Wallasch Date: Tue, 15 Jun 2021 19:21:30 +0000 (+0200) Subject: * Store config file in a sensible location. X-Git-Tag: v0.1.0~132 X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=0392ba84cc3ad77385b8e20f7e030f955aa2c550;p=jiten-pai.git * Store config file in a sensible location. --- diff --git a/jiten-pai.py b/jiten-pai.py index 0ce9806..fed4f39 100755 --- a/jiten-pai.py +++ b/jiten-pai.py @@ -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 ############################################################