wrote docstring for get()
[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         """Retrieve the filepath of the configuration file specified in the 'key' parameter."""
62         # 1. command-line options always override everything
63         if self.options_config.has_key(key) and self.options_config.get(key) is not None:
64             out = self.options_config.get(key)
65
66         # 2. user configuration 
67         elif self.user_config.has_key(key):
68             out = self.user_config.get(key)
69
70         # 2. system configuration
71         elif self.system_config.has_key(key):
72             out = self.system_config.get(key)
73
74         # 3. use the wallet file config
75         else:
76             out = self.wallet_config.get(key)
77
78         if out is None and default is not None:
79             out = default
80
81         # try to fix the type
82         if default is not None and type(out) != type(default):
83             import ast
84             try:
85                 out = ast.literal_eval(out)
86             except:
87                 print "type error, using default value"
88                 out = default
89
90         return out
91
92
93     def is_modifiable(self, key):
94         if self.options_config.has_key(key):
95             return False
96         elif self.user_config.has_key(key):
97             return True
98         elif self.system_config.has_key(key):
99             return False
100         else:
101             return True
102
103
104     def read_system_config(self):
105         """Parse and store the system config settings in electrum.conf into system_config[]."""
106         name = '/etc/electrum.conf'
107         if os.path.exists(name):
108             try:
109                 import ConfigParser
110             except ImportError:
111                 print "cannot parse electrum.conf. please install ConfigParser"
112                 return
113                 
114             p = ConfigParser.ConfigParser()
115             p.read(name)
116             try:
117                 for k, v in p.items('client'):
118                     self.system_config[k] = v
119             except ConfigParser.NoSectionError:
120                 pass
121
122
123     def read_user_config(self):
124         """Parse and store the user config settings in electrum.conf into user_config[]."""
125         if not self.user_dir: return
126
127         name = os.path.join( self.user_dir, 'electrum.conf')
128         if os.path.exists(name):
129             try:
130                 import ConfigParser
131             except ImportError:
132                 print "cannot parse electrum.conf. please install ConfigParser"
133                 return
134                 
135             p = ConfigParser.ConfigParser()
136             p.read(name)
137             try:
138                 for k, v in p.items('client'):
139                     self.user_config[k] = v
140             except ConfigParser.NoSectionError:
141                 pass
142
143
144     def init_path(self, path):
145         """Set the path of the wallet."""
146
147         if not path:
148             path = self.get('default_wallet_path')
149
150         if path is not None:
151             self.path = path
152             return
153
154         # Look for wallet file in the default data directory.
155         # Make wallet directory if it does not yet exist.
156         if not os.path.exists(self.user_dir):
157             os.mkdir(self.user_dir)
158         self.path = os.path.join(self.user_dir, "electrum.dat")
159
160
161     def save_user_config(self):
162         if not self.user_dir: return
163
164         import ConfigParser
165         config = ConfigParser.RawConfigParser()
166         config.add_section('client')
167         for k,v in self.user_config.items():
168             config.set('client', k, v)
169
170         with open( os.path.join( self.user_dir, 'electrum.conf'), 'wb') as configfile:
171             config.write(configfile)
172         
173
174
175
176     def read_wallet_config(self, path):
177         """Read the contents of the wallet file."""
178         try:
179             with open(self.path, "r") as f:
180                 data = f.read()
181         except IOError:
182             return
183         try:
184             d = ast.literal_eval( data )  #parse raw data from reading wallet file
185         except:
186             raise IOError("Cannot read wallet file.")
187
188         self.wallet_config = d
189         self.wallet_file_exists = True
190
191
192
193     def save(self):
194         self.save_wallet_config()
195
196
197     def save_wallet_config(self):
198         s = repr(self.wallet_config)
199         f = open(self.path,"w")
200         f.write( s )
201         f.close()
202         if self.get('gui') != 'android':
203             import stat
204             os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
205