def __write(self, lines): try: with open(self.path, 'w') as fd: for line in lines: fd.write(str(line) + '\n') except EnvironmentError as e: # should never happen raise cinv.Error(str(e))
def __setitem__(self, key, value): try: with open(os.path.join(self.path, key), "w") as fd: if type(value) == type([]): for v in value: fd.write(str(v) + '\n') else: fd.write(str(value) + '\n') except EnvironmentError as e: raise cinv.Error(str(e))
def __set__(self, instance, value): path = self._get_path(instance) if value: try: with open(path, "w") as fd: fd.write("%s\n" % str(value)) except EnvironmentError as e: raise cinv.Error(str(e)) else: try: os.remove(path) except EnvironmentError: pass
def __set__(self, instance, value): path = self._get_path(instance) if value: try: open(path, "w").close() except EnvironmentError as e: raise cinv.Error(str(e)) else: try: os.remove(path) except EnvironmentError: # ignore pass
def __init__(self, path, initial=None, **kwargs): if not os.path.isabs(path): raise AbsolutePathRequiredError(path) self.path = path try: # create directory if it doesn't exist if not os.path.isdir(self.path): os.mkdir(self.path) except EnvironmentError as e: raise cinv.Error(str(e)) if initial is not None: self.update(initial) if kwargs: self.update(kwargs)
def __len__(self): try: return len(os.listdir(self.path)) except EnvironmentError as e: raise cinv.Error(str(e))