summaryrefslogtreecommitdiff
path: root/qutebrowser/browser/shared.py
diff options
context:
space:
mode:
Diffstat (limited to 'qutebrowser/browser/shared.py')
-rw-r--r--qutebrowser/browser/shared.py67
1 files changed, 52 insertions, 15 deletions
diff --git a/qutebrowser/browser/shared.py b/qutebrowser/browser/shared.py
index 78b475835..94332ffcb 100644
--- a/qutebrowser/browser/shared.py
+++ b/qutebrowser/browser/shared.py
@@ -23,7 +23,7 @@ import os
import sys
import html
import netrc
-from typing import Callable, Mapping, List
+from typing import Callable, Mapping, List, Optional
import tempfile
from PyQt5.QtCore import QUrl
@@ -360,23 +360,60 @@ def choose_file(multiple: bool) -> List[str]:
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
+ 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,
+ )
- 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()
+def _execute_fileselect_command(
+ command: List[str],
+ multiple: bool,
+ tmpfilename: Optional[str] = None
+) -> List[str]:
+ """Execute external command to choose file.
+ Args:
+ multiple: Should selecting multiple files be allowed.
+ tmpfilename: Path to the temporary file if used, otherwise None.
+
+ 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.
+ """
+ proc = guiprocess.GUIProcess(what='choose-file')
+ proc.start(command[0], command[1:])
+
+ loop = qtutils.EventLoop()
+ proc.finished.connect(lambda _code, _status: loop.exit())
+ loop.exec()
+
+ if tmpfilename is None:
+ selected_files = proc.final_stdout.splitlines()
+ else:
try:
with open(tmpfilename, mode='r', encoding=sys.getfilesystemencoding()) as f:
selected_files = f.read().splitlines()