Ejemplo n.º 1
0
class TestSubConverter(unittest.TestCase):
    """SubConverter test suite."""

    subWithHeader = ["[INFORMATION]\n", "[TITLE]Subtitle\n", "[AUTHOR]Subtitle author\n",
        "[SOURCE]\n","[PRG]SubConvert\n", "[FILEPATH]\n", "[DELAY]0\n", "[CD TRACK]0\n",
        "[COMMENT]Converted to subviewer format with SubConvert\n", "[END INFORMATION]\n",
        "[SUBTITLE]\n", "[COLF]&HFFFFFF,[STYLE]no,[SIZE]24,[FONT]Tahoma\n",
        "00:00:00.00,00:00:01.00\n", "First subtitle\n", "\n",
        "00:00:02.00,00:00:03.00\n", "Second\n", "subtitle\n"]

    subWithoutHeader = ["0\n", "00:00:00,000 --> 00:00:01,000\n", "First subtitle\n",
        "\n", "1\n", "00:00:02,000 --> 00:00:03,000\n", "Second\n", "subtitle\n"]

    def setUp(self):
        self.subs = SubManager()
        self.subs.append(Subtitle(FrameTime(25.0, frames=0), FrameTime(25.0, frames=25), "First subtitle"))
        self.subs.append(Subtitle(FrameTime(25.0, frames=50), FrameTime(25.0, frames=75), "Second{gsp_nl}subtitle"))
        self.subs.header().add("title", "Subtitle")
        self.subs.header().add("author", "Subtitle author")

        self.c = SubConverter()

    def test_convertReturnsTheSameSubForSubtitleWithHeader(self):
        result = self.c.convert(SubViewer, self.subs)
        # Comparing bare strings has several benefits: it produces better visual output when
        # something goes wrong. Also, SubConvert produces lists that arelogically equal to the
        # input but differ somehow (e.g. newlines aren't stored in individual list elements but
        # together with subtitle)
        self.assertEqual(''.join(self.subWithHeader).strip(), ''.join(result).strip())

    def test_convertReturnsTheSameSubForSubtitleWithoutHeader(self):
        result = self.c.convert(SubRip, self.subs)
        self.assertEqual(''.join(self.subWithoutHeader).strip(), ''.join(result).strip())

    def test_convertFromHeaderlessToSubtitleWithHeader(self):
        self.subs.header().clear()
        result = self.c.convert(SubViewer, self.subs)
        # Note: this requires the knowledge of what SubViewer converter default header values are.
        # They may change at times...
        compSubWithHeader = ["[INFORMATION]\n", "[TITLE]\n", "[AUTHOR]\n",
            "[SOURCE]\n","[PRG]SubConvert\n", "[FILEPATH]\n", "[DELAY]0\n", "[CD TRACK]0\n",
            "[COMMENT]Converted to subviewer format with SubConvert\n", "[END INFORMATION]\n",
            "[SUBTITLE]\n", "[COLF]&HFFFFFF,[STYLE]no,[SIZE]24,[FONT]Tahoma\n",
            "00:00:00.00,00:00:01.00\n", "First subtitle\n", "\n",
            "00:00:02.00,00:00:03.00\n", "Second\n", "subtitle\n"]
        self.assertEqual(''.join(compSubWithHeader).strip(), ''.join(result).strip())

    def test_convertSubtitleWithHeaderToHeaderless(self):
        result = self.c.convert(SubRip, self.subs)
        self.assertEqual(''.join(self.subWithoutHeader).strip(), ''.join(result).strip())
Ejemplo n.º 2
0
    def run(self):
        try:
            outputFormat = self.getOutputFormat()
            converter = SubConverter()

            if len(self._args.files) == 0:
                log.warning(_("No files selected."))

            if self._args.autoFps:
                log.warning(_("-A, --auto-fps switches are deprecated."))
                log.warning(_("  note: FPS is now automatically fetched whenever it's suitable."))

            for filePath in self._args.files:
                log.info(_("Starting a job for file: %s") % filePath)
                try:
                    subFile = File(filePath)
                except IOError:
                    log.warning( _("File '%s' doesn't exist. Skipping...") % filePath)
                    continue

                data = self.createSubData(subFile, outputFormat)

                if data is not None:
                    convertedSubtitles = converter.convert(data.outputFormat, data.subtitles)
                    outputFilePath = self.getOutputFilePath(subFile, data.outputFormat.EXTENSION)
                    self.writeSubtitles(convertedSubtitles, outputFilePath, data.outputEncoding)

        except SubException as msg:
            log.debug(_("Unhandled Subconvert exception occured:"))
            log.critical(str(msg))
            return 127

        return 0
Ejemplo n.º 3
0
    def _writeFile(self, filePath, newFilePath=None):
        if newFilePath is None:
            newFilePath = filePath

        data = self._subtitleData.data(filePath)
        converter = SubConverter()
        content = converter.convert(data.outputFormat, data.subtitles)

        if File.exists(newFilePath):
            file_ = File(newFilePath)
            file_.overwrite(content, data.outputEncoding)
        else:
            File.write(newFilePath, content, data.outputEncoding)
        self._subtitleData.setCleanState(filePath)
        self.__updateMenuItemsState()