def validate_article_xml(file_xml_path, print_error=True):

    result = {}
    logger.debug(file_xml_path)
    try:
        xmlvalidator = XMLValidator.parse(file_xml_path)
        if config.get("VALIDATE_ALL") == "TRUE":
            is_valid, errors = xmlvalidator.validate_all()
        else:
            is_valid, errors = xmlvalidator.validate()
    except (exceptions.XMLSPSVersionError, etree.LxmlError) as e:
        result[str(e)] = {
            "count": 1,
            "lineno": [1],
            "message": [str(e)],
            "filename": {file_xml_path},
        }
        return result

    if not is_valid:
        for error in errors:
            if print_error:
                logger.error("%s - %s - %s", error.level, error.line,
                             error.message)

            message = error.message[:80]
            data = {
                "count": 1,
                "lineno": [error.line],
                "message": [error.message],
                "filename": {file_xml_path},
            }
            dicts.merge(result, message, data)

    return result
def validate_article_ALLxml(move_to_processed_source=False,
                            move_to_valid_xml=False):
    logger.info("Iniciando Validação dos xmls")
    list_files_xmls = files.xml_files_list(config.get("CONVERSION_PATH"))

    success_path = config.get("VALID_XML_PATH")
    errors_path = config.get("XML_ERRORS_PATH")
    func = shutil.move if move_to_valid_xml else shutil.copyfile

    result = {}
    for file_xml in tqdm(list_files_xmls):

        filename, _ = files.extract_filename_ext_by_path(file_xml)
        converted_file = os.path.join(config.get("CONVERSION_PATH"), file_xml)

        try:
            errors = validate_article_xml(converted_file, False)

            for k_error, v_error in errors.items():
                dicts.merge(result, k_error, v_error)

            if errors_path:
                manage_error_file(
                    errors,
                    os.path.join(errors_path, "%s.err" % filename),
                    converted_file,
                )

            if not errors:
                if success_path:
                    func(converted_file, os.path.join(success_path, file_xml))

                if move_to_processed_source:
                    files.move_xml_to(
                        "%s.xml" % filename,
                        config.get("SOURCE_PATH"),
                        config.get("PROCESSED_SOURCE_PATH"),
                    )

        except Exception as ex:
            logger.exception(ex)
            raise

    analase = sorted(result.items(), key=lambda x: x[1]["count"], reverse=True)
    for k_result, v_result in analase:
        logger.error("%s - %s", k_result, v_result["count"])
Example #3
0
    def test_merge(self):
        result = {"1": {"count": 2, "files": ("a", "b")}}

        data = {"count": 1, "files": ("c", "d")}
        dicts.merge(result, "1", data)
        self.assertEqual(result["1"]["count"], 3)