X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=lib%2Fsimple_config.py;h=c90a7379b810b8079696d60e51a549964a7f5377;hb=fff3ed9b77d7a5c66bb8797e1f0ba245d83116f7;hp=ddec085a11e0f841ac779c67b6f8993c82c00ee6;hpb=8d0b81a3b733c780d050651550d16847f9139124;p=electrum-nvc.git diff --git a/lib/simple_config.py b/lib/simple_config.py index ddec085..c90a737 100644 --- a/lib/simple_config.py +++ b/lib/simple_config.py @@ -1,207 +1,175 @@ -import json, ast -import os, ast -from util import user_dir - -from version import ELECTRUM_VERSION, SEED_VERSION - - -# old stuff.. should be removed at some point -def replace_keys(obj, old_key, new_key): - if isinstance(obj, dict): - if old_key in obj: - obj[new_key] = obj[old_key] - del obj[old_key] - for elem in obj.itervalues(): - replace_keys(elem, old_key, new_key) - elif isinstance(obj, list): - for elem in obj: - replace_keys(elem, old_key, new_key) - -def old_to_new(d): - replace_keys(d, 'blk_hash', 'block_hash') - replace_keys(d, 'pos', 'index') - replace_keys(d, 'nTime', 'timestamp') - replace_keys(d, 'is_in', 'is_input') - replace_keys(d, 'raw_scriptPubKey', 'raw_output_script') - - - -class SimpleConfig: - - def __init__(self, options=None): - - self.wallet_config = {} - if options and options.wallet_path: - self.read_wallet_config(options.wallet_path) - - # system conf, readonly - self.system_config = {} - self.read_system_config() - - # user conf, writeable - self.user_config = {} - self.read_user_config() - - # command-line options - self.options_config = {} - if options: - if options.server: self.options_config['server'] = options.server - if options.proxy: self.options_config['proxy'] = options.proxy - if options.gui: self.options_config['gui'] = options.gui - - - - def set_key(self, key, value, save = False): - # find where a setting comes from and save it there - if self.options_config.get(key): - return +import ast +import threading +import os - elif self.user_config.get(key): - self.user_config[key] = value - if save: self.save_user_config() +from util import user_dir, print_error, print_msg - elif self.system_config.get(key): - self.system_config[key] = value - print "warning: cannot save", key +SYSTEM_CONFIG_PATH = "/etc/electrum.conf" - elif self.wallet_config.get(key): - self.wallet_config[key] = value - if save: self.save_wallet_config() +config = None - else: - # add key to wallet config - self.wallet_config[key] = value - if save: self.save_wallet_config() +def get_config(): + global config + return config - def get(self, key, default=None): - # 1. command-line options always override everything - if self.options_config.has_key(key): - # print "found", key, "in options" - out = self.options_config.get(key) - # 2. user configuration - elif self.user_config.has_key(key): - out = self.user_config.get(key) +def set_config(c): + global config + config = c - # 2. system configuration - elif self.system_config.has_key(key): - out = self.system_config.get(key) - # 3. use the wallet file config - else: - out = self.wallet_config.get(key) +class SimpleConfig(object): + """ + The SimpleConfig class is responsible for handling operations involving + configuration files. - if out is None and default is not None: - out = default + There are 3 different sources of possible configuration values: + 1. Command line options. + 2. User configuration (in the user's config directory) + 3. System configuration (in /etc/) + They are taken in order (1. overrides config options set in 2., that + override config set in 3.) + """ + def __init__(self, options=None, read_system_config_function=None, + read_user_config_function=None, read_user_dir_function=None): - # try to fix the type - if default is not None and type(out) != type(default): - import ast - out = ast.literal_eval(out) - - return out + # This is the holder of actual options for the current user. + self.current_options = {} + # This lock needs to be acquired for updating and reading the config in + # a thread-safe way. + self.lock = threading.RLock() + # The path for the config directory. This is set later by init_path() + self.path = None + if options is None: + options = {} # Having a mutable as a default value is a bad idea. - def is_modifiable(self, 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 + # The following two functions are there for dependency injection when + # testing. + if read_system_config_function is None: + read_system_config_function = read_system_config + if read_user_config_function is None: + read_user_config_function = read_user_config + if read_user_dir_function is None: + self.user_dir = user_dir else: - return True - - - def read_system_config(self): - name = '/etc/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.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 + self.user_dir = read_user_dir_function + # Save the command-line keys to make sure we don't override them. + self.command_line_keys = options.keys() + # Save the system config keys to make sure we don't override them. + self.system_config_keys = [] - def init_path(self, wallet_path): - """Set the path of the wallet.""" - if wallet_path is not None: - self.path = wallet_path - return + if options.get('portable') is not True: + # system conf + system_config = read_system_config_function() + self.system_config_keys = system_config.keys() + self.current_options.update(system_config) - # Look for wallet file in the default data directory. - # Keeps backwards compatibility. - wallet_dir = user_dir() + # update the current options with the command line options last (to + # override both others). + self.current_options.update(options) - # Make wallet directory if it does not yet exist. - if not os.path.exists(wallet_dir): - os.mkdir(wallet_dir) - self.path = os.path.join(wallet_dir, "electrum.dat") + # init path + self.init_path() + # user config. + self.user_config = read_user_config_function(self.path) + # The user config is overwritten by the current config! + self.user_config.update(self.current_options) + self.current_options = self.user_config - 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) + set_config(self) # Make a singleton instance of 'self' - with open( os.path.join( user_dir(), 'electrum.conf'), 'wb') as configfile: - config.write(configfile) - + def init_path(self): + # Read electrum path in the command line configuration + self.path = self.current_options.get('electrum_path') + # If not set, use the user's default data directory. + if self.path is None: + self.path = self.user_dir() - def read_wallet_config(self, path): - """Read the contents of the wallet file.""" - self.wallet_file_exists = False - self.init_path(path) - try: - with open(self.path, "r") as f: - data = f.read() - except IOError: + # Make directory if it does not yet exist. + if not os.path.exists(self.path): + os.mkdir(self.path) + + print_error( "electrum directory", self.path) + + def set_key(self, key, value, save = True): + if not self.is_modifiable(key): + print "Warning: not changing key '%s' because it is not modifiable" \ + " (passed as command line option or defined in /etc/electrum.conf)"%key return - try: - d = ast.literal_eval( data ) #parse raw data from reading wallet file - old_to_new(d) - except: - raise IOError("Cannot read wallet file.") - self.wallet_config = d - self.wallet_file_exists = True + with self.lock: + self.user_config[key] = value + self.current_options[key] = value + if save: + self.save_user_config() + return + def get(self, key, default=None): + out = None + with self.lock: + out = self.current_options.get(key, default) + return out - def save(self): - self.save_wallet_config() + def is_modifiable(self, key): + if key in self.command_line_keys: + return False + if key in self.system_config_keys: + return False + return True + def save_user_config(self): + if not self.path: return - def save_wallet_config(self): - s = repr(self.wallet_config) - f = open(self.path,"w") + path = os.path.join(self.path, "config") + s = repr(self.user_config) + f = open(path,"w") f.write( s ) f.close() - import stat - os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE) + if self.get('gui') != 'android': + import stat + os.chmod(path, stat.S_IREAD | stat.S_IWRITE) + +def read_system_config(): + """Parse and return the system config settings in /etc/electrum.conf.""" + if os.path.exists(SYSTEM_CONFIG_PATH): + try: + import ConfigParser + except ImportError: + print "cannot parse electrum.conf. please install ConfigParser" + return + + p = ConfigParser.ConfigParser() + p.read(SYSTEM_CONFIG_PATH) + result = {} + try: + for k, v in p.items('client'): + result[k] = v + except ConfigParser.NoSectionError: + pass + return result + +def read_user_config(path): + """Parse and store the user config settings in electrum.conf into user_config[].""" + if not path: return + + config_path = os.path.join(path, "config") + if os.path.exists(config_path): + try: + with open(config_path, "r") as f: + data = f.read() + except IOError: + return + try: + d = ast.literal_eval( data ) #parse raw data from reading wallet file + except Exception: + print_msg("Error: Cannot read config file.") + return + return d