From 005009ab5da44539e496ba92bf737c790d909221 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Tue, 11 Oct 2011 09:38:03 +0200 Subject: [PATCH] finish FileStringProperty Signed-off-by: Steven Armstrong --- lib/cdist/util/fsproperty.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/cdist/util/fsproperty.py b/lib/cdist/util/fsproperty.py index b855daa4..c18a1553 100644 --- a/lib/cdist/util/fsproperty.py +++ b/lib/cdist/util/fsproperty.py @@ -271,9 +271,9 @@ class FileBooleanProperty(object): def __delete__(self, obj): raise AttributeError("can't delete attribute") -# FIXME: should have same anchestor as FileList + class FileStringProperty(object): - """A string property which stores its state in a file. + """A string property which stores its value in a file. """ def __init__(self, path): """ @@ -307,15 +307,21 @@ class FileStringProperty(object): value = "" try: with open(path, "r") as fd: - value = fd.read().rstrip('\n') + value = fd.read() except EnvironmentError: pass return value def __set__(self, obj, value): path = self._get_path(obj) - with open(path, "w") as fd: - fd.write(str(value)) + if value: + with open(path, "w") as fd: + fd.write(str(value)) + else: + try: + os.unlink(path) + except EnvironmentError: + pass def __delete__(self, obj): - raise AttributeError("can't delete attribute") + raise AttributeError("Can't delete attribute. Set it's value to an empty string to remove the underlying file.")