def test_should_hide_100_percent_of_text(self): tuner = Tuner() tuner.add_filter(HideTextFilter(100)) subtitles_tuned = tuner.tune(SrtReader(self.file_srt_2_subtitles_path)) srt_reader = SrtReader(self.file_srt_2_subtitles_hide_100_path) subtitles_tuned_reference = srt_reader.read_subtitles() self.assertEqual(subtitles_tuned_reference, subtitles_tuned)
def test_should_read_and_write_all_subtitles(self): reader = SrtReader(self.file_srt_2_subtitles_path) subtitles = reader.read_subtitles() with SrtWriter(self.file_srt_temp_path) as writer: writer.write(subtitles) with open(self.file_srt_2_subtitles_path) as file_srt_2_subtitles,\ open(self.file_srt_temp_path) as file_srt_temp: self.assertEqual(file_srt_2_subtitles.read(), file_srt_temp.read())
def test_should_read_and_write_all_subtitles_utf8(self): reader = SrtReader(self.file_srt_2_subtitles_utf8_path) subtitles = reader.read_subtitles() with SrtWriter(self.file_srt_temp_path) as writer: writer.write(subtitles) with open(self.file_srt_2_subtitles_utf8_path) as file_srt_2_subtitles_utf8,\ open(self.file_srt_temp_path) as file_srt_temp: self.assertEqual(file_srt_2_subtitles_utf8.read(), file_srt_temp.read())
def test_should_read_subtitles_iso_8859_1(self): subtitle_1 = Subtitle( 1, '00:00:00,490', '00:00:02,830', u"Anteriormente en Marvel's\nAgents of S.H.I.E.L.D....\n") subtitle_2 = Subtitle( 2, '00:00:02,860', '00:00:05,090', u"Se supone que Malick es\nla última cabeza de Hydra,\n") subtitles_reference = [subtitle_1, subtitle_2] with SrtReader(self.file_srt_2_subtitles_iso_8859_1_path) as reader: subtitles = reader.read_subtitles() self.assertEqual(subtitles_reference, subtitles) with SrtReader(open( self.file_srt_2_subtitles_iso_8859_1_path)) as reader: subtitles = reader.read_subtitles() self.assertEqual(subtitles_reference, subtitles)
def post(file_str): with SrtReader(file_str) as reader: try: subtitles = reader.read_subtitles_dict() except InvalidSrtFormatError: raise session.set_subtitles(subtitles) session.set_preview(subtitles)
def test_should_read_subtitles_utf8(self): subtitle_1 = Subtitle(1249, '01:24:09,860', '01:24:11,300', u'Te escuché.\n') subtitle_2 = Subtitle( 1250, '01:24:15,020', '01:24:17,900', u'¡Qué más da, es hora de\nser Sherlock Holmes!\n') subtitles_reference = [subtitle_1, subtitle_2] with SrtReader(self.file_srt_2_subtitles_utf8_path) as reader: subtitles = reader.read_subtitles() self.assertEqual(subtitles_reference, subtitles)
def test_should_read_subtitles(self): subtitle_1 = Subtitle(1249, '01:24:09,860', '01:24:11,300', u'I heard you.\n') subtitle_2 = Subtitle( 1250, '01:24:15,020', '01:24:17,900', u'Anyway, time to go and\nbe Sherlock Holmes.\n') subtitles_reference = [subtitle_1, subtitle_2] with SrtReader(self.file_srt_2_subtitles_path) as reader: subtitles = reader.read_subtitles() self.assertEqual(subtitles_reference, subtitles)
def test_should_read_next_subtitle_containing_1_line_text_and_next_subtitle_containing_2_lines_text( self): with SrtReader(self.file_srt_2_subtitles_path) as reader: subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 1249) self.assertEqual(subtitle.timestamp_begin, '01:24:09,860') self.assertEqual(subtitle.timestamp_end, '01:24:11,300') self.assertEqual(subtitle.text, u'I heard you.\n') subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 1250) self.assertEqual(subtitle.timestamp_begin, '01:24:15,020') self.assertEqual(subtitle.timestamp_end, '01:24:17,900') self.assertEqual(subtitle.text, u'Anyway, time to go and\nbe Sherlock Holmes.\n')
def test_should_read_next_subtitle_when_text_has_no_lines_in_non_strict_parsing( self): with SrtReader(self.file_srt_invalid_text_has_no_lines, False) as reader: subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 1249) self.assertEqual(subtitle.timestamp_begin, '01:24:09,860') self.assertEqual(subtitle.timestamp_end, '01:24:11,300') self.assertEqual(subtitle.text, '\n') subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 1250) self.assertEqual(subtitle.timestamp_begin, '01:24:15,020') self.assertEqual(subtitle.timestamp_end, '01:24:17,900') self.assertEqual(subtitle.text, u'Anyway, time to go and\nbe Sherlock Holmes.\n')
def test_should_reset_srt_file(self): subtitle_1 = Subtitle(1, '00:00:07,130', '00:00:09,131', u'Uh, yeah, no.\n') subtitle_2 = Subtitle(2, '00:00:09,132', '00:00:11,133', u'I mean, you people called me.\n') with SrtReader(self.file_srt_with_bom, False) as reader: subtitle = reader.read_next_subtitle() self.assertEqual(subtitle, subtitle_1) subtitle = reader.read_next_subtitle() self.assertEqual(subtitle, subtitle_2) reader.reset() subtitle = reader.read_next_subtitle() self.assertEqual(subtitle, subtitle_1) subtitle = reader.read_next_subtitle() self.assertEqual(subtitle, subtitle_2)
def test_should_read_next_subtitle_when_more_than_1_newlines_after_subtitle( self): with SrtReader( self.file_srt_invalid_more_than_1_newlines_after_subtitle, False) as reader: subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 758) self.assertEqual(subtitle.timestamp_begin, '00:55:36,000') self.assertEqual(subtitle.timestamp_end, '00:55:39,440') self.assertEqual(subtitle.text, u'Chinese ceramic statue sold for\n') subtitle = reader.read_next_subtitle() self.assertEqual(subtitle.identifier, 759) self.assertEqual(subtitle.timestamp_begin, '00:55:39,440') self.assertEqual(subtitle.timestamp_end, '00:55:42,840') self.assertEqual( subtitle.text, u'Look, a month before that,\nChinese painting,\n')
def main(): parser = argparse.ArgumentParser(description='Process subtitles.') parser.add_argument('srt_file', help='path of the srt file containing the subtitles', type=str, nargs='?', default=sys.stdin) parser.add_argument('output', help='path of the output file', type=str, nargs='?', default=sys.stdout) args = parser.parse_args() with SrtReader(args.srt_file) as reader, SrtWriter(args.output) as writer: try: for subtitle in reader: writer.write(subtitle) except InvalidSrtFormatError as e: sys.stdout.write( "There was an error while reading the file: {0}".format( e.message)) return 0
def test_should_raise_invalid_format_error_when_reading_next_subtitle_with_invalid_text_is_eof(self): reader = SrtReader(self.file_srt_invalid_text_is_eof) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_raise_invalid_format_error_when_reading_next_subtitle_ends_without_newline(self): reader = SrtReader(self.file_srt_invalid_subtitle_ends_without_newline) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_not_tune_when_tuning_without_filters(self): srt_reader = SrtReader(self.file_srt_2_subtitles_path) subtitles = srt_reader.read_subtitles() tuner = Tuner() subtitles_tuned = tuner.tune(SrtReader(self.file_srt_2_subtitles_path)) self.assertEqual(subtitles, subtitles_tuned)
def test_should_raise_invalid_format_error_when_next_subtitle_text_has_no_lines_in_strict_parsing(self): reader = SrtReader(self.file_srt_invalid_text_has_no_lines, True) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_raise_invalid_format_error_when_reading_next_subtitle_with_invalid_text_is_eof( self): reader = SrtReader(self.file_srt_invalid_text_is_eof) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_raise_invalid_format_error_when_reading_next_subtitle_ends_without_newline( self): reader = SrtReader(self.file_srt_invalid_subtitle_ends_without_newline) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_raise_invalid_format_error_when_next_subtitle_text_has_no_lines_in_strict_parsing( self): reader = SrtReader(self.file_srt_invalid_text_has_no_lines, True) with self.assertRaises(InvalidSrtFormatError): reader.read_next_subtitle()
def test_should_read_actual_non_strict_srt_file(self): with SrtReader(self.file_srt_actual_non_strict, False) as reader: subtitles = [subtitle for subtitle in reader] self.assertEqual(1162, len(subtitles))