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()
28 # user conf, writeable
30 if options.get('portable') == False:
31 self.read_user_config()
33 # command-line options
34 self.options_config = options
41 # Read electrum path in the system configuration
42 self.path = self.system_config.get('electrum_path')
44 # If not set, use the user's default data directory.
46 self.path = user_dir()
48 # Make directory if it does not yet exist.
49 if not os.path.exists(self.path):
52 print_error( "electrum directory", self.path)
54 # portable wallet: use the same directory for wallet and headers file
55 #if options.get('portable'):
56 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
58 def set_key(self, key, value, save = True):
59 # find where a setting comes from and save it there
60 if self.options_config.get(key) is not None:
61 print "Warning: not changing '%s' because it was passed as a command-line option"%key
64 elif self.system_config.get(key) is not None:
65 if str(self.system_config[key]) != str(value):
66 print "Warning: not changing '%s' because it was set in the system configuration"%key
71 self.user_config[key] = value
73 self.save_user_config()
77 def get(self, key, default=None):
81 # 1. command-line options always override everything
82 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
83 out = self.options_config.get(key)
85 # 2. user configuration
86 elif self.user_config.has_key(key):
87 out = self.user_config.get(key)
89 # 2. system configuration
90 elif self.system_config.has_key(key):
91 out = self.system_config.get(key)
93 if out is None and default is not None:
97 if default is not None and type(out) != type(default):
100 out = ast.literal_eval(out)
102 print "type error for '%s': using default value"%key
108 def is_modifiable(self, key):
109 """Check if the config file is modifiable."""
110 if self.options_config.has_key(key):
112 elif self.user_config.has_key(key):
114 elif self.system_config.has_key(key):
120 def read_system_config(self):
121 """Parse and store the system config settings in electrum.conf into system_config[]."""
122 name = '/etc/electrum.conf'
123 if os.path.exists(name):
127 print "cannot parse electrum.conf. please install ConfigParser"
130 p = ConfigParser.ConfigParser()
133 for k, v in p.items('client'):
134 self.system_config[k] = v
135 except ConfigParser.NoSectionError:
139 def read_user_config(self):
140 """Parse and store the user config settings in electrum.conf into user_config[]."""
141 if not self.path: return
143 path = os.path.join(self.path, "config")
144 if os.path.exists(path):
146 with open(path, "r") as f:
151 d = ast.literal_eval( data ) #parse raw data from reading wallet file
153 raise IOError("Cannot read config file.")
158 def save_user_config(self):
159 if not self.path: return
161 path = os.path.join(self.path, "config")
162 s = repr(self.user_config)
166 if self.get('gui') != 'android':
168 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)