def test_images_supported(mocker): mocker.patch("os.path.isdir", return_value=False) mocker.patch("os.path.isfile", return_value=True) mocker.patch("imghdr.what", return_value=True) images, directories = files.supported(["a", "b"]) assert images == ["a", "b"] assert not directories
def _load_single(path: str) -> None: """Populate list of paths in same directory for single path.""" if path in _paths: goto(_paths.index(path) + 1) # goto is indexed from 1 else: directory = os.path.dirname(path) paths, _ = files.supported(files.listdir(directory)) _load_paths(paths, path)
def _get_content(self, directory: str) -> Tuple[List[str], List[str]]: """Get supported content of directory. Returns: images: List of images inside the directory. directories: List of directories inside the directory. """ show_hidden = settings.library.show_hidden.value paths = files.listdir(directory, show_hidden=show_hidden) return files.supported(paths)
def on_text_changed(self, text: str) -> None: """Update completion options when text changes.""" directory = self._get_directory(text) # Nothing changed if os.path.abspath(directory) == self._last_directory: return # Prepare self._last_directory = os.path.abspath(directory) # No completions for non-existent directory if not os.path.isdir(os.path.expanduser(directory)): return # Retrieve supported paths images, directories = files.supported(files.listdir(directory)) # Format data self.set_data( self._create_row(os.path.join(directory, os.path.basename(path))) for path in images + directories)
def open_paths(paths: Iterable[str]) -> None: """Open one or more paths. **syntax:** ``:open path [path ...]`` If any path given is an image, all valid images are opened in image mode. Otherwise the first valid directory is opened. If both fails, an error is displayed. positional arguments: * ``paths``: The path(s) to open. """ images, directories = files.supported(paths) if images: working_directory.handler.chdir(os.path.dirname(images[0])) signals.load_images.emit(images) modes.IMAGE.enter() elif directories: working_directory.handler.chdir(directories[0]) modes.LIBRARY.enter() else: raise commands.CommandError("No valid paths")