|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Fix missing ctx by adding the proper DPI fix code before chart creation. |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def fix_missing_ctx(file_path): |
| 10 | + """Add the DPI fix code before chart creation.""" |
| 11 | + |
| 12 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 13 | + content = f.read() |
| 14 | + |
| 15 | + # The working DPI fix from exemplar |
| 16 | + dpi_fix = ''' |
| 17 | + // Fix blurry canvas on high-DPI displays |
| 18 | + const ctx = document.getElementById('severityChart').getContext('2d'); |
| 19 | + const dpr = window.devicePixelRatio || 1; |
| 20 | + const canvas = document.getElementById('severityChart'); |
| 21 | + const rect = canvas.getBoundingClientRect(); |
| 22 | + canvas.width = rect.width * dpr; |
| 23 | + canvas.height = rect.height * dpr; |
| 24 | + ctx.scale(dpr, dpr); |
| 25 | +''' |
| 26 | + |
| 27 | + # Find where chart is created and insert before it if ctx doesn't exist |
| 28 | + if 'const chart = new Chart(ctx,' in content: |
| 29 | + # Check if ctx is already defined before this |
| 30 | + chart_pos = content.find('const chart = new Chart(ctx,') |
| 31 | + before_chart = content[:chart_pos] |
| 32 | + |
| 33 | + if 'const ctx = ' not in before_chart: |
| 34 | + # Insert the DPI fix right before chart creation |
| 35 | + # Find a good insertion point - after the mouseleave event listener |
| 36 | + if 'chartCanvas.addEventListener(\'mouseleave\'' in before_chart: |
| 37 | + # Insert after this event listener |
| 38 | + pattern = r"(chartCanvas\.addEventListener\('mouseleave'[^}]+\}\);)\s*" |
| 39 | + replacement = r"\1\n" + dpi_fix + "\n " |
| 40 | + content = re.sub(pattern, replacement, content) |
| 41 | + else: |
| 42 | + # Just insert before chart creation |
| 43 | + content = content.replace( |
| 44 | + 'const chart = new Chart(ctx,', |
| 45 | + dpi_fix + '\n const chart = new Chart(ctx,' |
| 46 | + ) |
| 47 | + |
| 48 | + # Write back |
| 49 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 50 | + f.write(content) |
| 51 | + |
| 52 | + print(f"Updated: {file_path.name}") |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + """Process all severity chart files.""" |
| 57 | + |
| 58 | + # Find all BAU and PM severity charts |
| 59 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 60 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 61 | + |
| 62 | + all_charts = bau_charts + pm_charts |
| 63 | + |
| 64 | + if not all_charts: |
| 65 | + print("No severity charts found!") |
| 66 | + return |
| 67 | + |
| 68 | + print(f"Found {len(all_charts)} severity charts") |
| 69 | + print(f"Fixing missing ctx definitions...\n") |
| 70 | + |
| 71 | + for chart_file in all_charts: |
| 72 | + fix_missing_ctx(chart_file) |
| 73 | + |
| 74 | + print(f"\nCompleted! Updated {len(all_charts)} files.") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + main() |
0 commit comments