ahserver_g/ahserver/functionProcessor.py

46 lines
1.1 KiB
Python
Raw Normal View History

2019-11-28 15:28:33 +08:00
2020-12-16 10:15:43 +08:00
import inspect
2019-11-28 15:28:33 +08:00
from appPublic.dictObject import DictObject
from appPublic.registerfunction import RegisterFunction
2019-12-09 19:06:55 +08:00
from aiohttp import web
2019-11-28 15:28:33 +08:00
from aiohttp.web_response import Response, StreamResponse
from .baseProcessor import BaseProcessor
class FunctionProcessor(BaseProcessor):
@classmethod
def isMe(self,name):
return False
def __init__(self,path,resource, opts):
self.config_opts = opts
2019-11-29 11:35:33 +08:00
BaseProcessor.__init__(self,path,resource)
2019-11-28 15:28:33 +08:00
2020-12-16 10:15:43 +08:00
async def path_call(self, request, path):
path1 = request.path[len(self.config_opts['leading']):]
args = []
if len(path1) > 0:
if path1[0] == '/':
path1 = path1[1:]
args = path1.split('/')
rfname = self.config_opts['registerfunction']
ns = DictObject(**self.run_ns)
2019-11-28 15:28:33 +08:00
rf = RegisterFunction()
2020-12-16 10:15:43 +08:00
f = rf.get(rfname)
self.run_ns['request'] = request
globals().update(self.run_ns)
if inspect.iscoroutinefunction(f):
return await f(*args, **self.run_ns)
return f(*args)
async def datahandle(self,request):
x = await self.path_call(request, self.path)
2019-11-28 15:28:33 +08:00
if isinstance(x,Response):
self.retResponse = x
2019-12-09 19:06:55 +08:00
elif isinstance(x,web.FileResponse):
self.retResponse = x
2019-11-28 15:28:33 +08:00
else:
self.content = x