def rename(self, src: str, dst: str) -> "Answer": src, dst = get_abspath(src), get_abspath(dst) for path in [src, dst]: if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) try: self._file_system.rename(src, dst) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def copy(self, src: str, dst: str, args: List[str]) -> "Answer": src, dst = get_abspath(src), get_abspath(dst) recursive = "-r" in args for path in [src, dst]: if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) if not self._is_valid_params(["-r"], args): error = OptionError() return Answer(returncode=1, error=error) try: self._file_system.copy(src, dst, recursive) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def __init__(self, root: str = None): super(FileManager, self).__init__() self.intro = "Добро пожаловать в простой кроссплатформенный awesome файловый менеджер. " \ "Введи help или ? для получения списка команд.\n" self.root = root or get_abspath(os.getcwd()) self.prompt = self._prompt() self.out_prompt = "--- " self.err_prompt = "*** " self._executor = Manager(str(self.root)) self._parser = Parser()
def listdir(self, path: str, mask: str) -> "Answer": path = get_abspath(path) if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) try: data = self._file_system.listdir(path, mask) return Answer(returncode=0, payload=data) except OSError as e: return Answer(returncode=1, error=e)
def write_file(self, path: str, data: str) -> "Answer": path = get_abspath(path) if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) try: self._file_system.write_file(path, data) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def remove_dir(self, path: str, args: List[str]) -> "Answer": path = get_abspath(path) recursive = "-r" in args if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) if not self._is_valid_params(["-r"], args): error = OptionError() return Answer(returncode=1, error=error) try: self._file_system.remove_dir(path, recursive=recursive) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def create_file(self, path: str, args: List[str]) -> "Answer": path = get_abspath(path) exist_ok = "-q" in args if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) if not self._is_valid_params(["-q"], args): error = OptionError() return Answer(returncode=1, error=error) try: self._file_system.create_file(path, exist_ok=exist_ok) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def change_dir(self, path: str) -> "Answer": if re.fullmatch(r"\.{2,}", path): n_steps = len(path) - 2 if n_steps >= len(self.cwd.parents): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) path = self.cwd.parents[n_steps] else: path = get_abspath(path) if not self._in_scope_root(path): error = RootScopeError(self._root, path) return Answer(returncode=1, error=error) try: self._file_system.change_dir(path) except OSError as e: return Answer(returncode=1, error=e) return Answer(returncode=0)
def __init__(self, root: str): self._root = get_abspath(root) os.chdir(self._root) self._file_system = FileSystemApi