Михаил Капелько 7 months ago
parent
commit
1b3005a101
4 changed files with 128 additions and 71 deletions
  1. +6
    -0
      v4/Controller.py
  2. +25
    -0
      v4/cli.py
  3. +84
    -25
      v4/cli_test.py
  4. +13
    -46
      v4/main.py

+ 6
- 0
v4/Controller.py View File

@@ -29,6 +29,12 @@ class Controller:
def registerCallback(self, cb):
self.callbacks.append(cb)

def registerFieldCallback(self, fieldName, cb):
def execCB(c):
if c.recentField == fieldName:
cb(c)
self.callbacks.append(execCB)

def registerFunction(self, f):
self.functions.append(f)



+ 25
- 0
v4/cli.py View File

@@ -1,6 +1,31 @@
from memory_Context import *
from llm import *

# Exit
#
# Conditions:
# 1. `e`, `exit`, `q`, or `quit` was entered
@llm_by_value
def cli_exit(
c: memory_Context
) -> memory_Context:
if (
c.recentField == "input" and
(
c.input == "e" or
c.input == "exit" or
c.input == "q" or
c.input == "quit"
)
):
c.exit = True
c.recentField = "exit"
return c
#}
c.recentField = "none"
return c
#}

# Greet the user
#
# Conditions:


+ 84
- 25
v4/cli_test.py View File

@@ -2,6 +2,62 @@ from cli import *
from memory import *
from memory_Context import *

def cli_test_exit_e(
) -> str:
c = memory_createContext()
c.input = "e"
c.recentField = "input"
c = cli_exit(c)
if (
c.recentField == "exit"
):
return "OK: cli_exit_e"
#}
return "ERR: cli_exit_e"
#}

def cli_test_exit_exit(
) -> str:
c = memory_createContext()
c.input = "exit"
c.recentField = "input"
c = cli_exit(c)
if (
c.recentField == "exit"
):
return "OK: cli_exit_exit"
#}
return "ERR: cli_exit_e"
#}

def cli_test_exit_q(
) -> str:
c = memory_createContext()
c.input = "q"
c.recentField = "input"
c = cli_exit(c)
if (
c.recentField == "exit"
):
return "OK: cli_exit_q"
#}
return "ERR: cli_exit_q"
#}

def cli_test_exit_quit(
) -> str:
c = memory_createContext()
c.input = "quit"
c.recentField = "input"
c = cli_exit(c)
if (
c.recentField == "exit"
):
return "OK: cli_exit_quit"
#}
return "ERR: cli_exit_quit"
#}

def cli_test_greetUser(
) -> str:
c = memory_createContext()
@@ -16,6 +72,34 @@ def cli_test_greetUser(
return "ERR: cli_greetUser"
#}

def cli_test_showHelp_h(
) -> str:
c = memory_createContext()
c.input = "h"
c.recentField = "input"
c = cli_showHelp(c)
if (
c.recentField == "outputHelp"
):
return "OK: cli_showHelp_h"
#}
return "ERR: cli_showHelp_h"
#}

def cli_test_showHelp_help(
) -> str:
c = memory_createContext()
c.input = "help"
c.recentField = "input"
c = cli_showHelp(c)
if (
c.recentField == "outputHelp"
):
return "OK: cli_showHelp_help"
#}
return "ERR: cli_showHelp_help"
#}

#def cli_test_selectItem(
#) -> str:
# c = cli_createContext()
@@ -61,28 +145,3 @@ def cli_test_greetUser(
# return "ERR: cli_shouldReportInvalidItemSelection"
##}
#
#def cli_test_showHelp_h(
#) -> str:
# c = cli_createContext()
# c.input = "h"
# c = cli_showHelp(c)
# if (
# c.recentField == "outputHelp"
# ):
# return "OK: cli_showHelp_h"
# #}
# return "ERR: cli_showHelp_h"
##}
#
#def cli_test_showHelp_help(
#) -> str:
# c = cli_createContext()
# c.input = "help"
# c = cli_showHelp(c)
# if (
# c.recentField == "outputHelp"
# ):
# return "OK: cli_showHelp_help"
# #}
# return "ERR: cli_showHelp_help"
##}

+ 13
- 46
v4/main.py View File

@@ -2,8 +2,7 @@ from cli import *
from cli_test import *
from memory_test import *
from Controller import *
#from shell import *
#import sys
import sys

print(memory_test_deselectMismatchedItems())
print(memory_test_deselectMismatchedItems_itemTwice())
@@ -14,24 +13,29 @@ print(memory_test_selectItem_1x())
print(memory_test_selectItem_2x())
print(memory_test_selectItem_3x())

print(cli_test_exit_e())
print(cli_test_exit_exit())
print(cli_test_exit_q())
print(cli_test_exit_quit())
print(cli_test_greetUser())
print(cli_test_showHelp_h())
print(cli_test_showHelp_help())
#print(cli_test_selectItem())
#print(cli_test_shouldPromptSelection())
##print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin())
#print(cli_test_showHelp_h())
#print(cli_test_showHelp_help())


ctrl = Controller(memory_createContext())
ctrl.registerFunction(cli_exit)
ctrl.registerFunction(cli_greetUser)
ctrl.registerFunction(cli_showHelp)
ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'"))

def printOutput(c):
if c.recentField.startswith("output"):
print(getattr(c, c.recentField))
ctrl.registerCallback(printOutput)

#ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'"))
ctrl.registerFieldCallback("exit", lambda c: sys.exit(0))

#
#c = shell_createContext()
@@ -46,43 +50,6 @@ ctrl.registerCallback(printOutput)

ctrl.set("didLaunch", True)

#for line in sys.stdin:
# c.input = line.rstrip()
# c = shell_processInput(c)
# if c.exit:
# break
# print(c.output)






import threading
import time

class KeyboardThread(threading.Thread):

def __init__(self, input_cbk = None, name='keyboard-input-thread'):
self.input_cbk = input_cbk
super(KeyboardThread, self).__init__(name=name, daemon=True)
self.start()

def run(self):
while True:
self.input_cbk(input()) #waits to get input + Return

showcounter = 0 #something to demonstrate the change

def my_callback(inp):
#evaluate the keyboard input
print('You Entered:', inp, ' Counter is at:', showcounter)

#start the Keyboard thread
kthread = KeyboardThread(my_callback)

while True:
#the normal program executes without blocking. here just counting up
showcounter += 1
time.sleep(0.5)

for line in sys.stdin:
ln = line.rstrip()
ctrl.set("input", ln)

Loading…
Cancel
Save