improved warning message
[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
7
8 # old stuff.. should be removed at some point
9 def replace_keys(obj, old_key, new_key):
10     if isinstance(obj, dict):
11         if old_key in obj:
12             obj[new_key] = obj[old_key]
13             del obj[old_key]
14         for elem in obj.itervalues():
15             replace_keys(elem, old_key, new_key)
16     elif isinstance(obj, list):
17         for elem in obj:
18             replace_keys(elem, old_key, new_key)
19
20 def old_to_new(d):
21     replace_keys(d, 'blk_hash', 'block_hash')
22     replace_keys(d, 'pos', 'index')
23     replace_keys(d, 'nTime', 'timestamp')
24     replace_keys(d, 'is_in', 'is_input')
25     replace_keys(d, 'raw_scriptPubKey', 'raw_output_script')
26
27
28
29 class SimpleConfig:
30
31     def __init__(self, options=None):
32
33         self.wallet_config = {}
34         if options and options.wallet_path:
35             self.read_wallet_config(options.wallet_path)
36
37         # system conf, readonly
38         self.system_config = {}
39         self.read_system_config()
40
41         # user conf, writeable
42         self.user_config = {}
43         self.read_user_config()
44
45         # command-line options
46         self.options_config = {}
47         if options:
48             if options.server: self.options_config['server'] = options.server
49             if options.proxy: self.options_config['proxy'] = options.proxy
50             if options.gui: self.options_config['gui'] = options.gui
51             
52         
53
54     def set_key(self, key, value, save = False):
55         # find where a setting comes from and save it there
56         if self.options_config.get(key):
57             return
58
59         elif self.user_config.get(key):
60             self.user_config[key] = value
61             if save: self.save_user_config()
62
63         elif self.system_config.get(key):
64             self.system_config[key] = value
65             print "Warning: cannot save '%s' because it is set in the system configuration file"%key
66
67         elif self.wallet_config.get(key):
68             self.wallet_config[key] = value
69             if save: self.save_wallet_config()
70
71         else:
72             # add key to wallet config
73             self.wallet_config[key] = value
74             if save: self.save_wallet_config()
75
76
77     def get(self, key, default=None):
78         # 1. command-line options always override everything
79         if self.options_config.has_key(key):
80             # print "found", key, "in options"
81             out = self.options_config.get(key)
82
83         # 2. user configuration 
84         elif self.user_config.has_key(key):
85             out = self.user_config.get(key)
86
87         # 2. system configuration
88         elif self.system_config.has_key(key):
89             out = self.system_config.get(key)
90
91         # 3. use the wallet file config
92         else:
93             out = self.wallet_config.get(key)
94
95         if out is None and default is not None:
96             out = default
97
98         # try to fix the type
99         if default is not None and type(out) != type(default):
100             import ast
101             out = ast.literal_eval(out)
102             
103         return out
104
105
106     def is_modifiable(self, key):
107         if self.options_config.has_key(key):
108             return False
109         elif self.user_config.has_key(key):
110             return True
111         elif self.system_config.has_key(key):
112             return False
113         else:
114             return True
115
116
117     def read_system_config(self):
118         name = '/etc/electrum.conf'
119         if os.path.exists(name):
120             try:
121                 import ConfigParser
122             except:
123                 print "cannot parse electrum.conf. please install ConfigParser"
124                 return
125                 
126             p = ConfigParser.ConfigParser()
127             p.read(name)
128             for k, v in p.items('client'):
129                 self.system_config[k] = v
130
131
132     def read_user_config(self):
133         name = os.path.join( user_dir(), 'electrum.conf')
134         if os.path.exists(name):
135             try:
136                 import ConfigParser
137             except:
138                 print "cannot parse electrum.conf. please install ConfigParser"
139                 return
140                 
141             p = ConfigParser.ConfigParser()
142             p.read(name)
143             for k, v in p.items('client'):
144                 self.user_config[k] = v
145
146
147     def init_path(self, wallet_path):
148         """Set the path of the wallet."""
149         if wallet_path is not None:
150             self.path = wallet_path
151             return
152
153         # Look for wallet file in the default data directory.
154         # Keeps backwards compatibility.
155         wallet_dir = user_dir()
156
157         # Make wallet directory if it does not yet exist.
158         if not os.path.exists(wallet_dir):
159             os.mkdir(wallet_dir)
160         self.path = os.path.join(wallet_dir, "electrum.dat")
161
162
163     def save_user_config(self):
164         import ConfigParser
165         config = ConfigParser.RawConfigParser()
166         config.add_section('client')
167         for k,v in self.user_config.items():
168             config.set('client', k, v)
169
170         with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
171             config.write(configfile)
172         
173
174
175
176     def read_wallet_config(self, path):
177         """Read the contents of the wallet file."""
178         self.wallet_file_exists = False
179         self.init_path(path)
180         try:
181             with open(self.path, "r") as f:
182                 data = f.read()
183         except IOError:
184             return
185         try:
186             d = ast.literal_eval( data )  #parse raw data from reading wallet file
187             old_to_new(d)
188         except:
189             raise IOError("Cannot read wallet file.")
190
191         self.wallet_config = d
192         self.wallet_file_exists = True
193
194
195
196     def save(self):
197         self.save_wallet_config()
198
199
200     def save_wallet_config(self):
201         s = repr(self.wallet_config)
202         f = open(self.path,"w")
203         f.write( s )
204         f.close()
205         import stat
206         os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
207