Merge pull request #737 from chrisglass/fix-user-config-writing
Fixes previously introduced bug
This commit is contained in:
commit
e3cb2621c5
|
@ -35,7 +35,7 @@ class SimpleConfig(object):
|
||||||
read_user_config_function=None, read_user_dir_function=None):
|
read_user_config_function=None, read_user_dir_function=None):
|
||||||
|
|
||||||
# This is the holder of actual options for the current user.
|
# This is the holder of actual options for the current user.
|
||||||
self.current_options = {}
|
self.read_only_options = {}
|
||||||
# This lock needs to be acquired for updating and reading the config in
|
# This lock needs to be acquired for updating and reading the config in
|
||||||
# a thread-safe way.
|
# a thread-safe way.
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
|
@ -65,26 +65,23 @@ class SimpleConfig(object):
|
||||||
# system conf
|
# system conf
|
||||||
system_config = read_system_config_function()
|
system_config = read_system_config_function()
|
||||||
self.system_config_keys = system_config.keys()
|
self.system_config_keys = system_config.keys()
|
||||||
self.current_options.update(system_config)
|
self.read_only_options.update(system_config)
|
||||||
|
|
||||||
# update the current options with the command line options last (to
|
# update the current options with the command line options last (to
|
||||||
# override both others).
|
# override both others).
|
||||||
self.current_options.update(options)
|
self.read_only_options.update(options)
|
||||||
|
|
||||||
# init path
|
# init path
|
||||||
self.init_path()
|
self.init_path()
|
||||||
|
|
||||||
# user config.
|
# user config.
|
||||||
self.user_config = read_user_config_function(self.path)
|
self.user_config = read_user_config_function(self.path)
|
||||||
# The user config is overwritten by the current config!
|
|
||||||
self.user_config.update(self.current_options)
|
|
||||||
self.current_options = self.user_config
|
|
||||||
|
|
||||||
set_config(self) # Make a singleton instance of 'self'
|
set_config(self) # Make a singleton instance of 'self'
|
||||||
|
|
||||||
def init_path(self):
|
def init_path(self):
|
||||||
# Read electrum path in the command line configuration
|
# Read electrum path in the command line configuration
|
||||||
self.path = self.current_options.get('electrum_path')
|
self.path = self.read_only_options.get('electrum_path')
|
||||||
|
|
||||||
# If not set, use the user's default data directory.
|
# If not set, use the user's default data directory.
|
||||||
if self.path is None:
|
if self.path is None:
|
||||||
|
@ -104,7 +101,6 @@ class SimpleConfig(object):
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.user_config[key] = value
|
self.user_config[key] = value
|
||||||
self.current_options[key] = value
|
|
||||||
if save:
|
if save:
|
||||||
self.save_user_config()
|
self.save_user_config()
|
||||||
|
|
||||||
|
@ -113,7 +109,9 @@ class SimpleConfig(object):
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
out = None
|
out = None
|
||||||
with self.lock:
|
with self.lock:
|
||||||
out = self.current_options.get(key, default)
|
out = self.read_only_options.get(key)
|
||||||
|
if not out:
|
||||||
|
out = self.user_config.get(key, default)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def is_modifiable(self, key):
|
def is_modifiable(self, key):
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import ast
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -135,6 +136,24 @@ class Test_SimpleConfig(unittest.TestCase):
|
||||||
config.set_key("electrum_path", another_path)
|
config.set_key("electrum_path", another_path)
|
||||||
self.assertEqual(another_path, config.get("electrum_path"))
|
self.assertEqual(another_path, config.get("electrum_path"))
|
||||||
|
|
||||||
|
def test_user_config_is_not_written_with_read_only_config(self):
|
||||||
|
"""The user config does not contain command-line options or system
|
||||||
|
options when saved."""
|
||||||
|
fake_read_system = lambda : {"something": "b"}
|
||||||
|
fake_read_user = lambda _: {"something": "a"}
|
||||||
|
read_user_dir = lambda : self.user_dir
|
||||||
|
self.options.update({"something": "c"})
|
||||||
|
config = SimpleConfig(options=self.options,
|
||||||
|
read_system_config_function=fake_read_system,
|
||||||
|
read_user_config_function=fake_read_user,
|
||||||
|
read_user_dir_function=read_user_dir)
|
||||||
|
config.save_user_config()
|
||||||
|
contents = None
|
||||||
|
with open(os.path.join(self.electrum_dir, "config"), "r") as f:
|
||||||
|
contents = f.read()
|
||||||
|
result = ast.literal_eval(contents)
|
||||||
|
self.assertEqual({"something": "a"}, result)
|
||||||
|
|
||||||
|
|
||||||
class TestSystemConfig(unittest.TestCase):
|
class TestSystemConfig(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue