Files
Guillaume Meyer (The Opinionated Man)andGitHub 723627716c chore: add Ruff linting and formatting with CI enforcement (#103)
Introduce Ruff (pinned at 0.16.3) as the project linter + formatter and
enforce it in CI:

- requirements-dev.txt: pin ruff==0.16.3 (exact pins, no drift)
- ruff.toml: line-length 100, target py312; rule set E/F/W/I/UP/B/SIM/RUF/PLW/S
  with deliberate ignores (E501 for content strings, S603 for safe_arg
  subprocess calls, S101 asserts in tests) and per-file test ignores
- Makefile: add lint / format / lint-fix targets
- .github/workflows/ci.yml: add lint job (ruff check + format --check)
- .gitignore: whitelist ruff.toml

Also fix every finding the new gate surfaced so CI is green:
- 109+ auto-fixes from ruff --fix (import sorting, simplifications,
  unused vars, re.I aliases, etc.)
- explicit check=False on all subprocess.run calls (PLW1510)
- harden sitemap XML parsing: reject DTD/entity declarations (S314)
- replace hardcoded /tmp paths in tests with tmp_path (S108)
- narrow/annotate intentional bare excepts (S110/S112), bind loop vars
  in closures (B023), raise ... from None (B904), strict= for zip (B905)
- ruff format applied across service/ and tests/

Verified: ruff check + ruff format --check pass; 287 tests pass, 1 skip.
2026-08-16 17:32:40 -07:00

87 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Inspect text for invisible Unicode / space homoglyphs (Layer A) and optional stylometry."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
# Allow running as script from any cwd
sys.path.insert(0, str(Path(__file__).resolve().parent))
from common import emit_json, read_text_input
from score_stylometry import print_human_stylometry_report, score_text_stylometry
from text_unicode import human_report, inspect_text
def main() -> int:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("path", nargs="?", default="-", help="Text file path, or - for stdin")
p.add_argument("--json", action="store_true", help="JSON report")
p.add_argument(
"--aggressive",
action="store_true",
help="Also flag Latin confusable / fullwidth lookalikes",
)
p.add_argument(
"--strip-emoji-glue",
action="store_true",
help="Paranoid: flag all load-bearing invisibles too (emoji glue, script joiners, flag tags, same-script fillers/selectors, orthographic Cf)",
)
p.add_argument(
"--stylometry",
action="store_true",
help="Also run zero-LLM statistical and stylometric AI cadence scoring",
)
p.add_argument(
"--threshold",
type=float,
default=0.65,
help="Score threshold for --stylometry exit code (default: 0.65)",
)
p.add_argument(
"--force-text",
action="store_true",
help="Scan even when the input looks like a binary container",
)
args = p.parse_args()
text = read_text_input(args.path, allow_binary=args.force_text)
if text is None:
return 2
report = inspect_text(
text,
aggressive=args.aggressive,
strip_emoji_glue=args.strip_emoji_glue,
)
stylometry_report = None
if args.stylometry:
input_label = "<stdin>" if args.path == "-" else args.path
stylometry_report = score_text_stylometry(text, path=input_label)
if args.json:
data = report.to_dict()
if stylometry_report:
data["stylometry"] = stylometry_report.to_dict()
emit_json(data)
else:
print(human_report(report))
if stylometry_report:
print("\n" + "=" * 50 + "\n")
print_human_stylometry_report(stylometry_report, explain=True)
exit_code = 0
if report.suspicious_total > 0:
exit_code = 1
if stylometry_report and stylometry_report.score >= args.threshold:
exit_code = 1
return exit_code
if __name__ == "__main__":
raise SystemExit(main())