6 from util import user_dir, print_error, print_msg
21 The SimpleConfig class is responsible for handling operations involving
22 configuration files. The constructor reads and stores the system and
23 user configurations from electrum.conf into separate dictionaries within
24 a SimpleConfig instance then reads the wallet file.
26 def __init__(self, options={}):
27 self.lock = threading.Lock()
29 # system conf, readonly
30 self.system_config = {}
31 if options.get('portable') is not True:
32 self.read_system_config()
34 # command-line options
35 self.options_config = options
40 # user conf, writeable
42 self.read_user_config()
50 # Read electrum path in the command line configuration
51 self.path = self.options_config.get('electrum_path')
53 # Read electrum path in the system configuration
55 self.path = self.system_config.get('electrum_path')
57 # If not set, use the user's default data directory.
59 self.path = user_dir()
61 # Make directory if it does not yet exist.
62 if not os.path.exists(self.path):
65 print_error( "electrum directory", self.path)
67 # portable wallet: use the same directory for wallet and headers file
68 #if options.get('portable'):
69 # self.wallet_config['blockchain_headers_path'] = os.path.dirname(self.path)
71 def set_key(self, key, value, save = True):
72 # find where a setting comes from and save it there
73 if self.options_config.get(key) is not None:
74 print "Warning: not changing '%s' because it was passed as a command-line option"%key
77 elif self.system_config.get(key) is not None:
78 if str(self.system_config[key]) != str(value):
79 print "Warning: not changing '%s' because it was set in the system configuration"%key
84 self.user_config[key] = value
86 self.save_user_config()
90 def get(self, key, default=None):
94 # 1. command-line options always override everything
95 if self.options_config.has_key(key) and self.options_config.get(key) is not None:
96 out = self.options_config.get(key)
98 # 2. user configuration
99 elif self.user_config.has_key(key):
100 out = self.user_config.get(key)
102 # 2. system configuration
103 elif self.system_config.has_key(key):
104 out = self.system_config.get(key)
106 if out is None and default is not None:
109 # try to fix the type
110 if default is not None and type(out) != type(default):
113 out = ast.literal_eval(out)
115 print "type error for '%s': using default value"%key
121 def is_modifiable(self, key):
122 """Check if the config file is modifiable."""
123 if self.options_config.has_key(key):
125 elif self.user_config.has_key(key):
127 elif self.system_config.has_key(key):
133 def read_system_config(self):
134 """Parse and store the system config settings in electrum.conf into system_config[]."""
135 name = '/etc/electrum.conf'
136 if os.path.exists(name):
140 print "cannot parse electrum.conf. please install ConfigParser"
143 p = ConfigParser.ConfigParser()
146 for k, v in p.items('client'):
147 self.system_config[k] = v
148 except ConfigParser.NoSectionError:
152 def read_user_config(self):
153 """Parse and store the user config settings in electrum.conf into user_config[]."""
154 if not self.path: return
156 path = os.path.join(self.path, "config")
157 if os.path.exists(path):
159 with open(path, "r") as f:
164 d = ast.literal_eval( data ) #parse raw data from reading wallet file
166 print_msg("Error: Cannot read config file.")
172 def save_user_config(self):
173 if not self.path: return
175 path = os.path.join(self.path, "config")
176 s = repr(self.user_config)
180 if self.get('gui') != 'android':
182 os.chmod(path, stat.S_IREAD | stat.S_IWRITE)