blob: f9202c2284f19561f7392585f75c6704e94562ee (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/python3
# wrapper for zathura that automatically handles HTTP(s) resources,
# without needing the monstrosity that is gvfs
import contextlib
import os
import shutil
import subprocess
import sys
import tempfile
import urllib.error
import urllib.request
args = sys.argv[:]
with contextlib.ExitStack() as stack:
# assume the file is the last argument
if (len(args) > 1 and args[-1].startswith('http') and not os.path.exists(args[-1])):
tf = stack.enter_context(tempfile.NamedTemporaryFile())
try:
resp = urllib.request.urlopen(args[-1])
shutil.copyfileobj(resp, tf)
tf.flush()
args[-1] = tf.name
except urllib.error.URLError as e:
sys.stderr.write('Error downloading the URL "%s" to a temporary file\n', str(e))
sys.exit(1)
args[0] = '/usr/bin/zathura'
subprocess.run(args)
|