encapsulate parse_proxy_options
[electrum-nvc.git] / lib / simple_config.py
1 import json, ast
2 import os, ast
3 from util import user_dir
4
5 from version import ELECTRUM_VERSION, SEED_VERSION
6
7
8 # old stuff.. should be removed at some point
9 def replace_keys(obj, old_key, new_key):
10     if isinstance(obj, dict):
11         if old_key in obj:
12             obj[new_key] = obj[old_key]
13             del obj[old_key]
14         for elem in obj.itervalues():
15             replace_keys(elem, old_key, new_key)
16     elif isinstance(obj, list):
17         for elem in obj:
18             replace_keys(elem, old_key, new_key)
19
20 def old_to_new(d):
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')
26
27
28
29 class SimpleConfig:
30
31     def __init__(self, options):
32
33         self.wallet_config = {}
34         self.read_wallet_config(options.wallet_path)
35
36         self.common_config = {}
37         self.read_common_config()
38
39         self.options_config = {}
40
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
44         
45         
46
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):
50             return
51
52         elif self.wallet_config.get(key):
53             self.wallet_config[key] = value
54             if save: self.save_wallet_config()
55
56         elif self.common_config.get(key):
57             self.common_config[key] = value
58             if save: self.save_common_config()
59
60         else:
61             # add key to wallet config
62             self.wallet_config[key] = value
63             if save: self.save_wallet_config()
64
65
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)
71
72         # 2. configuration file overrides wallet file
73         elif self.common_config.has_key(key):
74             out = self.common_config.get(key)
75             
76         else:
77             out = self.wallet_config.get(key)
78
79         if out is None and default is not None:
80             out = default
81         return out
82
83
84     def is_modifiable(self, key):
85         if self.options_config.has_key(key) or self.common_config.has_key(key):
86             return False
87         else:
88             return True
89
90
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):
94                 try:
95                     import ConfigParser
96                 except:
97                     print "cannot parse electrum.conf. please install ConfigParser"
98                     return
99                 
100                 p = ConfigParser.ConfigParser()
101                 p.read(name)
102                 try:
103                     self.common_config['server'] = p.get('client','server')
104                 except:
105                     pass
106                 try:
107                     self.common_config['proxy'] = p.get('client','proxy')
108                 except:
109                     pass
110                 try:
111                     self.common_config['gui'] = p.get('client','gui')
112                 except:
113                     pass
114                 break
115
116
117
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
122             return
123
124         # Look for wallet file in the default data directory.
125         # Keeps backwards compatibility.
126         wallet_dir = user_dir()
127
128         # Make wallet directory if it does not yet exist.
129         if not os.path.exists(wallet_dir):
130             os.mkdir(wallet_dir)
131         self.path = os.path.join(wallet_dir, "electrum.dat")
132
133
134
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
139
140
141
142     def read_wallet_config(self, path):
143         """Read the contents of the wallet file."""
144         self.wallet_file_exists = False
145         self.init_path(path)
146         try:
147             with open(self.path, "r") as f:
148                 data = f.read()
149         except IOError:
150             return
151         try:
152             d = ast.literal_eval( data )  #parse raw data from reading wallet file
153             old_to_new(d)
154         except:
155             raise IOError("Cannot read wallet file.")
156
157         self.wallet_config = d
158         self.wallet_file_exists = True
159
160
161     def set_interface(self, interface):
162         pass
163
164     def set_gui(self, gui):
165         pass
166
167     def save(self):
168         self.save_wallet_config()
169
170
171     def save_wallet_config(self):
172         s = repr(self.wallet_config)
173         f = open(self.path,"w")
174         f.write( s )
175         f.close()
176         import stat
177         os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
178