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
2 changed files with 18 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ Changelog
0.13.4 [future] 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 - A new option :opt:`strip_trailing_spaces` to optionally remove trailing
spaces from lines when copying to clipboard. 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])): 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}') raise SystemExit(f'The --place option can only be used with a single image, not {items}')
sys.stdout.buffer.write(b'\0337') # save cursor sys.stdout.buffer.write(b'\0337') # save cursor
url_pat = re.compile(r'https?://', flags=re.I)
for item in items: for item in items:
is_tempfile = False is_tempfile = False
try: try:
@@ -326,11 +327,22 @@ def main(args=sys.argv):
tf.write(item), tf.close() tf.write(item), tf.close()
item = tf.name item = tf.name
is_tempfile = True is_tempfile = True
if os.path.isdir(item): if url_pat.match(item) is not None:
for x in scan(item): from urllib.request import urlretrieve
process(item, args) with NamedTemporaryFile(prefix='url-image-data-', delete=False) as tf:
else: 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) 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: except NoImageMagick as e:
raise SystemExit(str(e)) raise SystemExit(str(e))
except ConvertFailed as e: except ConvertFailed as e: