3 from util import user_dir, print_error
5 from version import ELECTRUM_VERSION, SEED_VERSION
14 The SimpleConfig class is responsible for handling operations involving
15 configuration files. The constructor reads and stores the system and
16 user configurations from electrum.conf into separate dictionaries within
17 a SimpleConfig instance then reads the wallet file.
19 def __init__(self, options={}):
21 # system conf, readonly
22 self.system_config = {}
23 if options.get('portable') == False:
24 self.read_system_config()
26 # user conf, writeable
27 self.user_dir = user_dir()
29 if options.get('portable') == False:
30 self.read_user_config()
32 # command-line options
33 self.options_config = options
36 self.init_path(options)
38 print_error( "user dir", self.user_dir)
41 def init_path(self, options):
43 # Look for wallet file in the default data directory.
44 # Make wallet directory if it does not yet exist.
45 if not os.path.exists(self.user_dir):
46 os.mkdir(self.user_dir)
49 # portable wallet: use the same directory for wallet and headers file
50 #if options.get('portable'):
51 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
53 def set_key(self, key, value, save = True):
54 # find where a setting comes from and save it there
55 if self.options_config.get(key) is not None:
56 print "Warning: not changing '%s' because it was passed as a command-line option"%key
59 elif self.system_config.get(key) is not None:
60 if str(self.system_config[key]) != str(value):
61 print "Warning: not changing '%s' because it was set in the system configuration"%key
64 self.user_config[key] = value
65 if save: self.save_user_config()
69 def get(self, key, default=None):
73 # 1. command-line options always override everything
74 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
75 out = self.options_config.get(key)
77 # 2. user configuration
78 elif self.user_config.has_key(key):
79 out = self.user_config.get(key)
81 # 2. system configuration
82 elif self.system_config.has_key(key):
83 out = self.system_config.get(key)
85 if out is None and default is not None:
89 if default is not None and type(out) != type(default):
92 out = ast.literal_eval(out)
94 print "type error for '%s': using default value"%key
100 def is_modifiable(self, key):
101 """Check if the config file is modifiable."""
102 if self.options_config.has_key(key):
104 elif self.user_config.has_key(key):
106 elif self.system_config.has_key(key):
112 def read_system_config(self):
113 """Parse and store the system config settings in electrum.conf into system_config[]."""
114 name = '/etc/electrum.conf'
115 if os.path.exists(name):
119 print "cannot parse electrum.conf. please install ConfigParser"
122 p = ConfigParser.ConfigParser()
125 for k, v in p.items('client'):
126 self.system_config[k] = v
127 except ConfigParser.NoSectionError:
131 def read_user_config(self):
132 """Parse and store the user config settings in electrum.conf into user_config[]."""
133 if not self.user_dir: return
135 path = os.path.join(self.user_dir, "config")
136 if os.path.exists(path):
138 with open(path, "r") as f:
143 d = ast.literal_eval( data ) #parse raw data from reading wallet file
145 raise IOError("Cannot read config file.")
150 def save_user_config(self):
151 if not self.user_dir: return
153 path = os.path.join(self.user_dir, "config")
154 s = repr(self.user_config)
158 if self.get('gui') != 'android':
160 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)