From 86fc85a7bfbf949655212be4b95e0e598871cb46 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Fri, 17 Jul 2020 20:21:52 +0200 Subject: [PATCH] smol changes --- Rex.py | 63 ++++++++++++++++++++++++--------- __pycache__/Rex.cpython-38.pyc | Bin 0 -> 4959 bytes 2 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 __pycache__/Rex.cpython-38.pyc diff --git a/Rex.py b/Rex.py index 3b89a13..d5f4251 100644 --- a/Rex.py +++ b/Rex.py @@ -7,12 +7,8 @@ from prompt_toolkit.patch_stdout import patch_stdout from prompt_toolkit.completion import Completer from prompt_toolkit.completion import Completion from prompt_toolkit.auto_suggest import AutoSuggestFromHistory -from prompt_toolkit.formatted_text import HTML from fuzzywuzzy import fuzz -def bottom_toolbar(): - return [('bg:green', " All systems nominal")] - async def test(): print("pokemon go") @@ -47,7 +43,7 @@ cmds = { } } -class AutoCompleterPlus(Completer): +class _CompletionLookup(Completer): def get_completions(self, document, complete_event): words = document.text.split(" ") pos = cmds @@ -73,17 +69,27 @@ class AutoCompleterPlus(Completer): arg = args[len(words)-index-1] yield Completion("<"+str(arg)+">", start_position=0) -async def Rex(cmds=cmds): - session = PromptSession() - while True: +class Rex(): + def __init__(self, cmds=cmds, prompt="[~> ", hasToolbar = True, printExceptions = True, raiseExceptions = False, + pipeReturn = False): + self.cmds = cmds + self.prompt = prompt + self.hasToolbar = hasToolbar + self.session = PromptSession() + self.toolbar = [("", "")] + self.printExceptions = printExceptions + self.raiseExceptions = raiseExceptions + self.pipeReturn = pipeReturn + + async def once(self): with patch_stdout(): try: - inp = await session.prompt_async("[~> ", - completer = AutoCompleterPlus(), + inp = await self.session.prompt_async(self.prompt, + completer = _CompletionLookup(), auto_suggest = AutoSuggestFromHistory(), - bottom_toolbar = bottom_toolbar) + bottom_toolbar = [None,self._bottom_toolbar][self.hasToolbar]) except KeyboardInterrupt: - return True + return False try: words = inp.split(" ") pos = cmds @@ -95,10 +101,35 @@ async def Rex(cmds=cmds): break index = i if str(type(pos))=="": - await pos(*words[index+1:]) + if len(inspect.getfullargspec(pos)[0])!=len(words[index+1:]): + print("[!] The given commands expects "+str(len(inspect.getfullargspec(pos)[0]))+" arguments, "+ + "but "+str(len(words[index+1:]))+" were given") + else: + ret = await pos(*words[index+1:]) + if self.pipeReturn: + return ret else: print("[!] No such command") except Exception as e: - print("[!] An Exception Occured: "+str(e)) - for line in traceback.format_exc().split("\n"): - print("[ ] "+line) + if self.printExceptions: + print("[!] An Exception Occured: "+str(e)) + for line in traceback.format_exc().split("\n"): + print("[ ] "+line) + if self.raiseExceptions: + raise e + return True + + async def run(self): + if self.pipeReturn: + raise Exception("Cannot 'run', if pipeReturn is set to true") + while await self.once(): + pass + + def runFromSync(self): + asyncio.run(self.run()) + + def _bottom_toolbar(self): + return self.toolbar + + async def setToolbarMsg(self, msg: str, col: str = "bg:black"): + self.toolbar = [(col, " "+msg)] diff --git a/__pycache__/Rex.cpython-38.pyc b/__pycache__/Rex.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..23e35dc7c3606afca48c350c96f32d72e56daa9a GIT binary patch literal 4959 zcmbVQOOG4J5$>KDl0z<++?V!cMWz!VG7UrW69^n(BSncHvc0w?#a3uL#BjQo%OyF( z?P*54#Lx&#CpRbh0Y2;|CjmbDkYoH0ath{}OOzaQTfk7h>LK^RHDbWS?ds=rRaaMi zRkOdEoh>jtfBnyVePw~Me^Tf4lSk(|O7t%f!36KHT3YiOH+`#SncA+|sI89E&D1iS z*4UkFH&@G0&;LfaMj&seQ23StKJoG6M} z)bpYw%BUB_oR~+wC>F#b>LsxxE}*_3mc5*0x-bUosv7sz964hp@nExTb*X@(DZlIf{dpdT_tw`a&v_3C!ml~+Y5!o91T za!>o6urIDc{vGM-=<02`N99UyKXEi9A|?^BWL#1EoH!hVMjYay*@&gWJmjPoNA9)s z3HP)$gM~>3d?Q&#bFw?N&+Yzy4_ngPt*C-OuR@|K7dw0_Gb^|Ot3Bp4_x(9F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