Prevent a malformed tweaks.json file from stopping calibre startup

This commit is contained in:
Kovid Goyal 2019-04-25 09:42:23 +05:30
parent 7d6338caa8
commit 5a3678d8c6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -610,12 +610,20 @@ def exec_tweaks(path):
def read_custom_tweaks(): def read_custom_tweaks():
make_config_dir() make_config_dir()
tf = tweaks_file() tf = tweaks_file()
ans = {}
if os.path.exists(tf): if os.path.exists(tf):
with open(tf, 'rb') as f: with open(tf, 'rb') as f:
raw = f.read() raw = f.read()
raw = raw.strip()
if not raw:
return ans
try:
return json_loads(raw) return json_loads(raw)
except Exception:
import traceback
traceback.print_exc()
return ans
old_tweaks_file = tf.rpartition(u'.')[0] + u'.py' old_tweaks_file = tf.rpartition(u'.')[0] + u'.py'
ans = {}
if os.path.exists(old_tweaks_file): if os.path.exists(old_tweaks_file):
ans = exec_tweaks(old_tweaks_file) ans = exec_tweaks(old_tweaks_file)
ans = make_unicode(ans) ans = make_unicode(ans)