tie confirmation icons to payment verifier
[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:
35             # this will call read_wallet_config only if there is a wallet_path value in options
36             try:
37                 self.read_wallet_config(options.wallet_path)
38             except:
39                 pass
40             
41
42         # system conf, readonly
43         self.system_config = {}
44         self.read_system_config()
45
46         # user conf, writeable
47         self.user_config = {}
48         self.read_user_config()
49
50         # command-line options
51         self.options_config = {}
52         if options:
53             if options.server: self.options_config['server'] = options.server
54             if options.proxy: self.options_config['proxy'] = options.proxy
55             if options.gui: self.options_config['gui'] = options.gui
56             
57         
58
59     def set_key(self, key, value, save = False):
60         # find where a setting comes from and save it there
61         if self.options_config.get(key) is not None:
62             print "Warning: not changing '%s' because it was passed as a command-line option"%key
63             return
64
65         elif self.user_config.get(key) is not None:
66             self.user_config[key] = value
67             if save: self.save_user_config()
68
69         elif self.system_config.get(key) is not None:
70             if str(self.system_config[key]) != str(value):
71                 print "Warning: not changing '%s' because it was set in the system configuration"%key
72
73         elif self.wallet_config.get(key) is not None:
74             self.wallet_config[key] = value
75             if save: self.save_wallet_config()
76
77         else:
78             # add key to wallet config
79             self.wallet_config[key] = value
80             if save: self.save_wallet_config()
81
82
83     def get(self, key, default=None):
84         # 1. command-line options always override everything
85         if self.options_config.has_key(key):
86             # print "found", key, "in options"
87             out = self.options_config.get(key)
88
89         # 2. user configuration 
90         elif self.user_config.has_key(key):
91             out = self.user_config.get(key)
92
93         # 2. system configuration
94         elif self.system_config.has_key(key):
95             out = self.system_config.get(key)
96
97         # 3. use the wallet file config
98         else:
99             out = self.wallet_config.get(key)
100
101         if out is None and default is not None:
102             out = default
103
104         # try to fix the type
105         if default is not None and type(out) != type(default):
106             import ast
107             try:
108                 out = ast.literal_eval(out)
109             except:
110                 print "type error, using default value"
111                 out = default
112
113         return out
114
115
116     def is_modifiable(self, key):
117         if self.options_config.has_key(key):
118             return False
119         elif self.user_config.has_key(key):
120             return True
121         elif self.system_config.has_key(key):
122             return False
123         else:
124             return True
125
126
127     def read_system_config(self):
128         name = '/etc/electrum.conf'
129         if os.path.exists(name):
130             try:
131                 import ConfigParser
132             except:
133                 print "cannot parse electrum.conf. please install ConfigParser"
134                 return
135                 
136             p = ConfigParser.ConfigParser()
137             p.read(name)
138             try:
139                 for k, v in p.items('client'):
140                     self.system_config[k] = v
141             except ConfigParser.NoSectionError:
142                 pass
143
144
145     def read_user_config(self):
146         name = os.path.join( user_dir(), 'electrum.conf')
147         if os.path.exists(name):
148             try:
149                 import ConfigParser
150             except:
151                 print "cannot parse electrum.conf. please install ConfigParser"
152                 return
153                 
154             p = ConfigParser.ConfigParser()
155             p.read(name)
156             try:
157                 for k, v in p.items('client'):
158                     self.user_config[k] = v
159             except ConfigParser.NoSectionError:
160                 pass
161
162
163     def init_path(self, wallet_path):
164         """Set the path of the wallet."""
165         if wallet_path is not None:
166             self.path = wallet_path
167             return
168
169         # Look for wallet file in the default data directory.
170         # Keeps backwards compatibility.
171         wallet_dir = user_dir()
172
173         # Make wallet directory if it does not yet exist.
174         if not os.path.exists(wallet_dir):
175             os.mkdir(wallet_dir)
176         self.path = os.path.join(wallet_dir, "electrum.dat")
177
178
179     def save_user_config(self):
180         import ConfigParser
181         config = ConfigParser.RawConfigParser()
182         config.add_section('client')
183         for k,v in self.user_config.items():
184             config.set('client', k, v)
185
186         with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
187             config.write(configfile)
188         
189
190
191
192     def read_wallet_config(self, path):
193         """Read the contents of the wallet file."""
194         self.wallet_file_exists = False
195         self.init_path(path)
196         try:
197             with open(self.path, "r") as f:
198                 data = f.read()
199         except IOError:
200             return
201         try:
202             d = ast.literal_eval( data )  #parse raw data from reading wallet file
203             old_to_new(d)
204         except:
205             raise IOError("Cannot read wallet file.")
206
207         self.wallet_config = d
208         self.wallet_file_exists = True
209
210
211
212     def save(self):
213         self.save_wallet_config()
214
215
216     def save_wallet_config(self):
217         s = repr(self.wallet_config)
218         f = open(self.path,"w")
219         f.write( s )
220         f.close()
221         import stat
222         os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
223