cs-buffer 20250111__py2.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.
- cs/buffer.py +1395 -0
- cs_buffer-20250111.dist-info/METADATA +711 -0
- cs_buffer-20250111.dist-info/RECORD +4 -0
- cs_buffer-20250111.dist-info/WHEEL +5 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: cs-buffer
|
|
3
|
+
Version: 20250111
|
|
4
|
+
Summary: Facilities to do with buffers, particularly CornuCopyBuffer, an automatically refilling buffer to support parsing of data streams.
|
|
5
|
+
Keywords: python3
|
|
6
|
+
Author-email: Cameron Simpson <cs@cskk.id.au>
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Classifier: Programming Language :: Python
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
|
|
15
|
+
Requires-Dist: cs.deco>=20250103
|
|
16
|
+
Requires-Dist: cs.gimmicks>=20240316
|
|
17
|
+
Project-URL: MonoRepo Commits, https://bitbucket.org/cameron_simpson/css/commits/branch/main
|
|
18
|
+
Project-URL: Monorepo Git Mirror, https://github.com/cameron-simpson/css
|
|
19
|
+
Project-URL: Monorepo Hg/Mercurial Mirror, https://hg.sr.ht/~cameron-simpson/css
|
|
20
|
+
Project-URL: Source, https://github.com/cameron-simpson/css/blob/main/lib/python/cs/buffer.py
|
|
21
|
+
|
|
22
|
+
Facilities to do with buffers, particularly CornuCopyBuffer,
|
|
23
|
+
an automatically refilling buffer to support parsing of data streams.
|
|
24
|
+
|
|
25
|
+
*Latest release 20250111*:
|
|
26
|
+
DISTINFO: move python_version>=3.3 out to the "makers" section (presemly unused) because it was being used as a dependency, finding the python_version PyPI dsitribution.
|
|
27
|
+
|
|
28
|
+
## <a name="CopyingIterator"></a>Class `CopyingIterator`
|
|
29
|
+
|
|
30
|
+
Wrapper for an iterator that copies every item retrieved to a callable.
|
|
31
|
+
|
|
32
|
+
*`CopyingIterator.__init__(self, it, copy_to)`*:
|
|
33
|
+
Initialise with the iterator `it` and the callable `copy_to`.
|
|
34
|
+
|
|
35
|
+
## <a name="CornuCopyBuffer"></a>Class `CornuCopyBuffer(cs.deco.Promotable)`
|
|
36
|
+
|
|
37
|
+
An automatically refilling buffer intended to support parsing
|
|
38
|
+
of data streams.
|
|
39
|
+
|
|
40
|
+
Its primary purpose is to aid binary parsers
|
|
41
|
+
which do not themselves need to handle sources specially;
|
|
42
|
+
`CornuCopyBuffer`s are trivially made from `bytes`,
|
|
43
|
+
iterables of `bytes` and file-like objects.
|
|
44
|
+
See `cs.binary` for convenient parsing classes
|
|
45
|
+
which work with `CornuCopyBuffer`s.
|
|
46
|
+
|
|
47
|
+
A `CornuCopyBuffer` is iterable, yielding data in whatever
|
|
48
|
+
sizes come from its `input_data` source, preceeded by any
|
|
49
|
+
content in the internal buffer.
|
|
50
|
+
|
|
51
|
+
A `CornuCopyBuffer` also supports the file methods `.read`,
|
|
52
|
+
`.tell` and `.seek` supporting drop in use of the buffer in
|
|
53
|
+
many file contexts. Backward seeks are not supported. `.seek`
|
|
54
|
+
will take advantage of the `input_data`'s `.seek` method if it
|
|
55
|
+
has one, otherwise it will use consume the `input_data`
|
|
56
|
+
as required.
|
|
57
|
+
|
|
58
|
+
Attributes:
|
|
59
|
+
* `buf`: the first of any buffered leading chunks
|
|
60
|
+
buffer of unparsed data from the input, available
|
|
61
|
+
for direct inspection by parsers;
|
|
62
|
+
normally however parsers will use `.extend` and `.take`.
|
|
63
|
+
* `offset`: the logical offset of the buffer; this excludes
|
|
64
|
+
buffered data and unconsumed input data
|
|
65
|
+
|
|
66
|
+
*Note*: the initialiser may supply a cleanup function;
|
|
67
|
+
although this will be called via the buffer's `.__del__` method
|
|
68
|
+
a prudent user of a buffer should call the `.close()` method
|
|
69
|
+
when finished with the buffer to ensure prompt cleanup;
|
|
70
|
+
the `contextlib.closing` context manager provides an easy way
|
|
71
|
+
to do this in common cases.
|
|
72
|
+
|
|
73
|
+
The primary methods supporting parsing of data streams are
|
|
74
|
+
`.extend()` and `take()`.
|
|
75
|
+
Calling `.extend(min_size)` arranges that the internal buffer
|
|
76
|
+
contains at least `min_size` bytes.
|
|
77
|
+
Calling `.take(size)` fetches exactly `size` bytes from the
|
|
78
|
+
internal buffer and the input source if necessary and returns
|
|
79
|
+
them, adjusting the internal buffer.
|
|
80
|
+
|
|
81
|
+
len(`CornuCopyBuffer`) returns the length of any buffered data.
|
|
82
|
+
|
|
83
|
+
bool(`CornuCopyBuffer`) tests whether len() > 0.
|
|
84
|
+
|
|
85
|
+
Indexing a `CornuCopyBuffer` accesses the buffered data only,
|
|
86
|
+
returning an individual byte's value (an `int`).
|
|
87
|
+
|
|
88
|
+
*`CornuCopyBuffer.__init__(self, input_data, buf=None, offset=0, seekable=None, copy_offsets=None, copy_chunks=None, close=None, progress=None, final_offset=None)`*:
|
|
89
|
+
Prepare the buffer.
|
|
90
|
+
|
|
91
|
+
Parameters:
|
|
92
|
+
* `input_data`: an iterable of data chunks (`bytes`-like instances);
|
|
93
|
+
if your data source is a file see the `.from_file` factory;
|
|
94
|
+
if your data source is a file descriptor see the `.from_fd`
|
|
95
|
+
factory.
|
|
96
|
+
* `buf`: if not `None`, the initial state of the parse buffer
|
|
97
|
+
* `offset`: logical offset of the start of the buffer, default `0`
|
|
98
|
+
* `seekable`: whether `input_data` has a working `.seek` method;
|
|
99
|
+
the default is `None` meaning that it will be attempted on
|
|
100
|
+
the first skip or seek
|
|
101
|
+
* `copy_offsets`: if not `None`, a callable for parsers to
|
|
102
|
+
report pertinent offsets via the buffer's `.report_offset`
|
|
103
|
+
method
|
|
104
|
+
* `copy_chunks`: if not `None`, every fetched data chunk is
|
|
105
|
+
copied to this callable
|
|
106
|
+
|
|
107
|
+
The `input_data` is an iterable whose iterator may have
|
|
108
|
+
some optional additional properties:
|
|
109
|
+
* `seek`: if present, this is a seek method after the fashion
|
|
110
|
+
of `file.seek`; the buffer's `seek`, `skip` and `skipto`
|
|
111
|
+
methods will take advantage of this if available.
|
|
112
|
+
* `offset`: the current byte offset of the iterator; this
|
|
113
|
+
is used during the buffer initialisation to compute
|
|
114
|
+
`input_data_displacement`, the difference between the
|
|
115
|
+
buffer's logical offset and the input data iterable's logical offset;
|
|
116
|
+
if unavailable during initialisation this is presumed to
|
|
117
|
+
be `0`.
|
|
118
|
+
* `end_offset`: the end offset of the iterator if known.
|
|
119
|
+
* `close`: an optional callable
|
|
120
|
+
that may be provided for resource cleanup
|
|
121
|
+
when the user of the buffer calls its `.close()` method.
|
|
122
|
+
* `progress`: an optional `cs.Progress.progress` instance
|
|
123
|
+
to which to report data consumed from `input_data`;
|
|
124
|
+
any object supporting `+=` is acceptable
|
|
125
|
+
* `final_offset`: optional `int` specifying the largest
|
|
126
|
+
offset expected to be reached, intended for uses such as
|
|
127
|
+
callers presenting a pregress indication; this is, for
|
|
128
|
+
example, provided by `CornuCopyBuffer.from_fd` for regular
|
|
129
|
+
files using the `stat.st_size` field
|
|
130
|
+
|
|
131
|
+
*`CornuCopyBuffer.__del__(self)`*:
|
|
132
|
+
Release resources when the object is deleted.
|
|
133
|
+
|
|
134
|
+
*`CornuCopyBuffer.__getitem__(self, index)`*:
|
|
135
|
+
Fetch from the internal buffer.
|
|
136
|
+
This does not consume data from the internal buffer.
|
|
137
|
+
Note that this is an expensive way to access the buffer,
|
|
138
|
+
particularly if `index` is a slice.
|
|
139
|
+
|
|
140
|
+
If `index` is a `slice`, slice the join of the internal subbuffers.
|
|
141
|
+
This is quite expensive
|
|
142
|
+
and it is probably better to `take` or `takev`
|
|
143
|
+
some data from the buffer.
|
|
144
|
+
|
|
145
|
+
Otherwise `index` should be an `int` and the corresponding
|
|
146
|
+
buffered byte is returned.
|
|
147
|
+
|
|
148
|
+
This is usually not a very useful method;
|
|
149
|
+
its primary use case is to probe the buffer to make a parsing decision
|
|
150
|
+
instead of taking a byte off and (possibly) pushing it back.
|
|
151
|
+
|
|
152
|
+
*`CornuCopyBuffer.__len__(self)`*:
|
|
153
|
+
The length is the length of the internal buffer: data available without a fetch.
|
|
154
|
+
|
|
155
|
+
*`CornuCopyBuffer.__next__(self)`*:
|
|
156
|
+
Fetch a data chunk from the buffer.
|
|
157
|
+
|
|
158
|
+
*`CornuCopyBuffer.as_fd(self, maxlength=Ellipsis)`*:
|
|
159
|
+
Create a pipe and dispatch a `Thread` to copy
|
|
160
|
+
up to `maxlength` bytes from `bfr` into it.
|
|
161
|
+
Return the file descriptor of the read end of the pipe.
|
|
162
|
+
|
|
163
|
+
The default `maxlength` is `Ellipsis`, meaning to copy all data.
|
|
164
|
+
|
|
165
|
+
Note that the thread preemptively consumes from the buffer.
|
|
166
|
+
|
|
167
|
+
This is useful for passing buffer data to subprocesses.
|
|
168
|
+
|
|
169
|
+
*`CornuCopyBuffer.at_eof(self)`*:
|
|
170
|
+
Test whether the buffer is at end of input.
|
|
171
|
+
|
|
172
|
+
*Warning*: this will fetch from the `input_data` if the buffer
|
|
173
|
+
is empty and so it may block.
|
|
174
|
+
|
|
175
|
+
*`CornuCopyBuffer.bounded(self, end_offset) -> 'CornuCopyBuffer'`*:
|
|
176
|
+
Return a new `CornuCopyBuffer` operating on a bounded view
|
|
177
|
+
of this buffer.
|
|
178
|
+
|
|
179
|
+
This supports parsing of the buffer contents without risk
|
|
180
|
+
of consuming past a certain point, such as the known end
|
|
181
|
+
of a packet structure.
|
|
182
|
+
|
|
183
|
+
Parameters:
|
|
184
|
+
* `end_offset`: the ending offset of the new buffer.
|
|
185
|
+
Note that this is an absolute offset, not a length.
|
|
186
|
+
|
|
187
|
+
The new buffer starts with the same offset as `self` and
|
|
188
|
+
use of the new buffer affects `self`. After a flush both
|
|
189
|
+
buffers will again have the same offset and the data consumed
|
|
190
|
+
via the new buffer will also have been consumed from `self`.
|
|
191
|
+
|
|
192
|
+
Here is an example.
|
|
193
|
+
* Make a buffer `bfr` with 9 bytes of data in 3 chunks.
|
|
194
|
+
* Consume 2 bytes, advancing the offset to 2.
|
|
195
|
+
* Make a new bounded buffer `subbfr` extending to offset
|
|
196
|
+
5. Its inital offset is also 2.
|
|
197
|
+
* Iterate over it, yielding the remaining single byte chunk
|
|
198
|
+
from ``b'abc'`` and then the first 2 bytes of ``b'def'``.
|
|
199
|
+
The new buffer's offset is now 5.
|
|
200
|
+
* Try to take 2 more bytes from the new buffer - this fails.
|
|
201
|
+
* Flush the new buffer, synchronising with the original.
|
|
202
|
+
The original's offset is now also 5.
|
|
203
|
+
* Take 2 bytes from the original buffer, which succeeds.
|
|
204
|
+
|
|
205
|
+
Example:
|
|
206
|
+
|
|
207
|
+
>>> bfr = CornuCopyBuffer([b'abc', b'def', b'ghi'])
|
|
208
|
+
>>> bfr.offset
|
|
209
|
+
0
|
|
210
|
+
>>> bfr.take(2)
|
|
211
|
+
b'ab'
|
|
212
|
+
>>> bfr.offset
|
|
213
|
+
2
|
|
214
|
+
>>> subbfr = bfr.bounded(5)
|
|
215
|
+
>>> subbfr.offset
|
|
216
|
+
2
|
|
217
|
+
>>> for bs in subbfr:
|
|
218
|
+
... print(bs)
|
|
219
|
+
...
|
|
220
|
+
b'c'
|
|
221
|
+
b'de'
|
|
222
|
+
>>> subbfr.offset
|
|
223
|
+
5
|
|
224
|
+
>>> subbfr.take(2)
|
|
225
|
+
Traceback (most recent call last):
|
|
226
|
+
...
|
|
227
|
+
EOFError: insufficient input data, wanted 2 bytes but only found 0
|
|
228
|
+
>>> subbfr.flush()
|
|
229
|
+
>>> bfr.offset
|
|
230
|
+
5
|
|
231
|
+
>>> bfr.take(2)
|
|
232
|
+
b'fg'
|
|
233
|
+
|
|
234
|
+
*WARNING*: if the bounded buffer is not completely consumed
|
|
235
|
+
then it is critical to call the new `CornuCopyBuffer`'s `.flush`
|
|
236
|
+
method to push any unconsumed buffer back into this buffer.
|
|
237
|
+
Recommended practice is to always call `.flush` when finished
|
|
238
|
+
with the new buffer.
|
|
239
|
+
The `CornuCopyBuffer.subbuffer` method returns a context manager
|
|
240
|
+
which does this automatically.
|
|
241
|
+
|
|
242
|
+
Also, because the new buffer may buffer some of the unconsumed
|
|
243
|
+
data from this buffer, use of the original buffer should
|
|
244
|
+
be suspended.
|
|
245
|
+
|
|
246
|
+
*`CornuCopyBuffer.buf`*:
|
|
247
|
+
The first buffer, or `b''` if nothing is buffered.
|
|
248
|
+
|
|
249
|
+
*`CornuCopyBuffer.byte0(self)`*:
|
|
250
|
+
Consume the leading byte and return it as an `int` (`0`..`255`).
|
|
251
|
+
|
|
252
|
+
*`CornuCopyBuffer.close(self)`*:
|
|
253
|
+
Close the buffer.
|
|
254
|
+
This discards the internal buffer of "read but not consumed" data
|
|
255
|
+
and calls the `close` callable supplied when the buffer was
|
|
256
|
+
initialised, if any.
|
|
257
|
+
|
|
258
|
+
*`CornuCopyBuffer.end_offset`*:
|
|
259
|
+
Return the end offset of the input data (in buffer ordinates)
|
|
260
|
+
if known, otherwise `None`.
|
|
261
|
+
|
|
262
|
+
Note that this depends on the computation of the
|
|
263
|
+
`input_offset_displacement` which takes place at the buffer
|
|
264
|
+
initialisation, which in turn relies on the `input_data.offset`
|
|
265
|
+
attribute, which at initialisation is presumed to be 0 if missing.
|
|
266
|
+
|
|
267
|
+
*`CornuCopyBuffer.extend(self, min_size, short_ok=False)`*:
|
|
268
|
+
Extend the buffer to at least `min_size` bytes.
|
|
269
|
+
|
|
270
|
+
If `min_size` is `Ellipsis`, extend the buffer to consume all the input.
|
|
271
|
+
This should really only be used with bounded buffers
|
|
272
|
+
in order to avoid unconstrained memory consumption.
|
|
273
|
+
|
|
274
|
+
If there are insufficient data available then an `EOFError`
|
|
275
|
+
will be raised unless `short_ok` is true (default `False`)
|
|
276
|
+
in which case the updated buffer will be short.
|
|
277
|
+
|
|
278
|
+
*`CornuCopyBuffer.from_bytes(bs, offset=0, length=None, **kw)`*:
|
|
279
|
+
Return a `CornuCopyBuffer` fed from the supplied bytes `bs`
|
|
280
|
+
starting at `offset` and ending after `length`.
|
|
281
|
+
|
|
282
|
+
This is handy for callers parsing using buffers but handed bytes.
|
|
283
|
+
|
|
284
|
+
Parameters:
|
|
285
|
+
* `bs`: the bytes
|
|
286
|
+
* `offset`: a starting position for the data; the input
|
|
287
|
+
data will start this far into the bytes
|
|
288
|
+
* `length`: the maximium number of bytes to use; the input
|
|
289
|
+
data will be cropped this far past the starting point;
|
|
290
|
+
default: the number of bytes in `bs` after `offset`
|
|
291
|
+
Other keyword arguments are passed to the buffer constructor.
|
|
292
|
+
|
|
293
|
+
*`CornuCopyBuffer.from_fd(fd, readsize=None, offset=None, final_offset=None, **kw)`*:
|
|
294
|
+
Return a new `CornuCopyBuffer` attached to an open file descriptor.
|
|
295
|
+
|
|
296
|
+
Internally this constructs a `SeekableFDIterator` for regular
|
|
297
|
+
files or an `FDIterator` for other files, which provides the
|
|
298
|
+
iteration that `CornuCopyBuffer` consumes, but also seek
|
|
299
|
+
support if the underlying file descriptor is seekable.
|
|
300
|
+
|
|
301
|
+
Parameters:
|
|
302
|
+
* `fd`: the operating system file descriptor
|
|
303
|
+
* `readsize`: an optional preferred read size
|
|
304
|
+
* `offset`: a starting position for the data; the file
|
|
305
|
+
descriptor will seek to this offset, and the buffer will
|
|
306
|
+
start with this offset
|
|
307
|
+
Other keyword arguments are passed to the buffer constructor.
|
|
308
|
+
|
|
309
|
+
*`CornuCopyBuffer.from_file(f, readsize=None, offset=None, final_offset=None, **kw)`*:
|
|
310
|
+
Return a new `CornuCopyBuffer` attached to an open file.
|
|
311
|
+
|
|
312
|
+
Internally this constructs a `SeekableFileIterator`, which
|
|
313
|
+
provides the iteration that `CornuCopyBuffer` consumes
|
|
314
|
+
and also seek support if the underlying file is seekable.
|
|
315
|
+
|
|
316
|
+
Parameters:
|
|
317
|
+
* `f`: the file like object
|
|
318
|
+
* `readsize`: an optional preferred read size
|
|
319
|
+
* `offset`: a starting position for the data; the file
|
|
320
|
+
will seek to this offset, and the buffer will start with this
|
|
321
|
+
offset
|
|
322
|
+
Other keyword arguments are passed to the buffer constructor.
|
|
323
|
+
|
|
324
|
+
*`CornuCopyBuffer.from_filename(filename: str, offset=None, final_offset=None, **kw)`*:
|
|
325
|
+
Open the file named `filename` and return a new `CornuCopyBuffer`.
|
|
326
|
+
|
|
327
|
+
If `offset` is provided, skip to that position in the file.
|
|
328
|
+
A negative offset skips to a position that far from the end of the file
|
|
329
|
+
as determined by its `Stat.st_size`.
|
|
330
|
+
|
|
331
|
+
Other keyword arguments are passed to the buffer constructor.
|
|
332
|
+
|
|
333
|
+
*`CornuCopyBuffer.from_mmap(fd, readsize=None, offset=None, **kw)`*:
|
|
334
|
+
Return a new `CornuCopyBuffer` attached to an mmap of an open
|
|
335
|
+
file descriptor.
|
|
336
|
+
|
|
337
|
+
Internally this constructs a `SeekableMMapIterator`, which
|
|
338
|
+
provides the iteration that `CornuCopyBuffer` consumes, but
|
|
339
|
+
also seek support.
|
|
340
|
+
|
|
341
|
+
Parameters:
|
|
342
|
+
* `fd`: the operating system file descriptor
|
|
343
|
+
* `readsize`: an optional preferred read size
|
|
344
|
+
* `offset`: a starting position for the data; the file
|
|
345
|
+
descriptor will seek to this offset, and the buffer will
|
|
346
|
+
start with this offset
|
|
347
|
+
Other keyword arguments are passed to the buffer constructor.
|
|
348
|
+
|
|
349
|
+
*`CornuCopyBuffer.hint(self, size)`*:
|
|
350
|
+
Hint that the caller is seeking at least `size` bytes.
|
|
351
|
+
|
|
352
|
+
If the `input_data` iterator has a `hint` method, this is
|
|
353
|
+
passed to it.
|
|
354
|
+
|
|
355
|
+
*`CornuCopyBuffer.iter(self, maxlength)`*:
|
|
356
|
+
Yield chunks from the buffer
|
|
357
|
+
up to `maxlength` in total
|
|
358
|
+
or until EOF if `maxlength` is `Ellipsis`.
|
|
359
|
+
|
|
360
|
+
*`CornuCopyBuffer.next(self)`*:
|
|
361
|
+
Fetch a data chunk from the buffer.
|
|
362
|
+
|
|
363
|
+
*`CornuCopyBuffer.peek(self, size, short_ok=False)`*:
|
|
364
|
+
Examine the leading bytes of the buffer without consuming them,
|
|
365
|
+
a `take` followed by a `push`.
|
|
366
|
+
Returns the bytes.
|
|
367
|
+
|
|
368
|
+
*`CornuCopyBuffer.promote(obj)`*:
|
|
369
|
+
Promote `obj` to a `CornuCopyBuffer`,
|
|
370
|
+
used by the @cs.deco.promote` decorator.
|
|
371
|
+
|
|
372
|
+
Promotes:
|
|
373
|
+
* `int`: assumed to be a file descriptor of a file open for binary read
|
|
374
|
+
* `str`: assumed to be a filesystem pathname
|
|
375
|
+
* `bytes` and `bytes`like objects: data
|
|
376
|
+
* has a `.read1` or `.read` method: assume a file open for binary read
|
|
377
|
+
* iterable: assumed to be an iterable of `bytes`like objects
|
|
378
|
+
|
|
379
|
+
*`CornuCopyBuffer.push(self, bs)`*:
|
|
380
|
+
Push the chunk `bs` onto the front of the buffered data.
|
|
381
|
+
Rewinds the logical `.offset` by the length of `bs`.
|
|
382
|
+
|
|
383
|
+
*`CornuCopyBuffer.read(self, size, one_fetch=False)`*:
|
|
384
|
+
Compatibility method to allow using the buffer like a file.
|
|
385
|
+
|
|
386
|
+
Parameters:
|
|
387
|
+
* `size`: the desired data size
|
|
388
|
+
* `one_fetch`: do a single data fetch, default `False`
|
|
389
|
+
|
|
390
|
+
In `one_fetch` mode the read behaves like a POSIX file read,
|
|
391
|
+
returning up to to `size` bytes from a single I/O operation.
|
|
392
|
+
|
|
393
|
+
*`CornuCopyBuffer.read1(self, size)`*:
|
|
394
|
+
Shorthand method for `self.read(size,one_fetch=True)`.
|
|
395
|
+
|
|
396
|
+
*`CornuCopyBuffer.readline(self)`*:
|
|
397
|
+
Return a binary "line" from `self`, where a line is defined by
|
|
398
|
+
its ending `b'\n'` delimiter.
|
|
399
|
+
The final line from a buffer might not have a trailing newline;
|
|
400
|
+
`b''` is returned at EOF.
|
|
401
|
+
|
|
402
|
+
Example:
|
|
403
|
+
|
|
404
|
+
>>> bfr = CornuCopyBuffer([b'abc', b'def\nhij'])
|
|
405
|
+
>>> bfr.readline()
|
|
406
|
+
b'abcdef\n'
|
|
407
|
+
>>> bfr.readline()
|
|
408
|
+
b'hij'
|
|
409
|
+
>>> bfr.readline()
|
|
410
|
+
b''
|
|
411
|
+
>>> bfr.readline()
|
|
412
|
+
b''
|
|
413
|
+
|
|
414
|
+
*`CornuCopyBuffer.report_offset(self, offset)`*:
|
|
415
|
+
Report a pertinent offset.
|
|
416
|
+
|
|
417
|
+
*`CornuCopyBuffer.seek(self, offset, whence=None, short_ok=False)`*:
|
|
418
|
+
Compatibility method to allow using the buffer like a file.
|
|
419
|
+
This returns the resulting absolute offset.
|
|
420
|
+
|
|
421
|
+
Parameters are as for `io.seek` except as noted below:
|
|
422
|
+
* `whence`: (default `os.SEEK_SET`). This method only supports
|
|
423
|
+
`os.SEEK_SET` and `os.SEEK_CUR`, and does not support seeking to a
|
|
424
|
+
lower offset than the current buffer offset.
|
|
425
|
+
* `short_ok`: (default `False`). If true, the seek may not reach
|
|
426
|
+
the target if there are insufficent `input_data` - the
|
|
427
|
+
position will be the end of the `input_data`, and the
|
|
428
|
+
`input_data` will have been consumed; the caller must check
|
|
429
|
+
the returned offset to check that it is as expected. If
|
|
430
|
+
false, a `ValueError` will be raised; however, note that the
|
|
431
|
+
`input_data` will still have been consumed.
|
|
432
|
+
|
|
433
|
+
*`CornuCopyBuffer.selfcheck(self, msg='')`*:
|
|
434
|
+
Integrity check for the buffer, useful during debugging.
|
|
435
|
+
|
|
436
|
+
*`CornuCopyBuffer.skip(self, toskip, copy_skip=None, short_ok=False)`*:
|
|
437
|
+
Advance position by `skip_to`. Return the new offset.
|
|
438
|
+
|
|
439
|
+
Parameters:
|
|
440
|
+
* `toskip`: the distance to advance
|
|
441
|
+
* `copy_skip`: callable to receive skipped data.
|
|
442
|
+
* `short_ok`: default `False`; if true then skip may return before
|
|
443
|
+
`skipto` bytes if there are insufficient `input_data`.
|
|
444
|
+
|
|
445
|
+
*`CornuCopyBuffer.skipto(self, new_offset, copy_skip=None, short_ok=False)`*:
|
|
446
|
+
Advance to position `new_offset`. Return the new offset.
|
|
447
|
+
|
|
448
|
+
Parameters:
|
|
449
|
+
* `new_offset`: the target offset.
|
|
450
|
+
* `copy_skip`: callable to receive skipped data.
|
|
451
|
+
* `short_ok`: default `False`; if true then skipto may return before
|
|
452
|
+
`new_offset` if there are insufficient `input_data`.
|
|
453
|
+
|
|
454
|
+
Return values:
|
|
455
|
+
* `buf`: the new state of `buf`
|
|
456
|
+
* `offset`: the final offset; this may be short if `short_ok`.
|
|
457
|
+
|
|
458
|
+
*`CornuCopyBuffer.subbuffer(self, end_offset)`*:
|
|
459
|
+
Context manager wrapper for `.bounded`
|
|
460
|
+
which calls the `.flush` method automatically
|
|
461
|
+
on exiting the context.
|
|
462
|
+
|
|
463
|
+
Example:
|
|
464
|
+
|
|
465
|
+
# avoid buffer overrun
|
|
466
|
+
with bfr.subbuffer(bfr.offset+128) as subbfr:
|
|
467
|
+
id3v1 = ID3V1Frame.parse(subbfr)
|
|
468
|
+
# ensure the whole buffer was consumed
|
|
469
|
+
assert subbfr.at_eof()
|
|
470
|
+
|
|
471
|
+
*`CornuCopyBuffer.tail_extend(self, size)`*:
|
|
472
|
+
Extend method for parsers reading "tail"-like chunk streams,
|
|
473
|
+
typically raw reads from a growing file.
|
|
474
|
+
|
|
475
|
+
This may read 0 bytes at EOF, but a future read may read
|
|
476
|
+
more bytes if the file grows.
|
|
477
|
+
Such an iterator can be obtained from
|
|
478
|
+
``cs.fileutils.read_from(..,tail_mode=True)``.
|
|
479
|
+
|
|
480
|
+
*`CornuCopyBuffer.take(self, size, short_ok=False)`*:
|
|
481
|
+
Return the next `size` bytes.
|
|
482
|
+
Other arguments are as for `.extend()`.
|
|
483
|
+
|
|
484
|
+
This is a thin wrapper for the `.takev` method.
|
|
485
|
+
|
|
486
|
+
*`CornuCopyBuffer.takev(self, size, short_ok=False)`*:
|
|
487
|
+
Return the next `size` bytes as a list of chunks
|
|
488
|
+
(because the internal buffering is also a list of chunks).
|
|
489
|
+
Other arguments are as for `.extend()`.
|
|
490
|
+
|
|
491
|
+
See `.take()` to get a flat chunk instead of a list.
|
|
492
|
+
|
|
493
|
+
*`CornuCopyBuffer.tell(self)`*:
|
|
494
|
+
Compatibility method to allow using the buffer like a file.
|
|
495
|
+
|
|
496
|
+
## <a name="FDIterator"></a>Class `FDIterator(_FetchIterator)`
|
|
497
|
+
|
|
498
|
+
An iterator over the data of a file descriptor.
|
|
499
|
+
|
|
500
|
+
*`FDIterator.__init__(self, fd: int, offset=None, readsize=None, align=True)`*:
|
|
501
|
+
Initialise the iterator.
|
|
502
|
+
|
|
503
|
+
Parameters:
|
|
504
|
+
* `fd`: file descriptor
|
|
505
|
+
* `offset`: the initial logical offset, kept up to date by
|
|
506
|
+
iteration; the default is the current file position.
|
|
507
|
+
* `readsize`: a preferred read size; if omitted then
|
|
508
|
+
`DEFAULT_READSIZE` will be stored
|
|
509
|
+
* `align`: whether to align reads by default: if true then
|
|
510
|
+
the iterator will do a short read to bring the `offset`
|
|
511
|
+
into alignment with `readsize`; the default is `True`
|
|
512
|
+
|
|
513
|
+
*`FDIterator.close(self)`*:
|
|
514
|
+
Close `self.fd` if it is nt yet `None`.
|
|
515
|
+
|
|
516
|
+
## <a name="FileIterator"></a>Class `FileIterator(_FetchIterator, SeekableIteratorMixin)`
|
|
517
|
+
|
|
518
|
+
An iterator over the data of a file object.
|
|
519
|
+
|
|
520
|
+
*`FileIterator.__init__(self, fp, offset=None, readsize=None, align=False)`*:
|
|
521
|
+
Initialise the iterator.
|
|
522
|
+
|
|
523
|
+
Parameters:
|
|
524
|
+
* `fp`: file object
|
|
525
|
+
* `offset`: the initial logical offset, kept up to date by
|
|
526
|
+
iteration; the default is 0.
|
|
527
|
+
* `readsize`: a preferred read size; if omitted then
|
|
528
|
+
`DEFAULT_READSIZE` will be stored
|
|
529
|
+
* `align`: whether to align reads by default: if true then
|
|
530
|
+
the iterator will do a short read to bring the `offset`
|
|
531
|
+
into alignment with `readsize`; the default is `False`
|
|
532
|
+
|
|
533
|
+
*`FileIterator.close(self)`*:
|
|
534
|
+
Detach from the file. Does *not* call `fp.close()`.
|
|
535
|
+
|
|
536
|
+
## <a name="SeekableFDIterator"></a>Class `SeekableFDIterator(FDIterator, SeekableIteratorMixin)`
|
|
537
|
+
|
|
538
|
+
An iterator over the data of a seekable file descriptor.
|
|
539
|
+
|
|
540
|
+
*`SeekableFDIterator.end_offset`*:
|
|
541
|
+
The end offset of the file.
|
|
542
|
+
|
|
543
|
+
## <a name="SeekableFileIterator"></a>Class `SeekableFileIterator(FileIterator)`
|
|
544
|
+
|
|
545
|
+
An iterator over the data of a seekable file object.
|
|
546
|
+
|
|
547
|
+
*Note*: the iterator closes the file on __del__ or if its
|
|
548
|
+
.close method is called.
|
|
549
|
+
|
|
550
|
+
*`SeekableFileIterator.__init__(self, fp, offset=None, **kw)`*:
|
|
551
|
+
Initialise the iterator.
|
|
552
|
+
|
|
553
|
+
Parameters:
|
|
554
|
+
* `fp`: file object
|
|
555
|
+
* `offset`: the initial logical offset, kept up to date by
|
|
556
|
+
iteration; the default is the current file position.
|
|
557
|
+
* `readsize`: a preferred read size; if omitted then
|
|
558
|
+
`DEFAULT_READSIZE` will be stored
|
|
559
|
+
* `align`: whether to align reads by default: if true then
|
|
560
|
+
the iterator will do a short read to bring the `offset`
|
|
561
|
+
into alignment with `readsize`; the default is `False`
|
|
562
|
+
|
|
563
|
+
*`SeekableFileIterator.seek(self, new_offset, mode=0)`*:
|
|
564
|
+
Move the logical file pointer.
|
|
565
|
+
|
|
566
|
+
WARNING: moves the underlying file's pointer.
|
|
567
|
+
|
|
568
|
+
## <a name="SeekableIteratorMixin"></a>Class `SeekableIteratorMixin`
|
|
569
|
+
|
|
570
|
+
Mixin supplying a logical with a `seek` method.
|
|
571
|
+
|
|
572
|
+
*`SeekableIteratorMixin.seek(self, new_offset, mode=0)`*:
|
|
573
|
+
Move the logical offset.
|
|
574
|
+
|
|
575
|
+
## <a name="SeekableMMapIterator"></a>Class `SeekableMMapIterator(_FetchIterator, SeekableIteratorMixin)`
|
|
576
|
+
|
|
577
|
+
An iterator over the data of a mappable file descriptor.
|
|
578
|
+
|
|
579
|
+
*`SeekableMMapIterator.__init__(self, fd: int, offset=None, readsize=None, align=True)`*:
|
|
580
|
+
Initialise the iterator.
|
|
581
|
+
|
|
582
|
+
Parameters:
|
|
583
|
+
* `offset`: the initial logical offset, kept up to date by
|
|
584
|
+
iteration; the default is the current file position.
|
|
585
|
+
* `readsize`: a preferred read size; if omitted then
|
|
586
|
+
`DEFAULT_READSIZE` will be stored
|
|
587
|
+
* `align`: whether to align reads by default: if true then
|
|
588
|
+
the iterator will do a short read to bring the `offset`
|
|
589
|
+
into alignment with `readsize`; the default is `True`
|
|
590
|
+
|
|
591
|
+
*`SeekableMMapIterator.close(self)`*:
|
|
592
|
+
Close the mmap and detach.
|
|
593
|
+
|
|
594
|
+
*`SeekableMMapIterator.end_offset`*:
|
|
595
|
+
The end offset of the mmap memoryview.
|
|
596
|
+
|
|
597
|
+
# Release Log
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
*Release 20250111*:
|
|
602
|
+
DISTINFO: move python_version>=3.3 out to the "makers" section (presemly unused) because it was being used as a dependency, finding the python_version PyPI dsitribution.
|
|
603
|
+
|
|
604
|
+
*Release 20240630*:
|
|
605
|
+
* CornuCopyBuffer: new optional .final_offset attribute indicating the anticipated largest offset, automatcially set for regular files - aids progress bars etc.
|
|
606
|
+
* Drop cs.py3, get pread from os.pread, require python>=3.3.
|
|
607
|
+
* CornuCopyBuffer.close: set self.bufs = None to release memory and prevent accidental late use.
|
|
608
|
+
* _FetchIterator: provide a mutex; SeekableIteratorMixin.seek, FDIterator.read,close: take the mutex.
|
|
609
|
+
|
|
610
|
+
*Release 20240412*:
|
|
611
|
+
CornuCopyBuffer.buf: return b"" if nothing is buffered.
|
|
612
|
+
|
|
613
|
+
*Release 20240316*:
|
|
614
|
+
Fixed release upload artifacts.
|
|
615
|
+
|
|
616
|
+
*Release 20240201*:
|
|
617
|
+
CornuCopyBuffer: read1() method shorthand for read(..,one_fetch=True).
|
|
618
|
+
|
|
619
|
+
*Release 20230401*:
|
|
620
|
+
* CornuCopyBuffer.promote: document accepting an iterable as an iterable of bytes.
|
|
621
|
+
* CornuCopyBuffer: new readline() method to return a binary line from the buffer.
|
|
622
|
+
* CornuCopyBuffer.promote: assume objects with a .read1 or .read are files.
|
|
623
|
+
|
|
624
|
+
*Release 20230212.2*:
|
|
625
|
+
* BREAKING: drop @chunky, superceded by @promote for CornuCopyBuffer parameters.
|
|
626
|
+
* CornuCopyBuffer: subclass Promotable.
|
|
627
|
+
|
|
628
|
+
*Release 20230212.1*:
|
|
629
|
+
Add missing requirement for cs.gimmicks.
|
|
630
|
+
|
|
631
|
+
*Release 20230212*:
|
|
632
|
+
CornuCopyBuffer: new promote() method to promote int,str,bytes, also assumes nonspecific iterables yield byteslike instances.
|
|
633
|
+
|
|
634
|
+
*Release 20211208*:
|
|
635
|
+
CornuCopyBuffer.__init__: bugfix for self.input_data when copy_chunks is not None.
|
|
636
|
+
|
|
637
|
+
*Release 20210316*:
|
|
638
|
+
* New CornuCopyBuffer.from_filename factory method.
|
|
639
|
+
* New CornuCopyBuffer.peek method to examine the leading bytes from the buffer.
|
|
640
|
+
|
|
641
|
+
*Release 20210306*:
|
|
642
|
+
* CornuCopyBuffer.from_file: improve the seekability test, handle files with no .tell().
|
|
643
|
+
* CornuCopyBuffer.from_bytes: bugfix: set the buffer.offset to the supplied offset.
|
|
644
|
+
* CornuCopyBuffer.from_bytes: queue a memoryview of the supplied bytes.
|
|
645
|
+
|
|
646
|
+
*Release 20201102*:
|
|
647
|
+
CornuCopyBuffer: new optional `progress` parameter for reporting data consumed from `input_data`.
|
|
648
|
+
|
|
649
|
+
*Release 20201021*:
|
|
650
|
+
* CornuCopyBuffer.from_file: changes to the test for a .seek method.
|
|
651
|
+
* CornuCopyBuffer.read: call extend with short_ok=True.
|
|
652
|
+
* CornuCopyBuffer.from_fd: record the fd as .fd, lets users os.fstat(bfr.fd).
|
|
653
|
+
* New CornuCopyBuffer.as_fd method to return a readable file descriptor fed from the buffer by a Thread, intended for feeding subprocesses.
|
|
654
|
+
* New CornuCopyBuffer.iter(maxlength) to return an iterator of up to maxlength bytes.
|
|
655
|
+
* CornuCopyBuffer.__init__: new "close" parameter to release resources; new CornuCopyBuffer.close method to call this.
|
|
656
|
+
* Some small fixes.
|
|
657
|
+
|
|
658
|
+
*Release 20200517*:
|
|
659
|
+
* CornuCopyBuffer.skip: bugfix sanity check.
|
|
660
|
+
* FileIterator: do not close the supplied file, just set self.fp=None.
|
|
661
|
+
* Improve EOFError message text.
|
|
662
|
+
|
|
663
|
+
*Release 20200328*:
|
|
664
|
+
* CornuCopyBuffer.takev: bugfix adjustment of buf.offset, was not always done.
|
|
665
|
+
* CornuCopyBuffer.__getitem__: add slice support, note how expensive it is to use.
|
|
666
|
+
|
|
667
|
+
*Release 20200229*:
|
|
668
|
+
* New CornuCopyBuffer.byte0() method consuming the next byte and returning it as an int.
|
|
669
|
+
* CornuCopyBuffer.takev: bugfix for size=0, logic refactor.
|
|
670
|
+
* CornuCopyBuffer: new .selfcheck method.
|
|
671
|
+
|
|
672
|
+
*Release 20200130*:
|
|
673
|
+
CornuCopyBuffer.skip: bugfix adjustment of skipto for already buffered data.
|
|
674
|
+
|
|
675
|
+
*Release 20191230.1*:
|
|
676
|
+
Docstring updates. Semantic changes were in the previous release.
|
|
677
|
+
|
|
678
|
+
*Release 20191230*:
|
|
679
|
+
* CornuCopyBuffer: accept a size of Ellipsis in .take and .extend methods, indicating "all the remaining data".
|
|
680
|
+
* CornuCopyBuffer: refactor the buffering, replacing .buf with .bufs as an array of chunks;
|
|
681
|
+
* this enables support for the new .push method and reduces memory copying.
|
|
682
|
+
|
|
683
|
+
*Release 20181231*:
|
|
684
|
+
Small bugfix.
|
|
685
|
+
|
|
686
|
+
*Release 20181108*:
|
|
687
|
+
New at_eof() method. Python 2 tweak to support incidental import by python 2 even if unused.
|
|
688
|
+
|
|
689
|
+
*Release 20180823*:
|
|
690
|
+
Better handling of seekable and unseekable input data. Tiny bugfix for from_bytes sanity check.
|
|
691
|
+
|
|
692
|
+
*Release 20180810*:
|
|
693
|
+
* Refactor SeekableFDIterator and SeekableFileIterator to subclass new SeekableIterator.
|
|
694
|
+
* New SeekableMMapIterator to process a memory mapped file descriptor, intended for large files.
|
|
695
|
+
* New CornuCopyBuffer.hint method to pass a length hint through to the input_data iterator
|
|
696
|
+
* if it has a `hint` method, causing it possibly to make a differently sized fetch.
|
|
697
|
+
* SeekableIterator: new __del__ method calling self.close() - subclasses must provide
|
|
698
|
+
* a .close, which should be safe to call multiple times.
|
|
699
|
+
* CornuCopyBuffer: add support for .offset and .end_offset optional attributes on the input_data iterator.
|
|
700
|
+
* _BoundedBufferIterator: add .offset property plumbed to the underlying buffer offset.
|
|
701
|
+
* New CornuCopyBuffer.from_mmap to make a mmap backed buffer so that large data can be returned without penalty.
|
|
702
|
+
* Assorted fixes and doc improvements.
|
|
703
|
+
|
|
704
|
+
*Release 20180805*:
|
|
705
|
+
Bugfixes for at_eof method and end_offset initialisation.
|
|
706
|
+
|
|
707
|
+
*Release 20180726.1*:
|
|
708
|
+
Improve docstrings and release with better long_description.
|
|
709
|
+
|
|
710
|
+
*Release 20180726*:
|
|
711
|
+
First PyPI release: CornuCopyBuffer and friends.
|