From 816173be58ac16ad5d89e8d09a647dce7083149a Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 28 Nov 2019 15:24:09 +0800 Subject: [PATCH] checkifchange --- ahserver/baseProcessor.py | 22 +++++++++---------- ahserver/dsProcessor.py | 6 +----- ahserver/imgThumb.py | 35 ------------------------------ ahserver/processorResource.py | 40 ++++++++++++++++++++--------------- conf/config.json | 11 +++++++++- 5 files changed, 45 insertions(+), 69 deletions(-) delete mode 100644 ahserver/imgThumb.py diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index 20af0ae..1b77ab7 100644 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -58,7 +58,14 @@ class BaseProcessor: 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) if self.retResponse is not None: return self.retResponse @@ -71,7 +78,7 @@ class BaseProcessor: self.setheaders() return Response(text=self.content,headers=self.headers) - async def datahandle(self,txt,request): + async def datahandle(self,request): print('*******Error*************') self.content='' @@ -85,12 +92,7 @@ class TemplateProcessor(BaseProcessor): async def datahandle(self,request): path = request.path - g = ServerEnv() - ns = DictObject() - ns.update(g) - ns.update(self.resource.y_env) - ns.request = request - ns.ref_real_path = self.path + ns = self.run_ns te = g.tmpl_engine self.content = te.render(path,**ns) #self.content = await te.render_async(path,**ns) @@ -122,9 +124,7 @@ class PythonScriptProcessor(BaseProcessor): async def datahandle(self,request): g = ServerEnv() - lenv = {} - lenv.update(g) - lenv.update(self.resource.y_env) + lenv = self.run_ns if not g.get('dspy_cache',False): g.dspy_cache = ObjectCache() func = g.dspy_cache.get(self.path) diff --git a/ahserver/dsProcessor.py b/ahserver/dsProcessor.py index d2fb9d5..97be525 100755 --- a/ahserver/dsProcessor.py +++ b/ahserver/dsProcessor.py @@ -55,11 +55,7 @@ class DataSourceProcessor(BaseProcessor): with codecs.open(self.path,'r',config.website.coding) as f: b = f.read() dict_data = json.loads(b) - ns = DictObject() - g = ServerEnv() - ns.update(g) - ns.update(self.resource.y_env) - ns.update(self.resource.getGetArgs(request)) + ns = self.run_ns act = ns.get('action','getdata') action = self.actions.get(act) self.content = action(dict_data,ns,request) diff --git a/ahserver/imgThumb.py b/ahserver/imgThumb.py deleted file mode 100644 index 620f8af..0000000 --- a/ahserver/imgThumb.py +++ /dev/null @@ -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) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 5c15060..371eb7d 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -6,11 +6,11 @@ from aiohttp.web_urldispatcher import Optional, _ExpectHandler from aiohttp.web_urldispatcher import Path from aiohttp.web_response import Response, StreamResponse from aiohttp.web_exceptions import ( - HTTPException, - HTTPExpectationFailed, - HTTPForbidden, - HTTPMethodNotAllowed, - HTTPNotFound, + HTTPException, + HTTPExpectationFailed, + HTTPForbidden, + HTTPMethodNotAllowed, + HTTPNotFound, ) from aiohttp.web_fileresponse import FileResponse from aiohttp.web_request import Request @@ -24,6 +24,7 @@ from appPublic.dictObject import DictObject, multiDict2Dict from .baseProcessor import getProcessor from .xlsxdsProcessor import XLSXDataSourceProcessor from .sqldsProcessor import SQLDataSourceProcessor +from .functionProcessor import FunctionProcessor from .serverenv import ServerEnv from .url2file import Url2File from .filestorage import FileStorage @@ -45,18 +46,18 @@ def i18nDICT(request): class ProcessorResource(StaticResource): def __init__(self, prefix: str, directory: PathLike, - *, name: Optional[str]=None, - expect_handler: Optional[_ExpectHandler]=None, - chunk_size: int=256 * 1024, - show_index: bool=False, follow_symlinks: bool=False, - append_version: bool=False)-> None: + *, name: Optional[str]=None, + expect_handler: Optional[_ExpectHandler]=None, + chunk_size: int=256 * 1024, + show_index: bool=False, follow_symlinks: bool=False, + append_version: bool=False)-> None: super().__init__(prefix, directory, - name=name, - expect_handler=expect_handler, - chunk_size=chunk_size, - show_index=show_index, + name=name, + expect_handler=expect_handler, + chunk_size=chunk_size, + show_index=show_index, follow_symlinks=follow_symlinks, - append_version=append_version) + append_version=append_version) gr = self._routes.get('GET') self._routes.update({'POST':gr}) self._routes.update({'PUT':gr}) @@ -181,9 +182,14 @@ class ProcessorResource(StaticResource): raise HTTPNotFound dp = '/'.join(pp) path = path_decode(dp) - print('path=',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: if path.endswith(word): Klass = getProcessor(handlername) diff --git a/conf/config.json b/conf/config.json index e7a389e..7e438eb 100644 --- a/conf/config.json +++ b/conf/config.json @@ -63,7 +63,16 @@ [".tmpl","tmpl"], [".dspy","dspy"], [".md","md"] - ] + ], + "startswith":{ + "/thumb":{ + "registerfunction":"makeThumb", + "options":{ + "width":256, + "keep_ratio":1 + } + } + } }, "langMapping":{ "zh-Hans-CN":"zh-cn",