|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Move DPI fix to the correct location - right before chart creation. |
| 4 | +""" |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +import re |
| 8 | + |
| 9 | +def move_dpi_fix(file_path): |
| 10 | + """Move DPI fix to right before chart creation.""" |
| 11 | + |
| 12 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 13 | + content = f.read() |
| 14 | + |
| 15 | + # First, remove the existing DPI fix wherever it is |
| 16 | + dpi_fix_pattern = r'\n // Fix blurry canvas on high-DPI displays\n const dpr = window\.devicePixelRatio \|\| 1;\n const rect = canvas\.getBoundingClientRect\(\);\n canvas\.width = rect\.width \* dpr;\n canvas\.height = rect\.height \* dpr;\n ctx\.scale\(dpr, dpr\);\n' |
| 17 | + |
| 18 | + content = re.sub(dpi_fix_pattern, '', content) |
| 19 | + |
| 20 | + # Now add it right before "const chart = new Chart" |
| 21 | + dpi_fix = ''' // Fix blurry canvas on high-DPI displays |
| 22 | + const dpr = window.devicePixelRatio || 1; |
| 23 | + const rect = canvas.getBoundingClientRect(); |
| 24 | + canvas.width = rect.width * dpr; |
| 25 | + canvas.height = rect.height * dpr; |
| 26 | + ctx.scale(dpr, dpr); |
| 27 | +
|
| 28 | +''' |
| 29 | + |
| 30 | + # Find "const chart = new Chart" and add DPI fix before it |
| 31 | + chart_pattern = r'( const chart = new Chart\(ctx,)' |
| 32 | + replacement = dpi_fix + r'\1' |
| 33 | + |
| 34 | + if re.search(chart_pattern, content): |
| 35 | + content = re.sub(chart_pattern, replacement, content) |
| 36 | + |
| 37 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 38 | + f.write(content) |
| 39 | + |
| 40 | + print(f"Fixed: {file_path.name}") |
| 41 | + else: |
| 42 | + print(f"ERROR: Could not find chart creation in {file_path.name}") |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + """Process all severity chart files.""" |
| 47 | + |
| 48 | + # Find all BAU and PM severity charts |
| 49 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 50 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 51 | + |
| 52 | + all_charts = bau_charts + pm_charts |
| 53 | + |
| 54 | + if not all_charts: |
| 55 | + print("No severity charts found!") |
| 56 | + return |
| 57 | + |
| 58 | + print(f"Found {len(all_charts)} severity charts") |
| 59 | + print(f"Moving DPI fix to correct location...\n") |
| 60 | + |
| 61 | + for chart_file in all_charts: |
| 62 | + move_dpi_fix(chart_file) |
| 63 | + |
| 64 | + print(f"\nCompleted!") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + main() |
0 commit comments