4fa9c080a7c6b9c0d03a2a35d49c981f920d0c10
[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, print_msg
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         # command-line options
26         self.options_config = options
27
28         # init path
29         self.init_path()
30
31         # user conf, writeable
32         self.user_config = {}
33         self.read_user_config()
34
35
36
37
38
39     def init_path(self):
40
41         # Read electrum path in the command line configuration
42         self.path = self.options_config.get('electrum_path')
43
44         # Read electrum path in the system configuration
45         if self.path is None:
46             self.path = self.system_config.get('electrum_path')
47
48         # If not set, use the user's default data directory.
49         if self.path is None:
50             self.path = user_dir()
51
52         # Make directory if it does not yet exist.
53         if not os.path.exists(self.path):
54             os.mkdir(self.path)
55
56         print_error( "electrum directory", self.path)
57
58         # portable wallet: use the same directory for wallet and headers file
59         #if options.get('portable'):
60         #    self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
61             
62     def set_key(self, key, value, save = True):
63         # find where a setting comes from and save it there
64         if self.options_config.get(key) is not None:
65             print "Warning: not changing '%s' because it was passed as a command-line option"%key
66             return
67
68         elif self.system_config.get(key) is not None:
69             if str(self.system_config[key]) != str(value):
70                 print "Warning: not changing '%s' because it was set in the system configuration"%key
71
72         else:
73
74             with self.lock:
75                 self.user_config[key] = value
76                 if save: 
77                     self.save_user_config()
78
79
80
81     def get(self, key, default=None):
82
83         out = None
84
85         # 1. command-line options always override everything
86         if self.options_config.has_key(key) and self.options_config.get(key) is not None:
87             out = self.options_config.get(key)
88
89         # 2. user configuration 
90         elif self.user_config.has_key(key):
91             out = self.user_config.get(key)
92
93         # 2. system configuration
94         elif self.system_config.has_key(key):
95             out = self.system_config.get(key)
96
97         if out is None and default is not None:
98             out = default
99
100         # try to fix the type
101         if default is not None and type(out) != type(default):
102             import ast
103             try:
104                 out = ast.literal_eval(out)
105             except Exception:
106                 print "type error for '%s': using default value"%key
107                 out = default
108
109         return out
110
111
112     def is_modifiable(self, key):
113         """Check if the config file is modifiable."""
114         if self.options_config.has_key(key):
115             return False
116         elif self.user_config.has_key(key):
117             return True
118         elif self.system_config.has_key(key):
119             return False
120         else:
121             return True
122
123
124     def read_system_config(self):
125         """Parse and store the system config settings in electrum.conf into system_config[]."""
126         name = '/etc/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.system_config[k] = v
139             except ConfigParser.NoSectionError:
140                 pass
141
142
143     def read_user_config(self):
144         """Parse and store the user config settings in electrum.conf into user_config[]."""
145         if not self.path: return
146
147         path = os.path.join(self.path, "config")
148         if os.path.exists(path):
149             try:
150                 with open(path, "r") as f:
151                     data = f.read()
152             except IOError:
153                 return
154             try:
155                 d = ast.literal_eval( data )  #parse raw data from reading wallet file
156             except Exception:
157                 print_msg("Error: Cannot read config file.")
158                 return
159
160             self.user_config = d
161
162
163     def save_user_config(self):
164         if not self.path: return
165
166         path = os.path.join(self.path, "config")
167         s = repr(self.user_config)
168         f = open(path,"w")
169         f.write( s )
170         f.close()
171         if self.get('gui') != 'android':
172             import stat
173             os.chmod(path, stat.S_IREAD | stat.S_IWRITE)