This commit is contained in:
yumoqing 2020-12-16 10:15:43 +08:00
parent 4ce2ef4112
commit b089d4f66d
6 changed files with 73 additions and 55 deletions

View File

@ -122,19 +122,20 @@ class JSUIProcessor(TemplateProcessor):
def isMe(self,name): def isMe(self,name):
return name=='jsui' 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): async def datahandle(self, request):
params = await self.resource.y_env['request2ns']() params = await self.resource.y_env['request2ns']()
if params.get('_jsui',None): if params.get('_jsui',None):
super().datahandle(request) super().datahandle(request)
else: else:
ns = self.run_ns content0 = await self.path_call(request,'/header.tmpl')
te = self.run_ns['tmpl_engine'] content1 = await self.path_call(request,self.path)
print('handle /header.tmpl') content2 = await self.path_call(request,'/footer.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)
self.content = '%s%s%s' % (content0,content1,content2) self.content = '%s%s%s' % (content0,content1,content2)
class PythonScriptProcessor(BaseProcessor): class PythonScriptProcessor(BaseProcessor):
@ -142,9 +143,9 @@ class PythonScriptProcessor(BaseProcessor):
def isMe(self,name): def isMe(self,name):
return name=='dspy' return name=='dspy'
def loadScript(self): def loadScript(self, path):
data = '' data = ''
with codecs.open(self.path,'rb','utf-8') as f: with codecs.open(path,'rb','utf-8') as f:
data = f.read() data = f.read()
b= ''.join(data.split('\r')) b= ''.join(data.split('\r'))
lines = b.split('\n') lines = b.split('\n')
@ -152,25 +153,17 @@ class PythonScriptProcessor(BaseProcessor):
txt = "async def myfunc(request,**ns):\n" + '\n'.join(lines) txt = "async def myfunc(request,**ns):\n" + '\n'.join(lines)
return txt return txt
async def datahandle(self,request): async def path_call(self, request, path):
g = ServerEnv() g = ServerEnv()
lenv = self.run_ns lenv = self.run_ns
del lenv['request'] del lenv['request']
""" txt = self.loadScript(path)
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()
exec(txt,lenv,lenv) exec(txt,lenv,lenv)
func = lenv['myfunc'] 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): class MarkdownProcessor(BaseProcessor):
@classmethod @classmethod

View File

@ -21,11 +21,11 @@ class DataSourceProcessor(BaseProcessor):
} }
self.g = ServerEnv() self.g = ServerEnv()
def getData(self,dict_data,ns,request):pass async def getData(self,dict_data,ns,request):pass
def getPagingData(self,dict_data,ns,request):pass async def getPagingData(self,dict_data,ns,request):pass
def getArgumentsDesc(self,dict_data,ns,request):pass async def getArgumentsDesc(self,dict_data,ns,request):pass
def getDataDesc(self,dict_data,ns,request):pass async def getDataDesc(self,dict_data,ns,request):pass
def getGridlist(self,dict_data,ns,request): async def getGridlist(self,dict_data,ns,request):
ret = self.getDataDesc(dict_data,ns,request) ret = self.getDataDesc(dict_data,ns,request)
ffs = [ f for f in ret if f.get('frozen',False) ] ffs = [ f for f in ret if f.get('frozen',False) ]
fs = [ f for f in ret if not f['frozen'] ] fs = [ f for f in ret if not f['frozen'] ]
@ -49,15 +49,18 @@ class DataSourceProcessor(BaseProcessor):
} }
return ret return ret
async def datahandle(self,request): async def path_call(self, request, path):
dict_data = {} dict_data = {}
config = getConfig() 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() b = f.read()
dict_data = json.loads(b) dict_data = json.loads(b)
ns = self.run_ns ns = self.run_ns
act = ns.get('action','getdata') act = ns.get('action','getdata')
action = self.actions.get(act) 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)

View File

@ -1,4 +1,5 @@
import inspect
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.registerfunction import RegisterFunction from appPublic.registerfunction import RegisterFunction
from aiohttp import web from aiohttp import web
@ -14,13 +15,29 @@ class FunctionProcessor(BaseProcessor):
self.config_opts = opts self.config_opts = opts
BaseProcessor.__init__(self,path,resource) BaseProcessor.__init__(self,path,resource)
async def datahandle(self,request): async def path_call(self, request, path):
ns = self.config_opts.copy() path1 = request.path[len(self.config_opts['leading']):]
ns.update(self.run_ns) print('path1=',path1)
ns = DictObject(**ns) 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() rf = RegisterFunction()
f = rf.get(ns.registerfunction) f = rf.get(rfname)
x = await f(ns) 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): if isinstance(x,Response):
self.retResponse = x self.retResponse = x
elif isinstance(x,web.FileResponse): elif isinstance(x,web.FileResponse):

View File

@ -191,7 +191,7 @@ class ProcessorResource(StaticResource,Url2File):
self.y_env.request2ns = getArgs self.y_env.request2ns = getArgs
self.y_env.resource = self self.y_env.resource = self
self.y_env.gethost = gethost 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) self.user = await auth.get_auth(request)
path = request.path path = request.path
config = getConfig() config = getConfig()
@ -223,12 +223,6 @@ class ProcessorResource(StaticResource,Url2File):
path = path_decode(dp) path = path_decode(dp)
return await file_download(request, path) 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)) processor = self.url2processor(request, str(request.url))
if processor: if processor:
return await processor.handle(request) return await processor.handle(request)
@ -241,11 +235,18 @@ class ProcessorResource(StaticResource,Url2File):
return await super()._handle(request) return await super()._handle(request)
def url2processor(self, request, url): def url2processor(self, request, url):
config = getConfig()
url = self.entireUrl(request, url) url = self.entireUrl(request, url)
host = '/'.join(str(request.url).split('/')[:3]) 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: for word, handlername in self.y_processors:
if url.endswith(word): if path.endswith(word):
Klass = getProcessor(handlername) Klass = getProcessor(handlername)
processor = Klass(self.abspath(path),self) processor = Klass(self.abspath(path),self)
return processor return processor
@ -261,6 +262,10 @@ class ProcessorResource(StaticResource,Url2File):
p = self.relatedurl(path,url) p = self.relatedurl(path,url)
return '%s%s' % (h, p) 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={}): def url_call(self,request, url,params={}):
processor = self.url2processor(request, url) processor = self.url2processor(request, url)
if processor: if processor:

View File

@ -35,7 +35,7 @@ class SQLDataSourceProcessor(DataSourceProcessor):
desc = dict_data.get('arguments',None) desc = dict_data.get('arguments',None)
return desc return desc
def getDataDesc(self,dict_data,ns,request): async def getDataDesc(self,dict_data,ns,request):
pool = DBPools() pool = DBPools()
@pool.runSQLResultFields @pool.runSQLResultFields
def sql(dbname,NS): def sql(dbname,NS):
@ -54,22 +54,22 @@ class SQLDataSourceProcessor(DataSourceProcessor):
f.close() f.close()
return rec return rec
def getData(self,dict_data,ns,request): async def getData(self,dict_data,ns,request):
pool = DBPools() pool = DBPools()
@pool.runSQL @pool.runSQL
def sql(dbname,NS): def sql(dbname,NS):
sqldesc = dict_data.get('sqldesc') sqldesc = dict_data.get('sqldesc')
return sqldesc return sqldesc
db = dict_data['sqldesc']['db'] db = dict_data['sqldesc']['db']
ret = [ i for i in sql(db,ns) ] ret = [ i for i in await sql(db,ns) ]
return ret return ret
def getPagingData(self,dict_data,ns,request): async def getPagingData(self,dict_data,ns,request):
pool = DBPools() pool = DBPools()
@pool.runSQLPaging @pool.runSQLPaging
def sql(dbname,NS): def sql(dbname,NS):
sqldesc = dict_data.get('sqldesc') sqldesc = dict_data.get('sqldesc')
return sqldesc return sqldesc
db = dict_data['sqldesc']['db'] db = dict_data['sqldesc']['db']
ret = sql(db,ns) ret = await sql(db,ns)
return ret return ret

View File

@ -31,19 +31,19 @@ class XLSXDataSourceProcessor(DataSourceProcessor):
def getArgumentsDesc(self,dict_data,ns,request): def getArgumentsDesc(self,dict_data,ns,request):
return None return None
def getDataDesc(self,dict_data,ns,request): async def getDataDesc(self,dict_data,ns,request):
path = dict_data.get('xlsxfile',None) path = dict_data.get('xlsxfile',None)
self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data) self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data)
ret = self.xlsxdata.getBaseFieldsInfo(ns) ret = self.xlsxdata.getBaseFieldsInfo(ns)
return ret return ret
def getData(self,dict_data,ns,request): async def getData(self,dict_data,ns,request):
path = dict_data.get('xlsxfile',None) path = dict_data.get('xlsxfile',None)
self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data) self.xlsxdata = XLSXData(self.g.abspath(self.g.absurl(request,path)),dict_data)
ret = self.xlsxdata.getData(ns) ret = self.xlsxdata.getData(ns)
return ret return ret
def getPagingData(self,dict_data,ns,request): async def getPagingData(self,dict_data,ns,request):
path = dict_data.get('xlsxfile',None) path = dict_data.get('xlsxfile',None)
self.xlsxdata = XLSXData(self.g.abspath(ns.absurl(request,path)),dict_data) self.xlsxdata = XLSXData(self.g.abspath(ns.absurl(request,path)),dict_data)
ret = self.xlsxdata.getPagingData(ns) ret = self.xlsxdata.getPagingData(ns)