File size: 3,259 Bytes
57d7008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Create Build/LocalAgent/local_agent.py from the read-only reference copy.

This is the ONE-TIME copy step.  It takes ..\\..\\LocalAgent\\local_agent.py
(the reference, never modified) and writes the build copy with every CSS font
size in the embedded web UI scaled up by FONT_SCALE, which is the low-vision
bump.  Every other change to the build copy is made by hand afterwards, so the
script refuses to overwrite an existing copy unless you pass --force.

Run:  python tools\\make_build_copy.py [--force] [--scale 1.10]
"""

from __future__ import annotations

import os
import re
import shutil
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
BUILD = os.path.dirname(HERE)
ROOT = os.path.dirname(BUILD)
REFERENCE = os.path.join(ROOT, "LocalAgent", "local_agent.py")
TARGET_DIR = os.path.join(BUILD, "LocalAgent")
TARGET = os.path.join(TARGET_DIR, "local_agent.py")

FONT_SCALE = 1.10

# font-size: 20.9px      /  font: 600 23.2px/1 var(--sans)
FONT_SIZE_RE = re.compile(r"(font-size:\s*)([0-9]*\.?[0-9]+)px")
FONT_SHORTHAND_RE = re.compile(r"(font:\s*)([^;{}\n]+)")
PX_RE = re.compile(r"([0-9]*\.?[0-9]+)px")


def _scale_px(match: "re.Match[str]", scale: float) -> str:
    value = float(match.group(1)) * scale
    text = ("%.1f" % value).rstrip("0").rstrip(".")
    return text + "px"


def scale_fonts(source: str, scale: float) -> tuple[str, int]:
    changed = 0

    def size_sub(m: "re.Match[str]") -> str:
        nonlocal changed
        changed += 1
        value = float(m.group(2)) * scale
        text = ("%.1f" % value).rstrip("0").rstrip(".")
        return m.group(1) + text + "px"

    def shorthand_sub(m: "re.Match[str]") -> str:
        nonlocal changed
        value = m.group(2)
        if "px" not in value:
            return m.group(0)
        changed += 1
        return m.group(1) + PX_RE.sub(lambda mm: _scale_px(mm, scale), value)

    out = FONT_SIZE_RE.sub(size_sub, source)
    out = FONT_SHORTHAND_RE.sub(shorthand_sub, out)
    return out, changed


def main() -> int:
    argv = sys.argv[1:]
    force = "--force" in argv
    scale = FONT_SCALE
    if "--scale" in argv:
        scale = float(argv[argv.index("--scale") + 1])

    if not os.path.isfile(REFERENCE):
        print("reference not found:", REFERENCE)
        return 1
    if os.path.isfile(TARGET) and not force:
        print("build copy already exists, refusing to clobber hand edits:")
        print("  ", TARGET)
        print("pass --force if you really want to start over")
        return 1

    os.makedirs(TARGET_DIR, exist_ok=True)
    with open(REFERENCE, "r", encoding="utf-8") as fh:
        source = fh.read()
    out, changed = scale_fonts(source, scale)
    with open(TARGET, "w", encoding="utf-8", newline="\n") as fh:
        fh.write(out)
    print("wrote %s (%d bytes)" % (TARGET, len(out)))
    print("scaled %d font declarations by %.2fx" % (changed, scale))

    model_pointer = os.path.join(ROOT, "LocalAgent", ".local_agent_model")
    if os.path.isfile(model_pointer):
        shutil.copy2(model_pointer, os.path.join(TARGET_DIR, ".local_agent_model"))
        print("copied model pointer:", open(model_pointer, encoding="utf-8").read().strip())
    return 0


if __name__ == "__main__":
    raise SystemExit(main())