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