diff options
Diffstat (limited to 'presentty/image.py')
-rw-r--r-- | presentty/image.py | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/presentty/image.py b/presentty/image.py index 939536f..0fc700b 100644 --- a/presentty/image.py +++ b/presentty/image.py @@ -13,15 +13,21 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import absolute_import, division, print_function + import subprocess -import HTMLParser import re import PIL import PIL.ExifTags import urwid -import slide +import six.moves.html_parser +from six.moves import range +from six.moves import input + +from . import slide + def nearest_color(x): if x < 0x30: return '0' @@ -36,7 +42,7 @@ class ANSIImage(urwid.Widget): super(ANSIImage, self).__init__() self.uri = uri image = self._loadImage() - self.htmlparser = HTMLParser.HTMLParser() + self.htmlparser = six.moves.html_parser.HTMLParser() self.ratio = float(image.size[0])/float(image.size[1]) self.hinter = hinter if scale > 1: @@ -89,12 +95,12 @@ class ANSIImage(urwid.Widget): def _blank(self, width, height): ret = [] for y in range(height): - ret.append("<span style='color:#000000; background-color:#000000;'>%s</span>" % ('.'*width)) - return '<br/>'.join(ret) + ret.append(b"<span style='color:#000000; background-color:#000000;'>%s</span>" % (b'.'*width)) + return b'<br/>'.join(ret) - SPAN_RE = re.compile(r"<span style='color:#(......); background-color:#(......);'>(.*)") + b_SPAN_RE = re.compile(br"<span style='color:#(......); background-color:#(......);'>(.*)") def render(self, size, focus=False): - spanre = self.SPAN_RE + b_spanre = self.b_SPAN_RE htmlparser = self.htmlparser # Calculate image size and any bounding box @@ -116,26 +122,26 @@ class ANSIImage(urwid.Widget): stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - except OSError, e: + except OSError as 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) + input("Press ENTER to continue.") + b_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() + b_data = jp2a.stdout.read() jp2a.stderr.read() jp2a.wait() - line_list = [] + b_line_list = [] attr_list = [] - line_text = '' + b_line_text = b'' line_attrs = [] current_attr = [None, 0] current_fg = None @@ -144,28 +150,28 @@ class ANSIImage(urwid.Widget): # Top pad for padding in range(0, top_pad): - line_list.append(' ' * total_width) + b_line_list.append(b' ' * total_width) attr_list.append([(padding_attr, 1)] * total_width) - for line in data.split('<br/>'): - if not line: + for b_line in b_data.split(b'<br/>'): + if not b_line: continue # Left pad - line_text += ' ' * left_pad + b_line_text += b' ' * left_pad for fake_attr in range(0, left_pad): line_attrs.append((padding_attr, 1)) - for span in line.split('</span>'): + for b_span in b_line.split(b'</span>'): - if not span: + if not b_span: continue - m = spanre.match(span) - fg, bg, char = m.groups() - if '&' in char: - char = htmlparser.unescape(char) - char = char.encode('utf8') - line_text += char + m = b_spanre.match(b_span) + fg, bg, b_char = m.groups() + if b'&' in b_char: + char = htmlparser.unescape(b_char.decode('utf-8')) + b_char = char.encode('utf8') + b_line_text += b_char props = [] # TODO: if bold is set, append bold to props fg = ('#'+ @@ -177,13 +183,13 @@ class ANSIImage(urwid.Widget): nearest_color(int(bg[2:4], 16)) + nearest_color(int(bg[4:6], 16))) if current_fg == fg and current_bg == bg and current_props == props: - current_attr[1] += len(char) + current_attr[1] += len(b_char) else: if current_attr[0]: line_attrs.append(tuple(current_attr)) fg = ', '.join(props + [fg]) attr = urwid.AttrSpec(fg, bg) - current_attr = [attr, len(char)] + current_attr = [attr, len(b_char)] current_fg = fg current_bg = bg current_props = props @@ -193,21 +199,21 @@ class ANSIImage(urwid.Widget): current_bg = None # Right pad - line_text += ' ' * right_pad + b_line_text += b' ' * right_pad for fake_attr in range(0, right_pad): line_attrs.append((padding_attr, 1)) - line_list.append(line_text) - line_text = '' + b_line_list.append(b_line_text) + b_line_text = b'' attr_list.append(line_attrs) line_attrs = [] # Bottom pad for padding in range(0, bottom_pad): - line_list.append(' ' * total_width) + b_line_list.append(b' ' * total_width) attr_list.append([(padding_attr, 1)] * total_width) - canvas = urwid.TextCanvas(line_list, attr_list) + canvas = urwid.TextCanvas(b_line_list, attr_list) return canvas def main(): @@ -227,4 +233,4 @@ def main(): if True: with screen.start(): screen.draw_screen((80,25), fill.render((80,25))) - raw_input() + input() |