icat kitten: Add support for displaying images at http(s) URLs

Fixes #1340
This commit is contained in:
Kovid Goyal 2019-01-27 21:28:46 +05:30
parent af9e633d56
commit a09fb7b20d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 18 additions and 4 deletions

View File

@ -6,6 +6,8 @@ Changelog
0.13.4 [future]
---------------------
- icat kitten: Add support for displaying images at http(s) URLs (:iss:`1340`)
- A new option :opt:`strip_trailing_spaces` to optionally remove trailing
spaces from lines when copying to clipboard.

View File

@ -318,6 +318,7 @@ def main(args=sys.argv):
if len(items) > 1 or (isinstance(items[0], str) and os.path.isdir(items[0])):
raise SystemExit(f'The --place option can only be used with a single image, not {items}')
sys.stdout.buffer.write(b'\0337') # save cursor
url_pat = re.compile(r'https?://', flags=re.I)
for item in items:
is_tempfile = False
try:
@ -326,11 +327,22 @@ def main(args=sys.argv):
tf.write(item), tf.close()
item = tf.name
is_tempfile = True
if os.path.isdir(item):
for x in scan(item):
process(item, args)
else:
if url_pat.match(item) is not None:
from urllib.request import urlretrieve
with NamedTemporaryFile(prefix='url-image-data-', delete=False) as tf:
try:
urlretrieve(item, filename=tf.name)
except Exception as e:
raise SystemExit('Failed to download image at URL: {} with error: {}'.format(item, e))
item = tf.name
is_tempfile = True
process(item, args, is_tempfile)
else:
if os.path.isdir(item):
for x in scan(item):
process(item, args)
else:
process(item, args, is_tempfile)
except NoImageMagick as e:
raise SystemExit(str(e))
except ConvertFailed as e: