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