reformat simple_config to comply with electrum and create config dir if it doesnt...
[electrum-nvc.git] / lib / simple_config.py
1 import json
2 import os
3 from util import user_dir
4
5 class SimpleConfig:
6
7     default_options = {"gui": "lite"}
8     
9     def __init__(self):
10         # Find electrum data folder
11         self.config_folder = user_dir()
12         # Read the file
13         if os.path.exists(self.config_file_path()):
14             self.load_config()
15         else:
16             self.config = self.default_options
17             # Make config directory if it does not yet exist.
18             if not os.path.exists(self.config_folder):
19                 os.mkdir(self.config_folder)
20             self.save_config()
21         
22     def set_key(self, key, value, save = True):
23         self.config[key] = value
24         if save == True:
25             self.save_config()
26     
27     def save_config(self):
28         f = open(self.config_file_path(), "w+")
29         f.write(json.dumps(self.config))
30     
31     def load_config(self):
32         f = open(self.config_file_path(), "r")
33         file_contents = f.read()
34         if file_contents:
35             self.config = json.loads(file_contents)
36         else:
37             self.config = self.default_options
38             self.save_config()
39     
40     def config_file_path(self):
41         return "%s" % (self.config_folder + "/config.json")
42