5 from util import user_dir, print_error, print_msg
7 SYSTEM_CONFIG_PATH = "/etc/electrum.conf"
22 class SimpleConfig(object):
24 The SimpleConfig class is responsible for handling operations involving
27 There are 3 different sources of possible configuration values:
28 1. Command line options.
29 2. User configuration (in the user's config directory)
30 3. System configuration (in /etc/)
31 They are taken in order (1. overrides config options set in 2., that
32 override config set in 3.)
34 def __init__(self, options=None, read_system_config_function=None,
35 read_user_config_function=None, read_user_dir_function=None):
37 # This is the holder of actual options for the current user.
38 self.read_only_options = {}
39 # This lock needs to be acquired for updating and reading the config in
41 self.lock = threading.RLock()
42 # The path for the config directory. This is set later by init_path()
46 options = {} # Having a mutable as a default value is a bad idea.
48 # The following two functions are there for dependency injection when
50 if read_system_config_function is None:
51 read_system_config_function = read_system_config
52 if read_user_config_function is None:
53 read_user_config_function = read_user_config
54 if read_user_dir_function is None:
55 self.user_dir = user_dir
57 self.user_dir = read_user_dir_function
59 # Save the command-line keys to make sure we don't override them.
60 self.command_line_keys = options.keys()
61 # Save the system config keys to make sure we don't override them.
62 self.system_config_keys = []
64 if options.get('portable') is not True:
66 system_config = read_system_config_function()
67 self.system_config_keys = system_config.keys()
68 self.read_only_options.update(system_config)
70 # update the current options with the command line options last (to
71 # override both others).
72 self.read_only_options.update(options)
78 self.user_config = read_user_config_function(self.path)
80 set_config(self) # Make a singleton instance of 'self'
83 # Read electrum path in the command line configuration
84 self.path = self.read_only_options.get('electrum_path')
86 # If not set, use the user's default data directory.
88 self.path = self.user_dir()
90 # Make directory if it does not yet exist.
91 if not os.path.exists(self.path):
94 print_error( "electrum directory", self.path)
96 def set_key(self, key, value, save = True):
97 if not self.is_modifiable(key):
98 print "Warning: not changing key '%s' because it is not modifiable" \
99 " (passed as command line option or defined in /etc/electrum.conf)"%key
103 self.user_config[key] = value
105 self.save_user_config()
109 def get(self, key, default=None):
112 out = self.read_only_options.get(key)
114 out = self.user_config.get(key, default)
117 def is_modifiable(self, key):
118 if key in self.command_line_keys:
120 if key in self.system_config_keys:
124 def save_user_config(self):
125 if not self.path: return
127 path = os.path.join(self.path, "config")
128 s = repr(self.user_config)
132 if self.get('gui') != 'android':
134 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)
136 def read_system_config(path=SYSTEM_CONFIG_PATH):
137 """Parse and return the system config settings in /etc/electrum.conf."""
139 if os.path.exists(path):
143 print "cannot parse electrum.conf. please install ConfigParser"
146 p = ConfigParser.ConfigParser()
149 for k, v in p.items('client'):
151 except (ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError):
156 def read_user_config(path):
157 """Parse and store the user config settings in electrum.conf into user_config[]."""
158 if not path: return {} # Return a dict, since we will call update() on it.
160 config_path = os.path.join(path, "config")
162 if os.path.exists(config_path):
165 with open(config_path, "r") as f:
167 result = ast.literal_eval( data ) #parse raw data from reading wallet file
170 print_msg("Error: Cannot read config file.")
173 if not type(result) is dict: