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('client','server')
109 self.common_config['proxy'] = parse_proxy_options(p.get('client','proxy'))
113 self.common_config['gui'] = p.get('client','gui')
120 def init_path(self, wallet_path):
121 """Set the path of the wallet."""
122 if wallet_path is not None:
123 self.path = wallet_path
126 # Look for wallet file in the default data directory.
127 # Keeps backwards compatibility.
128 wallet_dir = user_dir()
130 # Make wallet directory if it does not yet exist.
131 if not os.path.exists(wallet_dir):
133 self.path = os.path.join(wallet_dir, "electrum.dat")
137 def save_common_config(self):
138 s = repr(self.common_config)
139 # todo: decide what to do
140 print "not saving settings in common config:", s
144 def read_wallet_config(self, path):
145 """Read the contents of the wallet file."""
146 self.wallet_file_exists = False
149 with open(self.path, "r") as f:
154 d = ast.literal_eval( data ) #parse raw data from reading wallet file
157 raise IOError("Cannot read wallet file.")
159 self.wallet_config = d
160 self.wallet_file_exists = True
163 def set_interface(self, interface):
166 def set_gui(self, gui):
170 self.save_wallet_config()
173 def save_wallet_config(self):
174 s = repr(self.wallet_config)
175 f = open(self.path,"w")
179 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)