|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Text conversion helper that hides statOrder churn in diffs.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import io |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from typing import Final |
| 11 | + |
| 12 | +_RE_TABLE: Final = re.compile(r"(statOrder\s*=\s*\{)([^}]*)(\})", re.MULTILINE) |
| 13 | +_RE_SCALAR: Final = re.compile(r"(statOrder\s*=\s*)(?!\{)([^,\s}]+)", re.MULTILINE) |
| 14 | +_RE_BRACKET_KEY: Final = re.compile(r'(\["statOrder"\]\s*=\s*)([^,\s}]+)', re.MULTILINE) |
| 15 | +_RE_NOTABLE_ENTRY: Final = re.compile(r'(\["[^"]+"\]\s*=\s*)(\d+)', re.MULTILINE) |
| 16 | +_RE_STATDESC_BLOCK: Final = re.compile(r"(?m)(^\t)\[(\d+)\](\s*=\s*\{)") |
| 17 | +_RE_STATDESC_TAIL: Final = re.compile(r'(\["[^"]+"\]\s*=\s*)(\d+)(,?)$', re.MULTILINE) |
| 18 | +_STATDESC_SUFFIX = "_stat_descriptions.lua" |
| 19 | +_PLACEHOLDER: Final = "0" |
| 20 | + |
| 21 | + |
| 22 | +def _replace_scalar(match: re.Match[str]) -> str: |
| 23 | + return f"{match.group(1)}{_PLACEHOLDER}" |
| 24 | + |
| 25 | + |
| 26 | +def _replace_table(match: re.Match[str]) -> str: |
| 27 | + return f"{match.group(1)} {_PLACEHOLDER} {match.group(3)}" |
| 28 | + |
| 29 | + |
| 30 | +def _normalize(contents: str, file_hint: str | None) -> str: |
| 31 | + contents = _RE_TABLE.sub(_replace_table, contents) |
| 32 | + contents = _RE_SCALAR.sub(_replace_scalar, contents) |
| 33 | + contents = _RE_BRACKET_KEY.sub(_replace_scalar, contents) |
| 34 | + |
| 35 | + file_name = Path(file_hint).name if file_hint else "" |
| 36 | + |
| 37 | + if file_name == "ClusterJewels.lua" or "NotableSortOrder" in contents: |
| 38 | + contents = _RE_NOTABLE_ENTRY.sub(_replace_scalar, contents) |
| 39 | + if file_hint and "LegionPassives.lua" in file_hint: |
| 40 | + contents = re.sub(r'(\["oidx"\]\s*=\s*)(\d+)', _replace_scalar, contents) |
| 41 | + if file_name == "stat_descriptions.lua" or file_name.endswith(_STATDESC_SUFFIX): |
| 42 | + contents = _RE_STATDESC_BLOCK.sub(lambda m: f"{m.group(1)}[0]{m.group(3)}", contents) |
| 43 | + contents = _RE_STATDESC_TAIL.sub(lambda m: f'{m.group(1)}{_PLACEHOLDER}{m.group(3)}', contents) |
| 44 | + |
| 45 | + return contents |
| 46 | + |
| 47 | + |
| 48 | +def _read_source(path: str | None) -> str: |
| 49 | + if path: |
| 50 | + with io.open(path, "r", encoding="utf-8", errors="ignore") as handle: |
| 51 | + return handle.read() |
| 52 | + return sys.stdin.read() |
| 53 | + |
| 54 | + |
| 55 | +def main() -> None: |
| 56 | + path = sys.argv[1] if len(sys.argv) > 1 else None |
| 57 | + sys.stdout.write(_normalize(_read_source(path), path)) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments