|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Add DPI fix after medianValuesPlugin closing brace. |
| 4 | +""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +def add_dpi_fix(file_path): |
| 9 | + """Add DPI fix after medianValuesPlugin closes.""" |
| 10 | + |
| 11 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 12 | + lines = f.readlines() |
| 13 | + |
| 14 | + # Check if already has DPI fix |
| 15 | + for line in lines: |
| 16 | + if '// Fix blurry canvas on high-DPI displays' in line: |
| 17 | + print(f"Skipped (already has DPI fix): {file_path.name}") |
| 18 | + return |
| 19 | + |
| 20 | + dpi_fix_lines = [ |
| 21 | + '\n', |
| 22 | + ' // Fix blurry canvas on high-DPI displays\n', |
| 23 | + ' const dpr = window.devicePixelRatio || 1;\n', |
| 24 | + ' const canvas = document.getElementById(\'severityChart\');\n', |
| 25 | + ' const rect = canvas.getBoundingClientRect();\n', |
| 26 | + ' canvas.width = rect.width * dpr;\n', |
| 27 | + ' canvas.height = rect.height * dpr;\n', |
| 28 | + ' ctx.scale(dpr, dpr);\n' |
| 29 | + ] |
| 30 | + |
| 31 | + # Find the line with " };" that closes medianValuesPlugin |
| 32 | + # It should be after the plugin definition and before the chart creation |
| 33 | + new_lines = [] |
| 34 | + found_plugin_close = False |
| 35 | + |
| 36 | + for i, line in enumerate(lines): |
| 37 | + new_lines.append(line) |
| 38 | + |
| 39 | + # Look for the closing of medianValuesPlugin |
| 40 | + if not found_plugin_close and line.strip() == '};': |
| 41 | + # Check if this is after medianValues plugin definition |
| 42 | + # Look back to see if we recently saw the plugin |
| 43 | + lookback = ''.join(lines[max(0, i-200):i]) |
| 44 | + if 'medianValuesPlugin' in lookback and 'afterDatasetsDraw' in lookback: |
| 45 | + # Add DPI fix after this line |
| 46 | + new_lines.extend(dpi_fix_lines) |
| 47 | + found_plugin_close = True |
| 48 | + |
| 49 | + if not found_plugin_close: |
| 50 | + print(f"ERROR: Could not find medianValuesPlugin closing in {file_path.name}") |
| 51 | + return |
| 52 | + |
| 53 | + # Write back |
| 54 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 55 | + f.writelines(new_lines) |
| 56 | + |
| 57 | + print(f"Updated: {file_path.name}") |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + """Process all severity chart files.""" |
| 62 | + |
| 63 | + # Find all BAU and PM severity charts |
| 64 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 65 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 66 | + |
| 67 | + all_charts = bau_charts + pm_charts |
| 68 | + |
| 69 | + if not all_charts: |
| 70 | + print("No severity charts found!") |
| 71 | + return |
| 72 | + |
| 73 | + print(f"Found {len(all_charts)} severity charts") |
| 74 | + print(f"Adding DPI fix after medianValuesPlugin...\n") |
| 75 | + |
| 76 | + for chart_file in all_charts: |
| 77 | + add_dpi_fix(chart_file) |
| 78 | + |
| 79 | + print(f"\nCompleted!") |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + main() |
0 commit comments