checkifchange

This commit is contained in:
yumoqing 2019-11-28 15:24:09 +08:00
parent df7e6351f9
commit 816173be58
5 changed files with 45 additions and 69 deletions

View File

@ -58,7 +58,14 @@ class BaseProcessor:
async def handle(self,request): async def handle(self,request):
config = getConfig() g = ServerEnv()
self.run_ns = DictObject()
self.run_ns.update(g)
self.run_ns.update(self.resource.y_env)
self.run_ns.request = request
self.run_ns.ref_real_path = self.path
ns = self.resource.y_env.request2ns()
self.run_ns.update(ns)
await self.datahandle(request) await self.datahandle(request)
if self.retResponse is not None: if self.retResponse is not None:
return self.retResponse return self.retResponse
@ -71,7 +78,7 @@ class BaseProcessor:
self.setheaders() self.setheaders()
return Response(text=self.content,headers=self.headers) return Response(text=self.content,headers=self.headers)
async def datahandle(self,txt,request): async def datahandle(self,request):
print('*******Error*************') print('*******Error*************')
self.content='' self.content=''
@ -85,12 +92,7 @@ class TemplateProcessor(BaseProcessor):
async def datahandle(self,request): async def datahandle(self,request):
path = request.path path = request.path
g = ServerEnv() ns = self.run_ns
ns = DictObject()
ns.update(g)
ns.update(self.resource.y_env)
ns.request = request
ns.ref_real_path = self.path
te = g.tmpl_engine te = g.tmpl_engine
self.content = te.render(path,**ns) self.content = te.render(path,**ns)
#self.content = await te.render_async(path,**ns) #self.content = await te.render_async(path,**ns)
@ -122,9 +124,7 @@ class PythonScriptProcessor(BaseProcessor):
async def datahandle(self,request): async def datahandle(self,request):
g = ServerEnv() g = ServerEnv()
lenv = {} lenv = self.run_ns
lenv.update(g)
lenv.update(self.resource.y_env)
if not g.get('dspy_cache',False): if not g.get('dspy_cache',False):
g.dspy_cache = ObjectCache() g.dspy_cache = ObjectCache()
func = g.dspy_cache.get(self.path) func = g.dspy_cache.get(self.path)

View File

@ -55,11 +55,7 @@ class DataSourceProcessor(BaseProcessor):
with codecs.open(self.path,'r',config.website.coding) as f: with codecs.open(self.path,'r',config.website.coding) as f:
b = f.read() b = f.read()
dict_data = json.loads(b) dict_data = json.loads(b)
ns = DictObject() ns = self.run_ns
g = ServerEnv()
ns.update(g)
ns.update(self.resource.y_env)
ns.update(self.resource.getGetArgs(request))
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) self.content = action(dict_data,ns,request)

View File

@ -1,35 +0,0 @@
from PIL import Image
from io import BytesIO
def imageThumb(imgfilepath,width=None,height=None):
im = Image.open(imgfilepath)
mode = im.mode
if mode not in ('L', 'RGB'):
if mode == 'RGBA':
alpha = im.split()[3]
bgmask = alpha.point(lambda x: 255-x)
im = im.convert('RGB')
# paste(color, box, mask)
im.paste((255,255,255), None, bgmask)
else:
im = im.convert('RGB')
w, h = im.size
if not width and not height:
width = 256
if width:
height = int(float(width) * float(h) / float(w))
else:
width = int(float(height) * float(w) / float(h))
thumb = im.resize((width,height),Image.ANTIALIAS)
f = BytesIO()
thumb.save(f,format='jpeg',quality=60)
im.close()
v = f.getvalue()
with open('thumb.jpg','wb') as x:
x.write(v)
return v
if __name__ == '__main__':
imageThumb("/home/ymq/media/pictures/2019-08/IMG_20190804_113014.jpg", width=256)

View File

@ -6,11 +6,11 @@ from aiohttp.web_urldispatcher import Optional, _ExpectHandler
from aiohttp.web_urldispatcher import Path from aiohttp.web_urldispatcher import Path
from aiohttp.web_response import Response, StreamResponse from aiohttp.web_response import Response, StreamResponse
from aiohttp.web_exceptions import ( from aiohttp.web_exceptions import (
HTTPException, HTTPException,
HTTPExpectationFailed, HTTPExpectationFailed,
HTTPForbidden, HTTPForbidden,
HTTPMethodNotAllowed, HTTPMethodNotAllowed,
HTTPNotFound, HTTPNotFound,
) )
from aiohttp.web_fileresponse import FileResponse from aiohttp.web_fileresponse import FileResponse
from aiohttp.web_request import Request from aiohttp.web_request import Request
@ -24,6 +24,7 @@ from appPublic.dictObject import DictObject, multiDict2Dict
from .baseProcessor import getProcessor from .baseProcessor import getProcessor
from .xlsxdsProcessor import XLSXDataSourceProcessor from .xlsxdsProcessor import XLSXDataSourceProcessor
from .sqldsProcessor import SQLDataSourceProcessor from .sqldsProcessor import SQLDataSourceProcessor
from .functionProcessor import FunctionProcessor
from .serverenv import ServerEnv from .serverenv import ServerEnv
from .url2file import Url2File from .url2file import Url2File
from .filestorage import FileStorage from .filestorage import FileStorage
@ -45,18 +46,18 @@ def i18nDICT(request):
class ProcessorResource(StaticResource): class ProcessorResource(StaticResource):
def __init__(self, prefix: str, directory: PathLike, def __init__(self, prefix: str, directory: PathLike,
*, name: Optional[str]=None, *, name: Optional[str]=None,
expect_handler: Optional[_ExpectHandler]=None, expect_handler: Optional[_ExpectHandler]=None,
chunk_size: int=256 * 1024, chunk_size: int=256 * 1024,
show_index: bool=False, follow_symlinks: bool=False, show_index: bool=False, follow_symlinks: bool=False,
append_version: bool=False)-> None: append_version: bool=False)-> None:
super().__init__(prefix, directory, super().__init__(prefix, directory,
name=name, name=name,
expect_handler=expect_handler, expect_handler=expect_handler,
chunk_size=chunk_size, chunk_size=chunk_size,
show_index=show_index, show_index=show_index,
follow_symlinks=follow_symlinks, follow_symlinks=follow_symlinks,
append_version=append_version) append_version=append_version)
gr = self._routes.get('GET') gr = self._routes.get('GET')
self._routes.update({'POST':gr}) self._routes.update({'POST':gr})
self._routes.update({'PUT':gr}) self._routes.update({'PUT':gr})
@ -181,9 +182,14 @@ class ProcessorResource(StaticResource):
raise HTTPNotFound raise HTTPNotFound
dp = '/'.join(pp) dp = '/'.join(pp)
path = path_decode(dp) path = path_decode(dp)
print('path=',path)
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)
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)

View File

@ -63,7 +63,16 @@
[".tmpl","tmpl"], [".tmpl","tmpl"],
[".dspy","dspy"], [".dspy","dspy"],
[".md","md"] [".md","md"]
] ],
"startswith":{
"/thumb":{
"registerfunction":"makeThumb",
"options":{
"width":256,
"keep_ratio":1
}
}
}
}, },
"langMapping":{ "langMapping":{
"zh-Hans-CN":"zh-cn", "zh-Hans-CN":"zh-cn",