From da03f60dca8c2137e443e8eab8b667d3c71bf889 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 14 Jul 2018 19:51:31 -0700 Subject: [PATCH] Move StackDisplayWorld into world.py --- joy/gui/main.py | 34 ++++++---------------------------- joy/gui/world.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/joy/gui/main.py b/joy/gui/main.py index 5d47991..82d0c3f 100755 --- a/joy/gui/main.py +++ b/joy/gui/main.py @@ -12,7 +12,7 @@ from textwrap import dedent from joy.gui.textwidget import TextViewerWidget, tk, get_font, TEXT_BINDINGS from joy.gui.utils import init_home, FileFaker -from joy.gui.world import World +from joy.gui.world import StackDisplayWorld from joy.library import initialize from joy.utils.stack import stack_to_string @@ -58,35 +58,11 @@ def repo_relative_path(path): STACK_FN = os.path.join(JOY_HOME, 'stack.pickle') +REL_STACK_FN = repo_relative_path(STACK_FN) JOY_FN = os.path.join(JOY_HOME, 'scratch.txt') LOG_FN = os.path.join(JOY_HOME, 'log.txt') -class StackDisplayWorld(World): - - relative_STACK_FN = repo_relative_path(STACK_FN) - - def interpret(self, command): - print '\njoy?', command - super(StackDisplayWorld, self).interpret(command) - - def print_stack(self): - print '\n%s <-' % stack_to_string(self.stack) - - def save(self): - with open(STACK_FN, 'wb') as f: - os.chmod(STACK_FN, 0600) - pickle.dump(self.stack, f) - f.flush() - os.fsync(f.fileno()) - repo.stage([self.relative_STACK_FN]) - commit_id = repo.do_commit( - 'message', - committer='Simon Forman ', - ) - print >> sys.stderr, commit_id - - def key_bindings(*args): print dedent(''' Ctrl-Enter - Run the selection as Joy code. @@ -152,11 +128,13 @@ for func in ( mouse_bindings, ): D[func.__name__] = func + stack = load_stack() if stack is None: - world = StackDisplayWorld(dictionary=D) + world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, dictionary=D) else: - world = StackDisplayWorld(stack=stack, dictionary=D) + world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, stack=stack, dictionary=D) + t = TextViewerWidget(world, **defaults) log_window = tk.Toplevel() log_window.protocol("WM_DELETE_WINDOW", log_window.withdraw) diff --git a/joy/gui/world.py b/joy/gui/world.py index 568e1af..9c42246 100644 --- a/joy/gui/world.py +++ b/joy/gui/world.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU General Public License # along with joy.py. If not see . # +import os, pickle, sys from inspect import getdoc from joy.joy import run @@ -95,3 +96,33 @@ class World(object): self.text_widget.see(stack_out_index) s = stack_to_string(self.stack) + '\n' self.text_widget.insert(stack_out_index, s) + + +class StackDisplayWorld(World): + + + def __init__(self, repo, filename, rel_filename, stack=(), dictionary=None, text_widget=None): + World.__init__(self, stack, dictionary, text_widget) + self.repo = repo + self.filename = filename + self.relative_STACK_FN = rel_filename + + def interpret(self, command): + print '\njoy?', command + super(StackDisplayWorld, self).interpret(command) + + def print_stack(self): + print '\n%s <-' % stack_to_string(self.stack) + + def save(self): + with open(self.filename, 'wb') as f: + os.chmod(self.filename, 0600) + pickle.dump(self.stack, f) + f.flush() + os.fsync(f.fileno()) + self.repo.stage([self.relative_STACK_FN]) + commit_id = self.repo.do_commit( + 'message', + committer='Simon Forman ', + ) + print >> sys.stderr, commit_id -- 2.11.0