privname = os.path.join(self.basedir, "private", name)
open(privname, "w").write(value)
+ def get_private_config(self, name, default=_None):
+ """Read the (string) contents of a private config file (which is a
+ config file that resides within the subdirectory named 'private'),
+ and return it. Return a default, or raise an error if one was not
+ given.
+ """
+ privname = os.path.join(self.basedir, "private", name)
+ try:
+ return fileutil.read(privname)
+ except EnvironmentError:
+ if os.path.exists(privname):
+ raise
+ if default is _None:
+ raise MissingConfigEntry("The required configuration file %s is missing."
+ % (quote_output(privname),))
+ return default
+
def get_or_create_private_config(self, name, default=_None):
"""Try to get the (string) contents of a private config file (which
is a config file that resides within the subdirectory named
from foolscap.api import flushEventualQueue
from twisted.application import service
-from allmydata.node import Node, formatTimeTahoeStyle
+from allmydata.node import Node, formatTimeTahoeStyle, MissingConfigEntry
from allmydata.util import fileutil
import allmydata.test.common_util as testutil
u"\u2621"))
return d
+ def test_private_config(self):
+ basedir = "test_node/test_private_config"
+ privdir = os.path.join(basedir, "private")
+ fileutil.make_dirs(privdir)
+ f = open(os.path.join(privdir, 'already'), 'wt')
+ f.write("secret")
+ f.close()
+
+ n = TestNode(basedir)
+ self.failUnlessEqual(n.get_private_config("already"), "secret")
+ self.failUnlessEqual(n.get_private_config("not", "default"), "default")
+ self.failUnlessRaises(MissingConfigEntry, n.get_private_config, "not")
+ value = n.get_or_create_private_config("new", "start")
+ self.failUnlessEqual(value, "start")
+ self.failUnlessEqual(n.get_private_config("new"), "start")
+ counter = []
+ def make_newer():
+ counter.append("called")
+ return "newer"
+ value = n.get_or_create_private_config("newer", make_newer)
+ self.failUnlessEqual(len(counter), 1)
+ self.failUnlessEqual(value, "newer")
+ self.failUnlessEqual(n.get_private_config("newer"), "newer")
+
+ value = n.get_or_create_private_config("newer", make_newer)
+ self.failUnlessEqual(len(counter), 1) # don't call unless necessary
+ self.failUnlessEqual(value, "newer")
+
def test_timestamp(self):
# this modified logger doesn't seem to get used during the tests,
# probably because we don't modify the LogObserver that trial