Example #1
0
def choose_file(multiple: bool) -> List[str]:
    """Select file(s) for uploading, using external command defined in config.

    Args:
        multiple: Should selecting multiple files be allowed.

    Return:
        A list of selected file paths, or empty list if no file is selected.
        If multiple is False, the return value will have at most 1 item.
    """
    if multiple:
        command = config.val.fileselect.multiple_files.command
    else:
        command = config.val.fileselect.single_file.command
    use_tmp_file = any('{}' in arg for arg in command[1:])
    if use_tmp_file:
        handle = tempfile.NamedTemporaryFile(
            prefix='qutebrowser-fileselect-',
            delete=False,
        )
        handle.close()
        tmpfilename = handle.name
        with utils.cleanup_file(tmpfilename):
            command = (command[:1] +
                       [arg.replace('{}', tmpfilename) for arg in command[1:]])
            return _execute_fileselect_command(
                command=command,
                multiple=multiple,
                tmpfilename=tmpfilename,
            )
    else:
        return _execute_fileselect_command(
            command=command,
            multiple=multiple,
        )
Example #2
0
def choose_file(multiple: bool) -> List[str]:
    """Select file(s) for uploading, using external command defined in config.

    Args:
        multiple: Should selecting multiple files be allowed.

    Return:
        A list of selected file paths, or empty list if no file is selected.
        If multiple is False, the return value will have at most 1 item.
    """
    handle = tempfile.NamedTemporaryFile(prefix='qutebrowser-fileselect-', delete=False)
    handle.close()
    tmpfilename = handle.name
    with utils.cleanup_file(tmpfilename):
        if multiple:
            command = config.val.fileselect.multiple_files.command
        else:
            command = config.val.fileselect.single_file.command

        proc = guiprocess.GUIProcess(what='choose-file')
        proc.start(command[0],
                   [arg.replace('{}', tmpfilename) for arg in command[1:]])

        loop = qtutils.EventLoop()
        proc.finished.connect(lambda _code, _status: loop.exit())
        loop.exec()

        try:
            with open(tmpfilename, mode='r', encoding=sys.getfilesystemencoding()) as f:
                selected_files = f.read().splitlines()
        except OSError as e:
            message.error(f"Failed to open tempfile {tmpfilename} ({e})!")
            selected_files = []

    if not multiple:
        if len(selected_files) > 1:
            message.warning("More than one file chosen, using only the first")
            return selected_files[:1]
    return selected_files
Example #3
0
def choose_file(qb_mode: FileSelectionMode) -> List[str]:
    """Select file(s)/folder for uploading, using external command defined in config.

    Args:
        qb_mode: File selection mode

    Return:
        A list of selected file paths, or empty list if no file is selected.
        If multiple is False, the return value will have at most 1 item.
    """
    command = {
        FileSelectionMode.single_file:
        config.val.fileselect.single_file.command,
        FileSelectionMode.multiple_files:
        config.val.fileselect.multiple_files.command,
        FileSelectionMode.folder: config.val.fileselect.folder.command,
    }[qb_mode]
    use_tmp_file = any('{}' in arg for arg in command[1:])
    if use_tmp_file:
        handle = tempfile.NamedTemporaryFile(
            prefix='qutebrowser-fileselect-',
            delete=False,
        )
        handle.close()
        tmpfilename = handle.name
        with utils.cleanup_file(tmpfilename):
            command = (command[:1] +
                       [arg.replace('{}', tmpfilename) for arg in command[1:]])
            return _execute_fileselect_command(
                command=command,
                qb_mode=qb_mode,
                tmpfilename=tmpfilename,
            )
    else:
        return _execute_fileselect_command(
            command=command,
            qb_mode=qb_mode,
        )