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.current_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.current_options.update(system_config)
70 # update the current options with the command line options last (to
71 # override both others).
72 self.current_options.update(options)
78 self.user_config = read_user_config_function(self.path)
79 # The user config is overwritten by the current config!
80 self.user_config.update(self.current_options)
81 self.current_options = self.user_config
83 set_config(self) # Make a singleton instance of 'self'
86 # Read electrum path in the command line configuration
87 self.path = self.current_options.get('electrum_path')
89 # If not set, use the user's default data directory.
91 self.path = self.user_dir()
93 # Make directory if it does not yet exist.
94 if not os.path.exists(self.path):
97 print_error( "electrum directory", self.path)
99 def set_key(self, key, value, save = True):
100 if not self.is_modifiable(key):
101 print "Warning: not changing key '%s' because it is not modifiable" \
102 " (passed as command line option or defined in /etc/electrum.conf)"%key
106 self.user_config[key] = value
107 self.current_options[key] = value
109 self.save_user_config()
113 def get(self, key, default=None):
116 out = self.current_options.get(key, default)
119 def is_modifiable(self, key):
120 if key in self.command_line_keys:
122 if key in self.system_config_keys:
126 def save_user_config(self):
127 if not self.path: return
129 path = os.path.join(self.path, "config")
130 s = repr(self.user_config)
134 if self.get('gui') != 'android':
136 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)
138 def read_system_config(path=SYSTEM_CONFIG_PATH):
139 """Parse and return the system config settings in /etc/electrum.conf."""
141 if os.path.exists(path):
145 print "cannot parse electrum.conf. please install ConfigParser"
148 p = ConfigParser.ConfigParser()
151 for k, v in p.items('client'):
153 except (ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError):
158 def read_user_config(path):
159 """Parse and store the user config settings in electrum.conf into user_config[]."""
162 config_path = os.path.join(path, "config")
163 if os.path.exists(config_path):
165 with open(config_path, "r") as f:
170 d = ast.literal_eval( data ) #parse raw data from reading wallet file
172 print_msg("Error: Cannot read config file.")