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()
26 self.path = self.system_config.get('electrum_path')
28 self.path = user_dir()
30 # user conf, writeable
32 if options.get('portable') == False:
33 self.read_user_config()
35 # command-line options
36 self.options_config = options
41 print_error( "electrum path", self.path)
46 # Look for wallet file in the default data directory.
47 # Make wallet directory if it does not yet exist.
48 if not os.path.exists(self.path):
52 # portable wallet: use the same directory for wallet and headers file
53 #if options.get('portable'):
54 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
56 def set_key(self, key, value, save = True):
57 # find where a setting comes from and save it there
58 if self.options_config.get(key) is not None:
59 print "Warning: not changing '%s' because it was passed as a command-line option"%key
62 elif self.system_config.get(key) is not None:
63 if str(self.system_config[key]) != str(value):
64 print "Warning: not changing '%s' because it was set in the system configuration"%key
69 self.user_config[key] = value
71 self.save_user_config()
75 def get(self, key, default=None):
79 # 1. command-line options always override everything
80 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
81 out = self.options_config.get(key)
83 # 2. user configuration
84 elif self.user_config.has_key(key):
85 out = self.user_config.get(key)
87 # 2. system configuration
88 elif self.system_config.has_key(key):
89 out = self.system_config.get(key)
91 if out is None and default is not None:
95 if default is not None and type(out) != type(default):
98 out = ast.literal_eval(out)
100 print "type error for '%s': using default value"%key
106 def is_modifiable(self, key):
107 """Check if the config file is modifiable."""
108 if self.options_config.has_key(key):
110 elif self.user_config.has_key(key):
112 elif self.system_config.has_key(key):
118 def read_system_config(self):
119 """Parse and store the system config settings in electrum.conf into system_config[]."""
120 name = '/etc/electrum.conf'
121 if os.path.exists(name):
125 print "cannot parse electrum.conf. please install ConfigParser"
128 p = ConfigParser.ConfigParser()
131 for k, v in p.items('client'):
132 self.system_config[k] = v
133 except ConfigParser.NoSectionError:
137 def read_user_config(self):
138 """Parse and store the user config settings in electrum.conf into user_config[]."""
139 if not self.path: return
141 path = os.path.join(self.path, "config")
142 if os.path.exists(path):
144 with open(path, "r") as f:
149 d = ast.literal_eval( data ) #parse raw data from reading wallet file
151 raise IOError("Cannot read config file.")
156 def save_user_config(self):
157 if not self.path: return
159 path = os.path.join(self.path, "config")
160 s = repr(self.user_config)
164 if self.get('gui') != 'android':
166 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)