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