From 4ed8422258cfece31cd89cdbc882ed563cb29c32 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sun, 22 Jul 2018 11:50:34 -0700 Subject: [PATCH] Load JOY_HOME/definitions.txt You still can't edit other text files from within the UI, but at least now you have a place to persist your own definitions over restarts. I thought about having a [definitions] section in the config INI file, but for some reason I prefer a separate definitions.txt file. I dunno. Might change it in future. --- joy/gui/main.py | 4 ++-- joy/library.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/joy/gui/main.py b/joy/gui/main.py index 150028c..eb84be4 100755 --- a/joy/gui/main.py +++ b/joy/gui/main.py @@ -29,7 +29,7 @@ _log.info('Starting with JOY_HOME=%s', JOY_HOME) from joy.gui.textwidget import TextViewerWidget, tk, get_font from joy.gui.world import StackDisplayWorld -from joy.library import initialize +from joy.library import initialize, DefinitionWrapper from joy.utils.stack import stack_to_string @@ -48,7 +48,6 @@ def repo_relative_path(path): os.path.commonprefix((repo.controldir(), path)) ) - def commands(): # pylint: disable=unused-variable @@ -112,6 +111,7 @@ JOY_FN = os.path.join(JOY_HOME, 'scratch.txt') LOG_FN = os.path.join(JOY_HOME, 'log.txt') D = initialize() D.update(commands()) +DefinitionWrapper.load_definitions(os.path.join(JOY_HOME, 'definitions.txt'), D) world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, dictionary=D) defaults = dict(width=80, height=25) t = TextViewerWidget(world, **defaults) diff --git a/joy/library.py b/joy/library.py index e760ddb..9e55b7a 100644 --- a/joy/library.py +++ b/joy/library.py @@ -380,6 +380,13 @@ class DefinitionWrapper(object): _log.info('Adding definition %s := %s', F.name, expression_to_string(F.body)) dictionary[F.name] = F + @classmethod + def load_definitions(class_, filename, dictionary): + with open(filename) as f: + lines = [line for line in f if '==' in line] + for line in lines: + class_.add_def(line, dictionary) + def _text_to_defs(text): return (line.strip() for line in text.splitlines() if '==' in line)