This commit is contained in:
yumoqing 2023-10-12 15:46:05 +08:00
parent 7ec7e69f4e
commit 2d7c413ed9
5 changed files with 24 additions and 20 deletions

View File

@ -2,6 +2,7 @@ import os
import re import re
import json import json
import codecs import codecs
import aiofiles
from aiohttp.web_request import Request from aiohttp.web_request import Request
from aiohttp.web_response import Response, StreamResponse from aiohttp.web_response import Response, StreamResponse
@ -168,10 +169,10 @@ class PythonScriptProcessor(BaseProcessor):
def isMe(self,name): def isMe(self,name):
return name=='dspy' return name=='dspy'
def loadScript(self, path): async def loadScript(self, path):
data = '' data = ''
with codecs.open(path,'rb','utf-8') as f: async with aiofiles.open(path,'r', encoding='utf-8') as f:
data = f.read() data = await f.read()
b= ''.join(data.split('\r')) b= ''.join(data.split('\r'))
lines = b.split('\n') lines = b.split('\n')
lines = ['\t' + l for l in lines ] lines = ['\t' + l for l in lines ]
@ -182,7 +183,7 @@ class PythonScriptProcessor(BaseProcessor):
await self.set_run_env(request, params=params) await self.set_run_env(request, params=params)
lenv = self.run_ns lenv = self.run_ns
del lenv['request'] del lenv['request']
txt = self.loadScript(self.real_path) txt = await self.loadScript(self.real_path)
# print(self.real_path, "#########", txt) # print(self.real_path, "#########", txt)
exec(txt,lenv,lenv) exec(txt,lenv,lenv)
func = lenv['myfunc'] func = lenv['myfunc']
@ -198,8 +199,8 @@ class MarkdownProcessor(BaseProcessor):
async def datahandle(self,request:Request): async def datahandle(self,request:Request):
data = '' data = ''
with codecs.open(self.real_path,'rb','utf-8') as f: async with aiofiles.open(self.real_path,'r',encoding='utf-8') as f:
data = f.read() data = await f.read()
self.content = self.urlreplace(data, request) self.content = self.urlreplace(data, request)
def urlreplace(self,mdtxt,request): def urlreplace(self,mdtxt,request):

View File

@ -1,5 +1,6 @@
import codecs import codecs
import json import json
import aiofiles
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from .baseProcessor import BaseProcessor from .baseProcessor import BaseProcessor
@ -52,8 +53,8 @@ class DataSourceProcessor(BaseProcessor):
async def path_call(self, request, path): async def path_call(self, request, path):
dict_data = {} dict_data = {}
config = getConfig() config = getConfig()
with codecs.open(path,'r',config.website.coding) as f: async with aiofiles.open(path,'r',encoding=config.website.coding) as f:
b = f.read() b = await 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')

View File

@ -1,6 +1,7 @@
import os import os
import re import re
import codecs import codecs
import aiofiles
from traceback import print_exc from traceback import print_exc
# from showcallstack import showcallstack # from showcallstack import showcallstack
@ -303,7 +304,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
if processor: if processor:
return await processor.handle(request) return await processor.handle(request)
if self.request_filename and self.isHtml(self.request_filename): if self.request_filename and await self.isHtml(self.request_filename):
return await self.html_handle(request, self.request_filename) return await self.html_handle(request, self.request_filename)
if self.request_filename and os.path.isdir(self.request_filename): if self.request_filename and os.path.isdir(self.request_filename):
@ -324,10 +325,8 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
return '/'.join(str(request.url).split('/')[:3]) return '/'.join(str(request.url).split('/')[:3])
async def html_handle(self,request,filepath): async def html_handle(self,request,filepath):
with open(filepath,'rb') as f: async with aiofiles.open(filepath,'r', encoding='utf-8') as f:
b = f.read() txt = await f.read()
utxt = b.decode('unicode_escape')
txt = b.decode('utf-8')
headers = { headers = {
'Content-Type': 'text/html; utf-8', 'Content-Type': 'text/html; utf-8',
'Accept-Ranges': 'bytes', 'Accept-Ranges': 'bytes',
@ -336,10 +335,10 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
resp = Response(text=txt,headers=headers) resp = Response(text=txt,headers=headers)
return resp return resp
def isHtml(self,fn): async def isHtml(self,fn):
try: try:
with codecs.open(fn,'r','utf-8') as f: async with aiofiles.open(fn,'r',encoding='utf-8') as f:
b = f.read() b = await f.read()
while b[0] in ['\n',' ','\t']: while b[0] in ['\n',' ','\t']:
b = b[1:] b = b[1:]
if b.lower().startswith('<html>'): if b.lower().startswith('<html>'):

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
import aiohttp import aiohttp
import aiofiles
import json import json
import codecs import codecs
from aiohttp import web from aiohttp import web
@ -43,14 +44,15 @@ class XtermProcessor(BaseProcessor):
self.retResponse = ws self.retResponse = ws
return ws return ws
def get_login_info(self): async def get_login_info(self):
with codecs.open(self.real_path, 'r', 'utf-8') as f: async with aiofiles.open(self.real_path, 'r', encoding='utf-8') as f:
self.login_info = json.load(f) txt = await f.read()
self.login_info = json.loads(txt)
print(f'{self.login_info=}') print(f'{self.login_info=}')
async def create_process(self): async def create_process(self):
# id = lenv['params_kw'].get('termid') # id = lenv['params_kw'].get('termid')
self.get_login_info() await self.get_login_info()
host = self.login_info['host'] host = self.login_info['host']
port = self.login_info.get('port', 22) port = self.login_info.get('port', 22)
username = self.login_info.get('username', 'root') username = self.login_info.get('username', 'root')

View File

@ -5,6 +5,7 @@ cchardet
aiohttp aiohttp
aiohttp_session aiohttp_session
aiohttp_auth_autz aiohttp_auth_autz
aiohttp-cors
aiomysql aiomysql
aioredis aioredis
psycopg2-binary psycopg2-binary