async def forward(self) -> None: """Handle the forward action on the current file based on filetype.""" current_path = self.path if self._mode == PaneMode.fs: if self.current_selection.type == FileType.dir: try: self._files = await self._fs.cd( Path(self.current_selection.name)) except Exception as e: self.set_error(Notification(str(e))) self._files = await self._fs.cd(Path(current_path)) elif self._mode == PaneMode.s3: if (self.current_selection.type == FileType.dir or self.current_selection.type == FileType.bucket): try: self._files = await self._s3.cd(self.current_selection.name ) except Exception as e: self.set_error(Notification(str(e))) self._files = await self._fs.cd(Path(current_path)) else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) return await self.forward() await self.filter_files()
def test_get_title(app: App): app.set_error(Notification(message="hello", error_type=ErrorType.error)) assert app._error_pane._get_title() == [("class:error.error", "ERROR")] app.set_error(Notification(message="hello", error_type=ErrorType.warning)) assert app._error_pane._get_title() == [("class:error.warning", "WARNING")] app.set_error(Notification(message="hello", error_type=ErrorType.info)) assert app._error_pane._get_title() == [("class:error.info", "INFO")]
def test_get_text(app: App): app.set_error(Notification(message="hello", error_type=ErrorType.error)) assert app._error_pane._get_text() == [ ("", "\n"), ("class:error.error", "hello"), ("", "\n"), ("", "\n"), ("class:error.instruction", "Press any key to continue ..."), ] app.set_error(Notification(message="hello", error_type=ErrorType.warning)) assert app._error_pane._get_text() == [ ("", "\n"), ("class:error.warning", "hello"), ("", "\n"), ("", "\n"), ("class:error.instruction", "Press any key to continue ..."), ] app.set_error(Notification(message="hello", error_type=ErrorType.info)) assert app._error_pane._get_text() == [ ("", "\n"), ("class:error.info", "hello"), ("", "\n"), ("", "\n"), ("class:error.instruction", "Press any key to continue ..."), ]
async def cd( self, path: Optional[Path] = None, override: bool = False ) -> List[File]: """Navigate into another directory. Args: path: A :class:`pathlib.Path` object to cd into. If not provided, navigate to current path parent. override: Change directory to a absolute new path without any consideration of the current path. Returns: A list of files in the new directory. Raises: Bug: when the supplied :class`pathlib.Path` object is not a directory. """ if not path: self._path = self._path.parent.resolve() else: if not path.is_dir() and not self._path.joinpath(path).is_dir(): raise Notification("Target path %s is not a directory." % str(path)) if override: self._path = path.resolve() else: self._path = self._path.joinpath(path).resolve() return await self.get_paths()
def _get_pane_info(self) -> List[Tuple[str, str]]: """Get the top panel info of the current pane. This will be used to display some information at the top of the filepane. Returns: A list of tuples which can be parsed as :class:`prompt_toolkit.formatted_text.FormattedText`. Raises: Notification: When pane mode is not recognized. """ if not self._loaded: return [] display_info = [] color_class = ("class:filepane.focus_path" if self._focus() else "class:filepane.unfocus_path") if self._mode == PaneMode.s3: display_info.append((color_class, self._s3.uri)) elif self._mode == PaneMode.fs: display_info.append(( color_class, str(self._fs.path).replace(str(Path("~").expanduser()), "~"), )) else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) return self._get_pane_info() return display_info
def test_set_error(app: App): app.set_error(Notification(message="hello", error_type=ErrorType.warning)) assert app._error == "hello" assert app._error_type == ErrorType.warning app.set_error() assert app._error == ""
def current_filepane(self) -> FilePane: """:class:`~s3fm.ui.filepane.FilePane`: Get current focused filepane.""" try: return self.filepanes[self._filepane_focus] except KeyError: self.set_error( Notification("Unexpected focus.", error_type=ErrorType.warning)) self._filepane_focus = Pane.left return self.current_filepane
def path(self, value) -> None: if self.mode == PaneMode.s3: self._s3.path = Path(value) elif self.mode == PaneMode.fs: self._fs.path = Path(value) else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) self._fs.path = Path(value)
def path(self) -> str: """str: Current filepath.""" if self.mode == PaneMode.s3: return str(self._s3.path) elif self.mode == PaneMode.fs: return str(self._fs.path) else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) return self.path
def current_focus(self) -> "Container": """:class:`prompt_toolkit.layout.Container`: Get current focused pane.""" try: return { **self.filepanes, Pane.cmd: self._command_pane, Pane.error: self._error_pane, }[self._current_focus] except KeyError: self.set_error( Notification("Unexpected focus.", error_type=ErrorType.warning)) self.pane_focus(Pane.left) return self.current_focus
async def load_data(self) -> None: """Load the data into filepane. Raises: Notification: Current pane mode is not recognized. """ self._files = [] if self._mode == PaneMode.s3: try: self._files += await self._s3.get_paths() except: self.set_error( Notification( message= "Target S3 path %s is not valid or you do not have sufficent permissions." % self._s3.path)) self._s3.path = Path("") self._files += await self._s3.get_paths() elif self._mode == PaneMode.fs: try: self._files += await self._fs.get_paths() except: self.set_error( Notification( message="Target path %s is not a valid directory." % self._fs.path)) self._fs.path = Path("").resolve() self._files += await self._fs.get_paths() else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) return await self.load_data() await self.filter_files() self._loaded = True
async def backword(self) -> None: """Handle the backword action. Raises: Notification: Unexpected pane mode. """ if self._mode == PaneMode.fs: self._files = await self._fs.cd() elif self._mode == PaneMode.s3: self._files = await self._s3.cd() else: self._mode = PaneMode.fs self.set_error( Notification("Unexpected pane mode.", error_type=ErrorType.warning)) return await self.filter_files() await self.filter_files()
def layout(self) -> Layout: """:class:`prompt_toolkit.layout.Layout`: Get app layout dynamically.""" if self._layout_mode == LayoutMode.vertical: layout = HSplit([ VSplit([self._left_pane, self._right_pane]), self._command_pane ]) elif (self._layout_mode == LayoutMode.horizontal or self._layout_mode == LayoutMode.single): layout = HSplit( [self._left_pane, self._right_pane, self._command_pane]) else: self._layout_mode = LayoutMode.vertical self.set_error( Notification("Unexpected layout.", error_type=ErrorType.warning)) return self.layout if self._border: layout = Frame(layout) return Layout( FloatContainer( content=layout, floats=[Float(content=self._option_pane), self._error_pane], ))