| 1 |
"""Create and update HTML reports from templates. |
| 2 |
|
| 3 |
It supports placeholder substitution, JavaScript and CSS embedding, and ensures |
| 4 |
compatibility with existing report formats. The module also includes utilities |
| 5 |
for backing up and updating reports with the latest template files. |
| 6 |
""" |
| 7 |
|
| 8 |
import codecs |
| 9 |
import os |
| 10 |
import re |
| 11 |
import shutil |
| 12 |
import sys |
| 13 |
from time import strftime |
| 14 |
|
| 15 |
from DisplayCAL import jspacker |
| 16 |
from DisplayCAL import localization as lang |
| 17 |
from DisplayCAL.config import get_data_path, initcfg |
| 18 |
from DisplayCAL.meta import VERSION_STRING |
| 19 |
|
| 20 |
|
| 21 |
def create( |
| 22 |
report_path: str, |
| 23 |
placeholders_to_data: dict, |
| 24 |
pack: bool = True, |
| 25 |
template_name: str = "report", |
| 26 |
) -> None: |
| 27 |
"""Create a report with all placeholders substituted by data. |
| 28 |
|
| 29 |
Args: |
| 30 |
report_path (str): Path to the report file to create. |
| 31 |
placeholders_to_data (dict): Dictionary mapping placeholders to their data. |
| 32 |
pack (bool): Whether to pack JavaScript code. |
| 33 |
template_name (str): Name of the report template to use. |
| 34 |
""" |
| 35 |
# read report template |
| 36 |
template_filename = f"{template_name}.html" |
| 37 |
report_html_template_path = get_data_path(os.path.join("report", template_filename)) |
| 38 |
if not report_html_template_path: |
| 39 |
raise OSError(lang.getstr("file.missing", template_filename)) |
| 40 |
try: |
| 41 |
with open( |
| 42 |
report_html_template_path, "r", encoding="utf-8" |
| 43 |
) as report_html_template: |
| 44 |
report_html = report_html_template.read() |
| 45 |
except OSError as exception: |
| 46 |
raise exception.__class__( |
| 47 |
lang.getstr("error.file.open", report_html_template_path) |
| 48 |
) from exception |
| 49 |
|
| 50 |
# create report |
| 51 |
for placeholder in placeholders_to_data: |
| 52 |
data = placeholders_to_data[placeholder] |
| 53 |
report_html = report_html.replace(placeholder, data) |
| 54 |
|
| 55 |
for include in ( |
| 56 |
"base.css", |
| 57 |
"compare.css", |
| 58 |
"print.css", |
| 59 |
"jsapi-packages.js", |
| 60 |
"jsapi-patches.js", |
| 61 |
"compare.constants.js", |
| 62 |
"compare.variables.js", |
| 63 |
"compare.functions.js", |
| 64 |
"compare.init.js", |
| 65 |
"uniformity.functions.js", |
| 66 |
): |
| 67 |
path = get_data_path(os.path.join("report", include)) |
| 68 |
if not path: |
| 69 |
raise OSError(lang.getstr("file.missing", include)) |
| 70 |
try: |
| 71 |
with open(path, "r", encoding="utf-8") as f: |
| 72 |
if include.endswith(".js"): |
| 73 |
js = f.read() |
| 74 |
if pack: |
| 75 |
packer = jspacker.JavaScriptPacker() |
| 76 |
js = packer.pack(js, 62, True).strip() |
| 77 |
report_html = report_html.replace( |
| 78 |
f'src="{include}">', f">/*<![CDATA[*/\n{js}\n/*]]>*/" |
| 79 |
) |
| 80 |
else: |
| 81 |
report_html = report_html.replace( |
| 82 |
f'@import "{include}";', f.read().strip() |
| 83 |
) |
| 84 |
except OSError as exception: |
| 85 |
raise exception.__class__( |
| 86 |
lang.getstr("error.file.open", path) |
| 87 |
) from exception |
| 88 |
|
| 89 |
# write report |
| 90 |
try: |
| 91 |
with open(report_path, "w", encoding="utf-8") as report_html_file: |
| 92 |
report_html_file.write(report_html) |
| 93 |
except OSError as exception: |
| 94 |
raise exception.__class__( |
| 95 |
"{}\n\n{}".format(lang.getstr("error.file.create", report_path), exception) |
| 96 |
) from exception |
| 97 |
|
| 98 |
|
| 99 |
def update(report_path: str, pack: bool = True) -> None: |
| 100 |
"""Update existing report with current template files. |
| 101 |
|
| 102 |
Also creates a backup copy of the old report. |
| 103 |
|
| 104 |
Args: |
| 105 |
report_path (str): Path to the existing report file. |
| 106 |
pack (bool): Whether to pack JavaScript code. |
| 107 |
""" |
| 108 |
# read original report |
| 109 |
try: |
| 110 |
with open(report_path, "r", encoding="utf-8") as orig_report: |
| 111 |
orig_report_html = orig_report.read() |
| 112 |
except OSError as exception: |
| 113 |
raise exception.__class__( |
| 114 |
lang.getstr("error.file.open", report_path) |
| 115 |
) from exception |
| 116 |
|
| 117 |
data = ( |
| 118 |
("${PLANCKIAN}", r'id="FF_planckian"\s*(.*?)\s*disabled="disabled"', 0), |
| 119 |
("${DISPLAY}", r'"FF_display"\s*value="(.+?)"\s*/?>', 0), |
| 120 |
("${INSTRUMENT}", r'"FF_instrument"\s*value="(.+?)"\s*/?>', 0), |
| 121 |
("${CORRECTION_MATRIX}", r'"FF_correction_matrix"\s*value="(.+?)"\s*/?>', 0), |
| 122 |
("${BLACKPOINT}", r'"FF_blackpoint"\s*value="(.+?)"\s*/?>', 0), |
| 123 |
("${WHITEPOINT}", r'"FF_whitepoint"\s*value="(.+?)"\s*/?>', 0), |
| 124 |
( |
| 125 |
"${WHITEPOINT_NORMALIZED}", |
| 126 |
r'"FF_whitepoint_normalized"\s*value="(.+?)"\s*/?>', |
| 127 |
0, |
| 128 |
), |
| 129 |
("${PROFILE}", r'"FF_profile"\s*value="(.+?)"\s*/?>', 0), |
| 130 |
("${PROFILE_WHITEPOINT}", r'"FF_profile_whitepoint"\s*value="(.+?)"\s*/?>', 0), |
| 131 |
( |
| 132 |
"${PROFILE_WHITEPOINT_NORMALIZED}", |
| 133 |
r'"FF_profile_whitepoint_normalized"\s*value="(.+?)"\s*/?>', |
| 134 |
0, |
| 135 |
), |
| 136 |
("${SIMULATION_PROFILE}", r'SIMULATION_PROFILE\s*=\s*"(.+?)"[;,]', 0), |
| 137 |
("${TRC_GAMMA}", r"BT_1886_GAMMA\s*=\s*(.+?)[;,]", 0), |
| 138 |
("${TRC_GAMMA}", r"TRC_GAMMA\s*=\s*(.+?)[;,]", 0), |
| 139 |
("${TRC_GAMMA_TYPE}", r'BT_1886_GAMMA_TYPE\s*=\s*"(.+?)"[;,]', 0), |
| 140 |
("${TRC_GAMMA_TYPE}", r'TRC_GAMMA_TYPE\s*=\s*"(.+?)"[;,]', 0), |
| 141 |
("${TRC_OUTPUT_OFFSET}", r"TRC_OUTPUT_OFFSET\s*=\s*(.+?)[;,]", 0), |
| 142 |
("${TRC}", r'TRC\s*=\s*"(.+?)"[;,]', 0), |
| 143 |
("${WHITEPOINT_SIMULATION}", r"WHITEPOINT_SIMULATION\s*=\s*(.+?)[;,]", 0), |
| 144 |
( |
| 145 |
"${WHITEPOINT_SIMULATION_RELATIVE}", |
| 146 |
r"WHITEPOINT_SIMULATION_RELATIVE\s*=\s*(.+?)[;,]", |
| 147 |
0, |
| 148 |
), |
| 149 |
("${DEVICELINK_PROFILE}", r'DEVICELINK_PROFILE\s*=\s*"(.+?)"[;,]', 0), |
| 150 |
("${TESTCHART}", r'"FF_testchart"\s*value="(.+?)"\s*/?>', 0), |
| 151 |
("${ADAPTION}", r'"FF_adaption"\s*value="(.+?)"\s*/?>', 0), |
| 152 |
("${DATETIME}", r'"FF_datetime"\s*value="(.+?)"\s*/?>', 0), |
| 153 |
("${REF}", r'"FF_data_ref"\s*value="(.+?)"\s*/?>', re.DOTALL), |
| 154 |
("${MEASURED}", r'"FF_data_in"\s*value="(.+?)"\s*/?>', re.DOTALL), |
| 155 |
("${CAL_ENTRYCOUNT}", r"CAL_ENTRYCOUNT\s*=\s*(.+?)[;,]$", re.M), |
| 156 |
("${CAL_RGBLEVELS}", r"CAL_RGBLEVELS\s*=\s*(.+?)[;,]$", re.M), |
| 157 |
("${GRAYSCALE}", r"CRITERIA_GRAYSCALE\s*=\s*(.+?)[;,]$", re.M), |
| 158 |
("${REPORT_TYPE}", "<title>(.+?) Report", 0), |
| 159 |
# Uniformity report |
| 160 |
("${DISPLAY}", "\u2014 (.+?) \u2014", 0), |
| 161 |
("${DATETIME}", "\u2014 .+? \u2014 (.+?)</title>", 0), |
| 162 |
("${ROWS}", r"rows\s*=\s*(.+?)[;,]", 0), |
| 163 |
("${COLS}", r"cols\s*=\s*(.+?)[;,]", 0), |
| 164 |
("${RESULTS}", r"results\s*=\s*(.+?), locus = ", 0), |
| 165 |
("${LOCUS}", r"locus\s*=\s*'([^']+)'", 0), |
| 166 |
) |
| 167 |
|
| 168 |
placeholders2data = { |
| 169 |
"${REPORT_VERSION}": VERSION_STRING, |
| 170 |
"${CORRECTION_MATRIX}": "Unknown", |
| 171 |
"${ADAPTION}": "None", |
| 172 |
"${CAL_ENTRYCOUNT}": "null", |
| 173 |
"${CAL_RGBLEVELS}": "null", |
| 174 |
"${GRAYSCALE}": "null", |
| 175 |
"${BLACKPOINT}": "-1 -1 -1", |
| 176 |
"${TRC_GAMMA}": "null", |
| 177 |
"${TRC_OUTPUT_OFFSET}": "0", |
| 178 |
"${WHITEPOINT_SIMULATION}": "false", |
| 179 |
"${WHITEPOINT_SIMULATION_RELATIVE}": "false", |
| 180 |
} |
| 181 |
|
| 182 |
template_name = "report" |
| 183 |
for placeholder, pattern, flags in data: |
| 184 |
result = re.search(pattern, orig_report_html, flags) |
| 185 |
if result or not placeholders2data.get(placeholder): |
| 186 |
if ( |
| 187 |
placeholder == "${TRC}" |
| 188 |
and not result |
| 189 |
and "${TRC_GAMMA}" in placeholders2data |
| 190 |
): |
| 191 |
default = "BT.1886" |
| 192 |
else: |
| 193 |
default = "" |
| 194 |
placeholders2data[placeholder] = result.groups()[0] if result else default |
| 195 |
if result and placeholder == "${COLS}": |
| 196 |
template_name = "uniformity" |
| 197 |
|
| 198 |
# backup original report |
| 199 |
shutil.copy2( |
| 200 |
report_path, "{}.{}".format(report_path, strftime("%Y-%m-%d_%H-%M-%S")) |
| 201 |
) |
| 202 |
|
| 203 |
create(report_path, placeholders2data, pack, template_name) |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
initcfg() |
| 208 |
lang.init() |
| 209 |
if not sys.argv[1:]: |
| 210 |
print("Update existing report(s) with current template files.") |
| 211 |
print(f"Usage: {os.path.basename(sys.argv[0])} report1.html [report2.html...]") |
| 212 |
else: |
| 213 |
for arg in sys.argv[1:]: |
| 214 |
try: |
| 215 |
update(arg) |
| 216 |
except OSError as exception: |
| 217 |
print(exception) |