|
| 1 | +import sys |
| 2 | + |
| 3 | +from je_auto_control.utils.test_record.record_test_class import test_record |
| 4 | +from je_auto_control.utils.exception.exceptions import HTMLException |
| 5 | +from je_auto_control.utils.exception.exception_tag import html_generate_no_data_tag |
| 6 | +from threading import Lock |
| 7 | + |
| 8 | +lock = Lock() |
| 9 | + |
| 10 | +html_string = \ |
| 11 | + r""" |
| 12 | +<!DOCTYPE html> |
| 13 | +<html lang="en"> |
| 14 | +<head> |
| 15 | + <meta charset="UTF-8"/> |
| 16 | + <title>AutoControl Report</title> |
| 17 | +
|
| 18 | + <style> |
| 19 | + |
| 20 | + body{{ |
| 21 | + font-size: 100%; |
| 22 | + }} |
| 23 | +
|
| 24 | + h1{{ |
| 25 | + font-size: 2em; |
| 26 | + }} |
| 27 | +
|
| 28 | + .main_table {{ |
| 29 | + margin: 0 auto; |
| 30 | + border-collapse: collapse; |
| 31 | + width: 75%; |
| 32 | + font-size: 1.5em; |
| 33 | + }} |
| 34 | +
|
| 35 | + .event_table_head {{ |
| 36 | + border: 3px solid #262626; |
| 37 | + background-color: aqua; |
| 38 | + font-family: "Times New Roman", sans-serif; |
| 39 | + text-align: center; |
| 40 | + }} |
| 41 | + |
| 42 | + .failure_table_head {{ |
| 43 | + border: 3px solid #262626; |
| 44 | + background-color: #f84c5f; |
| 45 | + font-family: "Times New Roman", sans-serif; |
| 46 | + text-align: center; |
| 47 | + }} |
| 48 | +
|
| 49 | + .table_data_field_title {{ |
| 50 | + border: 3px solid #262626; |
| 51 | + padding: 0; |
| 52 | + margin: 0; |
| 53 | + background-color: #dedede; |
| 54 | + font-family: "Times New Roman", sans-serif; |
| 55 | + text-align: center; |
| 56 | + width: 25%; |
| 57 | + }} |
| 58 | +
|
| 59 | + .table_data_field_text {{ |
| 60 | + border: 3px solid #262626; |
| 61 | + padding: 0; |
| 62 | + margin: 0; |
| 63 | + background-color: #dedede; |
| 64 | + font-family: "Times New Roman", sans-serif; |
| 65 | + text-align: left; |
| 66 | + width: 75%; |
| 67 | + }} |
| 68 | +
|
| 69 | + .text {{ |
| 70 | + text-align: center; |
| 71 | + font-family: "Times New Roman", sans-serif; |
| 72 | + }} |
| 73 | + </style> |
| 74 | +</head> |
| 75 | +<body> |
| 76 | +<h1 class="text"> |
| 77 | + Test Report |
| 78 | +</h1> |
| 79 | +{event_table} |
| 80 | +</body> |
| 81 | +</html> |
| 82 | +""".strip() |
| 83 | + |
| 84 | +event_table = \ |
| 85 | + r""" |
| 86 | + <table class="main_table"> |
| 87 | + <thead> |
| 88 | + <tr> |
| 89 | + <th colspan="2" class="{table_head_class}">Test Report</th> |
| 90 | + </tr> |
| 91 | + </thead> |
| 92 | + <tbody> |
| 93 | + <tr> |
| 94 | + <td class="table_data_field_title">function_name</td> |
| 95 | + <td class="table_data_field_text">{function_name}</td> |
| 96 | + </tr> |
| 97 | + <tr> |
| 98 | + <td class="table_data_field_title">param</td> |
| 99 | + <td class="table_data_field_text">{param}</td> |
| 100 | + </tr> |
| 101 | + <tr> |
| 102 | + <td class="table_data_field_title">time</td> |
| 103 | + <td class="table_data_field_text">{time}</td> |
| 104 | + </tr> |
| 105 | + <tr> |
| 106 | + <td class="table_data_field_title">exception</td> |
| 107 | + <td class="table_data_field_text">{exception}</td> |
| 108 | + </tr> |
| 109 | + </tbody> |
| 110 | + </table> |
| 111 | + <br> |
| 112 | + """.strip() |
| 113 | + |
| 114 | + |
| 115 | +def make_html_table(event_str: str, record_data: dict, table_head: str): |
| 116 | + event_str = "".join( |
| 117 | + [ |
| 118 | + event_str, |
| 119 | + event_table.format( |
| 120 | + table_head_class=table_head, |
| 121 | + function_name=record_data.get("function_name"), |
| 122 | + param=record_data.get("local_param"), |
| 123 | + time=record_data.get("time"), |
| 124 | + exception=record_data.get("program_exception"), |
| 125 | + ) |
| 126 | + ] |
| 127 | + ) |
| 128 | + return event_str |
| 129 | + |
| 130 | + |
| 131 | +def generate_html(html_name: str = "default_name"): |
| 132 | + """ |
| 133 | + :param html_name: save html file name |
| 134 | + :return: html_string |
| 135 | + """ |
| 136 | + if len(test_record.total_record_list) == 0: |
| 137 | + raise HTMLException(html_generate_no_data_tag) |
| 138 | + else: |
| 139 | + event_str = "" |
| 140 | + for record_data in test_record.total_record_list: |
| 141 | + # because data on record_data all is str |
| 142 | + if record_data.get("program_exception") == "None": |
| 143 | + event_str = make_html_table(event_str, record_data, "event_table_head") |
| 144 | + else: |
| 145 | + event_str = make_html_table(event_str, record_data, "failure_table_head") |
| 146 | + new_html_string = html_string.format(event_table=event_str) |
| 147 | + try: |
| 148 | + lock.acquire() |
| 149 | + with open(html_name + ".html", "w+") as file_to_write: |
| 150 | + file_to_write.write( |
| 151 | + new_html_string |
| 152 | + ) |
| 153 | + except Exception as error: |
| 154 | + print(repr(error), file=sys.stderr) |
| 155 | + finally: |
| 156 | + lock.release() |
| 157 | + return new_html_string |
0 commit comments