From d2785f6f687c4a32b91a5b480f96cc61594e025c Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 23 Apr 2020 23:44:29 -0700 Subject: [PATCH] futurize stage2 vui --- joy/vui/core.py | 1 + joy/vui/display.py | 7 +- joy/vui/font_data.py | 4 +- joy/vui/init_joy_home.py | 8 ++- joy/vui/main.py | 8 ++- joy/vui/persist_task.py | 1 + joy/vui/stack_viewer.py | 150 +++++++++++++++++++-------------------- joy/vui/text_viewer.py | 20 +++--- joy/vui/viewer.py | 5 +- 9 files changed, 113 insertions(+), 91 deletions(-) diff --git a/joy/vui/core.py b/joy/vui/core.py index d31a84c..1a87588 100644 --- a/joy/vui/core.py +++ b/joy/vui/core.py @@ -29,6 +29,7 @@ a mainloop class that manages the, uh, main loop (the PyGame event queue.) ''' from __future__ import print_function +from builtins import object, str, range from sys import stderr from traceback import format_exc import pygame diff --git a/joy/vui/display.py b/joy/vui/display.py index 2e94417..7c442c2 100644 --- a/joy/vui/display.py +++ b/joy/vui/display.py @@ -30,6 +30,9 @@ There is a Display object that manages a pygame surface and N vertical tracks each of which manages zero or more viewers. ''' from __future__ import print_function +from __future__ import division +from builtins import next, object +from past.utils import old_div from copy import copy from sys import stderr from traceback import format_exc @@ -80,7 +83,7 @@ class Display(object): if not track_ratios: track_ratios = 1, 4 x, total = 0, sum(track_ratios) for ratio in track_ratios[:-1]: - track_width = self.w * ratio / total + track_width = old_div(self.w * ratio, total) assert track_width >= 10 # minimum width 10 pixels self._open_track(x, track_width) x += track_width @@ -307,6 +310,8 @@ class Display(object): elif event.type == pygame.KEYDOWN: self.focused_viewer.key_down( self, event.unicode, event.key, event.mod) + # This is not UnicodeType. TODO does this need to be fixed? + # self, event.str, event.key, event.mod) def _mouse_event(self, event): V, x, y = self.at(*event.pos) diff --git a/joy/vui/font_data.py b/joy/vui/font_data.py index 7f2fc48..2fd9fa2 100644 --- a/joy/vui/font_data.py +++ b/joy/vui/font_data.py @@ -18,7 +18,9 @@ # along with Thun. If not see . # from __future__ import print_function -from StringIO import StringIO +from future import standard_library +standard_library.install_aliases() +from io import StringIO import base64, zlib diff --git a/joy/vui/init_joy_home.py b/joy/vui/init_joy_home.py index ec494a8..9e4f45f 100644 --- a/joy/vui/init_joy_home.py +++ b/joy/vui/init_joy_home.py @@ -29,7 +29,9 @@ file, so you can just do, e.g.: ''' from __future__ import print_function -import base64, os, StringIO, zipfile +from future import standard_library +standard_library.install_aliases() +import base64, os, io, zipfile def initialize(joy_home): @@ -37,7 +39,7 @@ def initialize(joy_home): def create_data(from_dir='./default_joy_home'): - f = StringIO.StringIO() + f = io.StringIO() z = zipfile.ZipFile(f, mode='w') for fn in os.listdir(from_dir): from_fn = os.path.join(from_dir, fn) @@ -46,7 +48,7 @@ def create_data(from_dir='./default_joy_home'): return base64.encodestring(f.getvalue()) -Z = zipfile.ZipFile(StringIO.StringIO(base64.decodestring('''\ +Z = zipfile.ZipFile(io.StringIO(base64.decodestring('''\ UEsDBBQAAAAAAORmeE794BlRfgMAAH4DAAAPAAAAZGVmaW5pdGlvbnMudHh0c2VlX3N0YWNrID09 IGdvb2Rfdmlld2VyX2xvY2F0aW9uIG9wZW5fc3RhY2sNCnNlZV9yZXNvdXJjZXMgPT0gbGlzdF9y ZXNvdXJjZXMgZ29vZF92aWV3ZXJfbG9jYXRpb24gb3Blbl92aWV3ZXINCm9wZW5fcmVzb3VyY2Vf diff --git a/joy/vui/main.py b/joy/vui/main.py index 8cd5fe3..baeb1ad 100644 --- a/joy/vui/main.py +++ b/joy/vui/main.py @@ -26,6 +26,10 @@ Pulls everything together. ''' from __future__ import print_function +from __future__ import division +from past.builtins import execfile +from builtins import object +from past.utils import old_div import os, sys, traceback import pygame from joy.library import initialize, DefinitionWrapper, SimpleFunctionWrapper @@ -102,8 +106,8 @@ def init_context(screen, clock, pt): *((144 - 89, 144, 89) if FULLSCREEN else (89, 144)) ) log = d.init_text(pt, 0, 0, 'log.txt') - tho = d.init_text(pt, 0, d.h / 3, 'menu.txt') - t = d.init_text(pt, d.w / 2, 0, 'scratch.txt') + tho = d.init_text(pt, 0, old_div(d.h, 3), 'menu.txt') + t = d.init_text(pt, old_div(d.w, 2), 0, 'scratch.txt') loop = core.TheLoop(d, clock) stack_id, stack_holder = pt.open('stack.pickle') world = core.World(stack_id, stack_holder, D, d.broadcast, log) diff --git a/joy/vui/persist_task.py b/joy/vui/persist_task.py index 662fc43..49acb61 100644 --- a/joy/vui/persist_task.py +++ b/joy/vui/persist_task.py @@ -27,6 +27,7 @@ stack) to the git repo in the ``JOY_HOME`` directory. ''' from __future__ import print_function +from builtins import object import os, pickle, traceback from collections import Counter from dulwich.errors import NotGitRepository diff --git a/joy/vui/stack_viewer.py b/joy/vui/stack_viewer.py index 48a9a0a..25bdaa1 100644 --- a/joy/vui/stack_viewer.py +++ b/joy/vui/stack_viewer.py @@ -1,75 +1,75 @@ -# -*- coding: utf-8 -*- -# -# Copyright © 2019 Simon Forman -# -# This file is part of Thun -# -# Thun is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Thun is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Thun. If not see . -# -''' - -Stack Viewer -================= - -''' -from joy.utils.stack import expression_to_string, iter_stack -from joy.vui import core, text_viewer - - -MAX_WIDTH = 64 - - -def fsi(item): - '''Format Stack Item''' - if isinstance(item, tuple): - res = '[%s]' % expression_to_string(item) - elif isinstance(item, str): - res = '"%s"' % item - else: - assert not isinstance(item, unicode), repr(item) - res = str(item) - if len(res) > MAX_WIDTH: - return res[:MAX_WIDTH - 3] + '...' - return res - - -class StackViewer(text_viewer.TextViewer): - - def __init__(self, surface): - super(StackViewer, self).__init__(surface) - self.stack_holder = None - self.content_id = 'stack viewer' - - def _attach(self, display): - if self.stack_holder: - return - om = core.OpenMessage(self, 'stack.pickle') - display.broadcast(om) - if om.status != core.SUCCESS: - raise RuntimeError('stack unavailable') - self.stack_holder = om.thing - - def _update(self): - self.lines[:] = map(fsi, iter_stack(self.stack_holder[0])) or [''] - - def focus(self, display): - self._attach(display) - super(StackViewer, self).focus(display) - - def handle(self, message): - if (isinstance(message, core.ModifyMessage) - and message.subject is self.stack_holder - ): - self._update() - self.draw_body() +# -*- coding: utf-8 -*- +# +# Copyright © 2019 Simon Forman +# +# This file is part of Thun +# +# Thun is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Thun is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Thun. If not see . +# +''' + +Stack Viewer +================= + +''' +from builtins import map, str +from joy.utils.stack import expression_to_string, iter_stack +from joy.vui import core, text_viewer + + +MAX_WIDTH = 64 + + +def fsi(item): + '''Format Stack Item''' + if isinstance(item, tuple): + res = '[%s]' % expression_to_string(item) + elif isinstance(item, str): + res = '"%s"' % item + else: + res = str(item) + if len(res) > MAX_WIDTH: + return res[:MAX_WIDTH - 3] + '...' + return res + + +class StackViewer(text_viewer.TextViewer): + + def __init__(self, surface): + super(StackViewer, self).__init__(surface) + self.stack_holder = None + self.content_id = 'stack viewer' + + def _attach(self, display): + if self.stack_holder: + return + om = core.OpenMessage(self, 'stack.pickle') + display.broadcast(om) + if om.status != core.SUCCESS: + raise RuntimeError('stack unavailable') + self.stack_holder = om.thing + + def _update(self): + self.lines[:] = list(map(fsi, iter_stack(self.stack_holder[0]))) or [''] + + def focus(self, display): + self._attach(display) + super(StackViewer, self).focus(display) + + def handle(self, message): + if (isinstance(message, core.ModifyMessage) + and message.subject is self.stack_holder + ): + self._update() + self.draw_body() diff --git a/joy/vui/text_viewer.py b/joy/vui/text_viewer.py index 6e1f9b2..763bfa5 100644 --- a/joy/vui/text_viewer.py +++ b/joy/vui/text_viewer.py @@ -24,6 +24,10 @@ Text Viewer ''' from __future__ import print_function +from __future__ import division +from builtins import object, range, str, zip +from past.builtins import basestring +from past.utils import old_div import string import pygame from joy.utils.stack import expression_to_string @@ -96,8 +100,8 @@ class Font(object): i = self.LOOKUP.index(ch) except ValueError: # render a lil box... - r = (x + 1, self.line_h / 2 - 3, - self.char_w - 2, self.line_h / 2) + r = (x + 1, old_div(self.line_h, 2) - 3, + self.char_w - 2, old_div(self.line_h, 2)) pygame.draw.rect(surface, FG, r, 1) else: iy, ix = divmod(i, 26) @@ -217,8 +221,8 @@ class TextViewer(MenuViewer): self.grow_rect = pygame.rect.Rect(1, 1, w, h) self.body_surface = surface.subsurface(self.body_rect) - self.line_w = self.body_rect.w / FONT.char_w + 1 - self.h_in_lines = self.body_rect.h / FONT.line_h - 1 + self.line_w = old_div(self.body_rect.w, FONT.char_w) + 1 + self.h_in_lines = old_div(self.body_rect.h, FONT.line_h) - 1 self.command_rect = self.command = None self._sel_start = self._sel_end = None @@ -249,7 +253,7 @@ class TextViewer(MenuViewer): def draw_body(self): MenuViewer.draw_body(self) - ys = xrange(0, self.body_rect.height, FONT.line_h) + ys = range(0, self.body_rect.height, FONT.line_h) ls = self.lines[self.at_line:self.at_line + self.h_in_lines + 2] for y, line in zip(ys, ls): self.draw_line(y, line) @@ -310,7 +314,7 @@ class TextViewer(MenuViewer): return r = self.command_rect = pygame.Rect( word_start * FONT.char_w, # x - y / FONT.line_h * FONT.line_h, # y + old_div(y, FONT.line_h) * FONT.line_h, # y len(word) * FONT.char_w, # w FONT.line_h # h ) @@ -334,7 +338,7 @@ class TextViewer(MenuViewer): Given screen coordinates return the line, row, and column of the character there. ''' - row = self.at_line + y / FONT.line_h + row = self.at_line + old_div(y, FONT.line_h) try: line = self.lines[row] except IndexError: @@ -342,7 +346,7 @@ class TextViewer(MenuViewer): line = self.lines[row] column = len(line) else: - column = min(x / FONT.char_w, len(line)) + column = min(old_div(x, FONT.char_w), len(line)) return line, column, row # Event Processing diff --git a/joy/vui/viewer.py b/joy/vui/viewer.py index 1d7d81b..97fcb1c 100644 --- a/joy/vui/viewer.py +++ b/joy/vui/viewer.py @@ -24,6 +24,9 @@ Viewer ''' from __future__ import print_function +from __future__ import division +from builtins import chr, object +from past.utils import old_div import pygame from joy.vui.core import BACKGROUND, FOREGROUND @@ -242,5 +245,5 @@ class SomeViewer(MenuViewer): def draw_a(surface, color=FOREGROUND, blend=False): w, h = surface.get_width() - 2, surface.get_height() - 2 pygame.draw.aalines(surface, color, False, ( - (1, h), (w / 2, 1), (w, h), (1, h / 2) + (1, h), (old_div(w, 2), 1), (w, h), (1, old_div(h, 2)) ), blend)