-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.py
More file actions
174 lines (132 loc) · 4.71 KB
/
module.py
File metadata and controls
174 lines (132 loc) · 4.71 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import logging
import os
import re
from abc import ABC, abstractmethod
from extract import (
BitmapSubtitleExtractor,
ExtractorConfig,
MediaProber,
TextSubtitleExtractor,
SUPPORTED_VIDEO_EXTENSION,
SUPPORTED_SUBTITLE_FORMATS,
)
from postprocessing import SubtitleFormatter
logger = logging.getLogger(__name__)
class Module(ABC):
def __init__(
self,
excluded_enable: bool = False,
excluded_filelist: str = "",
excluded_append: bool = True,
**kwargs,
) -> None:
self.excluded_enable = excluded_enable
self.excluded_filelist = excluded_filelist
self.excluded_append = excluded_append
if excluded_enable:
if not excluded_append and not os.path.exists(excluded_filelist):
raise ValueError(
"excluded_enable is enabled while excluded_append is disabled but no filelist is provided"
)
else:
# test if the path can be writeable/readable
with open(self.excluded_filelist, "a") as f:
pass
@property
def should_add_excluded(self):
return self.excluded_enable and self.excluded_append
def add_excluded_files(self, paths: list[str]):
if len(paths) == 0:
return
logger.info(f"Adding {len(paths)} files to excluded")
with open(self.excluded_filelist, "a") as f:
f.write("\n".join(paths))
f.write("\n")
def get_excluded_files(self) -> set[str]:
if self.excluded_enable == False:
return set()
with open(self.excluded_filelist) as f:
return set(f.read().splitlines())
def get_filelist(self, path) -> list[str]:
extensions = "|".join(self.get_file_extensions())
regex = f"(?i)\\.({extensions})$"
excluded_files = self.get_excluded_files()
files = []
if os.path.isdir(path):
for root, dirs, filenames in os.walk(path):
for filename in filenames:
f = os.path.join(root, filename)
if re.search(regex, f) and f not in excluded_files:
files.append(f)
else:
files = [path]
logger.info(
f"Found {len(files)} files to be processed, {len(excluded_files)} excluded"
)
return files
@abstractmethod
def get_file_extensions(self) -> list[str]:
pass
@abstractmethod
def process(self, path):
pass
@classmethod
@abstractmethod
def from_dict(cls):
pass
class ExtractionModule(Module):
def __init__(
self, extract_conf: ExtractorConfig, extract_bitmap=False, **kwargs
) -> None:
super().__init__(**kwargs)
self.config = extract_conf
self.extract_bitmap = extract_bitmap
self.prober = MediaProber()
@classmethod
def from_dict(cls, settings: dict):
config = ExtractorConfig(**settings["config"])
return cls(config, **settings)
def get_file_extensions(self):
return SUPPORTED_VIDEO_EXTENSION
def process(self, filepaths: list[str]):
extractor1 = TextSubtitleExtractor(self.config, self.prober)
extractor2 = BitmapSubtitleExtractor(self.config, self.prober)
output_files = []
for path in filepaths:
try:
output_files += extractor1.extract(path)
if self.extract_bitmap:
output_files += extractor2.extract(path)
except Exception as e:
logger.critical(f"An error has occuerd while extracting: {e}")
if self.should_add_excluded:
self.add_excluded_files(filepaths)
else:
logger.debug("No adding excluded files")
return output_files
class PostprocessorModule(Module):
def __init__(
self,
workflow_file: str,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.workflow_file = workflow_file
@classmethod
def from_dict(cls, settings: dict):
return cls(settings["config"]["workflow_file"], **settings)
def get_file_extensions(self):
return SUPPORTED_SUBTITLE_FORMATS
def process(self, filepaths: list[str]):
formatter = SubtitleFormatter(self.workflow_file)
output_files = []
for path in filepaths:
try:
output_files += formatter.format(path)
except Exception as e:
logger.critical(f"An error has occuerd while formatting: {e}")
if self.should_add_excluded:
self.add_excluded_files(filepaths)
else:
logger.debug("No adding excluded files")
return output_files