diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-11-22 11:24:27 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-11-22 11:24:27 +0100 |
commit | feb9fa61ae0c0473da3b6ce77c1bbeaae7b44193 (patch) | |
tree | f751a8fc84b529c05bf4767b624b6dccfc9bdfa8 /bin/zathura_wrapper | |
parent | 4573d659a936e47980ac99bd53d4d627d3ab3f15 (diff) |
desktop: add a wrapper for zathura handling HTTP(s) URLs
Zathura can do it by itself, but that requires gvfs, which is a giant
gnome monster I do not want to install for such a simple thing.
Diffstat (limited to 'bin/zathura_wrapper')
-rwxr-xr-x | bin/zathura_wrapper | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/bin/zathura_wrapper b/bin/zathura_wrapper new file mode 100755 index 0000000..0129df3 --- /dev/null +++ b/bin/zathura_wrapper @@ -0,0 +1,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) |