コード例 #1
0
 def test_imsc_1_test_suite(self):
     for root, _subdirs, files in os.walk(
             "src/test/resources/ttml/imsc-tests/imsc1/ttml"):
         for filename in files:
             (name, ext) = os.path.splitext(filename)
             if ext == ".ttml":
                 with self.subTest(name):
                     tree = et.parse(os.path.join(root, filename))
                     doc = imsc_reader.to_model(tree)
                     srt_file = srt_writer.from_model(doc)
                     srt_reader.to_model(io.StringIO(srt_file))
コード例 #2
0
  def test_sample(self):
    # from https://en.wikipedia.org/wiki/SubRip
    SAMPLE = """1
00:02:16,612 --> 00:02:19,376
Senator, we're making
our final approach into Coruscant.

2
00:02:19,482 --> 00:02:21,609
Very good, Lieutenant.

3
00:03:13,336 --> 00:03:15,167
We made it.

4
00:03:18,608 --> 00:03:20,371
I guess I was wrong.

5
00:03:20,476 --> 00:03:22,671
There was no danger at all."""

    f = io.StringIO(SAMPLE)

    self.assertIsNotNone(to_model(f))
コード例 #3
0
  def test_blank_lines(self):
    # from https://en.wikipedia.org/wiki/SubRip
    SAMPLE = """
    
1
00:02:16,612 --> 00:02:19,376
Senator, we're making
our final approach into Coruscant.


2
00:02:19,482 --> 00:02:21,609
Very good, Lieutenant.

5
00:03:20,476 --> 00:03:22,671
There was no danger at all.



"""

    f = io.StringIO(SAMPLE)

    self.assertIsNotNone(to_model(f))
コード例 #4
0
  def test_bold(self):
    f = io.StringIO(r"""1
00:02:16,612 --> 00:02:19,376
Hello <bold>my</bold> name is Bob
""")
    doc = to_model(f)
    for e in doc.get_body().dfs_iterator():
      if e.get_style(styles.StyleProperties.FontWeight) == styles.FontWeightType.bold:
        break
    else:
      self.fail()
コード例 #5
0
  def test_italic_alt(self):
    f = io.StringIO(r"""1
00:02:16,612 --> 00:02:19,376
Hello {italic}my{/italic} name is Bob
""")
    doc = to_model(f)
    for e in doc.get_body().dfs_iterator():
      if e.get_style(styles.StyleProperties.FontStyle) == styles.FontStyleType.italic:
        break
    else:
      self.fail()     
コード例 #6
0
  def test_blue(self):
    f = io.StringIO(r"""1
00:02:16,612 --> 00:02:19,376
Hello <font color='blue'>my</font> name is Bob
""")

    doc = to_model(f)
    for e in doc.get_body().dfs_iterator():
      if e.get_style(styles.StyleProperties.Color) == styles.NamedColors.blue.value:
        break
    else:
      self.fail()
コード例 #7
0
  def test_underline_alt(self):
    f = io.StringIO(r"""1
00:02:16,612 --> 00:02:19,376
Hello {underline}my{/underline} name is Bob
""")
    doc = to_model(f)
    for e in doc.get_body().dfs_iterator():
      text_decoration = e.get_style(styles.StyleProperties.TextDecoration)
      if text_decoration is not None and text_decoration.underline:
        break
    else:
      self.fail()     
コード例 #8
0
  def test_long_hours(self):
    f = io.StringIO(r"""1
101:00:00,000 --> 101:00:01,000
Hello <bold>my</bold> name is Bob
""")
    doc = to_model(f)

    self.assertEqual(
      363600,
      doc.get_body().first_child().first_child().get_begin()
    )

    self.assertEqual(
      363601,
      doc.get_body().first_child().first_child().get_end()
    )
コード例 #9
0
ファイル: tt.py プロジェクト: sandflow/ttconv
def convert(args):
    '''Process input and output through the reader, converter, and writer'''

    inputfile = args.input
    outputfile = args.output

    # Note - Loading config data from a file takes priority over
    # data passed in as a json string
    json_config_data = None

    if args.config is not None:
        json_config_data = json.loads(args.config)
    if args.config_file is not None:
        with open(args.config_file) as json_file:
            json_config_data = json.load(json_file)

    general_config = read_config_from_json(GeneralConfiguration,
                                           json_config_data)

    if general_config is not None:

        if general_config.progress_bar is not None:
            progress.display_progress_bar = general_config.progress_bar

        if general_config.log_level is not None:
            LOGGER.setLevel(general_config.log_level)

    LOGGER.info("Input file is %s", inputfile)
    LOGGER.info("Output file is %s", outputfile)

    _input_filename, input_file_extension = os.path.splitext(inputfile)
    _output_filename, output_file_extension = os.path.splitext(outputfile)

    reader_type = FileTypes.get_file_type(args.itype, input_file_extension)
    writer_type = FileTypes.get_file_type(args.otype, output_file_extension)

    if reader_type is FileTypes.TTML:
        #
        # Parse the xml input file into an ElementTree
        #
        tree = et.parse(inputfile)

        #
        # Pass the parsed xml to the reader
        #
        model = imsc_reader.to_model(tree, progress_callback_read)

    elif reader_type is FileTypes.SCC:
        file_as_str = Path(inputfile).read_text()

        #
        # Read the config
        #
        reader_config = read_config_from_json(SccReaderConfiguration,
                                              json_config_data)

        #
        # Pass the parsed xml to the reader
        #
        model = scc_reader.to_model(file_as_str, reader_config,
                                    progress_callback_read)

    elif reader_type is FileTypes.STL:
        #
        # Read the config
        #
        reader_config = read_config_from_json(STLReaderConfiguration,
                                              json_config_data)

        #
        # Open the file and pass it to the reader
        #
        with open(inputfile, "rb") as f:
            model = stl_reader.to_model(f, reader_config,
                                        progress_callback_read)

    elif reader_type is FileTypes.SRT:

        #
        # Open the file and pass it to the reader
        #
        with open(inputfile, "r", encoding="utf-8") as f:
            model = srt_reader.to_model(f, None, progress_callback_read)

    else:
        if args.itype is not None:
            exit_str = f'Input type {args.itype} is not supported'
        else:
            exit_str = f'Input file {args.input} is not supported'

        LOGGER.error(exit_str)
        sys.exit(exit_str)

    if writer_type is FileTypes.TTML:
        #
        # Read the config
        #
        writer_config = read_config_from_json(IMSCWriterConfiguration,
                                              json_config_data)

        #
        # Construct and configure the writer
        #
        tree_from_model = imsc_writer.from_model(model, writer_config,
                                                 progress_callback_write)

        #
        # Write out the converted file
        #
        tree_from_model.write(outputfile, encoding="utf-8")

    elif writer_type is FileTypes.SRT:
        #
        # Read the config
        #
        writer_config = read_config_from_json(ISDConfiguration,
                                              json_config_data)

        #
        # Construct and configure the writer
        #
        srt_document = srt_writer.from_model(model, writer_config,
                                             progress_callback_write)

        #
        # Write out the converted file
        #
        with open(outputfile, "w", encoding="utf-8") as srt_file:
            srt_file.write(srt_document)

    elif writer_type is FileTypes.VTT:
        #
        # Read the config
        #
        writer_config = read_config_from_json(VTTWriterConfiguration,
                                              json_config_data)

        #
        # Construct and configure the writer
        #
        vtt_document = vtt_writer.from_model(model, writer_config,
                                             progress_callback_write)

        #
        # Write out the converted file
        #
        with open(outputfile, "w", encoding="utf-8") as vtt_file:
            vtt_file.write(vtt_document)

    else:
        if args.otype is not None:
            exit_str = f'Output type {args.otype} is not supported'
        else:
            exit_str = f'Output file is {args.output} is not supported'

        LOGGER.error(exit_str)
        sys.exit(exit_str)