Skip to content

Commit c577ff0

Browse files
committed
SimpleChatTC:SimpleProxy: Avoid circular deps wrt Type Checking
also move debug dump helper to its own module also remember to specify the Class name in quotes, similar to refering to a class within a member of th class wrt python type checking.
1 parent ed7fdb3 commit c577ff0

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

tools/server/public_simplechat/local.tools/pdfmagic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
import urllib.parse
55
import urlvalidator as uv
6-
import simpleproxy as root
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from simpleproxy import ProxyHandler
710

811

912
def process_pdf2text(url: str, startPN: int, endPN: int):
@@ -27,7 +30,7 @@ def process_pdf2text(url: str, startPN: int, endPN: int):
2730
return { 'status': 200, 'msg': "Pdf2Text Response follows", 'data': tPdf }
2831

2932

30-
def handle_pdf2text(ph: root.ProxyHandler, pr: urllib.parse.ParseResult):
33+
def handle_pdf2text(ph: 'ProxyHandler', pr: urllib.parse.ParseResult):
3134
"""
3235
Handle requests to pdf2text path, which is used to extract plain text
3336
from the specified pdf file.

tools/server/public_simplechat/local.tools/simpleproxy.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,6 @@ def handle_aum(ph: ProxyHandler, pr: urllib.parse.ParseResult):
173173
ph.end_headers()
174174

175175

176-
def debug_dump(meta: dict, data: dict):
177-
if not gMe['--debug']:
178-
return
179-
timeTag = f"{time.time():0.12f}"
180-
with open(f"/tmp/simpleproxy.{timeTag}.meta", '+w') as f:
181-
for k in meta:
182-
f.write(f"\n\n\n\n{k}:{meta[k]}\n\n\n\n")
183-
with open(f"/tmp/simpleproxy.{timeTag}.data", '+w') as f:
184-
for k in data:
185-
f.write(f"\n\n\n\n{k}:{data[k]}\n\n\n\n")
186-
187-
188176
def load_config():
189177
"""
190178
Allow loading of a json based config file

tools/server/public_simplechat/local.tools/webmagic.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
import urllib.parse
55
import urllib.request
6-
import simpleproxy as root
76
import urlvalidator as uv
87
from dataclasses import dataclass
98
import html.parser
9+
import debug
10+
from typing import TYPE_CHECKING
11+
12+
if TYPE_CHECKING:
13+
from simpleproxy import ProxyHandler
14+
1015

1116

1217
@dataclass(frozen=True)
@@ -21,7 +26,7 @@ class UrlReqResp:
2126
contentData: str = ""
2227

2328

24-
def handle_urlreq(ph: root.ProxyHandler, pr: urllib.parse.ParseResult, tag: str):
29+
def handle_urlreq(ph: 'ProxyHandler', pr: urllib.parse.ParseResult, tag: str):
2530
"""
2631
Common part of the url request handling used by both urlraw and urltext.
2732
@@ -58,13 +63,13 @@ def handle_urlreq(ph: root.ProxyHandler, pr: urllib.parse.ParseResult, tag: str)
5863
contentData = response.read().decode('utf-8')
5964
statusCode = response.status or 200
6065
contentType = response.getheader('Content-Type') or 'text/html'
61-
root.debug_dump({ 'url': req.full_url, 'headers': req.headers, 'ctype': contentType }, { 'cdata': contentData })
66+
debug.dump({ 'url': req.full_url, 'headers': req.headers, 'ctype': contentType }, { 'cdata': contentData })
6267
return UrlReqResp(True, statusCode, "", contentType, contentData)
6368
except Exception as exc:
6469
return UrlReqResp(False, 502, f"WARN:{tag}:Failed:{exc}")
6570

6671

67-
def handle_urlraw(ph: root.ProxyHandler, pr: urllib.parse.ParseResult):
72+
def handle_urlraw(ph: 'ProxyHandler', pr: urllib.parse.ParseResult):
6873
try:
6974
# Get requested url
7075
got = handle_urlreq(ph, pr, "HandleUrlRaw")
@@ -159,7 +164,7 @@ def get_stripped_text(self):
159164
return self.textStripped
160165

161166

162-
def handle_urltext(ph: root.ProxyHandler, pr: urllib.parse.ParseResult):
167+
def handle_urltext(ph: 'ProxyHandler', pr: urllib.parse.ParseResult):
163168
try:
164169
# Get requested url
165170
got = handle_urlreq(ph, pr, "HandleUrlText")
@@ -176,6 +181,6 @@ def handle_urltext(ph: root.ProxyHandler, pr: urllib.parse.ParseResult):
176181
ph.send_header('Access-Control-Allow-Origin', '*')
177182
ph.end_headers()
178183
ph.wfile.write(textHtml.get_stripped_text().encode('utf-8'))
179-
root.debug_dump({ 'RawText': 'yes', 'StrippedText': 'yes' }, { 'RawText': textHtml.text, 'StrippedText': textHtml.get_stripped_text() })
184+
debug.dump({ 'RawText': 'yes', 'StrippedText': 'yes' }, { 'RawText': textHtml.text, 'StrippedText': textHtml.get_stripped_text() })
180185
except Exception as exc:
181186
ph.send_error(502, f"WARN:UrlTextFailed:{exc}")

tools/server/public_simplechat/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ Trap http response errors and inform user the specific error returned by ai serv
575575
Initial go at a pdf2text tool call. For now it allows local pdf files to be read and their text content
576576
extracted and passed to ai model for further processing, as decided by ai and end user.
577577

578+
SimpleProxy
579+
* Convert from a single monolithic file into a collection of modules.
580+
* UrlValidator to cross check scheme and domain of requested urls,
581+
the whitelist inturn picked from config json
582+
578583
#### ToDo
579584

580585
Is the tool call promise land trap deep enough, need to think through and explore around this once later.

0 commit comments

Comments
 (0)