mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Attempts to fix common errors in jrnl_config automatically
This commit is contained in:
parent
8e30a14603
commit
6a81b18544
2 changed files with 30 additions and 9 deletions
10
jrnl/jrnl.py
10
jrnl/jrnl.py
|
@ -22,8 +22,6 @@ import tempfile
|
||||||
import subprocess
|
import subprocess
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
try: import simplejson as json
|
|
||||||
except ImportError: import json
|
|
||||||
|
|
||||||
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") if xdg_config else os.path.expanduser('~/.jrnl_config')
|
||||||
|
@ -123,13 +121,7 @@ def cli(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:
|
||||||
with open(CONFIG_PATH) as f:
|
config = util.load_and_fix_json(CONFIG_PATH)
|
||||||
try:
|
|
||||||
config = json.load(f)
|
|
||||||
except ValueError as e:
|
|
||||||
util.prompt("[There seems to be something wrong with your jrnl config at {0}: {1}]".format(CONFIG_PATH, e.message))
|
|
||||||
util.prompt("[Entry was NOT added to your journal]")
|
|
||||||
sys.exit(1)
|
|
||||||
install.upgrade_config(config, config_path=CONFIG_PATH)
|
install.upgrade_config(config, config_path=CONFIG_PATH)
|
||||||
|
|
||||||
original_config = config.copy()
|
original_config = config.copy()
|
||||||
|
|
29
jrnl/util.py
29
jrnl/util.py
|
@ -6,6 +6,9 @@ from tzlocal import get_localzone
|
||||||
import getpass as gp
|
import getpass as gp
|
||||||
import keyring
|
import keyring
|
||||||
import pytz
|
import pytz
|
||||||
|
try: import simplejson as json
|
||||||
|
except ImportError: import json
|
||||||
|
import re
|
||||||
|
|
||||||
PY3 = sys.version_info[0] == 3
|
PY3 = sys.version_info[0] == 3
|
||||||
PY2 = sys.version_info[0] == 2
|
PY2 = sys.version_info[0] == 2
|
||||||
|
@ -88,3 +91,29 @@ def get_local_timezone():
|
||||||
if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
|
if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
|
||||||
__cached_tz = "UTC"
|
__cached_tz = "UTC"
|
||||||
return __cached_tz
|
return __cached_tz
|
||||||
|
|
||||||
|
def load_and_fix_json(json_path):
|
||||||
|
"""Tries to load a json 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()
|
||||||
|
config = fixed = None
|
||||||
|
try:
|
||||||
|
return json.loads(json_str)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue