-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.py
More file actions
64 lines (52 loc) · 1.8 KB
/
classify.py
File metadata and controls
64 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
CLI entry point.
Usage:
python classify.py map.osu
python classify.py maps/ # processes all .osu files in a directory
python classify.py maps/ --out results.json
python classify.py maps/ --pretty
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from osu_parser import parse_osu_file
from feature_extractor import extract_features
def process_file(path: Path) -> dict:
try:
bm = parse_osu_file(path)
feats = extract_features(bm)
feats["file"] = str(path)
return feats
except Exception as exc:
return {"file": str(path), "error": str(exc)}
def main() -> None:
parser = argparse.ArgumentParser(description="Extract osu! beatmap features")
parser.add_argument("input", help=".osu file or directory of .osu files")
parser.add_argument("--out", help="Write JSON output to this file")
parser.add_argument("--pretty", action="store_true",
help="Pretty-print JSON (indent=2)")
args = parser.parse_args()
target = Path(args.input)
if target.is_dir():
paths = sorted(target.rglob("*.osu"))
elif target.is_file():
paths = [target]
else:
print(f"Error: '{target}' is not a file or directory.", file=sys.stderr)
sys.exit(1)
if not paths:
print("No .osu files found.", file=sys.stderr)
sys.exit(1)
results = [process_file(p) for p in paths]
indent = 2 if args.pretty else None
output = json.dumps(results if len(results) > 1 else results[0],
indent=indent)
if args.out:
Path(args.out).write_text(output, encoding="utf-8")
print(f"Wrote {len(results)} result(s) to {args.out}")
else:
print(output)
if __name__ == "__main__":
main()