3 from util import user_dir
5 from version import ELECTRUM_VERSION, SEED_VERSION
8 # old stuff.. should be removed at some point
9 def replace_keys(obj, old_key, new_key):
10 if isinstance(obj, dict):
12 obj[new_key] = obj[old_key]
14 for elem in obj.itervalues():
15 replace_keys(elem, old_key, new_key)
16 elif isinstance(obj, list):
18 replace_keys(elem, old_key, new_key)
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')
31 def __init__(self, options=None):
33 self.wallet_config = {}
35 # this will call read_wallet_config only if there is a wallet_path value in options
37 self.read_wallet_config(options.wallet_path)
42 # system conf, readonly
43 self.system_config = {}
44 self.read_system_config()
46 # user conf, writeable
48 self.read_user_config()
50 # command-line options
51 self.options_config = {}
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
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):
64 elif self.user_config.get(key):
65 self.user_config[key] = value
66 if save: self.save_user_config()
68 elif self.system_config.get(key):
69 if str(self.system_config[key]) != str(value):
70 print "Warning: not changing '%s' because it was set in the system configuration"%key
72 elif self.wallet_config.get(key):
73 self.wallet_config[key] = value
74 if save: self.save_wallet_config()
77 # add key to wallet config
78 self.wallet_config[key] = value
79 if save: self.save_wallet_config()
82 def get(self, key, default=None):
83 # 1. command-line options always override everything
84 if self.options_config.has_key(key):
85 # print "found", key, "in options"
86 out = self.options_config.get(key)
88 # 2. user configuration
89 elif self.user_config.has_key(key):
90 out = self.user_config.get(key)
92 # 2. system configuration
93 elif self.system_config.has_key(key):
94 out = self.system_config.get(key)
96 # 3. use the wallet file config
98 out = self.wallet_config.get(key)
100 if out is None and default is not None:
103 # try to fix the type
104 if default is not None and type(out) != type(default):
106 out = ast.literal_eval(out)
111 def is_modifiable(self, key):
112 if self.options_config.has_key(key):
114 elif self.user_config.has_key(key):
116 elif self.system_config.has_key(key):
122 def read_system_config(self):
123 name = '/etc/electrum.conf'
124 if os.path.exists(name):
128 print "cannot parse electrum.conf. please install ConfigParser"
131 p = ConfigParser.ConfigParser()
134 for k, v in p.items('client'):
135 self.system_config[k] = v
136 except ConfigParser.NoSectionError:
140 def read_user_config(self):
141 name = os.path.join( user_dir(), 'electrum.conf')
142 if os.path.exists(name):
146 print "cannot parse electrum.conf. please install ConfigParser"
149 p = ConfigParser.ConfigParser()
152 for k, v in p.items('client'):
153 self.user_config[k] = v
154 except ConfigParser.NoSectionError:
158 def init_path(self, wallet_path):
159 """Set the path of the wallet."""
160 if wallet_path is not None:
161 self.path = wallet_path
164 # Look for wallet file in the default data directory.
165 # Keeps backwards compatibility.
166 wallet_dir = user_dir()
168 # Make wallet directory if it does not yet exist.
169 if not os.path.exists(wallet_dir):
171 self.path = os.path.join(wallet_dir, "electrum.dat")
174 def save_user_config(self):
176 config = ConfigParser.RawConfigParser()
177 config.add_section('client')
178 for k,v in self.user_config.items():
179 config.set('client', k, v)
181 with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
182 config.write(configfile)
187 def read_wallet_config(self, path):
188 """Read the contents of the wallet file."""
189 self.wallet_file_exists = False
192 with open(self.path, "r") as f:
197 d = ast.literal_eval( data ) #parse raw data from reading wallet file
200 raise IOError("Cannot read wallet file.")
202 self.wallet_config = d
203 self.wallet_file_exists = True
208 self.save_wallet_config()
211 def save_wallet_config(self):
212 s = repr(self.wallet_config)
213 f = open(self.path,"w")
217 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)