Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: accept BOM theme configs in calendar helper

SlivenRed0f9e7d7d6e0a
diff --git a/tests/test_race_calendar.py b/tests/test_race_calendar.py
new file mode 100644
index 000000000..bd356ee84
--- /dev/null
+++ b/tests/test_race_calendar.py
@@ -0,0 +1,83 @@
+"""The calendar helper reads the same UTF-8 theme files as the launcher, without losing other keys."""
+import contextlib
+import datetime
+import io
+import json
+from pathlib import Path
+import sys
+import tempfile
+from types import SimpleNamespace
+import unittest
+from unittest import mock
+
+from tools import race_calendar
+
+
+RESPONSE = json.dumps({"MRData": {"RaceTable": {"season": "2026", "Races": [{
+ "round": "1", "raceName": "Example Grand Prix", "date": "2026-01-04", "time": "14:00:00Z",
+}]}}}).encode("utf-8")
+
+
+class FixedDatetime(datetime.datetime):
+ @classmethod
+ def now(cls, tz=None):
+ return cls(2026, 1, 1, tzinfo=tz)
+
+
+class CalendarConfigTest(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(self.tmp.cleanup)
+ self.theme = Path(self.tmp.name)
+ self.config = self.theme / "config.json"
+ opener = mock.patch.object(race_calendar.urllib.request, "urlopen",
+ side_effect=lambda *args, **kwargs: io.BytesIO(RESPONSE))
+ self.urlopen = opener.start()
+ self.addCleanup(opener.stop)
+
+ def run_main(self):
+ clock = SimpleNamespace(datetime=FixedDatetime, timedelta=datetime.timedelta,
+ timezone=datetime.timezone)
+ with mock.patch.object(sys, "argv", ["race_calendar.py", str(self.theme), "--season", "2026"]), \
+ mock.patch.object(race_calendar, "datetime", clock), \
+ contextlib.redirect_stdout(io.StringIO()):
+ race_calendar.main()
+
+ def test_utf8_theme_keeps_other_keys_when_calendar_is_updated(self):
+ original = {
+ "greeting_name": "客廳 Trịnh",
+ "columns": 6,
+ "dock_apps": ["com.example.player"],
+ "custom": {"label": "夜晚", "values": [True, None, {"amount": 7}]},
+ "race_calendar": [{"name": "old calendar"}],
+ }
+ text = json.dumps(original, ensure_ascii=False, indent=2) + "\n"
+ for encoding in ("utf-8", "utf-8-sig"):
+ for newline in ("\n", "\r\n"):
+ with self.subTest(encoding=encoding, newline=newline):
+ self.config.write_bytes(text.replace("\n", newline).encode(encoding))
+ self.urlopen.reset_mock()
+ self.run_main()
+ self.urlopen.assert_called_once()
+ written = self.config.read_bytes()
+ self.assertFalse(written.startswith(b"\xef\xbb\xbf"))
+ updated = json.loads(written.decode("utf-8"))
+ self.assertEqual({k: v for k, v in updated.items() if k != "race_calendar"},
+ {k: v for k, v in original.items() if k != "race_calendar"})
+ self.assertEqual(len(updated["race_calendar"]), 1)
+ self.assertEqual(updated["race_calendar"][0]["name"], "Example Grand Prix")
+
+ def test_malformed_theme_is_unchanged_and_never_fetches(self):
+ for encoding in ("utf-8", "utf-8-sig"):
+ with self.subTest(encoding=encoding):
+ data = '{"greeting_name":"客廳 Trịnh",'.encode(encoding)
+ self.config.write_bytes(data)
+ self.urlopen.reset_mock()
+ with self.assertRaises(json.JSONDecodeError):
+ self.run_main()
+ self.urlopen.assert_not_called()
+ self.assertEqual(self.config.read_bytes(), data)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/race_calendar.py b/tools/race_calendar.py
index ca1fc0d68..3fe000420 100644
--- a/tools/race_calendar.py
+++ b/tools/race_calendar.py
@@ -67,7 +67,7 @@ def main():
args = parser.parse_args()
config = Path(args.theme) / "config.json"
- theme = json.loads(config.read_text(encoding="utf-8")) if config.is_file() else {}
+ theme = json.loads(config.read_text(encoding="utf-8-sig")) if config.is_file() else {}
request = urllib.request.Request(API.format(season=args.season), headers={"User-Agent": "cleanyourtv"})
with urllib.request.urlopen(request, timeout=30) as response:
table = json.load(response)["MRData"]["RaceTable"]