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