From b089d4f66d8b219bd34be97fce55d041d35d8db5 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 16 Dec 2020 10:15:43 +0800 Subject: [PATCH] bugfix --- ahserver/baseProcessor.py | 41 +++++++++++++++-------------------- ahserver/dsProcessor.py | 19 +++++++++------- ahserver/functionProcessor.py | 29 ++++++++++++++++++++----- ahserver/processorResource.py | 23 ++++++++++++-------- ahserver/sqldsProcessor.py | 10 ++++----- ahserver/xlsxdsProcessor.py | 6 ++--- 6 files changed, 73 insertions(+), 55 deletions(-) diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index d5233f1..59ce1ee 100644 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -122,19 +122,20 @@ class JSUIProcessor(TemplateProcessor): def isMe(self,name): return name=='jsui' + async def path_call(self, request, path): + ns = self.run_ns + te = self.run_ns['tmpl_engine'] + return te.render(path,**ns) + + async def datahandle(self, request): params = await self.resource.y_env['request2ns']() if params.get('_jsui',None): super().datahandle(request) else: - ns = self.run_ns - te = self.run_ns['tmpl_engine'] - print('handle /header.tmpl') - content0 = te.render('/header.tmpl',**ns) - print('handle', self.path) - content1 = te.render(request.path, **ns) - print('handle footer.tmpl') - content2 = te.render('/footer.tmpl',**ns) + content0 = await self.path_call(request,'/header.tmpl') + content1 = await self.path_call(request,self.path) + content2 = await self.path_call(request,'/footer.tmpl') self.content = '%s%s%s' % (content0,content1,content2) class PythonScriptProcessor(BaseProcessor): @@ -142,9 +143,9 @@ class PythonScriptProcessor(BaseProcessor): def isMe(self,name): return name=='dspy' - def loadScript(self): + def loadScript(self, path): data = '' - with codecs.open(self.path,'rb','utf-8') as f: + with codecs.open(path,'rb','utf-8') as f: data = f.read() b= ''.join(data.split('\r')) lines = b.split('\n') @@ -152,25 +153,17 @@ class PythonScriptProcessor(BaseProcessor): txt = "async def myfunc(request,**ns):\n" + '\n'.join(lines) return txt - async def datahandle(self,request): + async def path_call(self, request, path): g = ServerEnv() lenv = self.run_ns del lenv['request'] - """ - if not g.get('dspy_cache',False): - g.dspy_cache = ObjectCache() - func = g.dspy_cache.get(self.path) - if not func: - txt = self.loadScript() - exec(txt,lenv,lenv) - func = lenv['myfunc'] - print('func=',func) - g.dspy_cache.store(self.path,func) - """ - txt = self.loadScript() + txt = self.loadScript(path) exec(txt,lenv,lenv) func = lenv['myfunc'] - self.content = await func(request,**lenv) + return await func(request,**lenv) + + async def datahandle(self,request): + self.content = await self.path_call(request, self.path) class MarkdownProcessor(BaseProcessor): @classmethod diff --git a/ahserver/dsProcessor.py b/ahserver/dsProcessor.py index 97be525..054833c 100644 --- a/ahserver/dsProcessor.py +++ b/ahserver/dsProcessor.py @@ -21,11 +21,11 @@ class DataSourceProcessor(BaseProcessor): } self.g = ServerEnv() - def getData(self,dict_data,ns,request):pass - def getPagingData(self,dict_data,ns,request):pass - def getArgumentsDesc(self,dict_data,ns,request):pass - def getDataDesc(self,dict_data,ns,request):pass - def getGridlist(self,dict_data,ns,request): + async def getData(self,dict_data,ns,request):pass + async def getPagingData(self,dict_data,ns,request):pass + async def getArgumentsDesc(self,dict_data,ns,request):pass + async def getDataDesc(self,dict_data,ns,request):pass + async def getGridlist(self,dict_data,ns,request): ret = self.getDataDesc(dict_data,ns,request) ffs = [ f for f in ret if f.get('frozen',False) ] fs = [ f for f in ret if not f['frozen'] ] @@ -49,15 +49,18 @@ class DataSourceProcessor(BaseProcessor): } return ret - async def datahandle(self,request): + async def path_call(self, request, path): dict_data = {} config = getConfig() - with codecs.open(self.path,'r',config.website.coding) as f: + with codecs.open(path,'r',config.website.coding) as f: b = f.read() dict_data = json.loads(b) ns = self.run_ns act = ns.get('action','getdata') action = self.actions.get(act) - self.content = action(dict_data,ns,request) + return await action(dict_data,ns,request) + + async def datahandle(self,request): + self.content = await path_call(request, self.path) diff --git a/ahserver/functionProcessor.py b/ahserver/functionProcessor.py index 8218c88..927b76c 100644 --- a/ahserver/functionProcessor.py +++ b/ahserver/functionProcessor.py @@ -1,4 +1,5 @@ +import inspect from appPublic.dictObject import DictObject from appPublic.registerfunction import RegisterFunction from aiohttp import web @@ -14,13 +15,29 @@ class FunctionProcessor(BaseProcessor): self.config_opts = opts BaseProcessor.__init__(self,path,resource) - async def datahandle(self,request): - ns = self.config_opts.copy() - ns.update(self.run_ns) - ns = DictObject(**ns) + async def path_call(self, request, path): + path1 = request.path[len(self.config_opts['leading']):] + print('path1=',path1) + args = [] + if len(path1) > 0: + if path1[0] == '/': + path1 = path1[1:] + args = path1.split('/') + + + print('FunctionProcessor():args=',args) + rfname = self.config_opts['registerfunction'] + ns = DictObject(**self.run_ns) rf = RegisterFunction() - f = rf.get(ns.registerfunction) - x = await f(ns) + 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) if isinstance(x,Response): self.retResponse = x elif isinstance(x,web.FileResponse): diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index c2084c6..dc18761 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -191,7 +191,7 @@ class ProcessorResource(StaticResource,Url2File): self.y_env.request2ns = getArgs self.y_env.resource = self self.y_env.gethost = gethost - self.y_env.url_call = partial(self.url_call,request) + self.y_env.path_call = partial(self.path_call,request) self.user = await auth.get_auth(request) path = request.path config = getConfig() @@ -223,12 +223,6 @@ class ProcessorResource(StaticResource,Url2File): path = path_decode(dp) return await file_download(request, path) - if config.website.startswiths: - for a in config.website.startswiths: - if path.startswith(a.leading): - processor = FunctionProcessor(self.abspath(path),self,a) - return await processor.handle(request) - processor = self.url2processor(request, str(request.url)) if processor: return await processor.handle(request) @@ -241,11 +235,18 @@ class ProcessorResource(StaticResource,Url2File): return await super()._handle(request) def url2processor(self, request, url): + config = getConfig() url = self.entireUrl(request, url) host = '/'.join(str(request.url).split('/')[:3]) - path = url[len(host):] + path = url[len(host):].split('?')[0] + if config.website.startswiths: + for a in config.website.startswiths: + if path.startswith(a.leading): + processor = FunctionProcessor(self.abspath(path),self,a) + return processor + for word, handlername in self.y_processors: - if url.endswith(word): + if path.endswith(word): Klass = getProcessor(handlername) processor = Klass(self.abspath(path),self) return processor @@ -261,6 +262,10 @@ class ProcessorResource(StaticResource,Url2File): p = self.relatedurl(path,url) return '%s%s' % (h, p) + async def path_call(self,request, path, params={}): + processor = self.url2processor(request, path) + return await processor.path_call(request, path) + def url_call(self,request, url,params={}): processor = self.url2processor(request, url) if processor: diff --git a/ahserver/sqldsProcessor.py b/ahserver/sqldsProcessor.py index db2d506..1cf3b77 100644 --- a/ahserver/sqldsProcessor.py +++ b/ahserver/sqldsProcessor.py @@ -35,7 +35,7 @@ class SQLDataSourceProcessor(DataSourceProcessor): desc = dict_data.get('arguments',None) return desc - def getDataDesc(self,dict_data,ns,request): + async def getDataDesc(self,dict_data,ns,request): pool = DBPools() @pool.runSQLResultFields def sql(dbname,NS): @@ -54,22 +54,22 @@ class SQLDataSourceProcessor(DataSourceProcessor): f.close() return rec - def getData(self,dict_data,ns,request): + async def getData(self,dict_data,ns,request): pool = DBPools() @pool.runSQL def sql(dbname,NS): sqldesc = dict_data.get('sqldesc') return sqldesc db = dict_data['sqldesc']['db'] - ret = [ i for i in sql(db,ns) ] + ret = [ i for i in await sql(db,ns) ] return ret - def getPagingData(self,dict_data,ns,request): + async def getPagingData(self,dict_data,ns,request): pool = DBPools() @pool.runSQLPaging def sql(dbname,NS): sqldesc = dict_data.get('sqldesc') return sqldesc db = dict_data['sqldesc']['db'] - ret = sql(db,ns) + ret = await sql(db,ns) return ret diff --git a/ahserver/xlsxdsProcessor.py b/ahserver/xlsxdsProcessor.py index 536aae6..7fb66a6 100644 --- a/ahserver/xlsxdsProcessor.py +++ b/ahserver/xlsxdsProcessor.py @@ -31,19 +31,19 @@ class XLSXDataSourceProcessor(DataSourceProcessor): def getArgumentsDesc(self,dict_data,ns,request): return None - def getDataDesc(self,dict_data,ns,request): + async def getDataDesc(self,dict_data,ns,request): path = dict_data.get('xlsxfile',None) self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data) ret = self.xlsxdata.getBaseFieldsInfo(ns) return ret - def getData(self,dict_data,ns,request): + async def getData(self,dict_data,ns,request): path = dict_data.get('xlsxfile',None) self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data) ret = self.xlsxdata.getData(ns) return ret - def getPagingData(self,dict_data,ns,request): + async def getPagingData(self,dict_data,ns,request): path = dict_data.get('xlsxfile',None) self.xlsxdata = XLSXData(self.g.abspath(ns.absurl(request,path)),dict_data) ret = self.xlsxdata.getPagingData(ns)