From 7bbecb25868a8bc798dfbc7f96c9efa0649a0de6 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 7 Oct 2011 00:48:06 +0200 Subject: [PATCH] implement descriptor protocol for FileList and DirectoryDict Signed-off-by: Steven Armstrong --- lib/cdist/core/property.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/cdist/core/property.py b/lib/cdist/core/property.py index 359cf32c..30887547 100644 --- a/lib/cdist/core/property.py +++ b/lib/cdist/core/property.py @@ -87,6 +87,22 @@ class FileList(collections.MutableSequence): self.__write(lines) +class FileListProperty(FileList): + # Descriptor Protocol + def __get__(self, obj, objtype=None): + if obj is None: + return self.__class__ + return self + + def __set__(self, obj, value): + os.unlink(self._path) + for item in value: + self.append(item) + + def __delete__(self, obj): + raise AttributeError("can't delete attribute") + + class DirectoryDict(collections.MutableMapping): """A dict that stores it's state in a directory. @@ -120,3 +136,20 @@ class DirectoryDict(collections.MutableMapping): def __len__(self): return len(os.listdir(self._path)) + + +class DirectoryDictProperty(DirectoryDict): + # Descriptor Protocol + def __get__(self, obj, objtype=None): + if obj is None: + return self.__class__ + return self + + def __set__(self, obj, value): + for name in self.keys(): + del self[name] + if value is not None: + self.update(value) + + def __delete__(self, obj): + raise AttributeError("can't delete attribute")