-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocessing.py
152 lines (105 loc) · 4.06 KB
/
postprocessing.py
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
145
146
147
148
149
150
151
152
import pysubs2
import yaml
import re
import logging
import copy
from pysubs2 import SSAFile
logger = logging.getLogger(__name__)
import actions as WORKFLOW_ACTIONS
class SubtitleRunner:
def __init__(self, workflows: list) -> None:
self.workflows = workflows
self.outputs = {}
def validate_workflow(self):
pass
def _run_task(self, args, uses, kwargs: dict = {}):
func = getattr(WORKFLOW_ACTIONS, uses)
outputs = self.outputs
for k in kwargs.keys():
m = re.match(r"{{(.*)}}", str(kwargs[k]))
if m:
eval_str = m.group(1).strip()
kwargs[k] = eval(eval_str)
return func(*args, **kwargs)
def run_selector(self, ssafile: SSAFile, conf: dict):
id = conf.get("id")
func_name = conf["uses"]
kwargs = conf.get("with", {})
out: list = self._run_task([ssafile], func_name, kwargs)
if id is not None:
self.outputs[id] = out
return out
def run_filter(self, ssafile: SSAFile, selections: list, conf: dict):
id = conf.get("id")
func_name = conf["uses"]
kwargs = conf.get("with", {})
out = [
item
for item in selections
if self._run_task([ssafile, item], func_name, kwargs)
]
if id is not None:
self.outputs[id] = out
return out
def run_actions(self, ssafile: SSAFile, items: list, conf: dict):
id = conf.get("id")
func_name = conf["uses"]
kwargs = conf.get("with", {})
output = []
for i in items:
out = self._run_task([ssafile, i], func_name, kwargs)
output.append(out)
if id is not None:
self.outputs[id] = output
return output
def run_misc(self, ssafile: SSAFile, conf: dict):
id = conf.get("id")
func_name = conf["uses"]
kwargs = conf.get("with", {})
out = self._run_task([ssafile], func_name, kwargs)
if id is not None:
self.outputs[id] = out
return out
def run_workflow(self, ssafile):
for task in self.workflows:
selections = []
selectors = task.get("selectors", [])
filters = task.get("filter", [])
actions = task.get("actions", [])
misc = task.get("misc", [])
for s_conf in selectors:
out = self.run_selector(ssafile, s_conf)
selections.extend(out)
for f_conf in filters:
selections = self.run_filter(ssafile, selections, f_conf)
for a_conf in actions:
self.run_actions(ssafile, selections, a_conf)
for e_conf in misc:
self.run_misc(ssafile, e_conf)
def format(self, path, ext=None, save=True):
ssafile = pysubs2.load(path, format_=ext)
# _format will prevent the auto detection error from rasing in mutliprocessing thread which causes all thread to crash
# related = https://stackoverflow.com/questions/70883678/python-multiprocessing-get-hung
self.run_workflow(ssafile)
if save == True:
ssafile.save(path)
return ssafile
class SubtitleFormatter:
def __init__(self, config_path: str) -> None:
self.log = logging.getLogger("SubtitleFormatter")
with open(config_path) as cfg_file:
self.config: dict = yaml.safe_load(cfg_file.read())
self.workflows = {fmt: self.config[fmt]["tasks"] for fmt in self.config}
def format(self, path, save=True):
path = str(path)
for ext in self.workflows:
if path.endswith(f".{ext}"):
runner = SubtitleRunner(self.workflows[ext])
self.log.debug(f"Formatting subtitle: {path}")
try:
runner.format(path, ext=ext, save=save)
except pysubs2.FormatAutodetectionError as e:
raise RuntimeError(str(e))
return path
else:
raise ValueError(f"Unsupported format: {path}")