From 3e4efbb2e0eb1ee33b0157468682b6aceffbadfe Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Sun, 20 Nov 2016 12:27:59 -0800 Subject: Fail gracefully if jp2a is not installed --- presentty/image.py | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/presentty/image.py b/presentty/image.py index 2dbd9ad..939536f 100644 --- a/presentty/image.py +++ b/presentty/image.py @@ -43,6 +43,9 @@ class ANSIImage(urwid.Widget): scale = 1 self.scale = scale self.background = background or 'black' + self._prime = True + self.render((3,1)) + self._prime = False def _loadImage(self): image = PIL.Image.open(self.uri) @@ -83,6 +86,12 @@ class ANSIImage(urwid.Widget): r = self.pack(size) return r[1] + def _blank(self, width, height): + ret = [] + for y in range(height): + ret.append("%s" % ('.'*width)) + return '
'.join(ret) + SPAN_RE = re.compile(r"(.*)") def render(self, size, focus=False): spanre = self.SPAN_RE @@ -99,20 +108,30 @@ class ANSIImage(urwid.Widget): right_pad = total_width - width - left_pad padding_attr = urwid.AttrSpec(self.background, self.background) - jp2a = subprocess.Popen(['jp2a', '--colors', '--fill', - '--width=%s' % width, - '--height=%s' % height, - '--html-raw', '-'], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - image = self._loadImage() - image = image.convert('RGBA') - image.save(jp2a.stdin, 'JPEG') - jp2a.stdin.close() - data = jp2a.stdout.read() - jp2a.stderr.read() - jp2a.wait() + try: + jp2a = subprocess.Popen(['jp2a', '--colors', '--fill', + '--width=%s' % width, + '--height=%s' % height, + '--html-raw', '-'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + except OSError, e: + if self._prime: + if e.errno == 2: + print("ERROR: jp2a is used but is not installed.") + else: + print("ERROR: unable to run jp2a: %s" % e) + raw_input("Press ENTER to continue.") + data = self._blank(width, height) + else: + image = self._loadImage() + image = image.convert('RGBA') + image.save(jp2a.stdin, 'JPEG') + jp2a.stdin.close() + data = jp2a.stdout.read() + jp2a.stderr.read() + jp2a.wait() line_list = [] attr_list = [] -- cgit v1.2.3