Files
m2dev-client/assets/root/stringcommander.py
d1str4ught 36602f7ad9 py2 -> py3
2026-02-09 00:31:19 +01:00

66 lines
1.5 KiB
Python

import _weakref
class CallBackFunction:
class __noarg_call__:
def __init__(self, cls, obj, func):
self.cls=cls
self.obj=_weakref.proxy(obj)
self.func=_weakref.proxy(func)
def __call__(self, *arg):
return self.func(self.obj)
class __arg_call__:
def __init__(self, cls, obj, func):
self.cls=cls
self.obj=_weakref.proxy(obj)
self.func=_weakref.proxy(func)
def __call__(self, *arg):
return self.func(self.obj, *arg)
def __init__(self, mfunc):
self.argCount=mfunc.__func__.__code__.co_argcount
if self.argCount>1:
self.call=CallBackFunction.__arg_call__(mfunc.__self__.__class__, mfunc.__self__, mfunc.__func__)
else:
self.call=CallBackFunction.__noarg_call__(mfunc.__self__.__class__, mfunc.__self__, mfunc.__func__)
def __call__(self, *arg):
return self.call(*arg)
def GetArgumentCount(self):
return self.argCount
class Analyzer:
def __init__(self):
self.cmdDict={}
def SAFE_RegisterCallBack(self, cmd, callBackFunc):
self.cmdDict[cmd]=CallBackFunction(callBackFunc)
def Run(self, line):
tokens=line.split()
if len(tokens)==0:
return 1
cmd=tokens.pop(0)
try:
callBackFunc=self.cmdDict[cmd]
except KeyError:
return 0
argCount=callBackFunc.GetArgumentCount()-1
if len(tokens)<argCount:
raise RuntimeError("Analyzer.Run(line=%s) - cmd=%s, curArgCount[%d]<needArgCount[%d]" % (line, cmd, len(tokens), argCount))
return 0
tokens=tokens[:argCount]
callBackFunc(*tokens)
return 1