feat: add PUT command and use shlex for command parsing
This commit is contained in:
parent
27f7f2ec38
commit
56187476c3
1 changed files with 40 additions and 4 deletions
44
merge_srt.py
44
merge_srt.py
|
@ -1,4 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
|
import shlex
|
||||||
|
import json
|
||||||
|
|
||||||
class SRTMerger:
|
class SRTMerger:
|
||||||
def __init__(self, srt1_filename, srt2_filename, commands_filename, output_filename):
|
def __init__(self, srt1_filename, srt2_filename, commands_filename, output_filename):
|
||||||
|
@ -33,12 +35,12 @@ class SRTMerger:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or line.startswith('#'):
|
if not line or line.startswith('#'):
|
||||||
continue
|
continue
|
||||||
parts = line.split()
|
parts = shlex.split(line)
|
||||||
if not parts:
|
if not parts:
|
||||||
continue
|
continue
|
||||||
command = parts[0].upper()
|
command = parts[0].upper()
|
||||||
if command == 'COPY':
|
if command == 'COPY':
|
||||||
if len(parts) != 3:
|
if len(parts) != 4:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
source = int(parts[1])
|
source = int(parts[1])
|
||||||
|
@ -51,7 +53,7 @@ class SRTMerger:
|
||||||
continue
|
continue
|
||||||
commands.append(('COPY', source, start, end))
|
commands.append(('COPY', source, start, end))
|
||||||
elif command == 'MAP':
|
elif command == 'MAP':
|
||||||
if len(parts) != 6:
|
if len(parts) != 7:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
text_source = int(parts[1])
|
text_source = int(parts[1])
|
||||||
|
@ -67,7 +69,7 @@ class SRTMerger:
|
||||||
continue
|
continue
|
||||||
commands.append(('MAP', text_source, text_start, time_source, time_start, count))
|
commands.append(('MAP', text_source, text_start, time_source, time_start, count))
|
||||||
elif command == 'SYNC':
|
elif command == 'SYNC':
|
||||||
if len(parts) != 4:
|
if len(parts) != 5:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
text_source = int(parts[1])
|
text_source = int(parts[1])
|
||||||
|
@ -80,6 +82,25 @@ class SRTMerger:
|
||||||
except (ValueError, AttributeError):
|
except (ValueError, AttributeError):
|
||||||
continue
|
continue
|
||||||
commands.append(('SYNC', text_source, text_index, time_index))
|
commands.append(('SYNC', text_source, text_index, time_index))
|
||||||
|
elif command == 'PUT':
|
||||||
|
if len(parts) != 4:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
source = int(parts[1])
|
||||||
|
if source not in (1, 2):
|
||||||
|
continue
|
||||||
|
index = int(parts[2])
|
||||||
|
if index < 1:
|
||||||
|
continue
|
||||||
|
json_str = parts[3]
|
||||||
|
data = json.loads(json_str)
|
||||||
|
if 'text' not in data:
|
||||||
|
continue
|
||||||
|
text = data['text']
|
||||||
|
except (ValueError, json.JSONDecodeError, TypeError) as e:
|
||||||
|
print(f"Error parsing PUT command: {e}")
|
||||||
|
continue
|
||||||
|
commands.append(('PUT', source, index, text))
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
return commands
|
return commands
|
||||||
|
@ -92,6 +113,8 @@ class SRTMerger:
|
||||||
self.handle_map(*cmd[1:])
|
self.handle_map(*cmd[1:])
|
||||||
elif cmd[0] == 'SYNC':
|
elif cmd[0] == 'SYNC':
|
||||||
self.handle_sync(*cmd[1:])
|
self.handle_sync(*cmd[1:])
|
||||||
|
elif cmd[0] == 'PUT':
|
||||||
|
self.handle_put(*cmd[1:])
|
||||||
self.write_output()
|
self.write_output()
|
||||||
|
|
||||||
def handle_copy(self, source, start, end):
|
def handle_copy(self, source, start, end):
|
||||||
|
@ -139,6 +162,19 @@ class SRTMerger:
|
||||||
'text': text_entry['text'],
|
'text': text_entry['text'],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def handle_put(self, source, index, text):
|
||||||
|
source_list = self.srt1 if source == 1 else self.srt2
|
||||||
|
index_idx = index - 1
|
||||||
|
if index_idx < 0 or index_idx >= len(source_list):
|
||||||
|
print(f"Skipping invalid PUT command: source {source}, index {index} is out of bounds")
|
||||||
|
return
|
||||||
|
entry = source_list[index_idx]
|
||||||
|
new_entry = {
|
||||||
|
'timestamp': entry['timestamp'],
|
||||||
|
'text': text,
|
||||||
|
}
|
||||||
|
self.output.append(new_entry)
|
||||||
|
|
||||||
def write_output(self):
|
def write_output(self):
|
||||||
if self.output_filename == '-':
|
if self.output_filename == '-':
|
||||||
out = sys.stdout
|
out = sys.stdout
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue