47a17e08f5920611f13107b1e14325a31c9e76e0
[electrum-nvc.git] / lib / simple_config.py
1 import json
2 import os
3
4 class SimpleConfig:
5   default_options = {"gui": "lite"}
6
7   def save_config(self):
8     f = open(self.config_file_path(), "w+")
9     f.write(json.dumps(self.config))
10
11   def load_config(self):
12     f = open(self.config_file_path(), "r")
13     file_contents = f.read()
14     if file_contents:
15       self.config = json.loads(file_contents)
16     else:
17       self.config = self.default_options
18       self.save_config()
19   
20   def config_file_path(self):
21     return "%s" % (self.config_folder + "/config.json")
22
23   def __init__(self):
24     # Find electrum data folder
25     if "HOME" in os.environ:
26       self.config_folder = os.path.join(os.environ["HOME"], ".electrum")
27     elif "LOCALAPPDATA" in os.environ:
28       self.config_folder = os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
29     elif "APPDATA" in os.environ:
30       self.config_folder = os.path.join(os.environ["APPDATA"], "Electrum")
31     else:
32       raise BaseException("No home directory found in environment variables.")
33
34     # Read the file
35     if os.path.exists(self.config_file_path()):
36       self.load_config()
37     else:
38       self.config = self.default_options
39       self.save_config()
40