refactor: split read_commands into per-command methods
This commit is contained in:
parent
5b2c6c8259
commit
974a0b44aa
1 changed files with 72 additions and 58 deletions
130
merge_srt.py
130
merge_srt.py
|
@ -40,70 +40,84 @@ class SRTMerger:
|
|||
continue
|
||||
command = parts[0].upper()
|
||||
if command == 'COPY':
|
||||
if len(parts) != 3:
|
||||
continue
|
||||
try:
|
||||
source = int(parts[1])
|
||||
if source not in (1, 2):
|
||||
continue
|
||||
start, end = map(int, parts[2].split('-'))
|
||||
if start > end:
|
||||
continue
|
||||
except (ValueError, AttributeError):
|
||||
continue
|
||||
commands.append(('COPY', source, start, end))
|
||||
parsed = self.parse_copy(parts)
|
||||
elif command == 'MAP':
|
||||
if len(parts) != 5:
|
||||
continue
|
||||
try:
|
||||
text_source = int(parts[1])
|
||||
text_start = int(parts[2])
|
||||
time_start = int(parts[3])
|
||||
count = int(parts[4])
|
||||
if text_source not in (1, 2):
|
||||
continue
|
||||
if text_start < 1 or time_start < 1 or count < 1:
|
||||
continue
|
||||
except (ValueError, AttributeError):
|
||||
continue
|
||||
commands.append(('MAP', text_source, text_start, time_start, count))
|
||||
parsed = self.parse_map(parts)
|
||||
elif command == 'SYNC':
|
||||
if len(parts) != 4:
|
||||
continue
|
||||
try:
|
||||
text_source = int(parts[1])
|
||||
text_index = int(parts[2])
|
||||
time_index = int(parts[3])
|
||||
if text_source not in (1, 2):
|
||||
continue
|
||||
if text_index < 1 or time_index < 2:
|
||||
continue
|
||||
except (ValueError, AttributeError):
|
||||
continue
|
||||
commands.append(('SYNC', text_source, text_index, time_index))
|
||||
parsed = self.parse_sync(parts)
|
||||
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))
|
||||
parsed = self.parse_put(parts)
|
||||
else:
|
||||
continue
|
||||
parsed = None
|
||||
if parsed is not None:
|
||||
commands.append(parsed)
|
||||
return commands
|
||||
|
||||
def parse_copy(self, parts):
|
||||
if len(parts) != 3:
|
||||
return None
|
||||
try:
|
||||
source = int(parts[1])
|
||||
if source not in (1, 2):
|
||||
return None
|
||||
start, end = map(int, parts[2].split('-'))
|
||||
if start > end:
|
||||
return None
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return None
|
||||
return ('COPY', source, start, end)
|
||||
|
||||
def parse_map(self, parts):
|
||||
if len(parts) != 5:
|
||||
return None
|
||||
try:
|
||||
text_source = int(parts[1])
|
||||
if text_source not in (1, 2):
|
||||
return None
|
||||
text_start = int(parts[2])
|
||||
time_start = int(parts[3])
|
||||
count = int(parts[4])
|
||||
if text_start < 1 or time_start < 1 or count < 1:
|
||||
return None
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return None
|
||||
return ('MAP', text_source, text_start, time_start, count)
|
||||
|
||||
def parse_sync(self, parts):
|
||||
if len(parts) != 4:
|
||||
return None
|
||||
try:
|
||||
text_source = int(parts[1])
|
||||
if text_source not in (1, 2):
|
||||
return None
|
||||
text_index = int(parts[2])
|
||||
time_index = int(parts[3])
|
||||
if text_index < 1 or time_index < 2:
|
||||
return None
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
return None
|
||||
return ('SYNC', text_source, text_index, time_index)
|
||||
|
||||
def parse_put(self, parts):
|
||||
if len(parts) != 4:
|
||||
return None
|
||||
try:
|
||||
source = int(parts[1])
|
||||
if source not in (1, 2):
|
||||
return None
|
||||
index = int(parts[2])
|
||||
if index < 1:
|
||||
return None
|
||||
json_str = parts[3]
|
||||
data = json.loads(json_str)
|
||||
if 'text' not in data:
|
||||
return None
|
||||
text = data['text']
|
||||
except (ValueError, json.JSONDecodeError, TypeError) as e:
|
||||
print(f"Error parsing PUT command: {e}")
|
||||
return None
|
||||
return ('PUT', source, index, text)
|
||||
|
||||
def process_commands(self):
|
||||
for cmd in self.commands:
|
||||
if cmd[0] == 'COPY':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue