This commit is contained in:
yumoqing 2020-12-20 11:43:00 +08:00
parent 072cad2343
commit 92d0cd5b1c
4 changed files with 48 additions and 62 deletions

View File

@ -47,6 +47,7 @@ class BaseProcessor:
def __init__(self,path,resource): def __init__(self,path,resource):
self.path = path self.path = path
print('self.path=',self.path)
self.resource = resource self.resource = resource
self.retResponse = None self.retResponse = None
# self.last_modified = os.path.getmtime(path) # self.last_modified = os.path.getmtime(path)
@ -58,6 +59,7 @@ class BaseProcessor:
self.content = '' self.content = ''
async def set_run_env(self, request): async def set_run_env(self, request):
self.real_path = self.resource.url2file(self.resource.entireUrl(request, self.path))
g = ServerEnv() g = ServerEnv()
self.run_ns = {} self.run_ns = {}
self.run_ns.update(g) self.run_ns.update(g)
@ -101,11 +103,17 @@ class TemplateProcessor(BaseProcessor):
def isMe(self,name): def isMe(self,name):
return name=='tmpl' return name=='tmpl'
async def datahandle(self,request): async def path_call(self, request):
path = request.path await self.set_run_env(request)
path = self.path
url = self.resource.entireUrl(request, path)
ns = self.run_ns ns = self.run_ns
te = self.run_ns['tmpl_engine'] te = self.run_ns['tmpl_engine']
self.content = te.render(path,**ns) print('****url=',url,'*****')
return te.render(url,**ns)
async def datahandle(self,request):
self.content = await self.path_call(request)
def setheaders(self): def setheaders(self):
super(TemplateProcessor,self).setheaders() super(TemplateProcessor,self).setheaders()
@ -121,20 +129,14 @@ 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:
content0 = await self.path_call(request,'/header.tmpl') content0 = await self.resource.path_call(request,'/header.tmpl')
content1 = await self.path_call(request,self.path) content1 = await self.resource.path_call(request,self.path)
content2 = await self.path_call(request,'/footer.tmpl') content2 = await self.resource.path_call(request,'/footer.tmpl')
self.content = '%s%s%s' % (content0,content1,content2) self.content = '%s%s%s' % (content0,content1,content2)
class PythonScriptProcessor(BaseProcessor): class PythonScriptProcessor(BaseProcessor):
@ -144,6 +146,7 @@ class PythonScriptProcessor(BaseProcessor):
def loadScript(self, path): def loadScript(self, path):
data = '' data = ''
print('path=',path)
with codecs.open(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'))
@ -152,17 +155,18 @@ 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 path_call(self, request, path): async def path_call(self, request):
await self.set_run_env(request) await self.set_run_env(request)
lenv = self.run_ns lenv = self.run_ns
del lenv['request'] del lenv['request']
txt = self.loadScript(path) txt = self.loadScript(self.real_path)
exec(txt,lenv,lenv) exec(txt,lenv,lenv)
func = lenv['myfunc'] func = lenv['myfunc']
return await func(request,**lenv) return await func(request,**lenv)
async def datahandle(self,request): async def datahandle(self,request):
self.content = await self.path_call(request, self.path) print('self.real_path=',self.real_path)
self.content = await self.path_call(request)
class MarkdownProcessor(BaseProcessor): class MarkdownProcessor(BaseProcessor):
@classmethod @classmethod
@ -171,7 +175,7 @@ class MarkdownProcessor(BaseProcessor):
async def datahandle(self,request:Request): async def datahandle(self,request:Request):
data = '' data = ''
with codecs.open(self.path,'rb','utf-8') as f: with codecs.open(self.real_path,'rb','utf-8') as f:
data = f.read() data = f.read()
b = data b = data
b = self.urlreplace(b,request) b = self.urlreplace(b,request)

View File

@ -16,6 +16,7 @@ class TmplLoader(BaseLoader, TmplUrl2File):
def get_source(self,env: Environment,template: str): def get_source(self,env: Environment,template: str):
config = getConfig() config = getConfig()
coding = config.website.coding coding = config.website.coding
fp = self.url2file(template) fp = self.url2file(template)
if not os.path.isfile(fp): if not os.path.isfile(fp):
raise TemplateNotFound(template) raise TemplateNotFound(template)

View File

@ -93,13 +93,10 @@ class ProcessorResource(StaticResource,Url2File):
def setIndexes(self, indexes): def setIndexes(self, indexes):
self.y_indexes = indexes self.y_indexes = indexes
def abspath(self,path:str): def abspath(self, request, path:str):
path = path[len(self.y_prefix):] url = self.entireUrl(request, path)
if len(path)>0 and path[0] == '/': print('url=',url, 'path=',path)
path = path[1:] return self.url2file(url)
rp = os.path.join(self.y_directory , path)
real_path = os.path.abspath(rp)
return real_path
async def getPostData(self,request: Request) -> dict: async def getPostData(self,request: Request) -> dict:
reader = await request.multipart() reader = await request.multipart()
@ -236,7 +233,7 @@ class ProcessorResource(StaticResource,Url2File):
return await self.html_handle(request, filepath) return await self.html_handle(request, filepath)
print(f'path={path} handler by StaticResource..') print(f'path={path} handler by StaticResource..')
if self.isFolder(path): if os.path.isdir(filepath):
config = getConfig() config = getConfig()
if not config.website.allowListFolder: if not config.website.allowListFolder:
raise HTTPNotFound raise HTTPNotFound
@ -272,16 +269,18 @@ class ProcessorResource(StaticResource,Url2File):
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):].split('?')[0] path = url[len(host):].split('?')[0]
real_path = self.abspath(request, path)
print('real_path=', real_path)
if config.website.startswiths: if config.website.startswiths:
for a in config.website.startswiths: for a in config.website.startswiths:
if path.startswith(a.leading): if path.startswith(a.leading):
processor = FunctionProcessor(self.abspath(path),self,a) processor = FunctionProcessor(path,self,a)
return processor return processor
for word, handlername in self.y_processors: for word, handlername in self.y_processors:
if path.endswith(word): if path.endswith(word):
Klass = getProcessor(handlername) Klass = getProcessor(handlername)
processor = Klass(self.abspath(path),self) processor = Klass(path,self)
return processor return processor
return None return None
@ -296,10 +295,9 @@ class ProcessorResource(StaticResource,Url2File):
return '%s%s' % (h, p) return '%s%s' % (h, p)
async def path_call(self,request, path, params={}): async def path_call(self,request, path, params={}):
processor = self.url2processor(request, path) url = self.entireUrl(request, path)
real_path = self.url2file(path) processor = self.url2processor(request, url)
print('processorResource.py:real_path=',real_path) return await processor.path_call(request)
return await processor.path_call(request, real_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)
@ -321,4 +319,5 @@ class ProcessorResource(StaticResource,Url2File):
if url.startswith('https://') or url.startswith('http://') : if url.startswith('https://') or url.startswith('http://') :
return url return url
path = request.path path = request.path
return self.relatedurl(path,url) new_path = self.relatedurl(path,url)
return self.entireUrl(request, new_path)

View File

@ -21,62 +21,44 @@ class Url2File:
break break
return '/'.join(items) return '/'.join(items)
def isFolder(self,url: str) ->bool: def url2file(self, url: str):
if url.startswith(self.starts):
rp = self.path + url[len(self.starts):]
real_path = os.path.abspath(rp)
if os.path.isdir(real_path):
return True
return False
def isFile(self,url:str) ->bool:
if url.startswith(self.starts):
rp = self.path + url[len(self.starts):]
real_path = os.path.abspath(rp)
if os.path.isfile(real_path):
return True
return False
def defaultIndex(self,url: str) -> str:
for p in self.indexes:
rp = url + '/' + p
r = self.url2file(rp)
if r is not None:
return r
return None
def url2file(self,url: str):
if url[-1] == '/': if url[-1] == '/':
url = url[:-1] url = url[:-1]
paths = url.split('/')
paths = url.split('/')[3:] if url.startswith('http://') or url.startswith('https://'):
paths = paths[3:]
f = os.path.join(self.path,*paths) f = os.path.join(self.path,*paths)
real_path = os.path.abspath(f) real_path = os.path.abspath(f)
print('0 - url2file():real_path=', real_path,'url=',url)
if os.path.isdir(real_path): if os.path.isdir(real_path):
for idx in self.indexes: for idx in self.indexes:
p = os.path.join(real_path,idx) p = os.path.join(real_path,idx)
if os.path.isfile(p): if os.path.isfile(p):
print('find it:',p, 'url=', url)
return p return p
if os.path.isfile(real_path): if os.path.isfile(real_path):
print('find it here:',real_path, 'url=', url)
return real_path return real_path
if not self.inherit: if not self.inherit:
print('1-real_path=', real_path, ' not exists')
return None return None
items = url.split('/') items = url.split('/')
if len(items) > 2: if len(items) > 2:
del items[-2] del items[-2]
url = '/'.join(items) url = '/'.join(items)
return self.url2file(url) return self.url2file(url)
print('2-real_path=', real_path, ' not exists')
return None return None
def relatedurl(self,url: str, name: str) -> str: def relatedurl(self,url: str, name: str) -> str:
if url[-1] == '/': if url[-1] == '/':
url = url[:-1] url = url[:-1]
if not self.isFolder(url): fp = self.url2file(url)
if os.path.isfile(fp):
items = url.split('/') items = url.split('/')
del items[-1] del items[-1]
url = '/'.join(items) url = '/'.join(items)
@ -88,7 +70,7 @@ class Url2File:
return self.url2file(url) return self.url2file(url)
class TmplUrl2File(Url2File): class TmplUrl2File(Url2File):
def __init__(self,paths,indexes, subffixes=['.tmpl'],inherit=False): def __init__(self,paths,indexes, subffixes=['.tmpl','.ui' ],inherit=False):
self.paths = paths self.paths = paths
self.u2fs = [ Url2File(p,prefix,indexes,inherit=inherit) \ self.u2fs = [ Url2File(p,prefix,indexes,inherit=inherit) \
for p,prefix in paths ] for p,prefix in paths ]