Михаил Капелько 6 maanden geleden
bovenliggende
commit
ee28d89f56
3 gewijzigde bestanden met toevoegingen van 54 en 28 verwijderingen
  1. +0
    -22
      v4/ContextController.py
  2. +44
    -0
      v4/Controller.py
  3. +10
    -6
      v4/main.py

+ 0
- 22
v4/ContextController.py Bestand weergeven

@@ -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 Bestand weergeven

@@ -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 Bestand weergeven

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

Laden…
Annuleren
Opslaan