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):
33 self.wallet_config = {}
34 self.read_wallet_config(options.wallet_path)
36 self.common_config = {}
37 self.read_common_config()
39 self.options_config = {}
41 if options.server: self.options_config['server'] = options.server
42 if options.proxy: self.options_config['proxy'] = options.proxy
43 if options.gui: self.options_config['gui'] = options.gui
47 def set_key(self, key, value, save = False):
48 # find where a setting comes from and save it there
49 if self.options_config.get(key):
52 elif self.wallet_config.get(key):
53 self.wallet_config[key] = value
54 if save: self.save_wallet_config()
56 elif self.common_config.get(key):
57 self.common_config[key] = value
58 if save: self.save_common_config()
61 # add key to wallet config
62 self.wallet_config[key] = value
63 if save: self.save_wallet_config()
66 def get(self, key, default=None):
67 # 1. command-line options always override everything
68 if self.options_config.has_key(key):
69 # print "found", key, "in options"
70 out = self.options_config.get(key)
72 # 2. configuration file overrides wallet file
73 elif self.common_config.has_key(key):
74 out = self.common_config.get(key)
77 out = self.wallet_config.get(key)
79 if out is None and default is not None:
84 def is_modifiable(self, key):
85 if self.options_config.has_key(key) or self.common_config.has_key(key):
91 def read_common_config(self):
92 for name in [ os.path.join( user_dir(), 'electrum.conf') , '/etc/electrum.conf']:
93 if os.path.exists(name):
97 print "cannot parse electrum.conf. please install ConfigParser"
100 p = ConfigParser.ConfigParser()
103 self.common_config['server'] = p.get('client','server')
107 self.common_config['proxy'] = p.get('client','proxy')
111 self.common_config['gui'] = p.get('client','gui')
118 def init_path(self, wallet_path):
119 """Set the path of the wallet."""
120 if wallet_path is not None:
121 self.path = wallet_path
124 # Look for wallet file in the default data directory.
125 # Keeps backwards compatibility.
126 wallet_dir = user_dir()
128 # Make wallet directory if it does not yet exist.
129 if not os.path.exists(wallet_dir):
131 self.path = os.path.join(wallet_dir, "electrum.dat")
135 def save_common_config(self):
136 s = repr(self.common_config)
137 # todo: decide what to do
138 print "not saving settings in common config:", s
142 def read_wallet_config(self, path):
143 """Read the contents of the wallet file."""
144 self.wallet_file_exists = False
147 with open(self.path, "r") as f:
152 d = ast.literal_eval( data ) #parse raw data from reading wallet file
155 raise IOError("Cannot read wallet file.")
157 self.wallet_config = d
158 self.wallet_file_exists = True
161 def set_interface(self, interface):
164 def set_gui(self, gui):
168 self.save_wallet_config()
171 def save_wallet_config(self):
172 s = repr(self.wallet_config)
173 f = open(self.path,"w")
177 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)