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 self.read_user_config()
41 # Read electrum path in the command line configuration
42 self.path = self.options_config.get('electrum_path')
44 # Read electrum path in the system configuration
46 self.path = self.system_config.get('electrum_path')
48 # If not set, use the user's default data directory.
50 self.path = user_dir()
52 # Make directory if it does not yet exist.
53 if not os.path.exists(self.path):
56 print_error( "electrum directory", self.path)
58 # portable wallet: use the same directory for wallet and headers file
59 #if options.get('portable'):
60 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
62 def set_key(self, key, value, save = True):
63 # find where a setting comes from and save it there
64 if self.options_config.get(key) is not None:
65 print "Warning: not changing '%s' because it was passed as a command-line option"%key
68 elif self.system_config.get(key) is not None:
69 if str(self.system_config[key]) != str(value):
70 print "Warning: not changing '%s' because it was set in the system configuration"%key
75 self.user_config[key] = value
77 self.save_user_config()
81 def get(self, key, default=None):
85 # 1. command-line options always override everything
86 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
87 out = self.options_config.get(key)
89 # 2. user configuration
90 elif self.user_config.has_key(key):
91 out = self.user_config.get(key)
93 # 2. system configuration
94 elif self.system_config.has_key(key):
95 out = self.system_config.get(key)
97 if out is None and default is not None:
100 # try to fix the type
101 if default is not None and type(out) != type(default):
104 out = ast.literal_eval(out)
106 print "type error for '%s': using default value"%key
112 def is_modifiable(self, key):
113 """Check if the config file is modifiable."""
114 if self.options_config.has_key(key):
116 elif self.user_config.has_key(key):
118 elif self.system_config.has_key(key):
124 def read_system_config(self):
125 """Parse and store the system config settings in electrum.conf into system_config[]."""
126 name = '/etc/electrum.conf'
127 if os.path.exists(name):
131 print "cannot parse electrum.conf. please install ConfigParser"
134 p = ConfigParser.ConfigParser()
137 for k, v in p.items('client'):
138 self.system_config[k] = v
139 except ConfigParser.NoSectionError:
143 def read_user_config(self):
144 """Parse and store the user config settings in electrum.conf into user_config[]."""
145 if not self.path: return
147 path = os.path.join(self.path, "config")
148 if os.path.exists(path):
150 with open(path, "r") as f:
155 d = ast.literal_eval( data ) #parse raw data from reading wallet file
157 print_msg("Error: 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)