wrap exceptions in cdist.Error

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-18 13:27:44 +02:00
parent e9a0aa1863
commit 91c1215566

View file

@ -64,9 +64,13 @@ class FileList(collections.MutableSequence):
return lines return lines
def __write(self, lines): def __write(self, lines):
with open(self.path, 'w') as fd: try:
for line in lines: with open(self.path, 'w') as fd:
fd.write(str(line) + '\n') for line in lines:
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
# create directory if it doesn't exist try:
if not os.path.isdir(self.path): # create directory if it doesn't exist
os.mkdir(self.path) if not os.path.isdir(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):
with open(os.path.join(self.path, key), "w") as fd: try:
fd.write(str(value)) with open(os.path.join(self.path, key), "w") as fd:
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):
return iter(os.listdir(self.path)) try:
return iter(os.listdir(self.path))
except EnvironmentError as e:
raise cdist.Error(str(e))
def __len__(self): def __len__(self):
return len(os.listdir(self.path)) try:
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:
open(path, "w").close() try:
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:
with open(path, "w") as fd: try:
fd.write(str(value)) with open(path, "w") as fd:
fd.write(str(value))
except EnvironmentError as e:
raise cdist.Error(str(e))
else: else:
try: try:
os.remove(path) os.remove(path)