def check_config_txt(self, contents, is_config_string=False): """ Check whether the given TXT config file contents (if ``is_config_string`` is ``False``) or TXT config string (if ``is_config_string`` is ``True``) is well-formed and it has all the required parameters. :param string contents: the TXT config file contents or TXT config string :param bool is_config_string: if ``True``, contents is a config string :rtype: :class:`~aeneas.validator.ValidatorResult` """ self.log(u"Checking contents TXT config file") self.result = ValidatorResult() if self._are_safety_checks_disabled(u"check_config_txt"): return self.result is_bstring = gf.is_bytes(contents) if is_bstring: self.log(u"Checking that contents is well formed") self.check_raw_string(contents, is_bstring=True) if not self.result.passed: return self.result contents = gf.safe_unicode(contents) if not is_config_string: self.log(u"Converting file contents to config string") contents = gf.config_txt_to_string(contents) self.log(u"Checking required parameters") required_parameters = self.TXT_REQUIRED_PARAMETERS parameters = gf.config_string_to_dict(contents, self.result) self._check_required_parameters(required_parameters, parameters) self.log([u"Checking contents: returning %s", self.result.passed]) return self.result
def test_is_bytes(self): tests = [ (None, False), (b"", True), (b"foo", True), (b"fo\xff", True), (u"foo", False), ([], False), ([b"foo"], False), ({b"foo": b"baz"}, False), ] if gf.PY2: tests.extend([ ("", True), ("foo", True), ("fox99", True), ]) else: tests.extend([ ("", False), ("foo", False), ("fox99", False), ]) for test in tests: self.assertEqual(gf.is_bytes(test[0]), test[1])
def test_is_bytes(self): tests = [ (None, False), (b"", True), (b"foo", True), (b"fo\xff", True), (u"foo", False), ([], False), ([b"foo"], False), ({ b"foo": b"baz" }, False), ] if gf.PY2: tests.extend([ ("", True), ("foo", True), ("fox99", True), ]) else: tests.extend([ ("", False), ("foo", False), ("fox99", False), ]) for test in tests: self.assertEqual(gf.is_bytes(test[0]), test[1])
def test_read_file_bytes(self): handler, path = gf.tmp_file() with io.open(path, "w", encoding="utf-8") as tmp_file: tmp_file.write(u"Foo bar") contents = gf.read_file_bytes(path) self.assertTrue(gf.is_bytes(contents)) self.assertEqual(len(contents), 7) gf.delete_file(handler, path)
def _check_utf8_encoding(self, bstring): """ Check whether the given sequence of bytes is properly encoded in UTF-8. :param bytes bstring: the byte string to be checked """ if not gf.is_bytes(bstring): self._failed(u"The given string is not a sequence of bytes") return if not gf.is_utf8_encoded(bstring): self._failed(u"The given string is not encoded in UTF-8.")
def check_configuration_string( self, config_string, is_job=True, external_name=False ): """ Check whether the given job or task configuration string is well-formed (if ``is_bstring`` is ``True``) and it has all the required parameters. :param string config_string: the byte string or Unicode string to be checked :param bool is_job: if ``True``, ``config_string`` is a job config string :param bool external_name: if ``True``, the task name is provided externally, and it is not required to appear in the config string :rtype: :class:`~aeneas.validator.ValidatorResult` """ if is_job: self.log(u"Checking job configuration string") else: self.log(u"Checking task configuration string") self.result = ValidatorResult() if self._are_safety_checks_disabled(u"check_configuration_string"): return self.result if is_job: required_parameters = self.JOB_REQUIRED_PARAMETERS elif external_name: required_parameters = self.TASK_REQUIRED_PARAMETERS_EXTERNAL_NAME else: required_parameters = self.TASK_REQUIRED_PARAMETERS is_bstring = gf.is_bytes(config_string) if is_bstring: self.log(u"Checking that config_string is well formed") self.check_raw_string(config_string, is_bstring=True) if not self.result.passed: return self.result config_string = gf.safe_unicode(config_string) self.log(u"Checking required parameters") parameters = gf.config_string_to_dict(config_string, self.result) self._check_required_parameters(required_parameters, parameters) self.log([u"Checking config_string: returning %s", self.result.passed]) return self.result