Михаил Капелько 7 months ago
parent
commit
ee28d89f56
3 changed files with 54 additions and 28 deletions
  1. +0
    -22
      v4/ContextController.py
  2. +44
    -0
      v4/Controller.py
  3. +10
    -6
      v4/main.py

+ 0
- 22
v4/ContextController.py View File

@@ -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)

+ 44
- 0
v4/Controller.py View File

@@ -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()

+ 10
- 6
v4/main.py View File

@@ -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)

Loading…
Cancel
Save