3 from util import user_dir, print_error
5 from version import ELECTRUM_VERSION, SEED_VERSION
11 def __init__(self, options={}):
13 # system conf, readonly
14 self.system_config = {}
15 self.read_system_config()
17 # user conf, writeable
18 self.user_dir = user_dir()
20 self.read_user_config()
22 # command-line options
23 self.options_config = options
25 self.wallet_config = {}
26 self.wallet_file_exists = False
27 self.init_path(self.options_config.get('wallet_path'))
28 print_error( "path", self.path )
30 self.read_wallet_config(self.path)
36 def set_key(self, key, value, save = False):
37 # find where a setting comes from and save it there
38 if self.options_config.get(key) is not None:
39 print "Warning: not changing '%s' because it was passed as a command-line option"%key
42 elif self.user_config.get(key) is not None:
43 self.user_config[key] = value
44 if save: self.save_user_config()
46 elif self.system_config.get(key) is not None:
47 if str(self.system_config[key]) != str(value):
48 print "Warning: not changing '%s' because it was set in the system configuration"%key
50 elif self.wallet_config.get(key) is not None:
51 self.wallet_config[key] = value
52 if save: self.save_wallet_config()
55 # add key to wallet config
56 self.wallet_config[key] = value
57 if save: self.save_wallet_config()
60 def get(self, key, default=None):
61 # 1. command-line options always override everything
62 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
63 out = self.options_config.get(key)
65 # 2. user configuration
66 elif self.user_config.has_key(key):
67 out = self.user_config.get(key)
69 # 2. system configuration
70 elif self.system_config.has_key(key):
71 out = self.system_config.get(key)
73 # 3. use the wallet file config
75 out = self.wallet_config.get(key)
77 if out is None and default is not None:
81 if default is not None and type(out) != type(default):
84 out = ast.literal_eval(out)
86 print "type error, using default value"
92 def is_modifiable(self, key):
93 if self.options_config.has_key(key):
95 elif self.user_config.has_key(key):
97 elif self.system_config.has_key(key):
103 def read_system_config(self):
104 """Parse and store the config settings in electrum.conf into system_config[]"""
105 name = '/etc/electrum.conf'
106 if os.path.exists(name):
110 print "cannot parse electrum.conf. please install ConfigParser"
113 p = ConfigParser.ConfigParser()
116 for k, v in p.items('client'):
117 self.system_config[k] = v
118 except ConfigParser.NoSectionError:
122 def read_user_config(self):
123 if not self.user_dir: return
125 name = os.path.join( self.user_dir, 'electrum.conf')
126 if os.path.exists(name):
130 print "cannot parse electrum.conf. please install ConfigParser"
133 p = ConfigParser.ConfigParser()
136 for k, v in p.items('client'):
137 self.user_config[k] = v
138 except ConfigParser.NoSectionError:
142 def init_path(self, path):
143 """Set the path of the wallet."""
146 path = self.get('default_wallet_path')
152 # Look for wallet file in the default data directory.
153 # Make wallet directory if it does not yet exist.
154 if not os.path.exists(self.user_dir):
155 os.mkdir(self.user_dir)
156 self.path = os.path.join(self.user_dir, "electrum.dat")
159 def save_user_config(self):
160 if not self.user_dir: return
163 config = ConfigParser.RawConfigParser()
164 config.add_section('client')
165 for k,v in self.user_config.items():
166 config.set('client', k, v)
168 with open( os.path.join( self.user_dir, 'electrum.conf'), 'wb') as configfile:
169 config.write(configfile)
174 def read_wallet_config(self, path):
175 """Read the contents of the wallet file."""
177 with open(self.path, "r") as f:
182 d = ast.literal_eval( data ) #parse raw data from reading wallet file
184 raise IOError("Cannot read wallet file.")
186 self.wallet_config = d
187 self.wallet_file_exists = True
192 self.save_wallet_config()
195 def save_wallet_config(self):
196 s = repr(self.wallet_config)
197 f = open(self.path,"w")
200 if self.get('gui') != 'android':
202 os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)