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 = {}
34 if options and options.wallet_path:
35 self.read_wallet_config(options.wallet_path)
37 # system conf, readonly
38 self.system_config = {}
39 self.read_system_config()
41 # user conf, writeable
43 self.read_user_config()
45 # command-line options
46 self.options_config = {}
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
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):
59 elif self.user_config.get(key):
60 self.user_config[key] = value
61 if save: self.save_user_config()
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
67 elif self.wallet_config.get(key):
68 self.wallet_config[key] = value
69 if save: self.save_wallet_config()
72 # add key to wallet config
73 self.wallet_config[key] = value
74 if save: self.save_wallet_config()
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)
83 # 2. user configuration
84 elif self.user_config.has_key(key):
85 out = self.user_config.get(key)
87 # 2. system configuration
88 elif self.system_config.has_key(key):
89 out = self.system_config.get(key)
91 # 3. use the wallet file config
93 out = self.wallet_config.get(key)
95 if out is None and default is not None:
99 if default is not None and type(out) != type(default):
101 out = ast.literal_eval(out)
106 def is_modifiable(self, key):
107 if self.options_config.has_key(key):
109 elif self.user_config.has_key(key):
111 elif self.system_config.has_key(key):
117 def read_system_config(self):
118 name = '/etc/electrum.conf'
119 if os.path.exists(name):
123 print "cannot parse electrum.conf. please install ConfigParser"
126 p = ConfigParser.ConfigParser()
128 for k, v in p.items('client'):
129 self.system_config[k] = v
132 def read_user_config(self):
133 name = os.path.join( user_dir(), 'electrum.conf')
134 if os.path.exists(name):
138 print "cannot parse electrum.conf. please install ConfigParser"
141 p = ConfigParser.ConfigParser()
143 for k, v in p.items('client'):
144 self.user_config[k] = v
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
153 # Look for wallet file in the default data directory.
154 # Keeps backwards compatibility.
155 wallet_dir = user_dir()
157 # Make wallet directory if it does not yet exist.
158 if not os.path.exists(wallet_dir):
160 self.path = os.path.join(wallet_dir, "electrum.dat")
163 def save_user_config(self):
165 config = ConfigParser.RawConfigParser()
166 config.add_section('client')
167 for k,v in self.user_config.items():
168 config.set('client', k, v)
170 with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
171 config.write(configfile)
176 def read_wallet_config(self, path):
177 """Read the contents of the wallet file."""
178 self.wallet_file_exists = False
181 with open(self.path, "r") as f:
186 d = ast.literal_eval( data ) #parse raw data from reading wallet file
189 raise IOError("Cannot read wallet file.")
191 self.wallet_config = d
192 self.wallet_file_exists = True
197 self.save_wallet_config()
200 def save_wallet_config(self):
201 s = repr(self.wallet_config)
202 f = open(self.path,"w")
206 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)