gamedata_reader: fix --root filter, sanitize --out filenames, and write JSON atomically (GAMEDATA-READER-FILTER-AND-OUT-IO) #1557

Open
github-bot wants to merge 1 commit from github-bot/gh-1557 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 04:30 UTC · original
IlyassRS3:fix/gamedata-reader-filter-and-out-io → main · +124 −18 · 2 files · 1 commits · open

What

Fix --root filtering, unsafe export filenames, and --out I/O handling in tools/gamedata_reader.py:

  1. Namespaced root tags crashed --out export (_safe_stem, run):
    • ET.iterparse expands XML namespaces to Clark notation ({http://example.com/ns}Root), so f"{tag}_{name}.json" contained / and : and crashed with FileNotFoundError / OSError.
    • Sanitize export stems by replacing path/Windows-illegal filename characters ([<>:"/\\|?*\x00-\x1f]) with _ while leaving ordinary ASCII tag/file stems unchanged.
  2. --root missed valid documents without a matching root_hint (run):
    • Skipping immediately when hint != root_filter dropped documents whose root_hint was empty (e.g. a self-closing root after <!DOCTYPE ...>) or lacked the namespace prefix of a Clark-notation --root filter, and never verified the parsed tag against root_filter.
    • Keep the fast pre-parse skip when hint is non-empty and matches neither root_filter nor its local name (root_filter.endswith("}" + hint)), and verify tag == root_filter after parsing.
  3. Empty/whitespace --out, atomic LF-terminated writes, and accurate write error diagnostics (main, run):
    • Reject empty or whitespace-only --out paths with ERROR: --out path must not be empty (exit 2) instead of silently ignoring "" or creating a whitespace-named folder.
    • Write exported JSON files atomically through sibling temp files (newline="\n", trailing "\n", file_mode.published_mode, replace_retry.replace), and report write OSErrors as ERROR: cannot write ... instead of ERROR: cannot read ....

Claimed in the coordination issue as

GAMEDATA-READER-FILTER-AND-OUT-IO — verified 0 open PRs and 0 prior claims; unable to post a comment on #14 because GitHub disabled commenting once #14 reached its 2,500-comment cap (and no successor "Coordinating who works on what" issue is open yet).

Testing

  • python3 -I -W error tests/test_gamedata_reader.py -v: 25/25 OK (including 3 new regression tests).

  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.

  • python3 -I scripts/check_no_game_data.py tools/gamedata_reader.py tests/test_gamedata_reader.py: clean.

  • python3 -m compileall -q tools/gamedata_reader.py tests/test_gamedata_reader.py: clean.

  • Mutation check against unpatched main:tools/gamedata_reader.py: all 3 new tests fail on main (FAILED (failures=3, errors=2)).

  • AI-assisted: yes

  • GPUs tested: none (CPU-only XML reader tests on macOS; no GPU or game files needed)

**@⁠IlyassRS3** on GitHub · 2026-11-25 04:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1557) `IlyassRS3:fix/gamedata-reader-filter-and-out-io` → `main` · +124 −18 · 2 files · 1 commits · open ## What Fix `--root` filtering, unsafe export filenames, and `--out` I/O handling in `tools/gamedata_reader.py`: 1. **Namespaced root tags crashed `--out` export (`_safe_stem`, `run`)**: - `ET.iterparse` expands XML namespaces to Clark notation (`{http://example.com/ns}Root`), so `f"{tag}_{name}.json"` contained `/` and `:` and crashed with `FileNotFoundError` / `OSError`. - Sanitize export stems by replacing path/Windows-illegal filename characters (`[<>:"/\\|?*\x00-\x1f]`) with `_` while leaving ordinary ASCII tag/file stems unchanged. 2. **`--root` missed valid documents without a matching `root_hint` (`run`)**: - Skipping immediately when `hint != root_filter` dropped documents whose `root_hint` was empty (e.g. a self-closing root after `<!DOCTYPE ...>`) or lacked the namespace prefix of a Clark-notation `--root` filter, and never verified the parsed `tag` against `root_filter`. - Keep the fast pre-parse skip when `hint` is non-empty and matches neither `root_filter` nor its local name (`root_filter.endswith("}" + hint)`), and verify `tag == root_filter` after parsing. 3. **Empty/whitespace `--out`, atomic LF-terminated writes, and accurate write error diagnostics (`main`, `run`)**: - Reject empty or whitespace-only `--out` paths with `ERROR: --out path must not be empty` (exit `2`) instead of silently ignoring `""` or creating a whitespace-named folder. - Write exported JSON files atomically through sibling temp files (`newline="\n"`, trailing `"\n"`, `file_mode.published_mode`, `replace_retry.replace`), and report write `OSError`s as `ERROR: cannot write ...` instead of `ERROR: cannot read ...`. ## Claimed in the coordination issue as `GAMEDATA-READER-FILTER-AND-OUT-IO` — verified 0 open PRs and 0 prior claims; unable to post a comment on `#14` because GitHub disabled commenting once `#14` reached its 2,500-comment cap (and no successor `"Coordinating who works on what"` issue is open yet). ## Testing - `python3 -I -W error tests/test_gamedata_reader.py -v`: **25/25 OK** (including 3 new regression tests). - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**. - `python3 -I scripts/check_no_game_data.py tools/gamedata_reader.py tests/test_gamedata_reader.py`: clean. - `python3 -m compileall -q tools/gamedata_reader.py tests/test_gamedata_reader.py`: clean. - Mutation check against unpatched `main:tools/gamedata_reader.py`: all 3 new tests fail on `main` (`FAILED (failures=3, errors=2)`). - AI-assisted: yes - GPUs tested: none (CPU-only XML reader tests on macOS; no GPU or game files needed)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:56:29 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 04:30 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 04:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1557#pullrequestreview-6051878444) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1557/head:github-bot/gh-1557'
git switch 'github-bot/gh-1557'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1557'
git switch 'github-bot/gh-1557'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1557'
git switch 'github-bot/gh-1557'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1557'
git switch 'main'
git merge --squash 'github-bot/gh-1557'
git switch 'main'
git merge --ff-only 'github-bot/gh-1557'
git switch 'main'
git merge 'github-bot/gh-1557'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.