def check_container(self, container_path, container_format=None, config_string=None): """ Check whether the given container is well-formed. :param string container_path: the path of the container to be checked :param container_format: the format of the container :type container_format: :class:`~aeneas.container.ContainerFormat` :param string config_string: the configuration string generated by the wizard :rtype: :class:`~aeneas.validator.ValidatorResult` """ self.log([u"Checking container '%s'", container_path]) self.result = ValidatorResult() if self._are_safety_checks_disabled(u"check_container"): return self.result if not (gf.file_exists(container_path) or gf.directory_exists(container_path)): self._failed(u"Container '%s' not found." % container_path) return self.result container = Container(container_path, container_format) try: self.log(u"Checking container has config file") if config_string is not None: self.log(u"Container with config string from wizard") self.check_config_txt(config_string, is_config_string=True) elif container.has_config_xml: self.log(u"Container has XML config file") contents = container.read_entry(container.entry_config_xml) if contents is None: self._failed(u"Unable to read the contents of XML config file.") return self.result self.check_config_xml(contents) elif container.has_config_txt: self.log(u"Container has TXT config file") contents = container.read_entry(container.entry_config_txt) if contents is None: self._failed(u"Unable to read the contents of TXT config file.") return self.result self.check_config_txt(contents, is_config_string=False) else: self._failed(u"Container does not have a TXT or XML configuration file.") self.log(u"Checking we have a valid job in the container") if not self.result.passed: return self.result self.log(u"Analyze the contents of the container") analyzer = AnalyzeContainer(container) if config_string is not None: job = analyzer.analyze(config_string=config_string) else: job = analyzer.analyze() self._check_analyzed_job(job, container) except OSError: self._failed(u"Unable to read the contents of the container.") return self.result
def test_wizard_analyze_valid(self): f = self.FILES[0] analyzer = AnalyzeContainer( Container(gf.absolute_path(f["path"], __file__))) job = analyzer.analyze(config_string=self.CONFIG_STRING) self.assertIsNotNone(job) self.assertEqual(len(job), f["length"])
def _check_container_with_txt_config_string( self, container, config_string=None ): """ Check whether the given container with TXT (INI-like) configuration is well-formed. :param container: the container :type container: Container :param config_string: the TXT (INI-like) configuration string :type config_string: string :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log("Checking container with TXT config file") result = ValidatorResult() # if no config string was passed, try to read it from container if config_string is None: self._log("Trying to read config file from container") config_in_container = True config_contents = container.read_entry(container.entry_config_txt) if config_contents is None: msg = "Unable to read the contents of TXT config file." result.passed = False result.add_error(msg) self._log(msg) return result self._log("Config file found in container") else: self._log("Config string passed as parameter") config_contents = config_string config_in_container = False # check the txt config contents or string result = self.check_contents_txt_config_file( config_contents, config_in_container ) if not result.passed: self._log("Failed") return result # analyze the container self._log("Analyze the contents of the container") analyzer = AnalyzeContainer(container) if config_in_container: job = analyzer.analyze() else: job = analyzer.analyze_from_wizard(config_string) self._check_analyzed_job(job, container, result) # return result self._log(["Checking container with TXT config file: returning %s", result.passed]) return result
def _check_container_with_txt_config_string(self, container, config_string=None): """ Check whether the given container with TXT (INI-like) configuration is well-formed. :param container: the container :type container: Container :param config_string: the TXT (INI-like) configuration string :type config_string: string :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log("Checking container with TXT config file") result = ValidatorResult() # if no config string was passed, try to read it from container if config_string is None: self._log("Trying to read config file from container") config_in_container = True config_contents = container.read_entry(container.entry_config_txt) if config_contents is None: msg = "Unable to read the contents of TXT config file." result.passed = False result.add_error(msg) self._log(msg) return result self._log("Config file found in container") else: self._log("Config string passed as parameter") config_contents = config_string config_in_container = False # check the txt config contents or string result = self.check_contents_txt_config_file(config_contents, config_in_container) if not result.passed: self._log("Failed") return result # analyze the container self._log("Analyze the contents of the container") analyzer = AnalyzeContainer(container) if config_in_container: job = analyzer.analyze() else: job = analyzer.analyze_from_wizard(config_string) self._check_analyzed_job(job, container, result) # return result self._log([ "Checking container with TXT config file: returning %s", result.passed ]) return result
def _check_container_with_xml_config( self, container, config_contents=None ): """ Check whether the given container with XML configuration is well-formed. :param container: the container :type container: Container :param config_contents: the contents of the XML config file :type config_contents: string :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log("Checking container with XML config file") result = ValidatorResult() # if no config contents was passed, try to read them from container if config_contents is None: self._log("Trying to read config file from container") config_contents = container.read_entry(container.entry_config_xml) if config_contents is None: msg = "Unable to read the contents of XML config file." result.passed = False result.add_error(msg) self._log(msg) return result # check the txt config contents or string result = self.check_contents_xml_config_file(config_contents) if not result.passed: self._log("Failed") return result # analyze the container self._log("Analyze the contents of the container") analyzer = AnalyzeContainer(container) job = analyzer.analyze() self._check_analyzed_job(job, container, result) # return result self._log(["Checking container: returning %s", result.passed]) return result
def _check_container_with_xml_config(self, container, config_contents=None): """ Check whether the given container with XML configuration is well-formed. :param container: the container :type container: Container :param config_contents: the contents of the XML config file :type config_contents: string :rtype: :class:`aeneas.validator.ValidatorResult` """ self._log("Checking container with XML config file") result = ValidatorResult() # if no config contents was passed, try to read them from container if config_contents is None: self._log("Trying to read config file from container") config_contents = container.read_entry(container.entry_config_xml) if config_contents is None: msg = "Unable to read the contents of XML config file." result.passed = False result.add_error(msg) self._log(msg) return result # check the txt config contents or string result = self.check_contents_xml_config_file(config_contents) if not result.passed: self._log("Failed") return result # analyze the container self._log("Analyze the contents of the container") analyzer = AnalyzeContainer(container) job = analyzer.analyze() self._check_analyzed_job(job, container, result) # return result self._log(["Checking container: returning %s", result.passed]) return result
def test_analyze(self): for f in self.FILES: analyzer = AnalyzeContainer(Container(gf.absolute_path(f["path"], __file__))) job = analyzer.analyze() self.assertEqual(len(job), f["length"])
def test_analyze_empty_container(self): for f in self.EMPTY_CONTAINERS: analyzer = AnalyzeContainer(Container(f)) job = analyzer.analyze() self.assertIsNone(job)
def test_container_not_existing(self): analyzer = AnalyzeContainer(Container(self.NOT_EXISTING_PATH)) job = analyzer.analyze() self.assertIsNone(job)
def test_check_container_xml_03(self): container_path = get_abs_path("res/validator/job_xml_config_not_root_nested") logger = Logger() analyzer = AnalyzeContainer(Container(container_path), logger=logger) job = analyzer.analyze() self.assertEqual(len(job), 3)
def load_job_from_container(self, container_path, config_string=None): """ Load the job from the given :class:`aeneas.container.Container` object. If ``config_string`` is ``None``, the container must contain a configuration file; otherwise use the provided config string (i.e., the wizard case). :param string container_path: the path to the input container :param string config_string: the configuration string (from wizard) :raises: :class:`~aeneas.executejob.ExecuteJobInputError`: if the given container does not contain a valid :class:`~aeneas.job.Job` """ self.log(u"Loading job from container...") # create working directory where the input container # will be decompressed self.working_directory = gf.tmp_directory(root=self.rconf[RuntimeConfiguration.TMP_PATH]) self.log([u"Created working directory '%s'", self.working_directory]) try: self.log(u"Decompressing input container...") input_container = Container(container_path, logger=self.logger) input_container.decompress(self.working_directory) self.log(u"Decompressing input container... done") except Exception as exc: self.clean() self.log_exc(u"Unable to decompress container '%s': %s" % (container_path, exc), None, True, ExecuteJobInputError) try: self.log(u"Creating job from working directory...") working_container = Container( self.working_directory, logger=self.logger ) analyzer = AnalyzeContainer(working_container, logger=self.logger) self.job = analyzer.analyze(config_string=config_string) self.log(u"Creating job from working directory... done") except Exception as exc: self.clean() self.log_exc(u"Unable to analyze container '%s': %s" % (container_path, exc), None, True, ExecuteJobInputError) if self.job is None: self.log_exc(u"The container '%s' does not contain a valid Job" % (container_path), None, True, ExecuteJobInputError) try: # set absolute path for text file and audio file # for each task in the job self.log(u"Setting absolute paths for tasks...") for task in self.job.tasks: task.text_file_path_absolute = gf.norm_join( self.working_directory, task.text_file_path ) task.audio_file_path_absolute = gf.norm_join( self.working_directory, task.audio_file_path ) self.log(u"Setting absolute paths for tasks... done") self.log(u"Loading job from container: succeeded") except Exception as exc: self.clean() self.log_exc(u"Error while setting absolute paths for tasks", exc, True, ExecuteJobInputError)
def test_wizard_analyze_valid(self): f = self.FILES[0] analyzer = AnalyzeContainer(Container(gf.absolute_path(f["path"], __file__))) job = analyzer.analyze(config_string=self.CONFIG_STRING) self.assertIsNotNone(job) self.assertEqual(len(job), f["length"])
def load_job_from_container(self, container_path, config_string=None): """ Load the job from the given :class:`aeneas.container.Container` object. If ``config_string`` is ``None``, the container must contain a configuration file; otherwise use the provided config string (i.e., the wizard case). :param string container_path: the path to the input container :param string config_string: the configuration string (from wizard) :raises: :class:`~aeneas.executejob.ExecuteJobInputError`: if the given container does not contain a valid :class:`~aeneas.job.Job` """ self.log(u"Loading job from container...") # create working directory where the input container # will be decompressed self.working_directory = gf.tmp_directory( root=self.rconf[RuntimeConfiguration.TMP_PATH]) self.log([u"Created working directory '%s'", self.working_directory]) try: self.log(u"Decompressing input container...") input_container = Container(container_path, logger=self.logger) input_container.decompress(self.working_directory) self.log(u"Decompressing input container... done") except Exception as exc: self.clean() self.log_exc( u"Unable to decompress container '%s': %s" % (container_path, exc), None, True, ExecuteJobInputError) try: self.log(u"Creating job from working directory...") working_container = Container(self.working_directory, logger=self.logger) analyzer = AnalyzeContainer(working_container, logger=self.logger) self.job = analyzer.analyze(config_string=config_string) self.log(u"Creating job from working directory... done") except Exception as exc: self.clean() self.log_exc( u"Unable to analyze container '%s': %s" % (container_path, exc), None, True, ExecuteJobInputError) if self.job is None: self.log_exc( u"The container '%s' does not contain a valid Job" % (container_path), None, True, ExecuteJobInputError) try: # set absolute path for text file and audio file # for each task in the job self.log(u"Setting absolute paths for tasks...") for task in self.job.tasks: task.text_file_path_absolute = gf.norm_join( self.working_directory, task.text_file_path) task.audio_file_path_absolute = gf.norm_join( self.working_directory, task.audio_file_path) self.log(u"Setting absolute paths for tasks... done") self.log(u"Loading job from container: succeeded") except Exception as exc: self.clean() self.log_exc(u"Error while setting absolute paths for tasks", exc, True, ExecuteJobInputError)
def test_wizard_container_not_existing(self): analyzer = AnalyzeContainer(Container(self.NOT_EXISTING_PATH)) job = analyzer.analyze(config_string=u"foo") self.assertIsNone(job)
def test_wizard_analyze_empty_container(self): for f in self.EMPTY_CONTAINERS: analyzer = AnalyzeContainer(Container(f)) job = analyzer.analyze(config_string=u"foo") self.assertIsNone(job)
def test_analyze(self): for f in self.FILES: analyzer = AnalyzeContainer( Container(gf.absolute_path(f["path"], __file__))) job = analyzer.analyze() self.assertEqual(len(job), f["length"])
def load_job_from_container(self, container_path, config_string=None): """ Validate the given container, and, if it is well formed, load the job from it. If ``config_string`` is ``None``, the container must contain a configuration file; otherwise use the provided config string (i.e., the wizard case). Return ``True`` if the job has been loaded successfully, ``False`` otherwise. :param container_path: the path to the input container :type container_path: string (path) :param config_string: the configuration string (from wizard) :type config_string: string :rtype: bool """ self._log("Loading job from container...") # validate container self._log("Validating container...") validator = Validator(logger=self.logger) if config_string is None: validator_result = validator.check_container(container_path) else: validator_result = validator.check_container_from_wizard( container_path, config_string) if not validator_result.passed: self._log("Validating container: failed") self._log("Loading job from container: failed") return False self._log("Validating container: succeeded") try: # create working directory where the input container # will be decompressed self.working_directory = tempfile.mkdtemp(dir=gf.custom_tmp_dir()) self._log( ["Created working directory '%s'", self.working_directory]) # decompress self._log("Decompressing input container...") input_container = Container(container_path, logger=self.logger) input_container.decompress(self.working_directory) self._log("Decompressing input container... done") # create job from the working directory self._log("Creating job from working directory...") working_container = Container(self.working_directory, logger=self.logger) analyzer = AnalyzeContainer(working_container, logger=self.logger) if config_string is None: self.job = analyzer.analyze() else: self.job = analyzer.analyze_from_wizard(config_string) self._log("Creating job from working directory... done") # set absolute path for text file and audio file # for each task in the job self._log("Setting absolute paths for tasks...") for task in self.job.tasks: task.text_file_path_absolute = gf.norm_join( self.working_directory, task.text_file_path) task.audio_file_path_absolute = gf.norm_join( self.working_directory, task.audio_file_path) self._log("Setting absolute paths for tasks... done") # return self._log("Loading job from container: succeeded") return True except: # failure: clean and return self.clean() self._log("Loading job from container: failed") return False
def load_job_from_container(self, container_path, config_string=None): """ Validate the given container, and, if it is well formed, load the job from it. If ``config_string`` is ``None``, the container must contain a configuration file; otherwise use the provided config string (i.e., the wizard case). Return ``True`` if the job has been loaded successfully, ``False`` otherwise. :param container_path: the path to the input container :type container_path: string (path) :param config_string: the configuration string (from wizard) :type config_string: string :rtype: bool """ self._log("Loading job from container...") # validate container self._log("Validating container...") validator = Validator(logger=self.logger) if config_string == None: validator_result = validator.check_container(container_path) else: validator_result = validator.check_container_from_wizard( container_path, config_string ) if not validator_result.passed: self._log("Validating container: failed") self._log("Loading job from container: failed") return False self._log("Validating container: succeeded") try: # create working directory where the input container # will be decompressed self.working_directory = tempfile.mkdtemp(dir=gf.custom_tmp_dir()) self._log("Created working directory '%s'" % self.working_directory) # decompress self._log("Decompressing input container...") input_container = Container(container_path, logger=self.logger) input_container.decompress(self.working_directory) self._log("Decompressing input container... done") # create job from the working directory self._log("Creating job from working directory...") working_container = Container( self.working_directory, logger=self.logger ) analyzer = AnalyzeContainer(working_container, logger=self.logger) if config_string == None: self.job = analyzer.analyze() else: self.job = analyzer.analyze_from_wizard(config_string) self._log("Creating job from working directory... done") # set absolute path for text file and audio file # for each task in the job self._log("Setting absolute paths for tasks...") for task in self.job.tasks: task.text_file_path_absolute = gf.norm_join( self.working_directory, task.text_file_path ) task.audio_file_path_absolute = gf.norm_join( self.working_directory, task.audio_file_path ) self._log("Setting absolute paths for tasks... done") # return self._log("Loading job from container: succeeded") return True except: # failure: clean and return self.clean() self._log("Loading job from container: failed") return False
def test_analyze(self): for f in self.FILES: analyzer = AnalyzeContainer(Container(get_abs_path(f["path"]))) job = analyzer.analyze() self.assertEqual(len(job), f["length"])
def test_check_container_txt_paged_03(self): container_path = get_abs_path("res/validator/job_txt_config_paged_3") logger = Logger() analyzer = AnalyzeContainer(Container(container_path), logger=logger) job = analyzer.analyze() self.assertEqual(len(job), 3)