示例#1
0
    def tag_delete(self, name: str) -> None:
        """Delete an existing tag.

        **syntax:** ``:tag-delete name``

        .. warning:: If you pass a tag group directory, the complete tree is deleted.

        positional arguments:
            * ``name``: Name of the tag to delete.
        """
        _logger.debug("Deleting tag '%s'", name)
        abspath = Tag.path(name)

        def safe_delete(operation: Callable) -> None:
            """Wrapper around delete operation logging exceptions."""
            try:
                operation(abspath)
            except PermissionError:
                raise commands.CommandError("Permission denied")

        if os.path.isfile(abspath):
            safe_delete(os.remove)
            _logger.debug("Removed regular tag file '%s'", name)
        elif os.path.isdir(abspath):
            safe_delete(shutil.rmtree)
            _logger.debug("Removed tag directory '%s'", name)
        else:
            raise commands.CommandError(f"No tag called '{name}'")
示例#2
0
def unbind(keybinding: str, mode: modes.Mode) -> None:
    """Remove keybinding from registry.

    See config/configcommands.unbind for the corresponding command.
    """
    for submode in modes.GLOBALS if mode is modes.GLOBAL else (mode, ):
        try:
            del _registry[submode][keybinding]
        except KeyError:
            raise commands.CommandError(f"No binding found for '{keybinding}'")
示例#3
0
    def __init__(self, name: str, read_only: bool = True):
        self.name = name
        abspath = Tag.path(name)
        exists = os.path.isfile(abspath)
        self._mode = "r" if read_only else ("r+" if exists else "a+")
        _logger.debug("Opened tag object: '%s'", self)
        xdg.makedirs(os.path.dirname(abspath))
        try:
            self._file = open(abspath, self._mode)
        except FileNotFoundError:  # For read-only if the file does not exist
            raise commands.CommandError(f"No tag called '{name}'")
        except OSError as e:
            raise commands.CommandError(f"Error reading '{name}': {e}")

        if read_only:
            _logger.debug("%s: Reading tag file", self)
        elif not exists:
            _logger.debug("%s: Creating new tag file", self)
            self._write_header()
        else:
            _logger.debug("%s: Appending to existing tag file", self)
示例#4
0
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")
示例#5
0
 def safe_delete(operation: Callable) -> None:
     """Wrapper around delete operation logging exceptions."""
     try:
         operation(abspath)
     except PermissionError:
         raise commands.CommandError("Permission denied")