android interface update and minor fixes
[electrum-nvc.git] / lib / simple_config.py
1 import json
2 import ast
3 import threading
4 import os
5
6 from util import user_dir, print_error
7
8
9
10 class SimpleConfig:
11     """
12 The SimpleConfig class is responsible for handling operations involving
13 configuration files.  The constructor reads and stores the system and 
14 user configurations from electrum.conf into separate dictionaries within
15 a SimpleConfig instance then reads the wallet file.
16 """
17     def __init__(self, options={}):
18         self.lock = threading.Lock()
19
20         # system conf, readonly
21         self.system_config = {}
22         if options.get('portable') is not True:
23             self.read_system_config()
24
25         # init path
26         self.init_path()
27
28         # user conf, writeable
29         self.user_config = {}
30         if options.get('portable') == False:
31             self.read_user_config()
32
33         # command-line options
34         self.options_config = options
35
36
37
38
39     def init_path(self):
40
41         # Read electrum path in the system configuration
42         self.path = self.system_config.get('electrum_path')
43
44         # If not set, use the user's default data directory.
45         if self.path is None:
46             self.path = user_dir()
47
48         # Make directory if it does not yet exist.
49         if not os.path.exists(self.path):
50             os.mkdir(self.path)
51
52         print_error( "electrum directory", self.path)
53
54         # portable wallet: use the same directory for wallet and headers file
55         #if options.get('portable'):
56         #    self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
57             
58     def set_key(self, key, value, save = True):
59         # find where a setting comes from and save it there
60         if self.options_config.get(key) is not None:
61             print "Warning: not changing '%s' because it was passed as a command-line option"%key
62             return
63
64         elif self.system_config.get(key) is not None:
65             if str(self.system_config[key]) != str(value):
66                 print "Warning: not changing '%s' because it was set in the system configuration"%key
67
68         else:
69
70             with self.lock:
71                 self.user_config[key] = value
72                 if save: 
73                     self.save_user_config()
74
75
76
77     def get(self, key, default=None):
78
79         out = None
80
81         # 1. command-line options always override everything
82         if self.options_config.has_key(key) and self.options_config.get(key) is not None:
83             out = self.options_config.get(key)
84
85         # 2. user configuration 
86         elif self.user_config.has_key(key):
87             out = self.user_config.get(key)
88
89         # 2. system configuration
90         elif self.system_config.has_key(key):
91             out = self.system_config.get(key)
92
93         if out is None and default is not None:
94             out = default
95
96         # try to fix the type
97         if default is not None and type(out) != type(default):
98             import ast
99             try:
100                 out = ast.literal_eval(out)
101             except:
102                 print "type error for '%s': using default value"%key
103                 out = default
104
105         return out
106
107
108     def is_modifiable(self, key):
109         """Check if the config file is modifiable."""
110         if self.options_config.has_key(key):
111             return False
112         elif self.user_config.has_key(key):
113             return True
114         elif self.system_config.has_key(key):
115             return False
116         else:
117             return True
118
119
120     def read_system_config(self):
121         """Parse and store the system config settings in electrum.conf into system_config[]."""
122         name = '/etc/electrum.conf'
123         if os.path.exists(name):
124             try:
125                 import ConfigParser
126             except ImportError:
127                 print "cannot parse electrum.conf. please install ConfigParser"
128                 return
129                 
130             p = ConfigParser.ConfigParser()
131             p.read(name)
132             try:
133                 for k, v in p.items('client'):
134                     self.system_config[k] = v
135             except ConfigParser.NoSectionError:
136                 pass
137
138
139     def read_user_config(self):
140         """Parse and store the user config settings in electrum.conf into user_config[]."""
141         if not self.path: return
142
143         path = os.path.join(self.path, "config")
144         if os.path.exists(path):
145             try:
146                 with open(path, "r") as f:
147                     data = f.read()
148             except IOError:
149                 return
150             try:
151                 d = ast.literal_eval( data )  #parse raw data from reading wallet file
152             except:
153                 raise IOError("Cannot read config file.")
154
155             self.user_config = d
156
157
158     def save_user_config(self):
159         if not self.path: return
160
161         path = os.path.join(self.path, "config")
162         s = repr(self.user_config)
163         f = open(path,"w")
164         f.write( s )
165         f.close()
166         if self.get('gui') != 'android':
167             import stat
168             os.chmod(path, stat.S_IREAD | stat.S_IWRITE)