summaryrefslogtreecommitdiff
path: root/bin/zathura_wrapper
blob: 0129df331fad22d27ae5ae62a5e5111c19a8f33b (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
#!/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)
            args[-1] = tf.name
        except urllib.error.URLError as e:
            sys.stderr.write('Error downloading the URL "%s" to a temporary file', str(e))
            sys.exit(1)

    args[0] = '/usr/bin/zathura'

    subprocess.run(args)