3 from util import user_dir
5 from version import ELECTRUM_VERSION, SEED_VERSION
6 from interface import parse_proxy_options
9 # old stuff.. should be removed at some point
10 def replace_keys(obj, old_key, new_key):
11 if isinstance(obj, dict):
13 obj[new_key] = obj[old_key]
15 for elem in obj.itervalues():
16 replace_keys(elem, old_key, new_key)
17 elif isinstance(obj, list):
19 replace_keys(elem, old_key, new_key)
22 replace_keys(d, 'blk_hash', 'block_hash')
23 replace_keys(d, 'pos', 'index')
24 replace_keys(d, 'nTime', 'timestamp')
25 replace_keys(d, 'is_in', 'is_input')
26 replace_keys(d, 'raw_scriptPubKey', 'raw_output_script')
32 def __init__(self, options):
34 self.wallet_config = {}
35 self.read_wallet_config(options.wallet_path)
37 self.common_config = {}
38 self.read_common_config()
40 self.options_config = {}
42 if options.server: self.options_config['server'] = options.server
43 if options.proxy: self.options_config['proxy'] = parse_proxy_options(options.proxy)
44 if options.gui: self.options_config['gui'] = options.gui
48 def set_key(self, key, value, save = False):
49 # find where a setting comes from and save it there
50 if self.options_config.get(key):
53 elif self.wallet_config.get(key):
54 self.wallet_config[key] = value
55 if save: self.save_wallet_config()
57 elif self.common_config.get(key):
58 self.common_config[key] = value
59 if save: self.save_common_config()
62 # add key to wallet config
63 self.wallet_config[key] = value
64 if save: self.save_wallet_config()
67 def get(self, key, default=None):
68 # 1. command-line options always override everything
69 if self.options_config.has_key(key):
70 # print "found", key, "in options"
71 out = self.options_config.get(key)
73 # 2. configuration file overrides wallet file
74 elif self.common_config.has_key(key):
75 out = self.common_config.get(key)
78 out = self.wallet_config.get(key)
80 if out is None and default is not None:
85 def is_modifiable(self, key):
86 if self.options_config.has_key(key) or self.common_config.has_key(key):
92 def read_common_config(self):
93 for name in [ os.path.join( user_dir(), 'electrum.conf') , '/etc/electrum.conf']:
94 if os.path.exists(name):
95 from interface import parse_proxy_options
99 print "cannot parse electrum.conf. please install ConfigParser"
102 p = ConfigParser.ConfigParser()
105 self.common_config['server'] = p.get('interface','server')
109 self.common_config['proxy'] = parse_proxy_options(p.get('interface','proxy'))
116 def init_path(self, wallet_path):
117 """Set the path of the wallet."""
118 if wallet_path is not None:
119 self.path = wallet_path
122 # Look for wallet file in the default data directory.
123 # Keeps backwards compatibility.
124 wallet_dir = user_dir()
126 # Make wallet directory if it does not yet exist.
127 if not os.path.exists(wallet_dir):
129 self.path = os.path.join(wallet_dir, "electrum.dat")
133 def save_common_config(self):
134 s = repr(self.common_config)
135 # todo: decide what to do
136 print "not saving settings in common config:", s
140 def read_wallet_config(self, path):
141 """Read the contents of the wallet file."""
142 self.wallet_file_exists = False
145 with open(self.path, "r") as f:
150 d = ast.literal_eval( data ) #parse raw data from reading wallet file
153 raise IOError("Cannot read wallet file.")
155 self.wallet_config = d
156 self.wallet_file_exists = True
159 def set_interface(self, interface):
162 def set_gui(self, gui):
166 self.save_wallet_config()
169 def save_wallet_config(self):
170 s = repr(self.wallet_config)
171 f = open(self.path,"w")
175 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)