From c2a8726a9caef70923542b2b67b21e16891c0870 Mon Sep 17 00:00:00 2001 From: gailweiss Date: Fri, 1 Mar 2024 13:30:34 +0100 Subject: [PATCH] Modulise clean (#17) * make all the imports relative, so all this can be imported in python, towards integration goals. * add a __main__.py to RASP_support, that starts the REPL, so the module can still be used directly as a REPL, now by calling python3 -m RASP_support. * update rasp.sh and tests/test_all.py to use this new call * add init file to the module, exposing rasp repl for easy use after import --- RASP_support/DrawCompFlow.py | 14 +++++++++++--- RASP_support/Environment.py | 4 ++-- RASP_support/Evaluator.py | 8 ++++---- RASP_support/FunctionalSupport.py | 8 ++++---- RASP_support/REPL.py | 12 ++++++------ RASP_support/Sugar.py | 14 ++++++++------ RASP_support/__init__.py | 1 + RASP_support/__main__.py | 3 +++ RASP_support/analyse.py | 5 ++++- rasp.sh | 4 ++-- tests/test_all.py | 4 ++-- 11 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 RASP_support/__init__.py create mode 100644 RASP_support/__main__.py diff --git a/RASP_support/DrawCompFlow.py b/RASP_support/DrawCompFlow.py index 43c840b..6679f2a 100644 --- a/RASP_support/DrawCompFlow.py +++ b/RASP_support/DrawCompFlow.py @@ -1,9 +1,14 @@ -from FunctionalSupport import Unfinished, guarded_contains, base_tokens, \ +from .FunctionalSupport import Unfinished, guarded_contains, base_tokens, \ tokens_asis -from Support import clean_val +from .Support import clean_val import os import string -import analyse # adds useful functions to all the Unfinisheds + +# adds useful functions to all the Unfinisheds +# using dummyimport to import analyse with the relative "." because python +# won't let me do "import .analyse" +from .analyse import dummyimport + # fix: in ordering, we always connect bottom FF to top select. but sometimes, # there is no FF (if go straight into next select), or there is no rendered @@ -602,3 +607,6 @@ def draw_comp_flow(self, w, filename=None, g.view() if not keep_dot: os.remove(filename) + + +dummyimport = None diff --git a/RASP_support/Environment.py b/RASP_support/Environment.py index c80636d..0dddc1e 100644 --- a/RASP_support/Environment.py +++ b/RASP_support/Environment.py @@ -1,6 +1,6 @@ -from FunctionalSupport import Unfinished, RASPTypeError, tokens_asis, \ +from .FunctionalSupport import Unfinished, RASPTypeError, tokens_asis, \ tokens_str, tokens_int, tokens_bool, tokens_float, indices -from Evaluator import RASPFunction +from .Evaluator import RASPFunction class UndefinedVariable(Exception): diff --git a/RASP_support/Evaluator.py b/RASP_support/Evaluator.py index ee566d4..310f6f9 100644 --- a/RASP_support/Evaluator.py +++ b/RASP_support/Evaluator.py @@ -1,10 +1,10 @@ -from FunctionalSupport import select, zipmap, aggregate, \ +from .FunctionalSupport import select, zipmap, aggregate, \ or_selects, and_selects, not_select, indices, \ Unfinished, UnfinishedSequence, UnfinishedSelect -from Sugar import tplor, tpland, tplnot, toseq, full_s -from Support import RASPTypeError, RASPError +from .Sugar import tplor, tpland, tplnot, toseq, full_s +from .Support import RASPTypeError, RASPError from collections.abc import Iterable -from zzantlr.RASPParser import RASPParser +from .zzantlr.RASPParser import RASPParser ENCODER_NAME = "s-op" diff --git a/RASP_support/FunctionalSupport.py b/RASP_support/FunctionalSupport.py index e970792..da3920c 100644 --- a/RASP_support/FunctionalSupport.py +++ b/RASP_support/FunctionalSupport.py @@ -1,7 +1,7 @@ -from Support import aggregate as _aggregate -from Support import Sequence, RASPTypeError -from Support import select as _select -from Support import zipmap as _zipmap +from .Support import aggregate as _aggregate +from .Support import Sequence, RASPTypeError +from .Support import select as _select +from .Support import zipmap as _zipmap import traceback import sys # for readable exception handling from collections.abc import Iterable diff --git a/RASP_support/REPL.py b/RASP_support/REPL.py index ac79aaa..ea8474b 100644 --- a/RASP_support/REPL.py +++ b/RASP_support/REPL.py @@ -1,13 +1,13 @@ from antlr4.error.ErrorListener import ErrorListener from antlr4 import CommonTokenStream, InputStream from collections.abc import Iterable -from zzantlr.RASPLexer import RASPLexer -from zzantlr.RASPParser import RASPParser -from Environment import Environment, UndefinedVariable, ReservedName -from FunctionalSupport import UnfinishedSequence, UnfinishedSelect, Unfinished -from Evaluator import Evaluator, NamedVal, NamedValList, JustVal, \ +from .zzantlr.RASPLexer import RASPLexer +from .zzantlr.RASPParser import RASPParser +from .Environment import Environment, UndefinedVariable, ReservedName +from .FunctionalSupport import UnfinishedSequence, UnfinishedSelect, Unfinished +from .Evaluator import Evaluator, NamedVal, NamedValList, JustVal, \ RASPFunction, ArgsError, RASPTypeError, RASPValueError -from Support import Select, Sequence, lazy_type_check +from .Support import Select, Sequence, lazy_type_check ENCODER_NAME = "s-op" diff --git a/RASP_support/Sugar.py b/RASP_support/Sugar.py index 95516be..da80d51 100644 --- a/RASP_support/Sugar.py +++ b/RASP_support/Sugar.py @@ -1,11 +1,13 @@ -from FunctionalSupport import Unfinished as _Unfinished -from FunctionalSupport import UnfinishedSequence as _UnfinishedSequence -from FunctionalSupport import select, zipmap -from make_operators import add_ops -import DrawCompFlow +from .FunctionalSupport import Unfinished as _Unfinished +from .FunctionalSupport import UnfinishedSequence as _UnfinishedSequence +from .FunctionalSupport import select, zipmap +from .make_operators import add_ops +from .DrawCompFlow import dummyimport # DrawCompFlow is not at all necessary for sugar, but sugar is really the # top-level rasp file we import, and nice to have draw_comp_flow added into -# the sequences already on load +# the sequences already on load. also, don't *really* need to import +# dummy specifically, but python won't accept "import .DrawCompFlow" +# while it will accept some form of "from .DrawCompFlow import ..." def _apply_unary_op(self, f): diff --git a/RASP_support/__init__.py b/RASP_support/__init__.py new file mode 100644 index 0000000..70e5792 --- /dev/null +++ b/RASP_support/__init__.py @@ -0,0 +1 @@ +from .REPL import REPL diff --git a/RASP_support/__main__.py b/RASP_support/__main__.py new file mode 100644 index 0000000..3ee2c72 --- /dev/null +++ b/RASP_support/__main__.py @@ -0,0 +1,3 @@ +from .REPL import REPL + +REPL().run() diff --git a/RASP_support/analyse.py b/RASP_support/analyse.py index fc1b185..1a023fe 100644 --- a/RASP_support/analyse.py +++ b/RASP_support/analyse.py @@ -1,4 +1,4 @@ -from FunctionalSupport import Unfinished, UnfinishedSequence, \ +from .FunctionalSupport import Unfinished, UnfinishedSequence, \ UnfinishedSelect, guarded_contains, guarded_compare, zipmap from collections import defaultdict, Counter from copy import copy @@ -353,3 +353,6 @@ def pre_aggregate_comp(seq): if isinstance(vreal, tuple): # equivalently, if seq.output_index >= 0: vreal = vreal[seq.output_index] return vreal + + +dummyimport = None diff --git a/rasp.sh b/rasp.sh index ad1edd8..1591c81 100755 --- a/rasp.sh +++ b/rasp.sh @@ -2,9 +2,9 @@ source raspenv/bin/activate if [[ $(rlwrap -v) == rlwrap* ]]; then # the better option. requires rlwrap - rlwrap python3 RASP_support/REPL.py + rlwrap python3 -m RASP_support else - python3 RASP_support/REPL.py + python3 -m RASP_support fi deactivate \ No newline at end of file diff --git a/tests/test_all.py b/tests/test_all.py index 8fe57e5..104a077 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -15,7 +15,7 @@ def check_equal(f1, f2): def run_input(name): - os.system("python3 RASP_support/REPL.py <" + + os.system("python3 -m RASP_support <" + joinpath(inpath, name)+" >"+joinpath(outpath, name)) fix_file_paths(joinpath(outpath, name), curr_path_marker) return check_equal(joinpath(outpath, name), joinpath(tgtpath, name)) @@ -34,7 +34,7 @@ def run_inputs(): def test_broken_lib(lib): os.system("cp "+joinpath(libspath, lib)+" RASP_support/rasplib.rasp") - os.system("python3 RASP_support/REPL.py <"+joinpath(libtestspath, + os.system("python3 -m RASP_support <"+joinpath(libtestspath, "empty.txt") + " >"+joinpath(liboutspath, lib)) return check_equal(joinpath(liboutspath, lib), joinpath(libtgtspath, lib))