SimpleSallap:SimpleMCP:FileMagic switch to TCOutResponse
This commit is contained in:
parent
4ce55eb0af
commit
cb1d91999d
|
|
@ -4,21 +4,10 @@
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import debug
|
import debug
|
||||||
|
import toolcall as mTC
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class Response:
|
|
||||||
"""
|
|
||||||
Used to return result wrt urlreq helper below.
|
|
||||||
"""
|
|
||||||
callOk: bool
|
|
||||||
statusCode: int
|
|
||||||
statusMsg: str = ""
|
|
||||||
contentType: str = ""
|
|
||||||
contentData: bytes = b""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]):
|
def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]):
|
||||||
"""
|
"""
|
||||||
|
|
@ -52,9 +41,9 @@ def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, st
|
||||||
contentType = response.getheader('Content-Type') or inContentType
|
contentType = response.getheader('Content-Type') or inContentType
|
||||||
print(f"DBUG:FM:GFW:Resp:{response.status}:{response.msg}")
|
print(f"DBUG:FM:GFW:Resp:{response.status}:{response.msg}")
|
||||||
debug.dump({ 'op': 'FileMagic.GetFromWeb', 'url': req.full_url, 'req.headers': req.headers, 'resp.headers': response.headers, 'ctype': contentType }, { 'cdata': contentData })
|
debug.dump({ 'op': 'FileMagic.GetFromWeb', 'url': req.full_url, 'req.headers': req.headers, 'resp.headers': response.headers, 'ctype': contentType }, { 'cdata': contentData })
|
||||||
return Response(True, statusCode, statusMsg, contentType, contentData)
|
return mTC.TCOutResponse(True, statusCode, statusMsg, contentType, contentData)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return Response(False, 502, f"WARN:{tag}:Failed:{exc}")
|
return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")
|
||||||
|
|
||||||
|
|
||||||
def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType: str):
|
def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType: str):
|
||||||
|
|
@ -64,9 +53,9 @@ def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType:
|
||||||
try:
|
try:
|
||||||
fPdf = open(urlParts.path, 'rb')
|
fPdf = open(urlParts.path, 'rb')
|
||||||
dPdf = fPdf.read()
|
dPdf = fPdf.read()
|
||||||
return Response(True, 200, "", inContentType, dPdf)
|
return mTC.TCOutResponse(True, 200, "", inContentType, dPdf)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return Response(False, 502, f"WARN:{tag}:Failed:{exc}")
|
return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")
|
||||||
|
|
||||||
|
|
||||||
def get_file(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]={}):
|
def get_file(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]={}):
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ def process_pdftext(url: str, startPN: int, endPN: int):
|
||||||
return mTC.TCOutResponse(False, gotVU.statusCode, gotVU.statusMsg)
|
return mTC.TCOutResponse(False, gotVU.statusCode, gotVU.statusMsg)
|
||||||
gotFile = mFile.get_file(url, "ProcessPdfText", "application/pdf", {})
|
gotFile = mFile.get_file(url, "ProcessPdfText", "application/pdf", {})
|
||||||
if not gotFile.callOk:
|
if not gotFile.callOk:
|
||||||
return mTC.TCOutResponse(False, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData)
|
return gotFile
|
||||||
tPdf = ""
|
tPdf = ""
|
||||||
oPdf = pypdf.PdfReader(io.BytesIO(gotFile.contentData))
|
oPdf = pypdf.PdfReader(io.BytesIO(gotFile.contentData))
|
||||||
if (startPN <= 0):
|
if (startPN <= 0):
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,7 @@ def handle_urlreq(url: str, inHeaders: http.client.HTTPMessage, tag: str):
|
||||||
'Accept-Language': hAL
|
'Accept-Language': hAL
|
||||||
}
|
}
|
||||||
# Get requested url
|
# Get requested url
|
||||||
gotFile = mFile.get_file(url, tag, "text/html", headers)
|
return mFile.get_file(url, tag, "text/html", headers)
|
||||||
return mTC.TCOutResponse(gotFile.callOk, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData)
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")
|
return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue