1cf51d1b995f8d5cd5c7aef82f7906751d09a8a6
[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 = {
8         "gui": "lite",
9         "proxy": None,
10         "winpos-qt": [100, 100, 840, 400],
11         "winpos-lite": [4, 25, 351, 149],
12         "history": False
13         }
14     
15     def __init__(self):
16         # Find electrum data folder
17         self.config_folder = user_dir()
18         # Read the file
19         if os.path.exists(self.config_file_path()):
20             self.load_config()
21         else:
22             self.config = self.default_options
23             # Make config directory if it does not yet exist.
24             if not os.path.exists(self.config_folder):
25                 os.mkdir(self.config_folder)
26             self.save_config()
27
28         # This is a friendly fallback to the old style default proxy options
29         if(self.config.get("proxy") is not None and self.config["proxy"]["mode"] == "none"):
30             self.set_key("proxy", None, True)
31
32     def set_key(self, key, value, save = True):
33         self.config[key] = value
34         if save == True:
35             self.save_config()
36
37     def save_config(self):
38         if not os.path.exists(self.config_folder):
39             os.mkdir(self.config_folder)
40         f = open(self.config_file_path(), "w+")
41         f.write(json.dumps(self.config))
42
43     def load_config(self):
44         f = open(self.config_file_path(), "r")
45         file_contents = f.read()
46         if file_contents:
47             user_config = json.loads(file_contents)
48             for i in user_config:
49                 self.config[i] = user_config[i]
50         else:
51             self.config = self.default_options
52             self.save_config()
53   
54     def config_file_path(self):
55         return "%s" % (self.config_folder + "/config.json")
56
57     def __init__(self):
58         # Find electrum data folder
59         self.config_folder = user_dir()
60         self.config = self.default_options
61         # Read the file
62         if os.path.exists(self.config_file_path()):
63             self.load_config()
64         self.save_config()
65
66