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