Add join flag

This commit is contained in:
Augusto Gunsch 2021-11-15 18:14:33 -03:00
parent dc7c0da24c
commit 3aa66545ea
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
6 changed files with 110 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
dist/
*.egg-info/
__pycache__/
test_*.srt
*.test_*.srt

View File

@ -229,6 +229,12 @@ class SubripFile:
self.subs.append(sub)
line_number += len(sub) + 1
def __iadd__(self, other):
shift_time = self.subs[-1].time_end
other.shift(shift_time)
self.subs += other.subs
return self
def clean(self, expressions):
if len(expressions) == 0:
return
@ -318,6 +324,13 @@ def parse_args(args):
type=argparse.FileType('r')
)
parser.add_argument(
'-j', '--join',
help='join all files into the first, shifting their time ' +
'accordingly (this will delete files)',
action='store_true'
)
parser.add_argument(
'files',
help='list of input files (they all must be SubRip files)',
@ -347,14 +360,22 @@ def run(args):
for file in args.files:
parsed_files.append(SubripFile(file))
# TODO: join, split files
if args.join:
first = parsed_files.pop(0)
while True:
try:
first += parsed_files.pop(0)
except IndexError:
break
parsed_files.append(first)
for file in parsed_files:
file.process(args, config)
def main():
run(list(iter(sys.argv).next()))
sys.argv.pop(0)
run(sys.argv)
if __name__ == '__main__':

View File

@ -8,22 +8,31 @@ from pathlib import Path
class TestFsub(unittest.TestCase):
samples = Path('tests/samples')
maxDiff = None
def run_on(self, args, sample, ofile):
ifile = inspect.stack()[1][3] + '.srt'
def run_on(self, args, samples, ofiles):
caller = inspect.stack()[1][3]
ifiles = []
sample = str(self.samples / sample) + '.srt'
samples = map(lambda s: str(self.samples / s) + '.srt', samples)
i = 1
for sample in samples:
ifile = str(i) + '.' + caller + '.srt'
shutil.copy(sample, ifile)
args.append(ifile)
ifiles.append(ifile)
i += 1
fsub.run(args)
limit = len(ofiles)
for i, ifile in enumerate(ifiles):
if i == limit:
break
out = open(ifile)
result = out.read()
out.close()
ofile = str(self.samples / ofile) + '.srt'
ofile = str(self.samples / ofiles[i]) + '.srt'
cmp_file = open(ofile)
cmp = cmp_file.read()
cmp_file.close()
@ -33,29 +42,34 @@ class TestFsub(unittest.TestCase):
def test_cleaned(self):
args = ['-f', str(self.samples / 'blacklist')]
self.run_on(args, 'sample1', 'sample1-cleaned')
self.run_on(args, ['sample1'], ['sample1-cleaned'])
def test_stripped(self):
self.run_on(['-n'], 'sample1', 'sample1-stripped')
self.run_on(['-n'], ['sample1'], ['sample1-stripped'])
def test_cleaned_stripped(self):
args = ['-c', '-f', str(self.samples / 'blacklist'), '-n']
self.run_on(args, 'sample1', 'sample1-cleaned-stripped')
self.run_on(args, ['sample1'], ['sample1-cleaned-stripped'])
def test_cleaned_stripped_shifted_1h(self):
args = ['-c',
'-f', str(self.samples / 'blacklist'),
'-n',
'-s', '3600000']
self.run_on(args, 'sample1', 'sample1-cleaned-stripped-shifted-1h')
self.run_on(args, ['sample1'], ['sample1-cleaned-stripped-shifted-1h'])
def test_shifted_minus_1h(self):
args = ['-s', '-3600000']
self.run_on(args, 'sample1', 'sample1-shifted-minus-1h')
self.run_on(args, ['sample1'], ['sample1-shifted-minus-1h'])
def test_shifted_minus_52s(self):
args = ['-s', '-52000']
self.run_on(args, 'sample1', 'sample1-shifted-minus-52s')
self.run_on(args, ['sample1'], ['sample1-shifted-minus-52s'])
def test_joined(self):
args = ['-j']
self.run_on(args, ['sample1', 'sample2', 'sample3'],
['sample1-sample2-sample3-joined'])
if __name__ == '__main__':

View File

@ -0,0 +1,37 @@
1
00:00:48,900 --> 00:00:49,800
<b>This one is full of HTML tags.</b>
<i>Above, below, everywhere</i>
2
00:00:51,800 --> 00:00:52,700
<a href='dummy'>Even <a>'s!</a>
3
00:00:53,500 --> 00:00:55,200
<html>The script should not
care whether the tag is
valid or not</html>
4
00:00:56,000 --> 00:00:57,000
<p>It should just strip all of
them mercilessly</p>
5
00:00:58,100 --> 00:00:59,600
<ul>Including this one!</ul>
6
00:01:59,600 --> 00:02:00,600
Just a dummy line, I'm sorry.
But there's whitespace!
7
10:05:00,600 --> 10:06:12,800
This one has even more whitespace!
8
10:07:00,600 --> 10:07:02,600
And it's waaay longer!
Amazing

View File

@ -1,6 +1,6 @@
1
00:01:00,000 --> 00:01:01,000
Just a dummy line, I'm sorry
Just a dummy line, I'm sorry.
But there's whitespace!

13
tests/samples/sample3.srt Normal file
View File

@ -0,0 +1,13 @@
1
10:03:00,000 --> 10:04:12,200
This one has even more whitespace!
2
10:05:00,000 --> 10:05:02,000
And it's waaay longer!
Amazing