From: Brian Warner Date: Tue, 28 Aug 2007 02:07:12 +0000 (-0700) Subject: node.py: change get_or_create_config() to accept a function X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/something?a=commitdiff_plain;h=89ceb493093597de4e84b20b334e755c78f270de;p=tahoe-lafs%2Ftahoe-lafs.git node.py: change get_or_create_config() to accept a function --- diff --git a/src/allmydata/client.py b/src/allmydata/client.py index 896c5e61..7ff46b68 100644 --- a/src/allmydata/client.py +++ b/src/allmydata/client.py @@ -103,6 +103,8 @@ class Client(node.Node, Referenceable): def tub_ready(self): self.log("tub_ready") + # we use separate get_config/write_config here because we want to + # update the connection hints each time. my_old_name = None my_old_furl = self.get_config("myself.furl") if my_old_furl is not None: diff --git a/src/allmydata/node.py b/src/allmydata/node.py index 279d8e80..40f48da2 100644 --- a/src/allmydata/node.py +++ b/src/allmydata/node.py @@ -78,14 +78,21 @@ class Node(service.MultiService): return None raise - def get_or_create_config(self, name, default, mode="w"): - """Try to get the (string) contents of a config file. If the file - does not exist, create it with the given default value, and return - the default value. Any leading or trailing whitespace will be - stripped from the data.""" + def get_or_create_config(self, name, default_fn, mode="w"): + """Try to get the (string) contents of a config file, and return it. + Any leading or trailing whitespace will be stripped from the data. + + If the file does not exist, try to create it using default_fn, and + then return the value that was written. If 'default_fn' is a string, + use it as a default value. If not, treat it as a 0-argument callable + which is expected to return a string. + """ value = self.get_config(name) if value is None: - value = default + if isinstance(default_fn, (str, unicode)): + value = default_fn + else: + value = default_fn() fn = os.path.join(self.basedir, name) try: open(fn, mode).write(value)