wrap exceptions in cdist.Error
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
parent
e9a0aa1863
commit
91c1215566
1 changed files with 35 additions and 13 deletions
|
@ -64,9 +64,13 @@ class FileList(collections.MutableSequence):
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def __write(self, lines):
|
def __write(self, lines):
|
||||||
|
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:
|
||||||
|
# should never happen
|
||||||
|
raise cdist.Error(str(e))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(list(self))
|
return repr(list(self))
|
||||||
|
@ -106,9 +110,12 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
if not os.path.isabs(path):
|
if not os.path.isabs(path):
|
||||||
raise AbsolutePathRequiredError(path)
|
raise AbsolutePathRequiredError(path)
|
||||||
self.path = path
|
self.path = path
|
||||||
|
try:
|
||||||
# create directory if it doesn't exist
|
# create directory if it doesn't exist
|
||||||
if not os.path.isdir(self.path):
|
if not os.path.isdir(self.path):
|
||||||
os.mkdir(self.path)
|
os.mkdir(self.path)
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
if initial is not None:
|
if initial is not None:
|
||||||
self.update(initial)
|
self.update(initial)
|
||||||
if kwargs:
|
if kwargs:
|
||||||
|
@ -125,8 +132,11 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
|
try:
|
||||||
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))
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
try:
|
try:
|
||||||
|
@ -135,10 +145,16 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
try:
|
||||||
return iter(os.listdir(self.path))
|
return iter(os.listdir(self.path))
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
|
try:
|
||||||
return len(os.listdir(self.path))
|
return len(os.listdir(self.path))
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
|
|
||||||
|
|
||||||
class FileBasedProperty(object):
|
class FileBasedProperty(object):
|
||||||
|
@ -234,7 +250,10 @@ class FileBooleanProperty(FileBasedProperty):
|
||||||
def __set__(self, instance, value):
|
def __set__(self, instance, value):
|
||||||
path = self._get_path(instance)
|
path = self._get_path(instance)
|
||||||
if value:
|
if value:
|
||||||
|
try:
|
||||||
open(path, "w").close()
|
open(path, "w").close()
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
@ -262,8 +281,11 @@ class FileStringProperty(FileBasedProperty):
|
||||||
def __set__(self, instance, value):
|
def __set__(self, instance, value):
|
||||||
path = self._get_path(instance)
|
path = self._get_path(instance)
|
||||||
if value:
|
if value:
|
||||||
|
try:
|
||||||
with open(path, "w") as fd:
|
with open(path, "w") as fd:
|
||||||
fd.write(str(value))
|
fd.write(str(value))
|
||||||
|
except EnvironmentError as e:
|
||||||
|
raise cdist.Error(str(e))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
|
|
Loading…
Reference in a new issue