bugfix
This commit is contained in:
parent
54393c736e
commit
3877126d03
@ -1,5 +1,7 @@
|
|||||||
from inspect import isfunction, isroutinefunction
|
import asyncio
|
||||||
|
from inspect import isfunction, iscoroutinefunction
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from appPublic.dictObject import DictObject
|
||||||
from appPublic.Singleton import SingletonDecorator
|
from appPublic.Singleton import SingletonDecorator
|
||||||
|
|
||||||
@SingletonDecorator
|
@SingletonDecorator
|
||||||
@ -8,14 +10,45 @@ class RegisterFunction:
|
|||||||
self.registKW = {}
|
self.registKW = {}
|
||||||
|
|
||||||
def register(self,name,func):
|
def register(self,name,func):
|
||||||
if not isfunction(func) and not isroutinefunction(func):
|
if not isfunction(func) and not iscoroutinefunction(func):
|
||||||
error(f'RegisterFunction.register({name}, {func}): func is not a function or routine')
|
error(f'RegisterFunction.register({name}, {func}): func is not a function or routine')
|
||||||
return
|
return
|
||||||
self.registKW[name] = func
|
self.registKW[name] = func
|
||||||
|
|
||||||
def get(self,name):
|
def get(self,name):
|
||||||
return self.registKW.get(name,None)
|
return self.registKW.get(name,None)
|
||||||
|
|
||||||
|
async def exe(name, *args, **kw):
|
||||||
|
f = self.get(name)
|
||||||
|
if f is None:
|
||||||
|
return None
|
||||||
|
if iscoroutinefunction(f):
|
||||||
|
return await f(*args, **kw)
|
||||||
|
return f(*args, **kw)
|
||||||
|
|
||||||
|
@SingletonDecorator
|
||||||
|
class RegisterCoroutine:
|
||||||
|
def __init__(self):
|
||||||
|
self.kw = DictObject()
|
||||||
|
|
||||||
|
def register(self, name, func):
|
||||||
|
if not isfunction(func) and not iscoroutinefunction(func):
|
||||||
|
error(f'RegisterFunction.register({name}, {func}): func is not a function or routine')
|
||||||
|
return
|
||||||
|
if not self.kw.get(name):
|
||||||
|
self.kw[name] = [func]
|
||||||
|
else:
|
||||||
|
self.kw[name].append(func)
|
||||||
|
async def exe(self, name, *args, **kw):
|
||||||
|
fs = self.kw.get(name).copy()
|
||||||
|
fs.reverse()
|
||||||
|
if fs:
|
||||||
|
for f in fs:
|
||||||
|
if iscoroutinefunction(f):
|
||||||
|
await f(*args, **kw)
|
||||||
|
else:
|
||||||
|
f(*args, **kw)
|
||||||
|
return None
|
||||||
|
|
||||||
def getRegisterFunctionByName(name):
|
def getRegisterFunctionByName(name):
|
||||||
rf = RegisterFunction()
|
rf = RegisterFunction()
|
||||||
@ -25,20 +58,26 @@ def registerFunction(name, func):
|
|||||||
rf = RegisterFunction()
|
rf = RegisterFunction()
|
||||||
rf.register(name, func)
|
rf.register(name, func)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
d = {}
|
||||||
|
rf = RegisterCoroutine()
|
||||||
|
rf.register('test', z)
|
||||||
|
rf.register('test', y)
|
||||||
|
rf.register('test', x)
|
||||||
|
nd = await rf.exe('test', d)
|
||||||
|
print(nd)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
def x(a):
|
def x(dic):
|
||||||
print('x():a=',a)
|
dic['a'] = 'a'
|
||||||
|
return dic
|
||||||
|
|
||||||
def y(a):
|
async def y(dic):
|
||||||
print('y():a=',a)
|
dic['b'] = 'b'
|
||||||
|
return dic
|
||||||
|
|
||||||
def z():
|
def z(dic):
|
||||||
rf = RegisterFunction()
|
dic['c'] = 1
|
||||||
for name in ['func1', 'func2' ]:
|
return dic
|
||||||
f = rf.get(name)
|
|
||||||
print(name,f('hahah'))
|
|
||||||
|
|
||||||
rf = RegisterFunction()
|
asyncio.get_event_loop().run_until_complete(main())
|
||||||
rf.register('func1',x)
|
|
||||||
rf.register('func2',y)
|
|
||||||
z()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user