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) is not None:
62 print "Warning: not changing '%s' because it was passed as a command-line option"%key
65 elif self.user_config.get(key) is not None:
66 self.user_config[key] = value
67 if save: self.save_user_config()
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
73 elif self.wallet_config.get(key) is not None:
74 self.wallet_config[key] = value
75 if save: self.save_wallet_config()
78 # add key to wallet config
79 self.wallet_config[key] = value
80 if save: self.save_wallet_config()
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)
89 # 2. user configuration
90 elif self.user_config.has_key(key):
91 out = self.user_config.get(key)
93 # 2. system configuration
94 elif self.system_config.has_key(key):
95 out = self.system_config.get(key)
97 # 3. use the wallet file config
99 out = self.wallet_config.get(key)
101 if out is None and default is not None:
104 # try to fix the type
105 if default is not None and type(out) != type(default):
107 out = ast.literal_eval(out)
112 def is_modifiable(self, key):
113 if self.options_config.has_key(key):
115 elif self.user_config.has_key(key):
117 elif self.system_config.has_key(key):
123 def read_system_config(self):
124 name = '/etc/electrum.conf'
125 if os.path.exists(name):
129 print "cannot parse electrum.conf. please install ConfigParser"
132 p = ConfigParser.ConfigParser()
135 for k, v in p.items('client'):
136 self.system_config[k] = v
137 except ConfigParser.NoSectionError:
141 def read_user_config(self):
142 name = os.path.join( user_dir(), 'electrum.conf')
143 if os.path.exists(name):
147 print "cannot parse electrum.conf. please install ConfigParser"
150 p = ConfigParser.ConfigParser()
153 for k, v in p.items('client'):
154 self.user_config[k] = v
155 except ConfigParser.NoSectionError:
159 def init_path(self, wallet_path):
160 """Set the path of the wallet."""
161 if wallet_path is not None:
162 self.path = wallet_path
165 # Look for wallet file in the default data directory.
166 # Keeps backwards compatibility.
167 wallet_dir = user_dir()
169 # Make wallet directory if it does not yet exist.
170 if not os.path.exists(wallet_dir):
172 self.path = os.path.join(wallet_dir, "electrum.dat")
175 def save_user_config(self):
177 config = ConfigParser.RawConfigParser()
178 config.add_section('client')
179 for k,v in self.user_config.items():
180 config.set('client', k, v)
182 with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
183 config.write(configfile)
188 def read_wallet_config(self, path):
189 """Read the contents of the wallet file."""
190 self.wallet_file_exists = False
193 with open(self.path, "r") as f:
198 d = ast.literal_eval( data ) #parse raw data from reading wallet file
201 raise IOError("Cannot read wallet file.")
203 self.wallet_config = d
204 self.wallet_file_exists = True
209 self.save_wallet_config()
212 def save_wallet_config(self):
213 s = repr(self.wallet_config)
214 f = open(self.path,"w")
218 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)