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