Changed how load_config works so it always supports new config vars
[electrum-nvc.git] / lib / simple_config.py
1 import json
2 import os
3 from util import user_dir
4
5 class SimpleConfig:
6   default_options = {"gui": "lite", "proxy": { "mode": "none", "host":"localhost", "port":"8080" } }
7
8   def set_key(self, key, value, save = True):
9     self.config[key] = value
10     if save == True:
11       self.save_config()
12
13   def save_config(self):
14     f = open(self.config_file_path(), "w+")
15     f.write(json.dumps(self.config))
16
17   def load_config(self):
18     f = open(self.config_file_path(), "r")
19     file_contents = f.read()
20     if file_contents:
21       user_config = json.loads(file_contents)
22       for i in user_config:
23           self.config[i] = user_config[i]
24     else:
25       self.config = self.default_options
26       self.save_config()
27   
28   def config_file_path(self):
29     return "%s" % (self.config_folder + "/config.json")
30
31   def __init__(self):
32     # Find electrum data folder
33     self.config_folder = user_dir()
34     self.config = self.default_options
35     # Read the file
36     if os.path.exists(self.config_file_path()):
37       self.load_config()
38     self.save_config()
39