system config file is read-only, user config file can be written

This commit is contained in:
thomasv 2012-10-12 14:44:56 +02:00
parent db730dd179
commit 8d0b81a3b7
2 changed files with 78 additions and 33 deletions

8
electrum.conf.sample Normal file
View File

@ -0,0 +1,8 @@
# Configuration file for the electrum client
# Settings defined here are shared across wallets
#
# copy this file to /etc/electrum.conf if you want read-only settings
# copy it into your ~/.electrum/ directory if you want settings that can be rewritten by the client
[client]
winpos-qt = [799, 226, 877, 435]
server = electrum.novit.ro:50001:t

View File

@ -28,16 +28,23 @@ def old_to_new(d):
class SimpleConfig: class SimpleConfig:
def __init__(self, options): def __init__(self, options=None):
self.wallet_config = {} self.wallet_config = {}
if options and options.wallet_path:
self.read_wallet_config(options.wallet_path) self.read_wallet_config(options.wallet_path)
self.common_config = {} # system conf, readonly
self.read_common_config() self.system_config = {}
self.read_system_config()
# user conf, writeable
self.user_config = {}
self.read_user_config()
# command-line options
self.options_config = {} self.options_config = {}
if options:
if options.server: self.options_config['server'] = options.server if options.server: self.options_config['server'] = options.server
if options.proxy: self.options_config['proxy'] = options.proxy if options.proxy: self.options_config['proxy'] = options.proxy
if options.gui: self.options_config['gui'] = options.gui if options.gui: self.options_config['gui'] = options.gui
@ -49,14 +56,18 @@ class SimpleConfig:
if self.options_config.get(key): if self.options_config.get(key):
return return
elif self.user_config.get(key):
self.user_config[key] = value
if save: self.save_user_config()
elif self.system_config.get(key):
self.system_config[key] = value
print "warning: cannot save", key
elif self.wallet_config.get(key): elif self.wallet_config.get(key):
self.wallet_config[key] = value self.wallet_config[key] = value
if save: self.save_wallet_config() if save: self.save_wallet_config()
elif self.common_config.get(key):
self.common_config[key] = value
if save: self.save_common_config()
else: else:
# add key to wallet config # add key to wallet config
self.wallet_config[key] = value self.wallet_config[key] = value
@ -69,9 +80,13 @@ class SimpleConfig:
# print "found", key, "in options" # print "found", key, "in options"
out = self.options_config.get(key) out = self.options_config.get(key)
# 2. configuration file overrides wallet file # 2. user configuration
elif self.common_config.has_key(key): elif self.user_config.has_key(key):
out = self.common_config.get(key) out = self.user_config.get(key)
# 2. system configuration
elif self.system_config.has_key(key):
out = self.system_config.get(key)
# 3. use the wallet file config # 3. use the wallet file config
else: else:
@ -89,14 +104,18 @@ class SimpleConfig:
def is_modifiable(self, key): def is_modifiable(self, key):
if self.options_config.has_key(key) or self.common_config.has_key(key): if self.options_config.has_key(key):
return False
elif self.user_config.has_key(key):
return True
elif self.system_config.has_key(key):
return False return False
else: else:
return True return True
def read_common_config(self): def read_system_config(self):
for name in ['/etc/electrum.conf', os.path.join( user_dir(), 'electrum.conf')]: name = '/etc/electrum.conf'
if os.path.exists(name): if os.path.exists(name):
try: try:
import ConfigParser import ConfigParser
@ -107,9 +126,22 @@ class SimpleConfig:
p = ConfigParser.ConfigParser() p = ConfigParser.ConfigParser()
p.read(name) p.read(name)
for k, v in p.items('client'): for k, v in p.items('client'):
self.common_config[k] = v self.system_config[k] = v
def read_user_config(self):
name = os.path.join( user_dir(), 'electrum.conf')
if os.path.exists(name):
try:
import ConfigParser
except:
print "cannot parse electrum.conf. please install ConfigParser"
return
p = ConfigParser.ConfigParser()
p.read(name)
for k, v in p.items('client'):
self.user_config[k] = v
def init_path(self, wallet_path): def init_path(self, wallet_path):
@ -128,11 +160,16 @@ class SimpleConfig:
self.path = os.path.join(wallet_dir, "electrum.dat") self.path = os.path.join(wallet_dir, "electrum.dat")
def save_user_config(self):
import ConfigParser
config = ConfigParser.RawConfigParser()
config.add_section('client')
for k,v in self.user_config.items():
config.set('client', k, v)
with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile:
config.write(configfile)
def save_common_config(self):
s = repr(self.common_config)
# todo: decide what to do
print "not saving settings in common config:", s