diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0227a65 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git +.venv +.superpowers +.pytest_cache +__pycache__ +tests +docs diff --git a/Dockerfile b/Dockerfile index e0f4b81..3c136de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,6 @@ ENV LCD_LIB_PATH=/opt/lcd \ TZ=Europe/Prague COPY assets ./assets -COPY lcdkit.py poller.py view.py timeutil.py weather_data.py weather_view.py stats_data.py server_view.py \ - registry.py runner.py main.py ./ +COPY *.py ./ CMD ["python", "main.py"] diff --git a/README.md b/README.md index 3d1a448..811e207 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Edit `VIEW:` in `docker-compose.yml`, commit, `git push`, press **Deploy** in Co ## Add a view -1. Write a module with a class that follows the `View` protocol in `view.py` (`start`, `stop`, `frame_key`, +1. Write a module (any `*.py` in the project root is copied into the image) with a class that follows the `View` protocol in `view.py` (`start`, `stop`, `frame_key`, `render`, `patch`). Put anything slow (network, system reads) in a `Poller` so drawing never blocks. 2. Add one entry to `REGISTRY` in `registry.py`. 3. Add a line to the table above, a sample to `preview.py`, and tests. @@ -58,8 +58,10 @@ else keeps working. - **Upside-down:** this screen is mounted the opposite way to the bus board. Set `SCREEN_ORIENTATION` to `REVERSE_LANDSCAPE`. -- **Blank screen:** `docker compose logs second-screen`. If it says the screen is not available it retries every 30 s; - check the cable and `ls -l /dev/serial/by-id/`. ModemManager can interfere (`systemctl status ModemManager`). +- **Blank screen:** `docker compose logs second-screen`. If the port cannot be opened the app retries every 30 s + (the screen library's own exit is caught); if the port disappears mid-run the app exits after about a minute of + failed draws and Docker restarts it. With the cable out *before* the container starts, Docker itself refuses to + start it (the compose `devices:` entry). Check the cable and `ls -l /dev/serial/by-id/`. ModemManager can interfere (`systemctl status ModemManager`). - **`STARÉ` tag:** the data is old (weather older than 1 h, server stats older than 30 s). The clock keeps running. - **Error screen at startup:** `VIEW` is not one of the valid views. diff --git a/runner.py b/runner.py index 48c6e0c..0bd21bc 100644 --- a/runner.py +++ b/runner.py @@ -36,7 +36,7 @@ def open_screen(factory: Callable[[], Any], *, running: Callable[[], bool], while running(): try: return factory() - except Exception as exc: + except (Exception, SystemExit) as exc: # the screen library calls sys.exit(0) when the port is missing if not logged: log.warning("screen not available (%s); retrying every %.0f s", exc, retry_s) logged = True @@ -73,7 +73,7 @@ def run(view: View, lcd, *, now_fn: Callable[[], datetime], running: Callable[[] lcd.DisplayPILImage(patch.image, patch.x, patch.y) last_second = now.second failures = 0 - except Exception: + except (Exception, SystemExit): # SystemExit: the library exits when it cannot reopen the port log.exception("draw failed") failures += 1 last_key = None # redraw everything on the next tick diff --git a/tests/test_packaging.py b/tests/test_packaging.py new file mode 100644 index 0000000..c762871 --- /dev/null +++ b/tests/test_packaging.py @@ -0,0 +1,28 @@ +import fnmatch +import re +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent + + +def copied_sources(): + sources = [] + for line in (ROOT / "Dockerfile").read_text().splitlines(): + match = re.match(r"^COPY\s+(.+)\s+\S+$", line.strip()) + if match: + sources += match.group(1).split() + return sources + + +def test_dockerfile_copies_every_top_level_module(): + """A new view module must reach the image without anyone editing the Dockerfile.""" + sources = copied_sources() + missing = [p.name for p in ROOT.glob("*.py") if not any(fnmatch.fnmatch(p.name, s) for s in sources)] + assert missing == [] + + +def test_dockerignore_keeps_dev_files_out_of_the_image_but_not_assets(): + ignored = (ROOT / ".dockerignore").read_text().split() + for name in (".venv", ".git", "tests", "docs", ".superpowers"): + assert name in ignored + assert "assets" not in ignored diff --git a/tests/test_runner.py b/tests/test_runner.py index 6d799b5..f4277cd 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -138,3 +138,28 @@ def test_render_error_size_and_has_ink(): def test_render_error_long_line_does_not_raise(): assert runner.render_error(["X" * 500]).size == (480, 320) + + +def test_open_screen_retries_when_the_library_calls_sys_exit(): + """The pinned screen library calls sys.exit(0) when the port is missing; that must not end the process.""" + attempts, sleeps = [], [] + + def factory(): + attempts.append(1) + if len(attempts) < 3: + raise SystemExit(0) + return "lcd" + + assert runner.open_screen(factory, running=lambda: True, sleep=sleeps.append) == "lcd" + assert sleeps == [30.0, 30.0] + + +def test_run_counts_library_sys_exit_as_a_failed_draw(): + class ExitingLcd: + def DisplayPILImage(self, *a, **k): + raise SystemExit(0) + + view = StubView(lambda now: "same") + with pytest.raises(SystemExit): # after max_failures; Docker then restarts and open_screen retries + runner.run(view, ExitingLcd(), now_fn=clock(T0, 0), running=ticks(50), sleep=lambda s: None, max_failures=3) + assert view.renders == 3 # it tried three times first diff --git a/tests/test_weather_view.py b/tests/test_weather_view.py index 76b586e..eafca49 100644 --- a/tests/test_weather_view.py +++ b/tests/test_weather_view.py @@ -137,3 +137,10 @@ def test_fewer_than_twelve_hours_still_renders(): def test_unknown_weather_code_renders(): assert view(value=weather(code=999)).render(NOW).size == (480, 320) + + +def test_two_digit_negative_temps_stay_clear_of_the_divider(): + for temp in (-10.4, -19.6): + img = view(value=weather(temp=temp)).render(NOW) + box = ink_bbox(img, (84, 180, 204, 236)) + assert box is not None and box[2] < 200, temp diff --git a/weather_view.py b/weather_view.py index 01fb14b..9439e93 100644 --- a/weather_view.py +++ b/weather_view.py @@ -133,7 +133,9 @@ class WeatherView: temp = weather.temp if weather else None if weather: draw_icon(d, icon_for(weather.code), 20, 182, 56) - end = lcdkit.number(d, lcdkit.fmt_temp(temp), 90, 184, 26, 46, 6, 7, ghost=False) + text = lcdkit.fmt_temp(temp) + w, gap = (26, 7) if len(text) <= 2 else (22, 5) # a third glyph (-10) must stay clear of the divider + end = lcdkit.number(d, text, 90, 184, w, 46, 6, gap, ghost=False) if temp is not None: d.ellipse((end + 4, 184, end + 14, 194), outline=INK, width=3) hi = lcdkit.fmt_temp(weather.today_max) if weather else "--"