From 881e15ae12bc2a0a39e20b22485d40031b991345 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Thu, 2 Apr 2015 10:00:07 +0200 Subject: [PATCH] store config as json --- lib/simple_config.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/simple_config.py b/lib/simple_config.py index b9ad42f4d..4a7e7b242 100644 --- a/lib/simple_config.py +++ b/lib/simple_config.py @@ -1,4 +1,5 @@ import ast +import json import threading import os @@ -122,12 +123,12 @@ class SimpleConfig(object): return True def save_user_config(self): - if not self.path: return - + if not self.path: + return path = os.path.join(self.path, "config") - s = repr(self.user_config) - f = open(path,"w") - f.write( s ) + s = json.dumps(self.user_config, indent=4, sort_keys=True) + f = open(path, "w") + f.write(s) f.close() if self.get('gui') != 'android': import stat @@ -155,22 +156,23 @@ def read_system_config(path=SYSTEM_CONFIG_PATH): def read_user_config(path): """Parse and store the user config settings in electrum.conf into user_config[].""" - if not path: return {} # Return a dict, since we will call update() on it. - + if not path: + return {} config_path = os.path.join(path, "config") - result = {} - if os.path.exists(config_path): + try: + with open(config_path, "r") as f: + data = f.read() + except IOError: + print_msg("Error: Cannot read config file.") + return {} + try: + result = json.loads(data) + except: try: - - with open(config_path, "r") as f: - data = f.read() - result = ast.literal_eval( data ) #parse raw data from reading wallet file - - except Exception: + result = ast.literal_eval(data) + except: print_msg("Error: Cannot read config file.") - result = {} - - if not type(result) is dict: return {} - + if not type(result) is dict: + return {} return result