mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-17 11:38:32 +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
|
||||
|
||||
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")
|
||||
|
||||
|
||||
|
@ -125,7 +126,7 @@ def run(manual_args=None):
|
|||
if not os.path.exists(CONFIG_PATH):
|
||||
config = install.install_jrnl(CONFIG_PATH)
|
||||
else:
|
||||
config = util.load_and_fix_json(CONFIG_PATH)
|
||||
config = util.load_config(CONFIG_PATH)
|
||||
install.upgrade_config(config, config_path=CONFIG_PATH)
|
||||
|
||||
if args.ls:
|
||||
|
|
|
@ -5,7 +5,7 @@ from __future__ import absolute_import
|
|||
import readline
|
||||
import glob
|
||||
import getpass
|
||||
import json
|
||||
import yaml
|
||||
import os
|
||||
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.
|
||||
This essentially automatically ports jrnl installations if new config parameters are introduced in later
|
||||
versions."""
|
||||
|
@ -43,16 +43,17 @@ def upgrade_config(config, config_path=os.path.expanduser("~/.jrnl_conf")):
|
|||
for key in missing_keys:
|
||||
config[key] = default_config[key]
|
||||
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]")
|
||||
|
||||
|
||||
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:
|
||||
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):
|
||||
expansions = glob.glob(os.path.expanduser(os.path.expandvars(text))+'*')
|
||||
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
|
||||
|
||||
# Write config to ~/.jrnl_conf
|
||||
# Write config to ~/.jrnl_conf.yml
|
||||
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
|
||||
if password:
|
||||
config['password'] = password
|
||||
|
|
31
jrnl/util.py
31
jrnl/util.py
|
@ -4,7 +4,8 @@ import sys
|
|||
import os
|
||||
import getpass as gp
|
||||
import keyring
|
||||
import json
|
||||
import yaml
|
||||
import StringIO
|
||||
if "win32" in sys.platform:
|
||||
import colorama
|
||||
colorama.init()
|
||||
|
@ -87,30 +88,20 @@ def yesno(prompt, default=True):
|
|||
raw = py23_input(prompt)
|
||||
return {'y': True, 'n': False}.get(raw.lower(), default)
|
||||
|
||||
def load_and_fix_json(json_path):
|
||||
"""Tries to load a json object from a file.
|
||||
def load_config(yaml_path):
|
||||
"""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).
|
||||
"""
|
||||
with open(json_path) as f:
|
||||
json_str = f.read()
|
||||
with open(yaml_path) as f:
|
||||
yaml_str = f.read()
|
||||
config = fixed = None
|
||||
try:
|
||||
return json.loads(json_str)
|
||||
f = StringIO.StringIO(yaml_str)
|
||||
return yaml.load(f)
|
||||
except ValueError as e:
|
||||
# Attempt to fix extra ,
|
||||
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]")
|
||||
sys.exit(1)
|
||||
prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(yaml_path, e.message))
|
||||
prompt("[Entry was NOT added to your journal]")
|
||||
sys.exit(1)
|
||||
|
||||
def get_text_from_editor(config, template=""):
|
||||
tmpfile = os.path.join(tempfile.mktemp(prefix="jrnl"))
|
||||
|
|
Loading…
Add table
Reference in a new issue