quak-camera 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. quak/__init__.py +7 -0
  2. quak/__main__.py +3 -0
  3. quak/camera.py +321 -0
  4. quak/cli/__init__.py +0 -0
  5. quak/cli/main.py +887 -0
  6. quak/config.py +98 -0
  7. quak/connect.py +75 -0
  8. quak/core/__init__.py +1 -0
  9. quak/core/codec.py +228 -0
  10. quak/core/errors.py +66 -0
  11. quak/core/proto.py +307 -0
  12. quak/core/redact.py +55 -0
  13. quak/core/registry.py +333 -0
  14. quak/core/tables.py +442 -0
  15. quak/describe.py +126 -0
  16. quak/e2e/__init__.py +1 -0
  17. quak/e2e/__main__.py +3 -0
  18. quak/e2e/gui.py +375 -0
  19. quak/e2e/gui_steps.py +232 -0
  20. quak/e2e/runner.py +252 -0
  21. quak/e2e/scenarios.py +436 -0
  22. quak/flows.py +309 -0
  23. quak/guard.py +167 -0
  24. quak/gui/__init__.py +1 -0
  25. quak/gui/app.py +683 -0
  26. quak/gui/assets/THIRD_PARTY_NOTICES.md +73 -0
  27. quak/gui/assets/favicon.png +0 -0
  28. quak/gui/assets/fonts/Inter.ttf +0 -0
  29. quak/gui/assets/fonts/JetBrainsMono.ttf +0 -0
  30. quak/gui/assets/fonts/OFL-Inter.txt +93 -0
  31. quak/gui/assets/fonts/OFL-JetBrainsMono.txt +93 -0
  32. quak/gui/assets/fonts/OFL-SpaceGrotesk.txt +93 -0
  33. quak/gui/assets/fonts/README.md +10 -0
  34. quak/gui/assets/fonts/SpaceGrotesk.ttf +0 -0
  35. quak/gui/assets/icon.ico +0 -0
  36. quak/gui/assets/icon.png +0 -0
  37. quak/gui/assets/icon_linux.png +0 -0
  38. quak/gui/assets/icon_web.png +0 -0
  39. quak/gui/assets/icon_windows.png +0 -0
  40. quak/gui/assets/licenses/GPL-3.0.txt +674 -0
  41. quak/gui/assets/licenses/LGPL-2.1.txt +501 -0
  42. quak/gui/assets/licenses/LGPL-3.0.txt +165 -0
  43. quak/gui/assets/logo-duck.svg +1 -0
  44. quak/gui/assets/logo-mono-black.svg +1 -0
  45. quak/gui/assets/logo-mono-white.svg +1 -0
  46. quak/gui/assets/logo.png +0 -0
  47. quak/gui/assets/logo.svg +1 -0
  48. quak/gui/kit.py +452 -0
  49. quak/gui/preview.py +109 -0
  50. quak/gui/screens/__init__.py +12 -0
  51. quak/gui/screens/add_camera.py +341 -0
  52. quak/gui/screens/cameras.py +304 -0
  53. quak/gui/screens/common.py +206 -0
  54. quak/gui/screens/live.py +318 -0
  55. quak/gui/screens/media.py +644 -0
  56. quak/gui/screens/more.py +546 -0
  57. quak/gui/screens/remote.py +481 -0
  58. quak/gui/screens/schedule.py +323 -0
  59. quak/gui/screens/settings.py +86 -0
  60. quak/gui/theme.py +110 -0
  61. quak/gui/tour.py +142 -0
  62. quak/net/__init__.py +1 -0
  63. quak/net/android_wifi.py +47 -0
  64. quak/net/hotspot.py +78 -0
  65. quak/net/http.py +212 -0
  66. quak/net/netsh.py +149 -0
  67. quak/net/nmcli.py +297 -0
  68. quak/net/profiles.py +3 -0
  69. quak/net/stream.py +259 -0
  70. quak/net/wifi.py +134 -0
  71. quak/service.py +472 -0
  72. quak/session.py +254 -0
  73. quak/transport/__init__.py +1 -0
  74. quak/transport/android.py +88 -0
  75. quak/transport/base.py +28 -0
  76. quak/transport/ble.py +174 -0
  77. quak/transport/bluez.py +110 -0
  78. quak/transport/sim.py +954 -0
  79. quak/transport/sim_http.py +302 -0
  80. quak_camera-0.1.0.dist-info/METADATA +156 -0
  81. quak_camera-0.1.0.dist-info/RECORD +87 -0
  82. quak_camera-0.1.0.dist-info/WHEEL +5 -0
  83. quak_camera-0.1.0.dist-info/entry_points.txt +3 -0
  84. quak_camera-0.1.0.dist-info/licenses/LICENSE +21 -0
  85. quak_camera-0.1.0.dist-info/licenses/NOTICE +10 -0
  86. quak_camera-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +73 -0
  87. quak_camera-0.1.0.dist-info/top_level.txt +1 -0
quak/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """Quak: an open-source remote for GoPro HERO11 cameras.
2
+
3
+ One backend (``quak.service.CameraService``) is shared by the ``quak`` CLI, the Flet GUI
4
+ and the end-to-end test runner.
5
+ """
6
+
7
+ __version__ = "0.1.0"
quak/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from quak.cli.main import main
2
+
3
+ main()
quak/camera.py ADDED
@@ -0,0 +1,321 @@
1
+ """Camera operations on top of a Session, returning Python values."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import datetime as dt
7
+ import time
8
+
9
+ from quak.core import codec, proto
10
+ from quak.core import registry as R
11
+ from quak.core.errors import Rejected, Timeout, Unsupported
12
+ from quak.core.registry import Action, Cmd, Feature, Query
13
+ from quak.session import Session
14
+
15
+
16
+ def local_tz() -> tuple[int, bool]:
17
+ """(UTC offset in minutes, DST active) for this machine right now."""
18
+ lt = time.localtime()
19
+ return lt.tm_gmtoff // 60, bool(lt.tm_isdst > 0)
20
+
21
+
22
+ class Camera:
23
+ def __init__(self, session: Session) -> None:
24
+ self.s = session
25
+ self._claimed = False
26
+ self._status_push: set[int] = set()
27
+
28
+ # ---------------------------------------------------------------- info and state
29
+
30
+ async def hardware_info(self) -> codec.HardwareInfo:
31
+ return codec.decode_hardware_info(await self.s.command(Cmd.HARDWARE_INFO))
32
+
33
+ async def statuses(self, ids=()) -> dict[int, int | str]:
34
+ body = await self.s.query(Query.STATUSES, ids)
35
+ return {k: R.decode_status_value(k, v) for k, v in codec.parse_tlv(body)}
36
+
37
+ async def status(self, status_id: int) -> int | str:
38
+ return (await self.statuses([status_id]))[status_id]
39
+
40
+ async def settings(self, ids=()) -> dict[int, int]:
41
+ body = await self.s.query(Query.SETTINGS, ids)
42
+ return {k: codec.to_int(v) for k, v in codec.parse_tlv(body)}
43
+
44
+ async def setting(self, setting_id: int) -> int:
45
+ return (await self.settings([setting_id]))[setting_id]
46
+
47
+ async def capabilities(self, ids=()) -> dict[int, list[int]]:
48
+ out: dict[int, list[int]] = {}
49
+ for k, v in codec.parse_tlv(await self.s.query(Query.CAPABILITIES, ids)):
50
+ out.setdefault(k, [])
51
+ if v:
52
+ out[k].append(codec.to_int(v))
53
+ return out
54
+
55
+ async def set_setting(self, setting_id: int, value: int, verify: bool = True) -> None:
56
+ name = R.setting_name(setting_id)
57
+ status = await self.s.write_setting(setting_id, R.encode_setting_value(setting_id, value))
58
+ if status:
59
+ allowed = (await self.capabilities([setting_id])).get(setting_id, [])
60
+ if not allowed:
61
+ hint = " (needs Pro controls)" if setting_id in R.PRO_ONLY else ""
62
+ raise Unsupported(f"{name} can't be changed in the camera's current mode{hint}")
63
+ choices = ", ".join(R.value_label(setting_id, v) for v in allowed)
64
+ raise Rejected(
65
+ f"set {name} = {R.value_label(setting_id, value)}",
66
+ f"{R.TLV_STATUS.get(status, status)}; allowed now: {choices}",
67
+ )
68
+ if verify:
69
+ got = await self.setting(setting_id)
70
+ if got != value:
71
+ raise Rejected(
72
+ f"set {name}",
73
+ f"read back {R.value_label(setting_id, got)}, expected {R.value_label(setting_id, value)}",
74
+ )
75
+
76
+ # ---------------------------------------------------------------- clock
77
+
78
+ async def clock(self) -> tuple[dt.datetime, int | None, bool | None]:
79
+ """Camera local time (naive), plus tz offset and DST when the camera reports them."""
80
+ try:
81
+ body = await self.s.command(Cmd.GET_LOCAL_DATETIME)
82
+ except Rejected:
83
+ body = await self.s.command(Cmd.GET_DATETIME)
84
+ return codec.decode_datetime(body[1 : 1 + body[0]])
85
+
86
+ async def sync_clock(self, when: dt.datetime | None = None) -> dt.datetime:
87
+ """Set the camera clock to this machine's local time (with UTC offset and DST)."""
88
+ tz, dst = local_tz()
89
+ if when is None: # land on the second boundary: the camera keeps whole seconds
90
+ frac = time.time() % 1
91
+ await asyncio.sleep((1 - frac) % 1)
92
+ when = dt.datetime.now().replace(microsecond=0)
93
+ await self.s.command(Cmd.SET_LOCAL_DATETIME, codec.encode_datetime(when, tz, dst))
94
+ return when
95
+
96
+ # ---------------------------------------------------------------- capture
97
+
98
+ async def shutter(self, on: bool) -> None:
99
+ await self.s.command(Cmd.SHUTTER, b"\x01" if on else b"\x00")
100
+
101
+ async def hilight(self) -> None:
102
+ await self.s.command(Cmd.HILIGHT)
103
+
104
+ async def sleep(self) -> None:
105
+ """Put the camera to sleep. The camera usually drops the BLE link right after."""
106
+ await self.s.command(Cmd.SLEEP)
107
+
108
+ async def load_group(self, group: str | int) -> None:
109
+ gid = R.PRESET_GROUPS[group] if isinstance(group, str) else group
110
+ await self.s.command(Cmd.LOAD_PRESET_GROUP, gid.to_bytes(2, "big"))
111
+
112
+ async def load_preset(self, preset_id: int) -> None:
113
+ await self.s.command(Cmd.LOAD_PRESET, preset_id.to_bytes(4, "big"))
114
+
115
+ async def presets(self, include_hidden: bool = False) -> list[dict]:
116
+ r = await self.s.protobuf(
117
+ Feature.QUERY,
118
+ Action.PRESET_STATUS,
119
+ "RequestGetPresetStatus",
120
+ {"include_hidden": include_hidden},
121
+ "NotifyPresetStatus",
122
+ )
123
+ return r["preset_group_array"]
124
+
125
+ async def camera_control(self, mode: str) -> None:
126
+ r = await self.s.protobuf(
127
+ Feature.COMMAND,
128
+ Action.CAMERA_CONTROL,
129
+ "RequestSetCameraControlStatus",
130
+ {"camera_control_status": proto.CAMERA_CONTROL[mode]},
131
+ "ResponseGeneric",
132
+ )
133
+ self._check(r, f"camera control {mode}")
134
+ if mode == "external" and not self._claimed:
135
+ self._claimed = True
136
+ self.s.add_cleanup(self._release_control)
137
+ elif mode == "idle":
138
+ self._claimed = False
139
+
140
+ async def _release_control(self) -> None:
141
+ if self._claimed:
142
+ await self.camera_control("idle")
143
+
144
+ async def turbo(self, on: bool) -> None:
145
+ r = await self.s.protobuf(
146
+ Feature.COMMAND, Action.TURBO, "RequestSetTurboActive", {"active": on}, "ResponseGeneric"
147
+ )
148
+ self._check(r, "turbo transfer")
149
+
150
+ @staticmethod
151
+ def _check(r: dict, what: str) -> None:
152
+ result = r.get("result")
153
+ if result == proto.RESULT_NOT_SUPPORTED:
154
+ raise Unsupported(f"{what}: not supported by this camera")
155
+ if result != proto.RESULT_SUCCESS:
156
+ raise Rejected(what, proto.RESULT.get(result, result))
157
+
158
+ # ---------------------------------------------------------------- pushes
159
+
160
+ async def watch_statuses(self, ids) -> tuple[dict[int, int | str], asyncio.Queue]:
161
+ """Register for status pushes. Returns current values and a queue of {id: value} updates."""
162
+ q = self.s.subscribe((R.CQ_QUERY_RESP, Query.STATUS_PUSH))
163
+ body = await self.s.query(Query.REGISTER_STATUSES, ids)
164
+ if not self._status_push:
165
+ self.s.add_cleanup(self._unregister_statuses)
166
+ self._status_push.update(ids)
167
+ updates: asyncio.Queue = asyncio.Queue()
168
+
169
+ async def pump() -> None:
170
+ while True:
171
+ _, msg = await q.get()
172
+ updates.put_nowait({k: R.decode_status_value(k, v) for k, v in codec.parse_tlv(msg[2:])})
173
+
174
+ task = asyncio.create_task(pump())
175
+ self.s.add_cleanup(lambda: _cancel(task))
176
+ return {k: R.decode_status_value(k, v) for k, v in codec.parse_tlv(body)}, updates
177
+
178
+ async def _unregister_statuses(self) -> None:
179
+ if self._status_push:
180
+ await self.s.query(Query.UNREGISTER_STATUSES, sorted(self._status_push))
181
+ self._status_push.clear()
182
+
183
+ async def wait_status(self, status_id: int, predicate, timeout: float, poll: float = 0.5) -> int | str:
184
+ """Poll a status until predicate(value) holds."""
185
+ deadline = asyncio.get_running_loop().time() + timeout
186
+ while True:
187
+ value = await self.status(status_id)
188
+ if predicate(value):
189
+ return value
190
+ if asyncio.get_running_loop().time() > deadline:
191
+ raise Timeout(f"{R.status_name(status_id)} stayed {value!r} for {timeout:.0f} s")
192
+ await asyncio.sleep(poll)
193
+
194
+ # ---------------------------------------------------------------- camera Wi-Fi AP
195
+
196
+ async def set_ap(self, on: bool) -> None:
197
+ await self.s.command(Cmd.AP_CONTROL, b"\x01" if on else b"\x00")
198
+
199
+ async def wifi_credentials(self) -> tuple[str, str]:
200
+ ssid = (await self.s.read(R.WAP_SSID)).decode("utf-8", errors="replace")
201
+ password = (await self.s.read(R.WAP_PASSWORD)).decode("utf-8", errors="replace")
202
+ return ssid, password
203
+
204
+ # ---------------------------------------------------------------- network provisioning
205
+
206
+ async def scan_networks(self, timeout: float = 30.0) -> list[dict]:
207
+ q = self.s.subscribe((R.NET_MGMT_RESP, Feature.NETWORK, Action.NOTIF_SCAN))
208
+ try:
209
+ r = await self.s.protobuf(
210
+ Feature.NETWORK, Action.SCAN_NETWORKS, "RequestStartScan", {}, "ResponseStartScanning"
211
+ )
212
+ self._check(r, "Wi-Fi scan")
213
+ while True:
214
+ _, msg = await asyncio.wait_for(q.get(), timeout)
215
+ n = proto.decode("NotifStartScanning", msg[2:])
216
+ if n.get("scanning_state") == proto.SCANNING_SUCCESS:
217
+ break
218
+ if n.get("scanning_state") in (3, 4):
219
+ raise Rejected("Wi-Fi scan", proto.SCANNING[n["scanning_state"]])
220
+ except asyncio.TimeoutError:
221
+ raise Timeout(f"the camera's Wi-Fi scan didn't finish within {timeout:.0f} s") from None
222
+ finally:
223
+ self.s.unsubscribe(q)
224
+ r = await self.s.protobuf(
225
+ Feature.NETWORK,
226
+ Action.GET_AP_ENTRIES,
227
+ "RequestGetApEntries",
228
+ {"start_index": 0, "max_entries": 100, "scan_id": n.get("scan_id", 0)},
229
+ "ResponseGetApEntries",
230
+ )
231
+ self._check(r, "Wi-Fi scan results")
232
+ return r["entries"]
233
+
234
+ async def join_network(self, ssid: str, password: str | None, configured: bool, timeout: float = 60.0) -> int:
235
+ """Ask the camera to join a Wi-Fi network. Returns the final provisioning state (5 or 6)."""
236
+ q = self.s.subscribe((R.NET_MGMT_RESP, Feature.NETWORK, Action.NOTIF_PROVISIONING))
237
+ try:
238
+ state = await self._provision(q, ssid, password, configured, timeout)
239
+ if state == 8 and configured and password is not None: # remembered entry with a stale password
240
+ state = await self._provision(q, ssid, password, False, timeout)
241
+ finally:
242
+ self.s.unsubscribe(q)
243
+ if state not in proto.PROVISIONING_DONE:
244
+ raise Rejected(f"join {ssid!r}", proto.PROVISIONING.get(state, state))
245
+ return state
246
+
247
+ async def _provision(
248
+ self, q: asyncio.Queue, ssid: str, password: str | None, configured: bool, timeout: float
249
+ ) -> int:
250
+ if configured:
251
+ r = await self.s.protobuf(
252
+ Feature.NETWORK, Action.CONNECT, "RequestConnect", {"ssid": ssid}, "ResponseConnect"
253
+ )
254
+ else:
255
+ r = await self.s.protobuf(
256
+ Feature.NETWORK,
257
+ Action.CONNECT_NEW,
258
+ "RequestConnectNew",
259
+ {"ssid": ssid, "password": password or "", "bypass_eula_check": True},
260
+ "ResponseConnectNew",
261
+ )
262
+ self._check(r, f"join {ssid!r}")
263
+ state = r.get("provisioning_state", 0)
264
+ deadline = asyncio.get_running_loop().time() + timeout
265
+ while state in (0, 1, 2):
266
+ left = deadline - asyncio.get_running_loop().time()
267
+ if left <= 0:
268
+ raise Timeout(f"the camera didn't finish joining {ssid!r} within {timeout:.0f} s")
269
+ try:
270
+ _, msg = await asyncio.wait_for(q.get(), left)
271
+ except asyncio.TimeoutError:
272
+ continue
273
+ state = proto.decode("NotifProvisioningState", msg[2:]).get("provisioning_state", 0)
274
+ return state
275
+
276
+ async def release_network(self) -> None:
277
+ r = await self.s.protobuf(
278
+ Feature.COMMAND, Action.RELEASE_NETWORK, "RequestReleaseNetwork", {}, "ResponseGeneric"
279
+ )
280
+ self._check(r, "release network")
281
+
282
+ # ---------------------------------------------------------------- livestream
283
+
284
+ async def livestream_status(self, register: bool = False) -> dict:
285
+ req = {"register_live_stream_status": list(proto.REGISTER_LIVE_STREAM_STATUS) if register else []}
286
+ return await self.s.protobuf(
287
+ Feature.QUERY, Action.LIVESTREAM_STATUS, "RequestGetLiveStreamStatus", req, "NotifyLiveStreamStatus"
288
+ )
289
+
290
+ async def set_livestream_mode(
291
+ self,
292
+ url: str,
293
+ *,
294
+ encode: bool = False,
295
+ resolution: int = 720,
296
+ min_kbps: int = 800,
297
+ max_kbps: int = 8000,
298
+ start_kbps: int = 4000,
299
+ lens: str = "wide",
300
+ ) -> None:
301
+ values = (
302
+ {
303
+ "url": url,
304
+ "encode": encode,
305
+ "window_size": proto.WINDOW_SIZE[resolution],
306
+ "minimum_bitrate": min_kbps,
307
+ "maximum_bitrate": max_kbps,
308
+ "starting_bitrate": start_kbps,
309
+ "lens": proto.LENS[lens],
310
+ }
311
+ if url
312
+ else {"url": ""}
313
+ )
314
+ r = await self.s.protobuf(
315
+ Feature.COMMAND, Action.LIVESTREAM_MODE, "RequestSetLiveStreamMode", values, "ResponseGeneric"
316
+ )
317
+ self._check(r, "set livestream mode")
318
+
319
+
320
+ async def _cancel(task: asyncio.Task) -> None:
321
+ task.cancel()
quak/cli/__init__.py ADDED
File without changes