From ee28d89f56906a5160835cf0eaf57cd1e8a62692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 19 May 2024 22:34:00 +0300 Subject: [PATCH] d --- v4/ContextController.py | 22 --------------------- v4/Controller.py | 44 +++++++++++++++++++++++++++++++++++++++++ v4/main.py | 16 +++++++++------ 3 files changed, 54 insertions(+), 28 deletions(-) delete mode 100644 v4/ContextController.py create mode 100644 v4/Controller.py diff --git a/v4/ContextController.py b/v4/ContextController.py deleted file mode 100644 index ee8e673..0000000 --- a/v4/ContextController.py +++ /dev/null @@ -1,22 +0,0 @@ -import memory_Context - -class ContextController: - def __init__(self): - self.c = memory_createContext() - - def executeFunctions(self): - pass - - def reportCallbacks(self): - pass - - def set(self, field, value): - setattr(self.c, field, value) - self.c.recentField = field - self.executeFunctions() - self.reportCallbacks() - -ctrl = ContextController() -ctrl.callback(printOutput) - -ctrl.set("didLaunch", True) diff --git a/v4/Controller.py b/v4/Controller.py new file mode 100644 index 0000000..c0310a1 --- /dev/null +++ b/v4/Controller.py @@ -0,0 +1,44 @@ +import copy + +class Controller: + def __init__(self, c): + self.callbacks = [] + self.context = c + self.functions = [] + self.isProcessingQueue = False + self.queue = [] + + def executeFunctions(self): + c = self.queue.pop(0) + for f in self.functions: + ctx = f(c) + if ctx.recentField != "none": + self.queue.append(ctx) + self.context = c + self.reportContext() + + def processQueue(self): + # Decline recursion. + if self.isProcessingQueue: + return + self.isProcessingQueue = True + while len(self.queue) > 0: + self.executeFunctions() + self.isProcessingQueue = False + + def registerCallback(self, cb): + self.callbacks.append(cb) + + def registerFunction(self, f): + self.functions.append(f) + + def reportContext(self): + for cb in self.callbacks: + cb(self.context) + + def set(self, fieldName, value): + c = copy.deepcopy(self.context) + setattr(c, fieldName, value) + c.recentField = fieldName + self.queue.append(c) + self.processQueue() diff --git a/v4/main.py b/v4/main.py index fecfcfa..cfb2a06 100644 --- a/v4/main.py +++ b/v4/main.py @@ -1,6 +1,7 @@ from cli import * from cli_test import * from memory_test import * +from Controller import * #from shell import * #import sys @@ -20,14 +21,15 @@ print(cli_test_greetUser()) #print(cli_test_showHelp_h()) #print(cli_test_showHelp_help()) -def printOutput(c): - if c.recentField.startswith("output")): - print(getatter(c, c.recentField)) -ctrl = ContextController() -ctrl.callback(printOutput) +ctrl = Controller(memory_createContext()) +ctrl.registerFunction(cli_greetUser) +ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'")) -ctrl.set("didLaunch", True) +def printOutput(c): + if c.recentField.startswith("output"): + print(getattr(c, c.recentField)) +ctrl.registerCallback(printOutput) # @@ -46,3 +48,5 @@ ctrl.set("didLaunch", True) # if c.exit: # break # print(c.output) + +ctrl.set("didLaunch", True)