diff --git a/.gitignore b/.gitignore index b218332..87ee51f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ dist/ *.egg-info/ __pycache__/ -test_*.srt +*.test_*.srt diff --git a/src/fsub/fsub.py b/src/fsub/fsub.py index 2ab5799..2e12c28 100755 --- a/src/fsub/fsub.py +++ b/src/fsub/fsub.py @@ -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__': diff --git a/tests/integration.py b/tests/integration.py index 0f870db..40a16bb 100644 --- a/tests/integration.py +++ b/tests/integration.py @@ -8,54 +8,68 @@ 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' - shutil.copy(sample, ifile) - args.append(ifile) + 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) - out = open(ifile) - result = out.read() - out.close() + 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' - cmp_file = open(ofile) - cmp = cmp_file.read() - cmp_file.close() + ofile = str(self.samples / ofiles[i]) + '.srt' + cmp_file = open(ofile) + cmp = cmp_file.read() + cmp_file.close() - self.assertEqual(result, cmp) - os.remove(ifile) + self.assertEqual(result, cmp) + os.remove(ifile) 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__': diff --git a/tests/samples/sample1-sample2-sample3-joined.srt b/tests/samples/sample1-sample2-sample3-joined.srt new file mode 100644 index 0000000..4a93ce7 --- /dev/null +++ b/tests/samples/sample1-sample2-sample3-joined.srt @@ -0,0 +1,37 @@ +1 +00:00:48,900 --> 00:00:49,800 +This one is full of HTML tags. +Above, below, everywhere + +2 +00:00:51,800 --> 00:00:52,700 +Even 's! + +3 +00:00:53,500 --> 00:00:55,200 +The script should not +care whether the tag is +valid or not + +4 +00:00:56,000 --> 00:00:57,000 +

It should just strip all of +them mercilessly

+ +5 +00:00:58,100 --> 00:00:59,600 + + +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 diff --git a/tests/samples/sample2.srt b/tests/samples/sample2.srt index 85d42e8..fba0658 100644 --- a/tests/samples/sample2.srt +++ b/tests/samples/sample2.srt @@ -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! diff --git a/tests/samples/sample3.srt b/tests/samples/sample3.srt new file mode 100644 index 0000000..06da696 --- /dev/null +++ b/tests/samples/sample3.srt @@ -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 + + +