def main(): """ Entry point """ if len(sys.argv) < 3: usage() return input_file_path = sys.argv[1] output_file_path = sys.argv[2] parameters = {} for i in range(3, len(sys.argv)): args = sys.argv[i].split("=") if len(args) == 2: key, value = args if key in [ gc.PPN_SYNCMAP_LANGUAGE, gc.PPN_TASK_OS_FILE_SMIL_AUDIO_REF, gc.PPN_TASK_OS_FILE_SMIL_PAGE_REF, "input_format", "output_format", "text_file" ]: parameters[key] = value if "input_format" in parameters: input_sm_format = parameters["input_format"] else: input_sm_format = gf.file_extension(input_file_path) if input_sm_format not in SyncMapFormat.ALLOWED_VALUES: print "[ERRO] Input sync map format '%s' is not allowed" % (input_sm_format) print "[INFO] Allowed formats: %s" % (" ".join(SyncMapFormat.ALLOWED_VALUES)) return if "output_format" in parameters: output_sm_format = parameters["output_format"] else: output_sm_format = gf.file_extension(output_file_path) if output_sm_format not in SyncMapFormat.ALLOWED_VALUES: print "[ERRO] Output sync map format '%s' is not allowed" % (output_sm_format) print "[INFO] Allowed sync map formats: %s" % (" ".join(SyncMapFormat.ALLOWED_VALUES)) return try: print "[INFO] Reading sync map in %s format from file %s ..." % (input_sm_format, input_file_path) syncmap = SyncMap() result = syncmap.read(input_sm_format, input_file_path, parameters) if not result: print "[ERRO] Error while reading sync map" return print "[INFO] Reading sync map in %s format from file %s ... done" % (input_sm_format, input_file_path) print "[INFO] Read %s sync map fragments" % (len(syncmap)) print "[INFO] Writing sync map in %s format to file %s ..." % (output_sm_format, output_file_path) result = syncmap.write(output_sm_format, output_file_path, parameters) if not result: print "[ERRO] Error while writing sync map (forgot required arguments?)" return print "[INFO] Writing sync map in %s format to file %s ... done" % (output_sm_format, output_file_path) except Exception as e: print "[ERRO] Uncaught exception %s" % (str(e)) return
def test_write_not_existing_path(self): syn = SyncMap() with self.assertRaises(OSError): syn.write(SyncMapFormat.SRT, self.NOT_WRITEABLE_SRT)
def test_write_none(self): syn = SyncMap() with self.assertRaises(ValueError): syn.write(None, self.NOT_EXISTING_SRT)
def test_write_invalid_format(self): syn = SyncMap() with self.assertRaises(ValueError): syn.write("foo", self.NOT_EXISTING_SRT)
def perform_command(self): """ Perform command and return the appropriate exit code. :rtype: int """ if len(self.actual_arguments) < 2: return self.print_help() input_file_path = self.actual_arguments[0] output_file_path = self.actual_arguments[1] output_html = self.has_option(u"--output-html") if not self.check_input_file(input_file_path): return self.ERROR_EXIT_CODE input_sm_format = self.has_option_with_value(u"--input-format") if input_sm_format is None: input_sm_format = gf.file_extension(input_file_path) if not self.check_format(input_sm_format): return self.ERROR_EXIT_CODE if not self.check_output_file(output_file_path): return self.ERROR_EXIT_CODE if output_html: if len(self.actual_arguments) < 3: return self.print_help() audio_file_path = self.actual_arguments[2] if not self.check_input_file(audio_file_path): return self.ERROR_EXIT_CODE else: output_sm_format = self.has_option_with_value(u"--output-format") if output_sm_format is None: output_sm_format = gf.file_extension(output_file_path) if not self.check_format(output_sm_format): return self.ERROR_EXIT_CODE # TODO add a way to specify a text file for input formats like SMIL # that do not carry the source text language = self.has_option_with_value(u"--language") audio_ref = self.has_option_with_value(u"--audio-ref") page_ref = self.has_option_with_value(u"--page-ref") parameters = { gc.PPN_SYNCMAP_LANGUAGE: language, gc.PPN_TASK_OS_FILE_SMIL_AUDIO_REF: audio_ref, gc.PPN_TASK_OS_FILE_SMIL_PAGE_REF: page_ref } try: self.print_info(u"Reading sync map in '%s' format from file '%s'" % (input_sm_format, input_file_path)) self.print_info(u"Reading sync map...") syncmap = SyncMap(logger=self.logger) syncmap.read(input_sm_format, input_file_path, parameters) self.print_info(u"Reading sync map... done") self.print_info(u"Read %d sync map fragments" % (len(syncmap))) except Exception as exc: self.print_error( u"An unexpected error occurred while reading the input sync map:" ) self.print_error(u"%s" % (exc)) return self.ERROR_EXIT_CODE if output_html: try: self.print_info(u"Writing HTML file...") syncmap.output_html_for_tuning(audio_file_path, output_file_path, parameters) self.print_info(u"Writing HTML file... done") self.print_success(u"Created HTML file '%s'" % (output_file_path)) return self.NO_ERROR_EXIT_CODE except Exception as exc: self.print_error( u"An unexpected error occurred while writing the output HTML file:" ) self.print_error(u"%s" % (exc)) else: try: self.print_info(u"Writing sync map...") syncmap.write(output_sm_format, output_file_path, parameters) self.print_info(u"Writing sync map... done") self.print_success(u"Created '%s' sync map file '%s'" % (output_sm_format, output_file_path)) return self.NO_ERROR_EXIT_CODE except Exception as exc: self.print_error( u"An unexpected error occurred while writing the output sync map:" ) self.print_error(u"%s" % (exc)) return self.ERROR_EXIT_CODE
def perform_command(self): """ Perform command and return the appropriate exit code. :rtype: int """ if len(self.actual_arguments) < 2: return self.print_help() input_file_path = self.actual_arguments[0] output_file_path = self.actual_arguments[1] output_html = self.has_option(u"--output-html") if not self.check_input_file(input_file_path): return self.ERROR_EXIT_CODE input_sm_format = self.has_option_with_value(u"--input-format") if input_sm_format is None: input_sm_format = gf.file_extension(input_file_path) if not self.check_format(input_sm_format): return self.ERROR_EXIT_CODE if not self.check_output_file(output_file_path): return self.ERROR_EXIT_CODE if output_html: if len(self.actual_arguments) < 3: return self.print_help() audio_file_path = self.actual_arguments[2] if not self.check_input_file(audio_file_path): return self.ERROR_EXIT_CODE else: output_sm_format = self.has_option_with_value(u"--output-format") if output_sm_format is None: output_sm_format = gf.file_extension(output_file_path) if not self.check_format(output_sm_format): return self.ERROR_EXIT_CODE # TODO add a way to specify a text file for input formats like SMIL # that do not carry the source text language = self.has_option_with_value(u"--language") audio_ref = self.has_option_with_value(u"--audio-ref") page_ref = self.has_option_with_value(u"--page-ref") parameters = { gc.PPN_SYNCMAP_LANGUAGE : language, gc.PPN_TASK_OS_FILE_SMIL_AUDIO_REF : audio_ref, gc.PPN_TASK_OS_FILE_SMIL_PAGE_REF : page_ref } try: self.print_info(u"Reading sync map in '%s' format from file '%s'" % (input_sm_format, input_file_path)) self.print_info(u"Reading sync map...") syncmap = SyncMap(logger=self.logger) syncmap.read(input_sm_format, input_file_path, parameters) self.print_info(u"Reading sync map... done") self.print_info(u"Read %d sync map fragments" % (len(syncmap))) except Exception as exc: self.print_error(u"An unexpected error occurred while reading the input sync map:") self.print_error(u"%s" % (exc)) return self.ERROR_EXIT_CODE if output_html: try: self.print_info(u"Writing HTML file...") syncmap.output_html_for_tuning(audio_file_path, output_file_path, parameters) self.print_info(u"Writing HTML file... done") self.print_success(u"Created HTML file '%s'" % (output_file_path)) return self.NO_ERROR_EXIT_CODE except Exception as exc: self.print_error(u"An unexpected error occurred while writing the output HTML file:") self.print_error(u"%s" % (exc)) else: try: self.print_info(u"Writing sync map...") syncmap.write(output_sm_format, output_file_path, parameters) self.print_info(u"Writing sync map... done") self.print_success(u"Created '%s' sync map file '%s'" % (output_sm_format, output_file_path)) return self.NO_ERROR_EXIT_CODE except Exception as exc: self.print_error(u"An unexpected error occurred while writing the output sync map:") self.print_error(u"%s" % (exc)) return self.ERROR_EXIT_CODE