|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +from typing import List |
| 7 | +from pymongo import MongoClient |
| 8 | +import click |
| 9 | +from ted_sws import config |
| 10 | + |
| 11 | +from ted_sws.core.adapters.cmd_runner import CmdRunner as BaseCmdRunner, DEFAULT_MAPPINGS_PATH, DEFAULT_OUTPUT_PATH |
| 12 | +from ted_sws.core.model.manifestation import XMLManifestation |
| 13 | +from ted_sws.core.model.notice import Notice |
| 14 | +from ted_sws.data_manager.adapters.mapping_suite_repository import MappingSuiteRepositoryInFileSystem |
| 15 | +from ted_sws.event_manager.adapters.logger import LOG_INFO_TEXT |
| 16 | +from ted_sws.mapping_suite_processor.entrypoints.cli import CONCEPTUAL_MAPPINGS_FILE_TEMPLATE |
| 17 | +from ted_sws.notice_validator.adapters.xpath_coverage_runner import CoverageRunner |
| 18 | +from ted_sws.notice_validator.services.xpath_coverage_runner import coverage_notice_xpath_report, \ |
| 19 | + xpath_coverage_html_report, xpath_coverage_json_report |
| 20 | + |
| 21 | +OUTPUT_FOLDER = '{mappings_path}/{mapping_suite_id}/' + DEFAULT_OUTPUT_PATH |
| 22 | +DEFAULT_TEST_SUITE_REPORT_FOLDER = "test_suite_report" |
| 23 | +REPORT_FILE = "xpath_cov_{id}" |
| 24 | +CMD_NAME = "CMD_XPATH_COVERAGE_RUNNER" |
| 25 | + |
| 26 | +""" |
| 27 | +USAGE: |
| 28 | +# xpath_coverage_runner --help |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +class CmdRunner(BaseCmdRunner): |
| 33 | + """ |
| 34 | + Keeps the logic to be used by Coverage Runner |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + mapping_suite_id, |
| 40 | + conceptual_mappings_file, |
| 41 | + mappings_path, |
| 42 | + xslt_transformer |
| 43 | + ): |
| 44 | + super().__init__(name=CMD_NAME) |
| 45 | + self.mapping_suite_id = mapping_suite_id |
| 46 | + self.mappings_path = mappings_path |
| 47 | + self.conceptual_mappings_file_path = Path(os.path.realpath(conceptual_mappings_file)) |
| 48 | + self.xslt_transformer = xslt_transformer |
| 49 | + self.output_folder = OUTPUT_FOLDER.format(mappings_path=self.mappings_path, |
| 50 | + mapping_suite_id=self.mapping_suite_id) |
| 51 | + |
| 52 | + if not self.conceptual_mappings_file_path.is_file(): |
| 53 | + error_msg = f"No such Conceptual Mappings file :: [{conceptual_mappings_file}]" |
| 54 | + self.log_failed_msg(error_msg) |
| 55 | + raise FileNotFoundError(error_msg) |
| 56 | + |
| 57 | + repository_path = Path(self.mappings_path) |
| 58 | + |
| 59 | + mapping_suite_repository = MappingSuiteRepositoryInFileSystem(repository_path=repository_path) |
| 60 | + self.mapping_suite = mapping_suite_repository.get(reference=self.mapping_suite_id) |
| 61 | + self.coverage_runner = CoverageRunner(mapping_suite_id=self.mapping_suite_id, |
| 62 | + conceptual_mappings_file_path=self.conceptual_mappings_file_path) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def save_json_report(cls, output_path, json_report: dict): |
| 66 | + with open(output_path, "w+") as f: |
| 67 | + json.dump(json_report, f, indent=4) |
| 68 | + f.close() |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def save_html_report(cls, output_path, html_report: str): |
| 72 | + with open(output_path, "w+") as f: |
| 73 | + f.write(html_report) |
| 74 | + f.close() |
| 75 | + |
| 76 | + def coverage_report(self, notices: List[Notice], output_path: Path, label: str): |
| 77 | + self.log("Generating coverage report for " + LOG_INFO_TEXT.format(label) + " ... ") |
| 78 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 79 | + report = coverage_notice_xpath_report(notices, |
| 80 | + self.mapping_suite_id, |
| 81 | + self.conceptual_mappings_file_path, |
| 82 | + self.coverage_runner, |
| 83 | + self.xslt_transformer) |
| 84 | + self.save_json_report(Path(str(output_path) + ".json"), xpath_coverage_json_report(report)) |
| 85 | + self.save_html_report(Path(str(output_path) + ".html"), xpath_coverage_html_report(report)) |
| 86 | + |
| 87 | + def run_cmd(self): |
| 88 | + output_path = Path(self.output_folder) |
| 89 | + notices: List[Notice] = [] |
| 90 | + for data in self.mapping_suite.transformation_test_data.test_data: |
| 91 | + notice: Notice = Notice(ted_id=Path(data.file_name).stem, |
| 92 | + xml_manifestation=XMLManifestation(object_data=data.file_content)) |
| 93 | + report_file = REPORT_FILE.format(id=notice.ted_id) |
| 94 | + report_path = output_path / notice.ted_id / DEFAULT_TEST_SUITE_REPORT_FOLDER / report_file |
| 95 | + self.coverage_report(notices=[notice], output_path=report_path, label=notice.ted_id) |
| 96 | + notices.append(notice) |
| 97 | + |
| 98 | + self.coverage_report(notices=notices, output_path=output_path / REPORT_FILE.format(id=self.mapping_suite_id), |
| 99 | + label='MappingSuite[' + self.mapping_suite_id + ']') |
| 100 | + |
| 101 | + return self.run_cmd_result() |
| 102 | + |
| 103 | + |
| 104 | +def run(mapping_suite_id=None, opt_conceptual_mappings_file=None, opt_mappings_folder=DEFAULT_MAPPINGS_PATH, |
| 105 | + xslt_transformer=None): |
| 106 | + if opt_conceptual_mappings_file: |
| 107 | + conceptual_mappings_file = opt_conceptual_mappings_file |
| 108 | + else: |
| 109 | + conceptual_mappings_file = CONCEPTUAL_MAPPINGS_FILE_TEMPLATE.format( |
| 110 | + mappings_path=opt_mappings_folder, |
| 111 | + mapping_suite_id=mapping_suite_id |
| 112 | + ) |
| 113 | + |
| 114 | + cmd = CmdRunner( |
| 115 | + mapping_suite_id=mapping_suite_id, |
| 116 | + conceptual_mappings_file=conceptual_mappings_file, |
| 117 | + mappings_path=opt_mappings_folder, |
| 118 | + xslt_transformer=xslt_transformer |
| 119 | + ) |
| 120 | + cmd.run() |
| 121 | + |
| 122 | + |
| 123 | +@click.command() |
| 124 | +@click.argument('mapping-suite-id', nargs=1, required=False) |
| 125 | +@click.option('-n', '--opt-conceptual-mappings-file', help="Use to overwrite default INPUT") |
| 126 | +@click.option('-m', '--opt-mappings-folder', default=DEFAULT_MAPPINGS_PATH) |
| 127 | +def main(mapping_suite_id, opt_conceptual_mappings_file, opt_mappings_folder): |
| 128 | + """ |
| 129 | + Generates Validation Summary for Notices |
| 130 | + """ |
| 131 | + run(mapping_suite_id, opt_conceptual_mappings_file, opt_mappings_folder) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
0 commit comments