remove format conversion; unneeded since new protocol
[electrum-nvc.git] / lib / simple_config.py
1 import json, ast
2 import os, ast
3 from util import user_dir, print_error
4
5 from version import ELECTRUM_VERSION, SEED_VERSION
6
7
8
9 class SimpleConfig:
10
11     def __init__(self, options=None):
12
13         # system conf, readonly
14         self.system_config = {}
15         self.read_system_config()
16
17         # user conf, writeable
18         self.user_config = {}
19         self.read_user_config()
20
21         # command-line options
22         self.options_config = {}
23         if options:
24             if options.server: self.options_config['server'] = options.server
25             if options.proxy: self.options_config['proxy'] = options.proxy
26             if options.gui: self.options_config['gui'] = options.gui
27
28
29         self.wallet_config = {}
30         self.wallet_file_exists = False
31         self.init_path(options)
32         print_error( "path", self.path )
33         if self.path:
34             self.read_wallet_config(self.path)
35             
36             
37         
38
39     def set_key(self, key, value, save = False):
40         # find where a setting comes from and save it there
41         if self.options_config.get(key) is not None:
42             print "Warning: not changing '%s' because it was passed as a command-line option"%key
43             return
44
45         elif self.user_config.get(key) is not None:
46             self.user_config[key] = value
47             if save: self.save_user_config()
48
49         elif self.system_config.get(key) is not None:
50             if str(self.system_config[key]) != str(value):
51                 print "Warning: not changing '%s' because it was set in the system configuration"%key
52
53         elif self.wallet_config.get(key) is not None:
54             self.wallet_config[key] = value
55             if save: self.save_wallet_config()
56
57         else:
58             # add key to wallet config
59             self.wallet_config[key] = value
60             if save: self.save_wallet_config()
61
62
63     def get(self, key, default=None):
64         # 1. command-line options always override everything
65         if self.options_config.has_key(key):
66             # print "found", key, "in options"
67             out = self.options_config.get(key)
68
69         # 2. user configuration 
70         elif self.user_config.has_key(key):
71             out = self.user_config.get(key)
72
73         # 2. system configuration
74         elif self.system_config.has_key(key):
75             out = self.system_config.get(key)
76
77         # 3. use the wallet file config
78         else:
79             out = self.wallet_config.get(key)
80
81         if out is None and default is not None:
82             out = default
83
84         # try to fix the type
85         if default is not None and type(out) != type(default):
86             import ast
87             try:
88                 out = ast.literal_eval(out)
89             except:
90                 print "type error, using default value"
91                 out = default
92
93         return out
94
95
96     def is_modifiable(self, key):
97         if self.options_config.has_key(key):
98             return False
99         elif self.user_config.has_key(key):
100             return True
101         elif self.system_config.has_key(key):
102             return False
103         else:
104             return True
105
106
107     def read_system_config(self):
108         name = '/etc/electrum.conf'
109         if os.path.exists(name):
110             try:
111                 import ConfigParser
112             except:
113                 print "cannot parse electrum.conf. please install ConfigParser"
114                 return
115                 
116             p = ConfigParser.ConfigParser()
117             p.read(name)
118             try:
119                 for k, v in p.items('client'):
120                     self.system_config[k] = v
121             except ConfigParser.NoSectionError:
122                 pass
123
124
125     def read_user_config(self):
126         name = os.path.join( user_dir(), 'electrum.conf')
127         if os.path.exists(name):
128             try:
129                 import ConfigParser
130             except:
131                 print "cannot parse electrum.conf. please install ConfigParser"
132                 return
133                 
134             p = ConfigParser.ConfigParser()
135             p.read(name)
136             try:
137                 for k, v in p.items('client'):
138                     self.user_config[k] = v
139             except ConfigParser.NoSectionError:
140                 pass
141
142
143     def init_path(self, options):
144         """Set the path of the wallet."""
145
146         path = None
147         if options:
148             # this will call read_wallet_config only if there is a wallet_path value in options
149             try:
150                 path = options.wallet_path
151             except:
152                 pass
153
154         if not path:
155             path = self.get('default_wallet_path')
156
157         if path is not None:
158             self.path = path
159             return
160
161         # Look for wallet file in the default data directory.
162         # Keeps backwards compatibility.
163         wallet_dir = user_dir()
164
165         # Make wallet directory if it does not yet exist.
166         if not os.path.exists(wallet_dir):
167             os.mkdir(wallet_dir)
168         self.path = os.path.join(wallet_dir, "electrum.dat")
169
170
171     def save_user_config(self):
172         import ConfigParser
173         config = ConfigParser.RawConfigParser()
174         config.add_section('client')
175         for k,v in self.user_config.items():
176             config.set('client', k, v)
177
178         with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
179             config.write(configfile)
180         
181
182
183
184     def read_wallet_config(self, path):
185         """Read the contents of the wallet file."""
186         try:
187             with open(self.path, "r") as f:
188                 data = f.read()
189         except IOError:
190             return
191         try:
192             d = ast.literal_eval( data )  #parse raw data from reading wallet file
193         except:
194             raise IOError("Cannot read wallet file.")
195
196         self.wallet_config = d
197         self.wallet_file_exists = True
198
199
200
201     def save(self):
202         self.save_wallet_config()
203
204
205     def save_wallet_config(self):
206         s = repr(self.wallet_config)
207         f = open(self.path,"w")
208         f.write( s )
209         f.close()
210         import stat
211         os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
212