forked from ungleich-public/cdist
rename property module to fsproperty
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
parent
0963237321
commit
915ec6d9ad
1 changed files with 75 additions and 17 deletions
|
@ -40,17 +40,17 @@ class FileList(collections.MutableSequence):
|
||||||
def __init__(self, path, initial=None):
|
def __init__(self, path, initial=None):
|
||||||
if not os.path.isabs(path):
|
if not os.path.isabs(path):
|
||||||
raise AbsolutePathRequiredError(path)
|
raise AbsolutePathRequiredError(path)
|
||||||
self._path = path
|
self.path = path
|
||||||
if initial:
|
if initial:
|
||||||
# delete existing file
|
# delete existing file
|
||||||
os.unlink(self._path)
|
os.unlink(self.path)
|
||||||
for i in initial:
|
for i in initial:
|
||||||
self.append(i)
|
self.append(i)
|
||||||
|
|
||||||
def __read(self):
|
def __read(self):
|
||||||
lines = []
|
lines = []
|
||||||
try:
|
try:
|
||||||
with open(self._path) as fd:
|
with open(self.path) as fd:
|
||||||
for line in fd:
|
for line in fd:
|
||||||
lines.append(line.rstrip('\n'))
|
lines.append(line.rstrip('\n'))
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
|
@ -60,7 +60,7 @@ class FileList(collections.MutableSequence):
|
||||||
|
|
||||||
def __write(self, lines):
|
def __write(self, lines):
|
||||||
try:
|
try:
|
||||||
with open(self._path, 'w') as fd:
|
with open(self.path, 'w') as fd:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
fd.write(str(line) + '\n')
|
fd.write(str(line) + '\n')
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
|
@ -98,14 +98,43 @@ class FileList(collections.MutableSequence):
|
||||||
|
|
||||||
|
|
||||||
class FileListProperty(FileList):
|
class FileListProperty(FileList):
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
"""
|
||||||
|
:param path: string or callable
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
class Foo(object):
|
||||||
|
parameters = DirectoryDictProperty(lambda obj: os.path.join(obj.absolute_path, 'parameter'))
|
||||||
|
other_dict = DirectoryDictProperty('/tmp/folder')
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.absolute_path = '/tmp/foo'
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.path = None
|
||||||
|
self.__path = path
|
||||||
|
|
||||||
|
def _set_path(self, *args, **kwargs):
|
||||||
|
if self.path is None:
|
||||||
|
path = self.__path
|
||||||
|
if callable(path):
|
||||||
|
path = path(*args, **kwargs)
|
||||||
|
if not os.path.isabs(path):
|
||||||
|
raise AbsolutePathRequiredError(path)
|
||||||
|
self.path = path
|
||||||
|
|
||||||
# Descriptor Protocol
|
# Descriptor Protocol
|
||||||
def __get__(self, obj, objtype=None):
|
def __get__(self, obj, objtype=None):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return self.__class__
|
return self.__class__
|
||||||
|
self._set_path(obj)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __set__(self, obj, value):
|
def __set__(self, obj, value):
|
||||||
os.unlink(self._path)
|
self._set_path(obj)
|
||||||
|
os.unlink(self.path)
|
||||||
for item in value:
|
for item in value:
|
||||||
self.append(item)
|
self.append(item)
|
||||||
|
|
||||||
|
@ -117,13 +146,13 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
"""A dict that stores it's state in a directory.
|
"""A dict that stores it's state in a directory.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, path, dict=None, **kwargs):
|
def __init__(self, path, initial=None, **kwargs):
|
||||||
if not os.path.isabs(path):
|
if not os.path.isabs(path):
|
||||||
raise AbsolutePathRequiredError(path)
|
raise AbsolutePathRequiredError(path)
|
||||||
self._path = path
|
self.path = path
|
||||||
if dict is not None:
|
if initial is not None:
|
||||||
self.update(dict)
|
self.update(initial)
|
||||||
if len(kwargs):
|
if kwargs:
|
||||||
self.update(kwargs)
|
self.update(kwargs)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -131,36 +160,65 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(self._path, key), "r") as fd:
|
with open(os.path.join(self.path, key), "r") as fd:
|
||||||
return fd.read().rstrip('\n')
|
return fd.read().rstrip('\n')
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
with open(os.path.join(self._path, key), "w") as fd:
|
with open(os.path.join(self.path, key), "w") as fd:
|
||||||
fd.write(str(value))
|
fd.write(str(value))
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
os.remove(os.path.join(self._path, key))
|
os.remove(os.path.join(self.path, key))
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(os.listdir(self._path))
|
return iter(os.listdir(self.path))
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(os.listdir(self._path))
|
return len(os.listdir(self.path))
|
||||||
|
|
||||||
|
|
||||||
class DirectoryDictProperty(DirectoryDict):
|
class DirectoryDictProperty(DirectoryDict):
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
"""
|
||||||
|
:param path: string or callable
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
class Foo(object):
|
||||||
|
parameters = DirectoryDictProperty(lambda obj: os.path.join(obj.absolute_path, 'parameter'))
|
||||||
|
other_dict = DirectoryDictProperty('/tmp/folder')
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.absolute_path = '/tmp/foo'
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.path = None
|
||||||
|
self.__path = path
|
||||||
|
|
||||||
|
def _set_path(self, *args, **kwargs):
|
||||||
|
if self.path is None:
|
||||||
|
path = self.__path
|
||||||
|
if callable(path):
|
||||||
|
path = path(*args, **kwargs)
|
||||||
|
if not os.path.isabs(path):
|
||||||
|
raise AbsolutePathRequiredError(path)
|
||||||
|
self.path = path
|
||||||
|
|
||||||
# Descriptor Protocol
|
# Descriptor Protocol
|
||||||
def __get__(self, obj, objtype=None):
|
def __get__(self, obj, objtype=None):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return self.__class__
|
return self.__class__
|
||||||
|
self._set_path(obj)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __set__(self, obj, value):
|
def __set__(self, obj, value):
|
||||||
for name in self.keys():
|
self._set_path(obj)
|
||||||
del self[name]
|
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
for name in self.keys():
|
||||||
|
del self[name]
|
||||||
self.update(value)
|
self.update(value)
|
||||||
|
|
||||||
def __delete__(self, obj):
|
def __delete__(self, obj):
|
Loading…
Reference in a new issue