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