From 958d0842ac8d404dc9a2505cd5223c4c869bff02 Mon Sep 17 00:00:00 2001 From: Kodjo Sossouvi Date: Fri, 17 Apr 2020 22:09:18 +0200 Subject: [PATCH] Added interactive mode --- main.py | 57 ++++++++++++++++++++++++++--------- requirements.txt | 4 +++ src/printer/SheerkaPrinter.py | 1 + 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index d5a9022..52e4ecd 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,28 @@ import getopt import sys -import logging - +from os import path +import click import core.utils from core.sheerka.Sheerka import Sheerka +from prompt_toolkit import prompt +from prompt_toolkit.auto_suggest import AutoSuggestFromHistory +from prompt_toolkit.history import FileHistory def usage(): print("Sheerka v0.1\n") print("usage:") - print(sys.argv[0] + "[-hdl:] command ") + print(sys.argv[0] + "[-hdl:] command | [-i]") def main(argv): + debug = False + interactive = False + loggers = set() + try: - opts, args = getopt.getopt(argv, "hdl:", ["help", "debug", "logger="]) - debug = False - loggers = set() + opts, args = getopt.getopt(argv, "hdl:i", ["help", "debug", "logger=", "interactive"]) + for o, a in opts: if o in ('-h', "--help"): usage() @@ -25,21 +31,44 @@ def main(argv): debug = True if o in ('-l', '-logger'): loggers.add(a) + if o in ('-i', '--interactive'): + interactive = True + except getopt.GetoptError: + usage() + sys.exit(2) - sheerka = Sheerka(debug=debug, loggers=loggers) - sheerka.initialize() + sheerka = Sheerka(debug=debug, loggers=loggers) + sheerka.initialize() + history_file = path.abspath(path.join(path.expanduser("~"), ".sheerka", "history.txt")) + if interactive: + while True: + try: + _in = prompt('sheerka> ', + history=FileHistory(history_file), + auto_suggest=AutoSuggestFromHistory(), + ) + if _in in ("exit", "quit", "bye"): + print("Take care.") + break + + if _in == '__': + _in = click.edit() + + result = sheerka.evaluate_user_input(_in) + sheerka.print(result) + except KeyboardInterrupt: + continue + except EOFError: + print("EOFError...") + sys.exit(3) + sys.exit(0) + else: _in = core.utils.sysarg_to_string(args) result = sheerka.evaluate_user_input(_in) sheerka.print(result) - # for res in result: - # logging.info(res) - return result[-1].status if len(result) > 0 else 1 - except getopt.GetoptError: - usage() - sys.exit(2) if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index ced1479..2f1d36b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,18 @@ appdirs==1.4.3 atomicwrites==1.3.0 attrs==19.3.0 +click==7.1.1 fs==2.4.11 +gprof2dot==2017.9.19 more-itertools==7.2.0 packaging==19.2 pluggy==0.13.0 +prompt-toolkit==3.0.5 py==1.8.0 Pygments==2.4.2 pyparsing==2.4.4 pytest==5.2.2 +pytest-profiling==1.7.0 pytz==2019.3 six==1.13.0 wcwidth==0.1.7 diff --git a/src/printer/SheerkaPrinter.py b/src/printer/SheerkaPrinter.py index ea2bf98..8959033 100644 --- a/src/printer/SheerkaPrinter.py +++ b/src/printer/SheerkaPrinter.py @@ -1,3 +1,4 @@ +import click from core.builtin_concepts import BuiltinConcepts from core.concept import Concept from printer.FormatInstructions import FormatInstructions, FormatDetailType