3 from util import user_dir, print_error
5 from version import ELECTRUM_VERSION, SEED_VERSION
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.
19 def __init__(self, options={}):
21 # system conf, readonly
22 self.system_config = {}
23 if options.get('portable') is not True:
24 self.read_system_config()
27 self.path = self.system_config.get('electrum_path')
29 self.path = user_dir()
31 # user conf, writeable
33 if options.get('portable') == False:
34 self.read_user_config()
36 # command-line options
37 self.options_config = options
42 print_error( "electrum path", self.path)
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):
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)
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
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
68 self.user_config[key] = value
69 if save: self.save_user_config()
73 def get(self, key, default=None):
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)
81 # 2. user configuration
82 elif self.user_config.has_key(key):
83 out = self.user_config.get(key)
85 # 2. system configuration
86 elif self.system_config.has_key(key):
87 out = self.system_config.get(key)
89 if out is None and default is not None:
93 if default is not None and type(out) != type(default):
96 out = ast.literal_eval(out)
98 print "type error for '%s': using default value"%key
104 def is_modifiable(self, key):
105 """Check if the config file is modifiable."""
106 if self.options_config.has_key(key):
108 elif self.user_config.has_key(key):
110 elif self.system_config.has_key(key):
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):
123 print "cannot parse electrum.conf. please install ConfigParser"
126 p = ConfigParser.ConfigParser()
129 for k, v in p.items('client'):
130 self.system_config[k] = v
131 except ConfigParser.NoSectionError:
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
139 path = os.path.join(self.path, "config")
140 if os.path.exists(path):
142 with open(path, "r") as f:
147 d = ast.literal_eval( data ) #parse raw data from reading wallet file
149 raise IOError("Cannot read config file.")
154 def save_user_config(self):
155 if not self.path: return
157 path = os.path.join(self.path, "config")
158 s = repr(self.user_config)
162 if self.get('gui') != 'android':
164 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)