1
0
mirror of https://github.com/kmein/niveum synced 2026-03-18 11:01:07 +01:00
Files
niveum/packages/pi/patch-permission-sound.py

36 lines
1003 B
Python

#!/usr/bin/env python3
"""Patch pi-hooks permission extension to play peon sounds via pw-play on Linux."""
import re
import sys
ts_file = sys.argv[1]
pw_play = sys.argv[2]
src = open(ts_file).read()
# Use a lambda for replacement to avoid re.sub interpreting backslash sequences
replacement = r"""function playPermissionSound(): void {
const isMac = process.platform === "darwin";
if (isMac) {
exec('afplay /System/Library/Sounds/Funk.aiff 2>/dev/null', (err) => {
if (err) process.stdout.write("\x07");
});
} else {
const n = Math.floor(Math.random() * 4) + 1;
exec(`""" + pw_play + r""" "$HOME/src/sounds/games/Warcraft III/Units/Orc/Peon/PeonWhat${n}.wav"`, () => {});
}
}"""
result = re.sub(
r'function playPermissionSound\(\): void \{.*?\n\}',
lambda m: replacement,
src,
flags=re.DOTALL,
)
if result == src:
print("ERROR: playPermissionSound function not found in", ts_file, file=sys.stderr)
sys.exit(1)
open(ts_file, 'w').write(result)