3 from util import user_dir, print_error
5 from version import ELECTRUM_VERSION, SEED_VERSION
11 def __init__(self, options=None):
13 # system conf, readonly
14 self.system_config = {}
15 self.read_system_config()
17 # user conf, writeable
19 self.read_user_config()
21 # command-line options
22 self.options_config = {}
24 if options.server: self.options_config['server'] = options.server
25 if options.proxy: self.options_config['proxy'] = options.proxy
26 if options.gui: self.options_config['gui'] = options.gui
29 self.wallet_config = {}
30 self.wallet_file_exists = False
31 self.init_path(options)
32 print_error( "path", self.path )
34 self.read_wallet_config(self.path)
39 def set_key(self, key, value, save = False):
40 # find where a setting comes from and save it there
41 if self.options_config.get(key) is not None:
42 print "Warning: not changing '%s' because it was passed as a command-line option"%key
45 elif self.user_config.get(key) is not None:
46 self.user_config[key] = value
47 if save: self.save_user_config()
49 elif self.system_config.get(key) is not None:
50 if str(self.system_config[key]) != str(value):
51 print "Warning: not changing '%s' because it was set in the system configuration"%key
53 elif self.wallet_config.get(key) is not None:
54 self.wallet_config[key] = value
55 if save: self.save_wallet_config()
58 # add key to wallet config
59 self.wallet_config[key] = value
60 if save: self.save_wallet_config()
63 def get(self, key, default=None):
64 # 1. command-line options always override everything
65 if self.options_config.has_key(key):
66 # print "found", key, "in options"
67 out = self.options_config.get(key)
69 # 2. user configuration
70 elif self.user_config.has_key(key):
71 out = self.user_config.get(key)
73 # 2. system configuration
74 elif self.system_config.has_key(key):
75 out = self.system_config.get(key)
77 # 3. use the wallet file config
79 out = self.wallet_config.get(key)
81 if out is None and default is not None:
85 if default is not None and type(out) != type(default):
88 out = ast.literal_eval(out)
90 print "type error, using default value"
96 def is_modifiable(self, key):
97 if self.options_config.has_key(key):
99 elif self.user_config.has_key(key):
101 elif self.system_config.has_key(key):
107 def read_system_config(self):
108 name = '/etc/electrum.conf'
109 if os.path.exists(name):
113 print "cannot parse electrum.conf. please install ConfigParser"
116 p = ConfigParser.ConfigParser()
119 for k, v in p.items('client'):
120 self.system_config[k] = v
121 except ConfigParser.NoSectionError:
125 def read_user_config(self):
126 name = os.path.join( user_dir(), 'electrum.conf')
127 if os.path.exists(name):
131 print "cannot parse electrum.conf. please install ConfigParser"
134 p = ConfigParser.ConfigParser()
137 for k, v in p.items('client'):
138 self.user_config[k] = v
139 except ConfigParser.NoSectionError:
143 def init_path(self, options):
144 """Set the path of the wallet."""
148 # this will call read_wallet_config only if there is a wallet_path value in options
150 path = options.wallet_path
155 path = self.get('default_wallet_path')
161 # Look for wallet file in the default data directory.
162 # Keeps backwards compatibility.
163 wallet_dir = user_dir()
165 # Make wallet directory if it does not yet exist.
166 if not os.path.exists(wallet_dir):
168 self.path = os.path.join(wallet_dir, "electrum.dat")
171 def save_user_config(self):
173 config = ConfigParser.RawConfigParser()
174 config.add_section('client')
175 for k,v in self.user_config.items():
176 config.set('client', k, v)
178 with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
179 config.write(configfile)
184 def read_wallet_config(self, path):
185 """Read the contents of the wallet file."""
187 with open(self.path, "r") as f:
192 d = ast.literal_eval( data ) #parse raw data from reading wallet file
194 raise IOError("Cannot read wallet file.")
196 self.wallet_config = d
197 self.wallet_file_exists = True
202 self.save_wallet_config()
205 def save_wallet_config(self):
206 s = repr(self.wallet_config)
207 f = open(self.path,"w")
211 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)