electrum-bitcoinprivate/lib/simple_config.py

183 lines
5.2 KiB
Python
Raw Normal View History

2013-09-29 09:33:54 -07:00
import json
import ast
import threading
import os
2014-06-05 12:55:11 -07:00
from util import user_dir, print_error, print_msg
config = None
def get_config():
global config
return config
def set_config(c):
global config
config = c
class SimpleConfig:
2012-11-19 04:56:25 -08:00
"""
2012-11-18 22:29:32 -08:00
The SimpleConfig class is responsible for handling operations involving
configuration files. The constructor reads and stores the system and
2013-06-16 16:21:22 -07:00
user configurations from electrum.conf into separate dictionaries within
2012-11-18 22:29:32 -08:00
a SimpleConfig instance then reads the wallet file.
"""
2012-11-18 02:34:52 -08:00
def __init__(self, options={}):
2013-09-29 09:33:54 -07:00
self.lock = threading.Lock()
# system conf, readonly
self.system_config = {}
2013-09-02 06:05:33 -07:00
if options.get('portable') is not True:
self.read_system_config()
# command-line options
self.options_config = options
# init path
self.init_path()
2013-09-02 06:05:33 -07:00
# user conf, writeable
self.user_config = {}
self.read_user_config()
set_config(self)
2013-09-02 06:05:33 -07:00
def init_path(self):
# Read electrum path in the command line configuration
self.path = self.options_config.get('electrum_path')
# Read electrum path in the system configuration
if self.path is None:
self.path = self.system_config.get('electrum_path')
# If not set, use the user's default data directory.
if self.path is None:
self.path = user_dir()
# Make directory if it does not yet exist.
2013-09-02 06:05:33 -07:00
if not os.path.exists(self.path):
os.mkdir(self.path)
print_error( "electrum directory", self.path)
# portable wallet: use the same directory for wallet and headers file
#if options.get('portable'):
# self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
def set_key(self, key, value, save = True):
# find where a setting comes from and save it there
2012-10-23 13:40:52 -07:00
if self.options_config.get(key) is not None:
print "Warning: not changing '%s' because it was passed as a command-line option"%key
return
2012-10-23 13:40:52 -07:00
elif self.system_config.get(key) is not None:
if str(self.system_config[key]) != str(value):
print "Warning: not changing '%s' because it was set in the system configuration"%key
else:
2013-09-29 09:33:54 -07:00
with self.lock:
self.user_config[key] = value
if save:
self.save_user_config()
def get(self, key, default=None):
out = None
2012-11-19 04:59:56 -08:00
# 1. command-line options always override everything
2012-11-18 02:34:52 -08:00
if self.options_config.has_key(key) and self.options_config.get(key) is not None:
out = self.options_config.get(key)
# 2. user configuration
elif self.user_config.has_key(key):
out = self.user_config.get(key)
# 2. system configuration
elif self.system_config.has_key(key):
out = self.system_config.get(key)
if out is None and default is not None:
out = default
# try to fix the type
if default is not None and type(out) != type(default):
import ast
try:
out = ast.literal_eval(out)
2013-11-09 21:23:57 -08:00
except Exception:
2013-01-05 06:23:35 -08:00
print "type error for '%s': using default value"%key
out = default
return out
def is_modifiable(self, key):
2012-11-18 22:56:32 -08:00
"""Check if the config file is modifiable."""
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
else:
return True
def read_system_config(self):
2012-11-18 20:30:56 -08:00
"""Parse and store the system config settings in electrum.conf into system_config[]."""
name = '/etc/electrum.conf'
if os.path.exists(name):
try:
import ConfigParser
except ImportError:
print "cannot parse electrum.conf. please install ConfigParser"
return
p = ConfigParser.ConfigParser()
p.read(name)
2012-10-12 12:31:30 -07:00
try:
for k, v in p.items('client'):
self.system_config[k] = v
except ConfigParser.NoSectionError:
pass
def read_user_config(self):
"""Parse and store the user config settings in electrum.conf into user_config[]."""
2013-09-02 06:05:33 -07:00
if not self.path: return
2012-11-18 02:34:52 -08:00
2013-09-02 06:05:33 -07:00
path = os.path.join(self.path, "config")
if os.path.exists(path):
try:
with open(path, "r") as f:
data = f.read()
except IOError:
return
2012-10-12 12:31:30 -07:00
try:
d = ast.literal_eval( data ) #parse raw data from reading wallet file
2013-11-09 21:23:57 -08:00
except Exception:
print_msg("Error: Cannot read config file.")
return
self.user_config = d
def save_user_config(self):
2013-09-02 06:05:33 -07:00
if not self.path: return
2012-11-18 02:34:52 -08:00
2013-09-02 06:05:33 -07:00
path = os.path.join(self.path, "config")
s = repr(self.user_config)
f = open(path,"w")
f.write( s )
f.close()
2012-11-18 02:34:52 -08:00
if self.get('gui') != 'android':
import stat
os.chmod(path, stat.S_IREAD | stat.S_IWRITE)