pyvisionect 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.
- pyvisionect/__init__.py +120 -0
- pyvisionect/devices/__init__.py +92 -0
- pyvisionect/devices/enums.py +403 -0
- pyvisionect/devices/panels.py +242 -0
- pyvisionect/devices/tclv.py +460 -0
- pyvisionect/imaging/README.md +175 -0
- pyvisionect/imaging/__init__.py +181 -0
- pyvisionect/imaging/checksum.py +114 -0
- pyvisionect/imaging/constants.py +131 -0
- pyvisionect/imaging/data/blue_noise_64.npy +0 -0
- pyvisionect/imaging/decode.py +172 -0
- pyvisionect/imaging/dither.py +357 -0
- pyvisionect/imaging/encoder.py +594 -0
- pyvisionect/imaging/geometry.py +129 -0
- pyvisionect/imaging/grey.py +104 -0
- pyvisionect/imaging/interlace.py +244 -0
- pyvisionect/imaging/pack.py +142 -0
- pyvisionect/imaging/panel.py +339 -0
- pyvisionect/imaging/partial.py +931 -0
- pyvisionect/imaging/rects.py +466 -0
- pyvisionect/io/__init__.py +33 -0
- pyvisionect/io/tcp.py +795 -0
- pyvisionect/io/usb/__init__.py +237 -0
- pyvisionect/io/usb/cli.py +278 -0
- pyvisionect/io/usb/commands.py +781 -0
- pyvisionect/io/usb/console.py +730 -0
- pyvisionect/io/usb/device.py +914 -0
- pyvisionect/io/usb/discovery.py +260 -0
- pyvisionect/io/usb/encryption.py +262 -0
- pyvisionect/io/usb/errors.py +175 -0
- pyvisionect/io/usb/fake.py +292 -0
- pyvisionect/io/usb/parsers.py +1464 -0
- pyvisionect/io/usb/provisioning.py +502 -0
- pyvisionect/packets/__init__.py +135 -0
- pyvisionect/packets/control.py +95 -0
- pyvisionect/packets/file.py +325 -0
- pyvisionect/packets/image.py +234 -0
- pyvisionect/packets/misc.py +202 -0
- pyvisionect/packets/param.py +284 -0
- pyvisionect/packets/status.py +518 -0
- pyvisionect/packets/stored.py +217 -0
- pyvisionect/py.typed +0 -0
- pyvisionect/session/__init__.py +100 -0
- pyvisionect/session/connection.py +1043 -0
- pyvisionect/session/device.py +587 -0
- pyvisionect/session/events.py +196 -0
- pyvisionect/session/filetransfer.py +251 -0
- pyvisionect/session/pending.py +425 -0
- pyvisionect/wire/__init__.py +91 -0
- pyvisionect/wire/blocks.py +350 -0
- pyvisionect/wire/checksum.py +115 -0
- pyvisionect/wire/errors.py +176 -0
- pyvisionect/wire/framing.py +334 -0
- pyvisionect-0.1.0.dist-info/METADATA +1030 -0
- pyvisionect-0.1.0.dist-info/RECORD +58 -0
- pyvisionect-0.1.0.dist-info/WHEEL +4 -0
- pyvisionect-0.1.0.dist-info/entry_points.txt +3 -0
- pyvisionect-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Events a :class:`~pyvisionect.session.connection.DeviceConnection` emits.
|
|
2
|
+
|
|
3
|
+
All events are frozen dataclasses with no behaviour. ``feed()`` and
|
|
4
|
+
``advance()`` return lists of them; the caller decides what to do.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from ..packets import (
|
|
13
|
+
ButtonPacket,
|
|
14
|
+
CborPacket,
|
|
15
|
+
ControlPacket,
|
|
16
|
+
FilePacket,
|
|
17
|
+
GpsPacket,
|
|
18
|
+
ParamPacket,
|
|
19
|
+
StatusPacket,
|
|
20
|
+
TouchPacket,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"Event",
|
|
25
|
+
"DeviceConnected",
|
|
26
|
+
"StatusReceived",
|
|
27
|
+
"Acked",
|
|
28
|
+
"Nacked",
|
|
29
|
+
"ParamsReceived",
|
|
30
|
+
"FileEventReceived",
|
|
31
|
+
"TouchReceived",
|
|
32
|
+
"ButtonReceived",
|
|
33
|
+
"GpsReceived",
|
|
34
|
+
"CborReceived",
|
|
35
|
+
"UnknownPacketType",
|
|
36
|
+
"ProtocolViolation",
|
|
37
|
+
"WatchdogFired",
|
|
38
|
+
"ReadTimeout",
|
|
39
|
+
"HeartbeatOverdue",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class Event:
|
|
45
|
+
"""Base class. Carries the device UUID when one is known."""
|
|
46
|
+
|
|
47
|
+
device_id: bytes = field(default=b"", kw_only=True)
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def uuid(self) -> str:
|
|
51
|
+
"""The UUID in canonical 8-4-4-4-12 form, or ``""``."""
|
|
52
|
+
if len(self.device_id) != 16:
|
|
53
|
+
return ""
|
|
54
|
+
h = self.device_id.hex()
|
|
55
|
+
return f"{h[0:8]}-{h[8:12]}-{h[12:16]}-{h[16:20]}-{h[20:32]}"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True, slots=True)
|
|
59
|
+
class DeviceConnected(Event):
|
|
60
|
+
"""The first status packet of a TCP connection arrived.
|
|
61
|
+
|
|
62
|
+
There is no handshake: the UUID in this packet's ``DataHeader`` is the entire
|
|
63
|
+
identity claim, and registration is auto-create on first connect. The
|
|
64
|
+
gateway requires the first packet to be a status packet
|
|
65
|
+
(``main.ErrNotFirstStatusPacket``).
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
status: StatusPacket = field(default_factory=StatusPacket)
|
|
69
|
+
packet_id: int = 0
|
|
70
|
+
connect_reason: int | None = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(frozen=True, slots=True)
|
|
74
|
+
class StatusReceived(Event):
|
|
75
|
+
"""A status packet (type 3). Also resets the server's inactivity watchdog."""
|
|
76
|
+
|
|
77
|
+
status: StatusPacket = field(default_factory=StatusPacket)
|
|
78
|
+
packet_id: int = 0
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass(frozen=True, slots=True)
|
|
82
|
+
class Acked(Event):
|
|
83
|
+
"""The device acked one of our requests (``Control{Flags & 1}``)."""
|
|
84
|
+
|
|
85
|
+
packet_id: int = 0
|
|
86
|
+
control: ControlPacket | None = None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass(frozen=True, slots=True)
|
|
90
|
+
class Nacked(Event):
|
|
91
|
+
"""The device refused one of our requests.
|
|
92
|
+
|
|
93
|
+
``charging`` is control flag bit 25 (``vio.ErrNackCharging``).
|
|
94
|
+
``error_code`` is the uint32 a device NACK carries in its payload, e.g.
|
|
95
|
+
``0x00008a00`` for the ``@screen_N`` open the 32" sign always refuses.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
packet_id: int = 0
|
|
99
|
+
control: ControlPacket | None = None
|
|
100
|
+
charging: bool = False
|
|
101
|
+
error_code: int | None = None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True, slots=True)
|
|
105
|
+
class ParamsReceived(Event):
|
|
106
|
+
"""A param packet (type 8) -- the reply to a read, or a write result."""
|
|
107
|
+
|
|
108
|
+
params: ParamPacket = field(default_factory=ParamPacket)
|
|
109
|
+
packet_id: int = 0
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass(frozen=True, slots=True)
|
|
113
|
+
class FileEventReceived(Event):
|
|
114
|
+
"""A file packet (type 10) from the device."""
|
|
115
|
+
|
|
116
|
+
file: FilePacket | None = None
|
|
117
|
+
packet_id: int = 0
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@dataclass(frozen=True, slots=True)
|
|
121
|
+
class TouchReceived(Event):
|
|
122
|
+
touch: TouchPacket | None = None
|
|
123
|
+
packet_id: int = 0
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@dataclass(frozen=True, slots=True)
|
|
127
|
+
class ButtonReceived(Event):
|
|
128
|
+
button: ButtonPacket | None = None
|
|
129
|
+
packet_id: int = 0
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@dataclass(frozen=True, slots=True)
|
|
133
|
+
class GpsReceived(Event):
|
|
134
|
+
gps: GpsPacket | None = None
|
|
135
|
+
packet_id: int = 0
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@dataclass(frozen=True, slots=True)
|
|
139
|
+
class CborReceived(Event):
|
|
140
|
+
"""The "AC"/JS-app channel. Firmware 7.4.4407 never sends this."""
|
|
141
|
+
|
|
142
|
+
cbor: CborPacket | None = None
|
|
143
|
+
packet_id: int = 0
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass(frozen=True, slots=True)
|
|
147
|
+
class UnknownPacketType(Event):
|
|
148
|
+
"""A packet type with no handler. We NACK it, as the gateway does."""
|
|
149
|
+
|
|
150
|
+
packet_type: int = 0
|
|
151
|
+
packet_id: int = 0
|
|
152
|
+
payload: bytes = b""
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@dataclass(frozen=True, slots=True)
|
|
156
|
+
class ProtocolViolation(Event):
|
|
157
|
+
"""Something the peer did that the protocol does not allow.
|
|
158
|
+
|
|
159
|
+
The connection should normally be closed. Carries the exception when one was
|
|
160
|
+
raised.
|
|
161
|
+
"""
|
|
162
|
+
|
|
163
|
+
reason: str = ""
|
|
164
|
+
error: Exception | None = None
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass(frozen=True, slots=True)
|
|
168
|
+
class WatchdogFired(Event):
|
|
169
|
+
"""The inactivity watchdog expired and a status request has been queued.
|
|
170
|
+
|
|
171
|
+
The gateway's behaviour exactly: on expiry it sends
|
|
172
|
+
``Command{Type: 11}`` ("status request") and closes the connection if that
|
|
173
|
+
request fails.
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
quiet_for: float = 0.0
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@dataclass(frozen=True, slots=True)
|
|
180
|
+
class ReadTimeout(Event):
|
|
181
|
+
"""No frame arrived within ``PacketTimeout`` seconds (config default 300)."""
|
|
182
|
+
|
|
183
|
+
quiet_for: float = 0.0
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
@dataclass(frozen=True, slots=True)
|
|
187
|
+
class HeartbeatOverdue(Event):
|
|
188
|
+
"""The device missed the deadline it announced in status tag 27 ``NextStatus``.
|
|
189
|
+
|
|
190
|
+
Advisory only: ``NextStatus`` is in **minutes** and the device's own
|
|
191
|
+
heartbeat interval (TCLV 29) is what really governs. Nothing in the gateway
|
|
192
|
+
acts on this; it is exposed because it is a cheap health signal.
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
expected_within: float = 0.0
|
|
196
|
+
quiet_for: float = 0.0
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"""Reading a file off the device, as a sans-io state machine.
|
|
2
|
+
|
|
3
|
+
The file protocol has no "read file" call. It has ``open``, ``read``,
|
|
4
|
+
``close``, and a reply channel, and the device's answers come back as
|
|
5
|
+
asynchronous ``packet.File`` events rather than as returns. So a caller who
|
|
6
|
+
wants a file ends up writing a small sequencer; this is that sequencer, written
|
|
7
|
+
once, pure, and testable without a socket.
|
|
8
|
+
|
|
9
|
+
seq = FileRead("/image0.pv2", length=134409)
|
|
10
|
+
seq.start(conn) # queues the open
|
|
11
|
+
for event in conn.feed(data): # as events arrive
|
|
12
|
+
seq.on_event(event, conn) # queues the next read, or the close
|
|
13
|
+
if seq.done:
|
|
14
|
+
raw = seq.data
|
|
15
|
+
|
|
16
|
+
What the device actually does
|
|
17
|
+
-----------------------------
|
|
18
|
+
|
|
19
|
+
Measured against firmware 7.4.4407:
|
|
20
|
+
|
|
21
|
+
* **Replies arrive as ``Op 6`` (``event``), not ``Op 2`` (``read``).** ``read``
|
|
22
|
+
is the request direction only.
|
|
23
|
+
* **One reply carries at most 1024 bytes**
|
|
24
|
+
(:data:`~pyvisionect.packets.file.DEVICE_FILE_READ_CHUNK`), whatever you ask
|
|
25
|
+
for. Asking for 32768 gets you 1024 and nothing else.
|
|
26
|
+
* **There is no seek.** ``open`` rewinds to zero and each reply advances the
|
|
27
|
+
cursor; nothing else moves it. So a reply that never comes cannot be
|
|
28
|
+
resumed past -- :attr:`FileRead.restart` exists because the only recovery is
|
|
29
|
+
to start the file again from offset 0.
|
|
30
|
+
* Throughput is **~2.3 KiB/s** (~420 ms per round trip). A 1.2 MB file is
|
|
31
|
+
nine minutes of chat. Do not put one behind a service call without telling
|
|
32
|
+
the user.
|
|
33
|
+
|
|
34
|
+
Timeouts are the caller's
|
|
35
|
+
-------------------------
|
|
36
|
+
|
|
37
|
+
Nothing here reads a clock. :attr:`FileRead.awaiting_reply` tells an io layer
|
|
38
|
+
when it is waiting on the device, and that is where a deadline belongs.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
from __future__ import annotations
|
|
42
|
+
|
|
43
|
+
from dataclasses import dataclass, field
|
|
44
|
+
from typing import Any
|
|
45
|
+
|
|
46
|
+
from ..devices.enums import FileMode
|
|
47
|
+
from ..packets.file import DEVICE_FILE_READ_CHUNK
|
|
48
|
+
from . import events as ev
|
|
49
|
+
|
|
50
|
+
__all__ = ["FileRead", "FileReadState"]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class FileReadState:
|
|
54
|
+
"""Where a :class:`FileRead` has got to."""
|
|
55
|
+
|
|
56
|
+
IDLE = "idle"
|
|
57
|
+
OPENING = "opening"
|
|
58
|
+
READING = "reading"
|
|
59
|
+
CLOSING = "closing"
|
|
60
|
+
DONE = "done"
|
|
61
|
+
FAILED = "failed"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class FileRead:
|
|
66
|
+
"""Sequence ``open -> read* -> close`` for one file on one device.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
filename: the device path, e.g. ``"/image0.pv2"``. A listing gives
|
|
70
|
+
these with their leading slash; pass them through unchanged.
|
|
71
|
+
length: how many bytes to read. Take it from the directory listing's
|
|
72
|
+
size column.
|
|
73
|
+
chunk: bytes per request. Values above
|
|
74
|
+
:data:`~pyvisionect.packets.file.DEVICE_FILE_READ_CHUNK` are
|
|
75
|
+
clamped, because the device would truncate them anyway and a
|
|
76
|
+
caller that believes its own number ends up mis-counting progress.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
filename: str
|
|
80
|
+
length: int
|
|
81
|
+
chunk: int = DEVICE_FILE_READ_CHUNK
|
|
82
|
+
stop_on_short: bool = False
|
|
83
|
+
"""Treat a reply shorter than the request as end-of-file.
|
|
84
|
+
|
|
85
|
+
Off for a real file, where *length* comes from the directory listing and a
|
|
86
|
+
short reply means something went wrong. On for a **directory listing**,
|
|
87
|
+
which is "read up to 10240 bytes of ``'.'``" and answers with however many
|
|
88
|
+
bytes the listing happens to be -- 129 on this firmware. Without this the
|
|
89
|
+
sequencer would keep asking for the remaining 10111 bytes of a file that
|
|
90
|
+
does not have them.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
state: str = FileReadState.IDLE
|
|
94
|
+
error: str | None = None
|
|
95
|
+
buffer: bytearray = field(default_factory=bytearray, repr=False)
|
|
96
|
+
reads: int = 0
|
|
97
|
+
"""How many reply chunks have been accepted."""
|
|
98
|
+
restarts: int = 0
|
|
99
|
+
"""How many times :meth:`restart` has thrown the buffer away."""
|
|
100
|
+
|
|
101
|
+
_last_request: int = field(default=0, repr=False)
|
|
102
|
+
_open_id: int | None = field(default=None, repr=False)
|
|
103
|
+
_close_id: int | None = field(default=None, repr=False)
|
|
104
|
+
_read_id: int | None = field(default=None, repr=False)
|
|
105
|
+
|
|
106
|
+
def __post_init__(self) -> None:
|
|
107
|
+
if self.length < 0:
|
|
108
|
+
raise ValueError(f"length must not be negative, got {self.length}")
|
|
109
|
+
self.chunk = max(1, min(int(self.chunk), DEVICE_FILE_READ_CHUNK))
|
|
110
|
+
|
|
111
|
+
# ------------------------------------------------------------- status
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def done(self) -> bool:
|
|
115
|
+
"""Whether the whole file is in :attr:`data`."""
|
|
116
|
+
return self.state == FileReadState.DONE
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def failed(self) -> bool:
|
|
120
|
+
return self.state == FileReadState.FAILED
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def finished(self) -> bool:
|
|
124
|
+
"""Done or failed -- i.e. nothing more will happen without a restart."""
|
|
125
|
+
return self.state in (FileReadState.DONE, FileReadState.FAILED)
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def awaiting_reply(self) -> bool:
|
|
129
|
+
"""Whether the device owes us something. Where a timeout belongs."""
|
|
130
|
+
return self.state in (
|
|
131
|
+
FileReadState.OPENING,
|
|
132
|
+
FileReadState.READING,
|
|
133
|
+
FileReadState.CLOSING,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
@property
|
|
137
|
+
def data(self) -> bytes:
|
|
138
|
+
return bytes(self.buffer)
|
|
139
|
+
|
|
140
|
+
@property
|
|
141
|
+
def bytes_read(self) -> int:
|
|
142
|
+
return len(self.buffer)
|
|
143
|
+
|
|
144
|
+
@property
|
|
145
|
+
def progress(self) -> float:
|
|
146
|
+
"""0.0 .. 1.0. Returns 1.0 for a zero-length file."""
|
|
147
|
+
if self.length <= 0:
|
|
148
|
+
return 1.0
|
|
149
|
+
return min(1.0, len(self.buffer) / self.length)
|
|
150
|
+
|
|
151
|
+
# ------------------------------------------------------------ driving
|
|
152
|
+
|
|
153
|
+
def start(self, conn: Any) -> int:
|
|
154
|
+
"""Queue the ``open``. Returns its packet id."""
|
|
155
|
+
self.state = FileReadState.OPENING
|
|
156
|
+
self.error = None
|
|
157
|
+
self._open_id = conn.file_open(self.filename, FileMode.READ)
|
|
158
|
+
return self._open_id
|
|
159
|
+
|
|
160
|
+
def restart(self, conn: Any) -> int:
|
|
161
|
+
"""Throw away what we have and open the file again from offset 0.
|
|
162
|
+
|
|
163
|
+
The only recovery the protocol offers: there is no seek, so a lost
|
|
164
|
+
reply means the cursor is in a place we cannot name.
|
|
165
|
+
"""
|
|
166
|
+
self.restarts += 1
|
|
167
|
+
self.buffer.clear()
|
|
168
|
+
self.reads = 0
|
|
169
|
+
self._read_id = None
|
|
170
|
+
self._close_id = None
|
|
171
|
+
return self.start(conn)
|
|
172
|
+
|
|
173
|
+
def fail(self, reason: str) -> None:
|
|
174
|
+
"""Give up with *reason*. Idempotent-ish; the first reason wins."""
|
|
175
|
+
if self.state != FileReadState.FAILED:
|
|
176
|
+
self.state = FileReadState.FAILED
|
|
177
|
+
self.error = reason
|
|
178
|
+
|
|
179
|
+
def on_event(self, event: Any, conn: Any) -> bool:
|
|
180
|
+
"""Feed one library event. Returns True if it belonged to this read.
|
|
181
|
+
|
|
182
|
+
Queues whatever comes next on *conn*; the caller still has to drain
|
|
183
|
+
and write ``conn.drain()``.
|
|
184
|
+
"""
|
|
185
|
+
if isinstance(event, ev.Nacked):
|
|
186
|
+
if event.packet_id in (self._open_id, self._read_id, self._close_id):
|
|
187
|
+
which = (
|
|
188
|
+
"open"
|
|
189
|
+
if event.packet_id == self._open_id
|
|
190
|
+
else "read"
|
|
191
|
+
if event.packet_id == self._read_id
|
|
192
|
+
else "close"
|
|
193
|
+
)
|
|
194
|
+
code = (
|
|
195
|
+
f" (error {event.error_code:#010x})"
|
|
196
|
+
if event.error_code is not None
|
|
197
|
+
else ""
|
|
198
|
+
)
|
|
199
|
+
self.fail(f"device refused the {which} of {self.filename!r}{code}")
|
|
200
|
+
return True
|
|
201
|
+
return False
|
|
202
|
+
|
|
203
|
+
if isinstance(event, ev.Acked):
|
|
204
|
+
if event.packet_id == self._open_id and self.state == FileReadState.OPENING:
|
|
205
|
+
if self.length == 0:
|
|
206
|
+
self._finish(conn)
|
|
207
|
+
else:
|
|
208
|
+
self._request_next(conn)
|
|
209
|
+
return True
|
|
210
|
+
if event.packet_id == self._close_id:
|
|
211
|
+
self.state = FileReadState.DONE
|
|
212
|
+
return True
|
|
213
|
+
# An ack for a read request is fine and carries nothing; the bytes
|
|
214
|
+
# come separately as a file event.
|
|
215
|
+
return event.packet_id == self._read_id
|
|
216
|
+
|
|
217
|
+
if isinstance(event, ev.FileEventReceived):
|
|
218
|
+
if self.state != FileReadState.READING:
|
|
219
|
+
return False
|
|
220
|
+
payload = event.file.raw if event.file is not None else b""
|
|
221
|
+
if not payload:
|
|
222
|
+
if self.stop_on_short:
|
|
223
|
+
self._finish(conn)
|
|
224
|
+
else:
|
|
225
|
+
self.fail(
|
|
226
|
+
f"empty reply at offset {len(self.buffer)} of "
|
|
227
|
+
f"{self.filename!r}"
|
|
228
|
+
)
|
|
229
|
+
return True
|
|
230
|
+
short = len(payload) < self._last_request
|
|
231
|
+
self.buffer += payload
|
|
232
|
+
self.reads += 1
|
|
233
|
+
if len(self.buffer) >= self.length or (self.stop_on_short and short):
|
|
234
|
+
del self.buffer[self.length :]
|
|
235
|
+
self._finish(conn)
|
|
236
|
+
else:
|
|
237
|
+
self._request_next(conn)
|
|
238
|
+
return True
|
|
239
|
+
|
|
240
|
+
return False
|
|
241
|
+
|
|
242
|
+
# ------------------------------------------------------------ internals
|
|
243
|
+
|
|
244
|
+
def _request_next(self, conn: Any) -> None:
|
|
245
|
+
self.state = FileReadState.READING
|
|
246
|
+
self._last_request = min(self.chunk, self.length - len(self.buffer))
|
|
247
|
+
self._read_id = conn.file_read(self._last_request)
|
|
248
|
+
|
|
249
|
+
def _finish(self, conn: Any) -> None:
|
|
250
|
+
self.state = FileReadState.CLOSING
|
|
251
|
+
self._close_id = conn.file_close()
|