finish FileStringProperty

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-11 09:38:03 +02:00
parent a1e1c8d665
commit 005009ab5d
1 changed files with 12 additions and 6 deletions

View File

@ -271,9 +271,9 @@ class FileBooleanProperty(object):
def __delete__(self, obj): def __delete__(self, obj):
raise AttributeError("can't delete attribute") raise AttributeError("can't delete attribute")
# FIXME: should have same anchestor as FileList
class FileStringProperty(object): 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): def __init__(self, path):
""" """
@ -307,15 +307,21 @@ class FileStringProperty(object):
value = "" value = ""
try: try:
with open(path, "r") as fd: with open(path, "r") as fd:
value = fd.read().rstrip('\n') value = fd.read()
except EnvironmentError: except EnvironmentError:
pass pass
return value return value
def __set__(self, obj, value): def __set__(self, obj, value):
path = self._get_path(obj) path = self._get_path(obj)
with open(path, "w") as fd: if value:
fd.write(str(value)) with open(path, "w") as fd:
fd.write(str(value))
else:
try:
os.unlink(path)
except EnvironmentError:
pass
def __delete__(self, obj): 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.")