mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-17 19:48:31 +02:00
replace error prone json config with yaml config
This commit is contained in:
parent
446e78f225
commit
4dde183484
3 changed files with 23 additions and 30 deletions
|
@ -19,7 +19,8 @@ import argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
xdg_config = os.environ.get('XDG_CONFIG_HOME')
|
xdg_config = os.environ.get('XDG_CONFIG_HOME')
|
||||||
CONFIG_PATH = os.path.join(xdg_config, "jrnl") if xdg_config else os.path.expanduser('~/.jrnl_config')
|
CONFIG_PATH = os.path.join(xdg_config, "jrnl.yml") if xdg_config else \
|
||||||
|
os.path.expanduser('~/.jrnl_config.yml')
|
||||||
PYCRYPTO = install.module_exists("Crypto")
|
PYCRYPTO = install.module_exists("Crypto")
|
||||||
|
|
||||||
|
|
||||||
|
@ -125,7 +126,7 @@ def run(manual_args=None):
|
||||||
if not os.path.exists(CONFIG_PATH):
|
if not os.path.exists(CONFIG_PATH):
|
||||||
config = install.install_jrnl(CONFIG_PATH)
|
config = install.install_jrnl(CONFIG_PATH)
|
||||||
else:
|
else:
|
||||||
config = util.load_and_fix_json(CONFIG_PATH)
|
config = util.load_config(CONFIG_PATH)
|
||||||
install.upgrade_config(config, config_path=CONFIG_PATH)
|
install.upgrade_config(config, config_path=CONFIG_PATH)
|
||||||
|
|
||||||
if args.ls:
|
if args.ls:
|
||||||
|
|
|
@ -5,7 +5,7 @@ from __future__ import absolute_import
|
||||||
import readline
|
import readline
|
||||||
import glob
|
import glob
|
||||||
import getpass
|
import getpass
|
||||||
import json
|
import yaml
|
||||||
import os
|
import os
|
||||||
from . import util
|
from . import util
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ default_config = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def upgrade_config(config, config_path=os.path.expanduser("~/.jrnl_conf")):
|
def upgrade_config(config, config_path):
|
||||||
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
|
"""Checks if there are keys missing in a given config dict, and if so, updates the config file accordingly.
|
||||||
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
||||||
versions."""
|
versions."""
|
||||||
|
@ -43,16 +43,17 @@ def upgrade_config(config, config_path=os.path.expanduser("~/.jrnl_conf")):
|
||||||
for key in missing_keys:
|
for key in missing_keys:
|
||||||
config[key] = default_config[key]
|
config[key] = default_config[key]
|
||||||
with open(config_path, 'w') as f:
|
with open(config_path, 'w') as f:
|
||||||
json.dump(config, f, indent=2)
|
yaml.safe_dump(config, f, indent=2, allow_unicode=True)
|
||||||
print("[.jrnl_conf updated to newest version]")
|
print("[.jrnl_conf updated to newest version]")
|
||||||
|
|
||||||
|
|
||||||
def save_config(config=default_config, config_path=os.path.expanduser("~/.jrnl_conf")):
|
# def save_config(config=default_config, config_path):
|
||||||
|
def save_config(config, config_path):
|
||||||
with open(config_path, 'w') as f:
|
with open(config_path, 'w') as f:
|
||||||
json.dump(config, f, indent=2)
|
yaml.safe_dump(config, f, indent=2, allow_unicode=True)
|
||||||
|
|
||||||
|
|
||||||
def install_jrnl(config_path='~/.jrnl_config'):
|
def install_jrnl(config_path):
|
||||||
def autocomplete(text, state):
|
def autocomplete(text, state):
|
||||||
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text))+'*')
|
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text))+'*')
|
||||||
expansions = [e+"/" if os.path.isdir(e) else e for e in expansions]
|
expansions = [e+"/" if os.path.isdir(e) else e for e in expansions]
|
||||||
|
@ -89,9 +90,9 @@ def install_jrnl(config_path='~/.jrnl_config'):
|
||||||
|
|
||||||
open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there
|
open(default_config['journals']['default'], 'a').close() # Touch to make sure it's there
|
||||||
|
|
||||||
# Write config to ~/.jrnl_conf
|
# Write config to ~/.jrnl_conf.yml
|
||||||
with open(config_path, 'w') as f:
|
with open(config_path, 'w') as f:
|
||||||
json.dump(default_config, f, indent=2)
|
yaml.dump(default_config, f, indent=2, allow_unicode=True)
|
||||||
config = default_config
|
config = default_config
|
||||||
if password:
|
if password:
|
||||||
config['password'] = password
|
config['password'] = password
|
||||||
|
|
27
jrnl/util.py
27
jrnl/util.py
|
@ -4,7 +4,8 @@ import sys
|
||||||
import os
|
import os
|
||||||
import getpass as gp
|
import getpass as gp
|
||||||
import keyring
|
import keyring
|
||||||
import json
|
import yaml
|
||||||
|
import StringIO
|
||||||
if "win32" in sys.platform:
|
if "win32" in sys.platform:
|
||||||
import colorama
|
import colorama
|
||||||
colorama.init()
|
colorama.init()
|
||||||
|
@ -87,28 +88,18 @@ def yesno(prompt, default=True):
|
||||||
raw = py23_input(prompt)
|
raw = py23_input(prompt)
|
||||||
return {'y': True, 'n': False}.get(raw.lower(), default)
|
return {'y': True, 'n': False}.get(raw.lower(), default)
|
||||||
|
|
||||||
def load_and_fix_json(json_path):
|
def load_config(yaml_path):
|
||||||
"""Tries to load a json object from a file.
|
"""Tries to load a yaml object from a file.
|
||||||
If that fails, tries to fix common errors (no or extra , at end of the line).
|
If that fails, tries to fix common errors (no or extra , at end of the line).
|
||||||
"""
|
"""
|
||||||
with open(json_path) as f:
|
with open(yaml_path) as f:
|
||||||
json_str = f.read()
|
yaml_str = f.read()
|
||||||
config = fixed = None
|
config = fixed = None
|
||||||
try:
|
try:
|
||||||
return json.loads(json_str)
|
f = StringIO.StringIO(yaml_str)
|
||||||
|
return yaml.load(f)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
# Attempt to fix extra ,
|
prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(yaml_path, e.message))
|
||||||
json_str = re.sub(r",[ \n]*}", "}", json_str)
|
|
||||||
# Attempt to fix missing ,
|
|
||||||
json_str = re.sub(r"([^{,]) *\n *(\")", r"\1,\n \2", json_str)
|
|
||||||
try:
|
|
||||||
config = json.loads(json_str)
|
|
||||||
with open(json_path, 'w') as f:
|
|
||||||
json.dump(config, f, indent=2)
|
|
||||||
prompt("[Some errors in your jrnl config have been fixed for you.]")
|
|
||||||
return config
|
|
||||||
except ValueError as e:
|
|
||||||
prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(json_path, e.message))
|
|
||||||
prompt("[Entry was NOT added to your journal]")
|
prompt("[Entry was NOT added to your journal]")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue