big refactoring: command line options and electrum.conf options override settings...
[electrum-nvc.git] / lib / simple_config.py
1 import json, ast
2 import os, ast
3 from util import user_dir
4
5 from version import ELECTRUM_VERSION, SEED_VERSION
6 from interface import parse_proxy_options
7
8
9 # old stuff.. should be removed at some point
10 def replace_keys(obj, old_key, new_key):
11     if isinstance(obj, dict):
12         if old_key in obj:
13             obj[new_key] = obj[old_key]
14             del obj[old_key]
15         for elem in obj.itervalues():
16             replace_keys(elem, old_key, new_key)
17     elif isinstance(obj, list):
18         for elem in obj:
19             replace_keys(elem, old_key, new_key)
20
21 def old_to_new(d):
22     replace_keys(d, 'blk_hash', 'block_hash')
23     replace_keys(d, 'pos', 'index')
24     replace_keys(d, 'nTime', 'timestamp')
25     replace_keys(d, 'is_in', 'is_input')
26     replace_keys(d, 'raw_scriptPubKey', 'raw_output_script')
27
28
29
30 class SimpleConfig:
31
32     def __init__(self, options):
33
34         self.wallet_config = {}
35         self.read_wallet_config(options.wallet_path)
36
37         self.common_config = {}
38         self.read_common_config()
39
40         self.options_config = {}
41
42         if options.server: self.options_config['server'] = options.server
43         if options.proxy: self.options_config['proxy'] = parse_proxy_options(options.proxy)
44         if options.gui: self.options_config['gui'] = options.gui
45         
46         
47
48     def set_key(self, key, value, save = False):
49         # find where a setting comes from and save it there
50         if self.options_config.get(key):
51             return
52
53         elif self.wallet_config.get(key):
54             self.wallet_config[key] = value
55             if save: self.save_wallet_config()
56
57         elif self.common_config.get(key):
58             self.common_config[key] = value
59             if save: self.save_common_config()
60
61         else:
62             # add key to wallet config
63             self.wallet_config[key] = value
64             if save: self.save_wallet_config()
65
66
67     def get(self, key, default=None):
68         # 1. command-line options always override everything
69         if self.options_config.has_key(key):
70             # print "found", key, "in options"
71             out = self.options_config.get(key)
72
73         # 2. configuration file overrides wallet file
74         elif self.common_config.has_key(key):
75             out = self.common_config.get(key)
76             
77         else:
78             out = self.wallet_config.get(key)
79
80         if out is None and default is not None:
81             out = default
82         return out
83
84
85     def is_modifiable(self, key):
86         if self.options_config.has_key(key) or self.common_config.has_key(key):
87             return False
88         else:
89             return True
90
91
92     def read_common_config(self):
93         for name in [ os.path.join( user_dir(), 'electrum.conf') , '/etc/electrum.conf']:
94             if os.path.exists(name):
95                 from interface import parse_proxy_options
96                 try:
97                     import ConfigParser
98                 except:
99                     print "cannot parse electrum.conf. please install ConfigParser"
100                     return
101                 
102                 p = ConfigParser.ConfigParser()
103                 p.read(name)
104                 try:
105                     self.common_config['server'] = p.get('interface','server')
106                 except:
107                     pass
108                 try:
109                     self.common_config['proxy'] = parse_proxy_options(p.get('interface','proxy'))
110                 except:
111                     pass
112                 break
113
114
115
116     def init_path(self, wallet_path):
117         """Set the path of the wallet."""
118         if wallet_path is not None:
119             self.path = wallet_path
120             return
121
122         # Look for wallet file in the default data directory.
123         # Keeps backwards compatibility.
124         wallet_dir = user_dir()
125
126         # Make wallet directory if it does not yet exist.
127         if not os.path.exists(wallet_dir):
128             os.mkdir(wallet_dir)
129         self.path = os.path.join(wallet_dir, "electrum.dat")
130
131
132
133     def save_common_config(self):
134         s = repr(self.common_config)
135         # todo: decide what to do 
136         print "not saving settings in common config:", s
137
138
139
140     def read_wallet_config(self, path):
141         """Read the contents of the wallet file."""
142         self.wallet_file_exists = False
143         self.init_path(path)
144         try:
145             with open(self.path, "r") as f:
146                 data = f.read()
147         except IOError:
148             return
149         try:
150             d = ast.literal_eval( data )  #parse raw data from reading wallet file
151             old_to_new(d)
152         except:
153             raise IOError("Cannot read wallet file.")
154
155         self.wallet_config = d
156         self.wallet_file_exists = True
157
158
159     def set_interface(self, interface):
160         pass
161
162     def set_gui(self, gui):
163         pass
164
165     def save(self):
166         self.save_wallet_config()
167
168
169     def save_wallet_config(self):
170         s = repr(self.wallet_config)
171         f = open(self.path,"w")
172         f.write( s )
173         f.close()
174         import stat
175         os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
176