"""숨은 문자를 드러내 보여 주는 작은 웹 뷰어. samples 폴더(make_samples.py 가 만듦)를 읽습니다.
samples 폴더가 있는 곳에서 python viewer_app.py 로 띄우고 http://127.0.0.1:5431 을 엽니다.
"""
import html
from pathlib import Path
from flask import Flask, abort, request
from agent_config_scan import hidden_class, render_html, scan
app = Flask(__name__)
SAMPLES = Path("samples") # 이 폴더에서 실행합니다
REPOS = {"bad": "오염된 저장소", "clean": "정상 저장소"}
STYLE = """"""
def page(title, body):
return f"
{html.escape(title)}{STYLE}{body}"
def files_of(repo):
if repo not in REPOS:
abort(404)
base = SAMPLES / repo
return base, sorted(p for p in base.rglob("*") if p.is_file())
@app.get("/")
def index():
out = ""
for repo, label in REPOS.items():
base, files = files_of(repo)
links = "".join(
f"{p.relative_to(base).as_posix()}"
for p in files)
out += f"{label} ({repo})
스캔 보고서 보기
"
return page("규칙 파일 뷰어", out)
@app.get("/view")
def view():
repo, rel = request.args.get("repo", ""), request.args.get("file", "")
base, files = files_of(repo)
path = next((p for p in files if p.relative_to(base).as_posix() == rel), None)
if path is None:
abort(404)
text = path.read_text(encoding="utf-8")
rows, hidden_total = "", 0
for n, line in enumerate(text.split("\n"), 1):
if not line:
continue
chips = "".join(
f"U+{ord(c):04X}" if hidden_class(c) else html.escape(c) for c in line)
hidden_total += sum(bool(hidden_class(c)) for c in line)
rows += f"| {n} | {html.escape(line)} | {chips} |
"
visible = sum(1 for c in text if not hidden_class(c))
summary = (f"글자 {len(text)}개, 바이트 {len(text.encode('utf-8'))}개. "
f"눈에 보이는 쪽으로 센 글자 {visible}개, 숨은 문자 {hidden_total}개.
")
body = (f"목록 / {REPOS[repo]} / {html.escape(rel)}
{summary}"
"| 줄 | 에디터에서 보이는 모습 | 코드포인트로 본 모습 |
" + rows + "
")
return page("규칙 파일 뷰어", body)
@app.get("/report")
def report():
repo = request.args.get("repo", "")
files_of(repo)
files, findings = scan([SAMPLES / repo])
return render_html(files, findings)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5431)