def open_file(self, path, mode): path = self.realpath(path=path) parent = path.parent() contents = self._tree.get(parent) if contents is None: raise exceptions.FileNotFound(path) if mode == "w" or mode == "wb": file = _BytesIOIsTerrible() self._tree = self._tree.set(parent, contents.set(path, file)) return file file = contents.get(path) if mode == "a" or mode == "ab": if file is None: combined = _BytesIOIsTerrible() else: combined = _BytesIOIsTerrible() combined.write(file._hereismyvalue) self._tree = self._tree.set(parent, contents.set(path, combined)) return combined elif file is None: raise exceptions.FileNotFound(path) else: return BytesIO(file._hereismyvalue)
def remove_file(self, path): parent = path.parent() contents = self._tree.get(parent) if contents is None: raise exceptions.FileNotFound(path) if path not in contents: raise exceptions.FileNotFound(path) self._tree = self._tree.set(parent, contents.remove(path))
def _tree_with(self, path): parent = path.parent() if not self.exists(path=parent): raise exceptions.FileNotFound(parent) elif not self.is_dir(path=parent): raise exceptions.NotADirectory(parent) return self._tree.set(parent, self._tree[parent].set(path, None))
def remove(self, path): if self.is_link(path=path): self._links = self._links.remove(path) elif path not in self._tree: raise exceptions.FileNotFound(path) else: self._tree = self._tree.remove(path)
def _remove_file(fs, path): try: os.remove(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) raise
def _remove_empty_directory(fs, path): try: os.rmdir(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.DirectoryNotEmpty.errno: raise exceptions.DirectoryNotEmpty(path) elif error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) raise
def _list_directory(fs, path): try: return os.listdir(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path) raise
def _create_directory(fs, path): try: os.mkdir(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path.parent()) elif error.errno == exceptions.FileExists.errno: raise exceptions.FileExists(path) raise
def open_file(self, path, mode): if mode.read: raise exceptions.FileNotFound(path) else: file = self._parent[self._name] = _File( name=self._name, parent=self._parent, ) return file.open_file(path=path, mode=mode)
def _open_file(fs, path, mode): try: return open(str(path), mode) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path) raise
def create_directory(self, path): if self.exists(path=path): raise exceptions.FileExists(path) parent = path.parent() if not self.exists(path=parent): raise exceptions.FileNotFound(parent) self._tree = self._tree.set(path, pmap())
def list_directory(self, path): if self.is_file(path=path): raise exceptions.NotADirectory(path) elif path not in self._tree: raise exceptions.FileNotFound(path) # FIXME: Inefficient return pset(child.basename() for child in self._tree[path]) | pset( subdirectory.basename() for subdirectory in self._tree if subdirectory.parent() == path and subdirectory != path)
def _lstat(fs, path): try: return os.lstat(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path) raise
def _link(fs, source, to): try: os.symlink(str(source), str(to)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(to.parent()) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(to.parent()) elif error.errno == exceptions.FileExists.errno: raise exceptions.FileExists(to) raise
def create_directory(self, path, with_parents): if with_parents: parent = self._parent.create_directory( path=path, with_parents=with_parents, ) directory = parent[self._name].create_directory( path=path, with_parents=False, ) return directory raise exceptions.FileNotFound(path.parent())
def create_file(self, path): if self.exists(path=path) or self.is_link(path=path): raise exceptions.FileExists(path) path = self.realpath(path=path) parent = path.parent() contents = self._tree.get(parent) if contents is None: raise exceptions.FileNotFound(path) file = _BytesIOIsTerrible() self._tree = self._tree.set(parent, contents.set(path, file)) return file
def _create_file(fs, path): try: fd = os.open(str(path), _CREATE_FLAGS) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.FileExists.errno: raise exceptions.FileExists(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path.parent()) raise return os.fdopen(fd, "w+b")
def _open_file(fs, path, mode): mode = common._parse_mode(mode) try: return io.open(str(path), mode.io_open_string()) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.IsADirectory.errno: raise exceptions.IsADirectory(path) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path) raise
def _remove_file(fs, path): try: os.remove(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.IsADirectory.errno: raise exceptions.IsADirectory(path) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path) elif error.errno == exceptions.PermissionError.errno: raise exceptions.PermissionError(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path.parent()) raise
def _readlink(fs, path): try: value = os.readlink(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path) elif error.errno == exceptions.NotASymlink.errno: raise exceptions.NotASymlink(path) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path) raise else: return Path.from_string(value)
def _create_directory(fs, path, with_parents): try: if with_parents: os.makedirs(str(path)) else: os.mkdir(str(path)) except (IOError, OSError) as error: if error.errno == exceptions.FileExists.errno: raise exceptions.FileExists(path) elif error.errno == exceptions.FileNotFound.errno: raise exceptions.FileNotFound(path.parent()) elif error.errno == exceptions.NotADirectory.errno: raise exceptions.NotADirectory(path.parent()) elif error.errno == exceptions.SymbolicLoop.errno: raise exceptions.SymbolicLoop(path.parent()) raise
def open_file(self, path, mode): raise exceptions.FileNotFound(path)
def remove_empty_directory(self, path): raise exceptions.FileNotFound(path)
def list_directory(self, path): raise exceptions.FileNotFound(path)
def create_file(self, path): raise exceptions.FileNotFound(path)
def remove_file(self, path): raise exceptions.FileNotFound(path)
def stat(self, path): raise exceptions.FileNotFound(path)
def readlink(self, path): raise exceptions.FileNotFound(path)
def link(self, source, to, fs, state): raise exceptions.FileNotFound(to.parent())