6 from util import user_dir, print_error
12 The SimpleConfig class is responsible for handling operations involving
13 configuration files. The constructor reads and stores the system and
14 user configurations from electrum.conf into separate dictionaries within
15 a SimpleConfig instance then reads the wallet file.
17 def __init__(self, options={}):
18 self.lock = threading.Lock()
20 # system conf, readonly
21 self.system_config = {}
22 if options.get('portable') is not True:
23 self.read_system_config()
25 # command-line options
26 self.options_config = options
31 # user conf, writeable
33 if options.get('portable') == False:
34 self.read_user_config()
42 # Read electrum path in the command line configuration
43 self.path = self.options_config.get('electrum_path')
45 # Read electrum path in the system configuration
47 self.path = self.system_config.get('electrum_path')
49 # If not set, use the user's default data directory.
51 self.path = user_dir()
53 # Make directory if it does not yet exist.
54 if not os.path.exists(self.path):
57 print_error( "electrum directory", self.path)
59 # portable wallet: use the same directory for wallet and headers file
60 #if options.get('portable'):
61 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
63 def set_key(self, key, value, save = True):
64 # find where a setting comes from and save it there
65 if self.options_config.get(key) is not None:
66 print "Warning: not changing '%s' because it was passed as a command-line option"%key
69 elif self.system_config.get(key) is not None:
70 if str(self.system_config[key]) != str(value):
71 print "Warning: not changing '%s' because it was set in the system configuration"%key
76 self.user_config[key] = value
78 self.save_user_config()
82 def get(self, key, default=None):
86 # 1. command-line options always override everything
87 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
88 out = self.options_config.get(key)
90 # 2. user configuration
91 elif self.user_config.has_key(key):
92 out = self.user_config.get(key)
94 # 2. system configuration
95 elif self.system_config.has_key(key):
96 out = self.system_config.get(key)
98 if out is None and default is not None:
101 # try to fix the type
102 if default is not None and type(out) != type(default):
105 out = ast.literal_eval(out)
107 print "type error for '%s': using default value"%key
113 def is_modifiable(self, key):
114 """Check if the config file is modifiable."""
115 if self.options_config.has_key(key):
117 elif self.user_config.has_key(key):
119 elif self.system_config.has_key(key):
125 def read_system_config(self):
126 """Parse and store the system config settings in electrum.conf into system_config[]."""
127 name = '/etc/electrum.conf'
128 if os.path.exists(name):
132 print "cannot parse electrum.conf. please install ConfigParser"
135 p = ConfigParser.ConfigParser()
138 for k, v in p.items('client'):
139 self.system_config[k] = v
140 except ConfigParser.NoSectionError:
144 def read_user_config(self):
145 """Parse and store the user config settings in electrum.conf into user_config[]."""
146 if not self.path: return
148 path = os.path.join(self.path, "config")
149 if os.path.exists(path):
151 with open(path, "r") as f:
156 d = ast.literal_eval( data ) #parse raw data from reading wallet file
158 raise IOError("Cannot read config file.")
163 def save_user_config(self):
164 if not self.path: return
166 path = os.path.join(self.path, "config")
167 s = repr(self.user_config)
171 if self.get('gui') != 'android':
173 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)