예제 #1
0
 def test_save_subs_as_stl(self):
     target_file_path = os.path.join(self.__resource_tmp,
                                     "subtitle_converted.stl")
     Undertest.save_subs_as_target_format(
         Undertest.load(self.__stl_file_path).subs, self.__srt_file_path,
         target_file_path)
     with open(target_file_path) as target:
         for j, lt in enumerate(target):
             pass
     target_line_num = j + 1
     self.assertEqual(32, target_line_num)
예제 #2
0
 def test_throw_exception_on_converting_to_unknown_subtitle(self):
     try:
         unknown_file_path = os.path.join(self.__resource_tmp,
                                          "subtitle_test.unknown")
         Path(unknown_file_path).touch()
         Undertest.save_subs_as_target_format(
             Undertest.load(self.__stl_file_path).subs,
             self.__srt_file_path, unknown_file_path)
     except Exception as e:
         self.assertTrue(isinstance(e, UnsupportedFormatException))
     else:
         self.fail("Should have thrown exception")
예제 #3
0
def main():
    if sys.version_info.major != 3:
        print("Cannot find Python 3")
        sys.exit(20)
    try:
        import subaligner
    except ModuleNotFoundError:
        print("Subaligner is not installed")
        sys.exit(20)

    from subaligner._version import __version__
    parser = argparse.ArgumentParser(
        description=
        "Convert a subtitle from input format to output format (v%s)" %
        __version__,
        formatter_class=argparse.RawTextHelpFormatter)
    required_args = parser.add_argument_group("required arguments")
    required_args.add_argument(
        "-i",
        "--input_subtitle_path",
        type=str,
        default="",
        help="File path or URL to the input subtitle file",
        required=True,
    )
    required_args.add_argument(
        "-o",
        "--output_subtitle_path",
        type=str,
        default="",
        help="File path to the output subtitle file",
        required=True,
    )
    parser.add_argument("-d",
                        "--debug",
                        action="store_true",
                        help="Print out debugging information")
    parser.add_argument("-q",
                        "--quiet",
                        action="store_true",
                        help="Switch off logging information")
    parser.add_argument("-ver",
                        "--version",
                        action="version",
                        version=__version__)
    FLAGS, unparsed = parser.parse_known_args()
    if FLAGS.input_subtitle_path == "":
        print("--input_subtitle_path was not passed in")
        sys.exit(21)
    if FLAGS.output_subtitle_path == "":
        print("--output_subtitle_path was not passed in")
        sys.exit(21)

    local_subtitle_path = FLAGS.input_subtitle_path

    from subaligner.logger import Logger
    Logger.VERBOSE = FLAGS.debug
    Logger.QUIET = FLAGS.quiet
    from subaligner.subtitle import Subtitle
    from subaligner.exception import UnsupportedFormatException, TerminalException
    from subaligner.utils import Utils

    try:
        if FLAGS.input_subtitle_path.lower().startswith("http"):
            _, local_subtitle_path = tempfile.mkstemp()
            _, subtitle_file_extension = os.path.splitext(
                FLAGS.input_subtitle_path.lower())
            local_subtitle_path = "{}{}".format(local_subtitle_path,
                                                subtitle_file_extension)
            Utils.download_file(FLAGS.input_subtitle_path, local_subtitle_path)

        subtitle = Subtitle.load(local_subtitle_path)
        Subtitle.save_subs_as_target_format(subtitle.subs, local_subtitle_path,
                                            FLAGS.output_subtitle_path)
        print("Subtitle converted and saved to: {}".format(
            FLAGS.output_subtitle_path))
    except UnsupportedFormatException as e:
        print("{}\n{}".format(
            str(e), "".join(traceback.format_stack()) if FLAGS.debug else ""))
        _remove_tmp_files(FLAGS, local_subtitle_path)
        sys.exit(23)
    except TerminalException as e:
        print("{}\n{}".format(
            str(e), "".join(traceback.format_stack()) if FLAGS.debug else ""))
        _remove_tmp_files(FLAGS, local_subtitle_path)
        sys.exit(24)
    except Exception as e:
        print("{}\n{}".format(
            str(e), "".join(traceback.format_stack()) if FLAGS.debug else ""))
        _remove_tmp_files(FLAGS, local_subtitle_path)
        sys.exit(1)
    else:
        _remove_tmp_files(FLAGS, local_subtitle_path)
        sys.exit(0)