This commit is contained in:
yumoqing 2020-12-18 14:26:16 +08:00
commit eb1971c398
6 changed files with 73 additions and 52 deletions

View File

@ -119,16 +119,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']
content0 = te.render('/header.tmpl',**ns)
content1 = te.render(self.path, **ns)
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):
@ -136,9 +140,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')
@ -146,25 +150,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

View File

@ -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)

View File

@ -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):

View File

@ -194,7 +194,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()
@ -226,12 +226,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)
@ -274,11 +268,18 @@ class ProcessorResource(StaticResource,Url2File):
return False
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
@ -294,6 +295,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:

View File

@ -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

View File

@ -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)