-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.py
More file actions
144 lines (128 loc) · 4.62 KB
/
split.py
File metadata and controls
144 lines (128 loc) · 4.62 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from pathlib import Path
import sys
import time
import traceback
from typing import Callable
from watchdog.events import FileModifiedEvent
from watchdog.observers import Observer
def do_split(input_file: Path, output_file: Path):
with open(input_file, mode='r') as f:
inputs = f.read().splitlines()
out_left = []
out_right = []
for line in inputs:
if not line:
continue
frame, *keys = line.split()
for key in keys:
if key[1] in {'W', 'A', 'D'}:
out_left.append((int(frame), key))
else:
out_right.append((int(frame), key))
for inputs in (out_left, out_right):
last_frame = 0
for i, (frame, key) in enumerate(inputs):
offset = frame - last_frame
last_frame = frame
inputs[i] = f'{offset} {key}'
with open(output_file, mode='w') as f:
f.write('# Left\n')
f.write('\n'.join(out_left))
f.write('\n\n# Right\n')
f.write('\n'.join(out_right))
print(f"Written split output to {output_file}")
def do_combine(input_file: Path, output_file: Path):
with open(input_file, mode='r') as f:
inputs = f.read().splitlines()
left_found, right_found = False, False
frame = 0
inputs_out = []
for line in inputs:
line = line.strip()
if not line:
continue
if line.startswith('#'):
section = line.removeprefix('#').strip()
if section == 'Left':
if left_found:
print("ERROR: Multiple left sections found")
sys.exit(1)
left_found = True
frame = 0
elif section == 'Right':
if right_found:
print("ERROR: Multiple right sections found")
sys.exit(1)
right_found = True
frame = 0
else:
print(f"ERROR: Unknown section: {section}")
sys.exit(1)
continue
components = line.split(maxsplit=1)
if len(components) == 2:
offset, keys = components
frame += int(offset)
inputs_out.append((frame, keys))
else:
offset = line
frame += int(offset)
inputs_out.sort()
with open(output_file, mode='w') as f:
f.write('\n'.join(f'{f} {k}' for f, k in inputs_out))
print(f"Written combined output to {output_file}")
def wrap_func(func: Callable, *args):
def wrapped():
try:
func(*args)
except SystemExit:
pass
except BaseException as e:
traceback.print_exception(e)
return wrapped
if __name__ == '__main__':
args = [arg for arg in sys.argv[1:] if not arg.startswith('--')]
flags = [arg for arg in sys.argv[1:] if arg.startswith('--')]
if len(args) == 1:
watch = True
original_file = Path(args[0])
input_file = original_file.with_stem(original_file.stem + '_split')
output_file = original_file.with_stem(original_file.stem + '_combined')
if not input_file.exists():
do_split(original_file, input_file)
func = wrap_func(do_combine, input_file, output_file)
elif args[0] == 'split':
watch = False
input_file = Path(args[1])
output_file = input_file.with_stem(input_file.stem + '_split')
func = wrap_func(do_split, input_file, output_file)
elif args[0] == 'combine':
watch = False
input_file = Path(args[1])
output_file = input_file.with_stem(input_file.stem.removesuffix('_split') + '_combined')
func = wrap_func(do_combine, input_file, output_file)
else:
print(f"ERROR: Unknown command: {args[0]}")
sys.exit(1)
if '--no-watch' in flags:
watch = False
elif '--watch' in flags:
watch = True
if watch:
from watchdog.events import FileModifiedEvent, FileSystemEventHandler
class EventHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path == str(input_file):
func()
observer = Observer()
observer.schedule(EventHandler(), str(input_file.parent), event_filter=[FileModifiedEvent])
observer.start()
try:
func()
while True:
time.sleep(1000)
finally:
observer.stop()
observer.join()
else:
func()