3 from util import user_dir, print_error
5 from version import ELECTRUM_VERSION, SEED_VERSION
11 The SimpleConfig class is responsible for handling operations involving
12 configuration files. The constructor reads and stores the system and
13 user configurations from electrum.conf into seperate dictionaries within
14 a SimpleConfig instance then reads the wallet file.
16 def __init__(self, options={}):
18 # system conf, readonly
19 self.system_config = {}
20 self.read_system_config()
22 # user conf, writeable
23 self.user_dir = user_dir()
25 self.read_user_config()
27 # command-line options
28 self.options_config = options
30 self.wallet_config = {}
31 self.wallet_file_exists = False
32 self.init_path(self.options_config.get('wallet_path'))
33 print_error( "path", self.path )
35 self.read_wallet_config(self.path)
41 def set_key(self, key, value, save = False):
42 # find where a setting comes from and save it there
43 if self.options_config.get(key) is not None:
44 print "Warning: not changing '%s' because it was passed as a command-line option"%key
47 elif self.user_config.get(key) is not None:
48 self.user_config[key] = value
49 if save: self.save_user_config()
51 elif self.system_config.get(key) is not None:
52 if str(self.system_config[key]) != str(value):
53 print "Warning: not changing '%s' because it was set in the system configuration"%key
55 elif self.wallet_config.get(key) is not None:
56 self.wallet_config[key] = value
57 if save: self.save_wallet_config()
60 # add key to wallet config
61 self.wallet_config[key] = value
62 if save: self.save_wallet_config()
65 def get(self, key, default=None):
66 """Retrieve the filepath of the configuration file specified in the 'key' parameter."""
67 # 1. command-line options always override everything
68 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
69 out = self.options_config.get(key)
71 # 2. user configuration
72 elif self.user_config.has_key(key):
73 out = self.user_config.get(key)
75 # 2. system configuration
76 elif self.system_config.has_key(key):
77 out = self.system_config.get(key)
79 # 3. use the wallet file config
81 out = self.wallet_config.get(key)
83 if out is None and default is not None:
87 if default is not None and type(out) != type(default):
90 out = ast.literal_eval(out)
92 print "type error, using default value"
98 def is_modifiable(self, key):
99 """Check if the config file is modifiable."""
100 if self.options_config.has_key(key):
102 elif self.user_config.has_key(key):
104 elif self.system_config.has_key(key):
110 def read_system_config(self):
111 """Parse and store the system config settings in electrum.conf into system_config[]."""
112 name = '/etc/electrum.conf'
113 if os.path.exists(name):
117 print "cannot parse electrum.conf. please install ConfigParser"
120 p = ConfigParser.ConfigParser()
123 for k, v in p.items('client'):
124 self.system_config[k] = v
125 except ConfigParser.NoSectionError:
129 def read_user_config(self):
130 """Parse and store the user config settings in electrum.conf into user_config[]."""
131 if not self.user_dir: return
133 name = os.path.join( self.user_dir, 'electrum.conf')
134 if os.path.exists(name):
138 print "cannot parse electrum.conf. please install ConfigParser"
141 p = ConfigParser.ConfigParser()
144 for k, v in p.items('client'):
145 self.user_config[k] = v
146 except ConfigParser.NoSectionError:
150 def init_path(self, path):
151 """Set the path of the wallet."""
154 path = self.get('default_wallet_path')
160 # Look for wallet file in the default data directory.
161 # Make wallet directory if it does not yet exist.
162 if not os.path.exists(self.user_dir):
163 os.mkdir(self.user_dir)
164 self.path = os.path.join(self.user_dir, "electrum.dat")
167 def save_user_config(self):
168 if not self.user_dir: return
171 config = ConfigParser.RawConfigParser()
172 config.add_section('client')
173 for k,v in self.user_config.items():
174 config.set('client', k, v)
176 with open( os.path.join( self.user_dir, 'electrum.conf'), 'wb') as configfile:
177 config.write(configfile)
182 def read_wallet_config(self, path):
183 """Read the contents of the wallet file."""
185 with open(self.path, "r") as f:
190 d = ast.literal_eval( data ) #parse raw data from reading wallet file
192 raise IOError("Cannot read wallet file.")
194 self.wallet_config = d
195 self.wallet_file_exists = True
200 self.save_wallet_config()
203 def save_wallet_config(self):
204 s = repr(self.wallet_config)
205 f = open(self.path,"w")
208 if self.get('gui') != 'android':
210 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)