Files
g0v0-server/scripts/generate_ruleset_attributes.py
MingxuanGame 33f321952d feat(custom-rulesets): support custom rulesets (#23)
* feat(custom_ruleset): add custom rulesets support

* feat(custom-ruleset): add version check

* feat(custom-ruleset): add LegacyIO API to get ruleset hashes

* feat(pp): add check for rulesets whose pp cannot be calculated

* docs(readme): update README to include support for custom rulesets

* fix(custom-ruleset): make `rulesets` empty instead of throw a error when version check is disabled

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore(custom-ruleset): apply the latest changes of generator

c891bcd159

and

e25041ad3b

* feat(calculator): add fallback performance calculation for unsupported modes

* fix(calculator): remove debug print

* fix: resolve reviews

* feat(calculator): add difficulty calculation checks

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-26 21:10:36 +08:00

65 lines
2.3 KiB
Python

import datetime
from pathlib import Path
import re
import subprocess
import sys
from datamodel_code_generator import DataModelType, generate
from datamodel_code_generator.format import Formatter
OUTPUT_FILE = Path("app/models/performance.py")
def generate_model(schema_file: Path, version: str = ""):
temp_file = OUTPUT_FILE.parent / "_temp.py"
if temp_file.exists():
temp_file.unlink()
temp_file.touch()
version = version or f"{datetime.datetime.now().strftime('%Y.%m%d')}.0"
generate(
input_=schema_file,
output=temp_file,
output_model_type=DataModelType.PydanticV2BaseModel,
disable_future_imports=True,
formatters=[Formatter.RUFF_CHECK, Formatter.RUFF_FORMAT],
custom_file_header=(
f"# Version: {version}\n"
"# Auto-generated by scripts/generate_ruleset_attributes.py.\n"
"# Schema generated by https://github.com/GooGuTeam/custom-rulesets\n"
"# Do not edit this file directly.\n"
"# ruff: noqa: E501\n"
),
)
code = temp_file.read_text()
output = code
performance_attributes_classes = re.findall(r"class (\w+PerformanceAttributes)", code)
performance_attributes_classes.append("PerformanceAttributes")
output += "PerformanceAttributesUnion = " + " | ".join(performance_attributes_classes) + "\n"
difficulty_attributes_classes = re.findall(r"class (\w+DifficultyAttributes)", code)
difficulty_attributes_classes.append("DifficultyAttributes")
output += "DifficultyAttributesUnion = " + " | ".join(difficulty_attributes_classes) + "\n"
OUTPUT_FILE.write_text(output)
temp_file.unlink(missing_ok=True)
subprocess.run(
["uv", "run", "ruff", "check", "--fix", "app/models/performance.py"], # nosec B607 # noqa: S607
capture_output=True,
)
subprocess.run(
["uv", "run", "ruff", "format", "app/models/performance.py"], # nosec B607 # noqa: S607
capture_output=True,
)
if __name__ == "__main__":
if len(sys.argv) < 1:
print("Usage: python scripts/generate_ruleset_attributes.py <schema_file> [version]")
sys.exit(1)
if len(sys.argv) > 2:
generate_model(Path(sys.argv[1]), sys.argv[2])
else:
generate_model(Path(sys.argv[1]))