Refactor tahoe.cfg handling to configutil.

Author: David Stainton <david@leastauthority.com>
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2015-12-21 21:59:15 +00:00
parent 41cf600820
commit 58841cab38
3 changed files with 66 additions and 16 deletions

View File

@ -12,6 +12,7 @@ from allmydata.util import fileutil, iputil, observer
from allmydata.util.assertutil import precondition, _assert
from allmydata.util.fileutil import abspath_expanduser_unicode
from allmydata.util.encodingutil import get_filesystem_encoding, quote_output
from allmydata.util import configutil
# Add our application versions to the data that Foolscap's LogPublisher
# reports.
@ -133,27 +134,13 @@ class Node(service.MultiService):
% (quote_output(fn), section, option))
return default
def set_config(self, section, option, value):
if not self.config.has_section(section):
self.config.add_section(section)
self.config.set(section, option, value)
assert self.config.get(section, option) == value
def read_config(self):
self.error_about_old_config_files()
self.config = ConfigParser.SafeConfigParser()
tahoe_cfg = os.path.join(self.basedir, "tahoe.cfg")
try:
f = open(tahoe_cfg, "rb")
try:
# Skip any initial Byte Order Mark. Since this is an ordinary file, we
# don't need to handle incomplete reads, and can assume seekability.
if f.read(3) != '\xEF\xBB\xBF':
f.seek(0)
self.config.readfp(f)
finally:
f.close()
self.config = configutil.get_config(tahoe_cfg)
except EnvironmentError:
if os.path.exists(tahoe_cfg):
raise
@ -165,7 +152,7 @@ class Node(service.MultiService):
# provide a value.
try:
file_tubport = fileutil.read(self._portnumfile).strip()
self.set_config("node", "tub.port", file_tubport)
configutil.set_config(self.config, "node", "tub.port", file_tubport)
except EnvironmentError:
if os.path.exists(self._portnumfile):
raise

View File

@ -0,0 +1,34 @@
import os.path
from twisted.trial import unittest
from allmydata.util import configutil
from allmydata.test.no_network import GridTestMixin
from .test_cli import CLITestMixin
class ConfigUtilTests(CLITestMixin, GridTestMixin, unittest.TestCase):
def test_config_utils(self):
self.basedir = "cli/ConfigUtilTests/test-config-utils"
self.set_up_grid()
tahoe_cfg = os.path.join(self.get_clientdir(i=0), "tahoe.cfg")
# test that at least one option was read correctly
config = configutil.get_config(tahoe_cfg)
self.failUnlessEqual(config.get("node", "nickname"), "client-0")
# test that set_config can mutate an existing option
configutil.set_config(config, "node", "nickname", "Alice!")
configutil.write_config(tahoe_cfg, config)
config = configutil.get_config(tahoe_cfg)
self.failUnlessEqual(config.get("node", "nickname"), "Alice!")
# test that set_config can set a new option
descriptor = "Twas brillig, and the slithy toves Did gyre and gimble in the wabe"
configutil.set_config(config, "node", "descriptor", descriptor)
configutil.write_config(tahoe_cfg, config)
config = configutil.get_config(tahoe_cfg)
self.failUnlessEqual(config.get("node", "descriptor"), descriptor)

View File

@ -0,0 +1,29 @@
from ConfigParser import SafeConfigParser
def get_config(tahoe_cfg):
config = SafeConfigParser()
f = open(tahoe_cfg, "rb")
try:
# Skip any initial Byte Order Mark. Since this is an ordinary file, we
# don't need to handle incomplete reads, and can assume seekability.
if f.read(3) != '\xEF\xBB\xBF':
f.seek(0)
config.readfp(f)
finally:
f.close()
return config
def set_config(config, section, option, value):
if not config.has_section(section):
config.add_section(section)
config.set(section, option, value)
assert config.get(section, option) == value
def write_config(tahoe_cfg, config):
f = open(tahoe_cfg, "wb")
try:
config.write(f)
finally:
f.close()