-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuffer.py
More file actions
1404 lines (1243 loc) · 45 KB
/
Copy pathbuffer.py
File metadata and controls
1404 lines (1243 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Functions associated with bytes, bytearrays, memoryviews and buffers in general.
# Also CornuCopyBuffer for managing a buffer and an input source.
# - Cameron Simpson <cs@cskk.id.au> 18mar2017
#
# pylint: disable=too-many-lines
#
''' Facilities to do with buffers, particularly CornuCopyBuffer,
an automatically refilling buffer to support parsing of data streams.
'''
from contextlib import contextmanager
import os
from os import fstat, pread, SEEK_SET, SEEK_CUR, SEEK_END
import mmap
from stat import S_ISREG
import sys
from threading import Lock, Thread
from typing import List
from cs.deco import Promotable
from cs.gimmicks import Buffer, r
__version__ = '20250428-post'
DISTINFO = {
'keywords': ["python3"],
'classifiers': [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Development Status :: 5 - Production/Stable",
],
'requires_python':
'>=3.3', # for os.pread
'install_requires': [
'cs.deco',
'cs.gimmicks',
],
}
DEFAULT_READSIZE = 131072
MEMORYVIEW_THRESHOLD = DEFAULT_READSIZE # tweak if this gets larger
# pylint: disable=too-many-public-methods,too-many-instance-attributes
class CornuCopyBuffer(Promotable):
''' An automatically refilling buffer intended to support parsing
of data streams.
Its primary purpose is to aid binary parsers
which do not themselves need to handle sources specially;
`CornuCopyBuffer`s are trivially made from `bytes`,
iterables of `bytes` and file-like objects.
See `cs.binary` for convenient parsing classes
which work with `CornuCopyBuffer`s.
A `CornuCopyBuffer` is iterable, yielding data in whatever
sizes come from its `input_data` source, preceeded by any
content in the internal buffer.
A `CornuCopyBuffer` also supports the file methods `.read`,
`.tell` and `.seek` supporting drop in use of the buffer in
many file contexts. Backward seeks are not supported. `.seek`
will take advantage of the `input_data`'s `.seek` method if it
has one, otherwise it will use consume the `input_data`
as required.
Attributes:
* `buf`: the first of any buffered leading chunks
buffer of unparsed data from the input, available
for direct inspection by parsers;
normally however parsers will use `.extend` and `.take`.
* `offset`: the logical offset of the buffer; this excludes
buffered data and unconsumed input data
*Note*: the initialiser may supply a cleanup function;
although this will be called via the buffer's `.__del__` method
a prudent user of a buffer should call the `.close()` method
when finished with the buffer to ensure prompt cleanup;
the `contextlib.closing` context manager provides an easy way
to do this in common cases.
The primary methods supporting parsing of data streams are
`.extend()` and `take()`.
Calling `.extend(min_size)` arranges that the internal buffer
contains at least `min_size` bytes.
Calling `.take(size)` fetches exactly `size` bytes from the
internal buffer and the input source if necessary and returns
them, adjusting the internal buffer.
len(`CornuCopyBuffer`) returns the length of any buffered data.
bool(`CornuCopyBuffer`) tests whether len() > 0.
Indexing a `CornuCopyBuffer` accesses the buffered data only,
returning an individual byte's value (an `int`).
'''
# pylint: disable=too-many-arguments
def __init__(
self,
input_data,
buf=None,
offset=0,
seekable=None,
copy_offsets=None,
copy_chunks=None,
close=None,
progress=None,
final_offset=None,
):
''' Prepare the buffer.
Parameters:
* `input_data`: an iterable of data chunks (`bytes`-like instances);
if your data source is a file see the `.from_file` factory;
if your data source is a file descriptor see the `.from_fd`
factory.
* `buf`: if not `None`, the initial state of the parse buffer
* `offset`: logical offset of the start of the buffer, default `0`
* `seekable`: whether `input_data` has a working `.seek` method;
the default is `None` meaning that it will be attempted on
the first skip or seek
* `copy_offsets`: if not `None`, a callable for parsers to
report pertinent offsets via the buffer's `.report_offset`
method
* `copy_chunks`: if not `None`, every fetched data chunk is
copied to this callable
The `input_data` is an iterable whose iterator may have
some optional additional properties:
* `seek`: if present, this is a seek method after the fashion
of `file.seek`; the buffer's `seek`, `skip` and `skipto`
methods will take advantage of this if available.
* `offset`: the current byte offset of the iterator; this
is used during the buffer initialisation to compute
`input_data_displacement`, the difference between the
buffer's logical offset and the input data iterable's logical offset;
if unavailable during initialisation this is presumed to
be `0`.
* `end_offset`: the end offset of the iterator if known.
* `close`: an optional callable
that may be provided for resource cleanup
when the user of the buffer calls its `.close()` method.
* `progress`: an optional `cs.Progress.progress` instance
to which to report data consumed from `input_data`;
any object supporting `+=` is acceptable
* `final_offset`: optional `int` specifying the largest
offset expected to be reached, intended for uses such as
callers presenting a pregress indication; this is, for
example, provided by `CornuCopyBuffer.from_fd` for regular
files using the `stat.st_size` field
'''
self.bufs = []
if buf is None or not buf:
self.buflen = 0
else:
self.bufs.append(buf)
self.buflen = len(buf)
self.offset = offset
self.seekable = seekable
input_data = iter(input_data)
if copy_chunks is not None:
input_data = CopyingIterator(input_data, copy_chunks)
self.input_data = input_data
self.copy_offsets = copy_offsets
# Try to compute the displacement between the input_data byte
# offset and the buffer's logical offset.
# NOTE: if the input_data iterator does not have a .offset
# attribute then we assume the iterator byte offset is 0, purely
# to reduce the burden on iterator implementors.
input_offset = getattr(input_data, 'offset', 0)
self.input_offset_displacement = input_offset - offset
self._close = close
self.progress = progress
self.final_offset = final_offset
def selfcheck(self, msg=''):
''' Integrity check for the buffer, useful during debugging.
'''
msgpfx = f'{self.__class__.__name__}.selfcheck'
if msg:
msgpfx += ': ' + msg
msgpfx += f', buflen={self.buflen}, bufs={[len(buf) for buf in self.bufs]}'
assert self.buflen == sum(
len(buf) for buf in self.bufs
), msgpfx + ": self.buflen != sum of .bufs"
assert all(
len(buf) > 0 for buf in self.bufs
), msgpfx + ": not all .bufs are nonempty"
@property
def buf(self):
''' The first buffer, or `b''` if nothing is buffered.
'''
try:
return self.bufs[0]
except IndexError:
return b''
def close(self):
''' Close the buffer.
This discards the internal buffer of "read but not consumed" data
and calls the `close` callable supplied when the buffer was
initialised, if any.
'''
self.bufs = None
if self._close:
self._close()
self._close = None
def __del__(self):
''' Release resources when the object is deleted.
'''
self.close()
@staticmethod
def _stat_final_offset(f):
''' Return the `final_offset` value from a file descriptor or file,
or `None` if it cannot be determined from `os.fstat()`.
'''
if isinstance(f, int):
st = fstat(f)
else:
try:
fd = f.fileno()
except AttributeError:
return None
if fd is None:
return None
st = fstat(fd)
if not S_ISREG(st.st_mode):
return None
return st.st_size
@classmethod
def from_fd(cls, fd, readsize=None, offset=None, final_offset=None, **kw):
''' Return a new `CornuCopyBuffer` attached to an open file descriptor.
Internally this constructs a `SeekableFDIterator` for regular
files or an `FDIterator` for other files, which provides the
iteration that `CornuCopyBuffer` consumes, but also seek
support if the underlying file descriptor is seekable.
Parameters:
* `fd`: the operating system file descriptor
* `readsize`: an optional preferred read size
* `offset`: a starting position for the data; the file
descriptor will seek to this offset, and the buffer will
start with this offset
Other keyword arguments are passed to the buffer constructor.
'''
st = fstat(fd)
if S_ISREG(st.st_mode):
if final_offset is None:
final_offset = st.st_size
it = SeekableFDIterator(fd, readsize=readsize, offset=offset)
else:
it = FDIterator(fd, readsize=readsize, offset=offset)
return cls(
it,
close=it.close,
offset=it.offset,
final_offset=final_offset,
**kw,
)
def as_fd(self, maxlength=Ellipsis):
''' Create a pipe and dispatch a `Thread` to copy
up to `maxlength` bytes from `bfr` into it.
Return the file descriptor of the read end of the pipe.
The default `maxlength` is `Ellipsis`, meaning to copy all data.
Note that the thread preemptively consumes from the buffer.
This is useful for passing buffer data to subprocesses.
'''
rfd, wfd = os.pipe()
def copy_buffer():
''' Copy data from the buffer to `wfd`,
closing `wfd` when finished.
'''
try:
for bs in self.iter(maxlength):
while bs:
try:
nbs = os.write(wfd, bs)
except OSError:
# rebuffer uncopied data and reraise
self.push(bs)
raise
bs = bs[nbs:]
finally:
os.close(wfd)
Thread(
name=f'{self}.copy_to_fd:{wfd=}:{rfd=}',
target=copy_buffer,
).start()
return rfd
@classmethod
def from_mmap(cls, fd, readsize=None, offset=None, **kw):
''' Return a new `CornuCopyBuffer` attached to an mmap of an open
file descriptor.
Internally this constructs a `SeekableMMapIterator`, which
provides the iteration that `CornuCopyBuffer` consumes, but
also seek support.
Parameters:
* `fd`: the operating system file descriptor
* `readsize`: an optional preferred read size
* `offset`: a starting position for the data; the file
descriptor will seek to this offset, and the buffer will
start with this offset
Other keyword arguments are passed to the buffer constructor.
'''
it = SeekableMMapIterator(fd, readsize=readsize, offset=offset)
return cls(
it,
offset=it.offset,
close=it.close,
final_offset=it.end_offset,
**kw,
)
@classmethod
def from_file(cls, f, readsize=None, offset=None, final_offset=None, **kw):
''' Return a new `CornuCopyBuffer` attached to an open file.
Internally this constructs a `SeekableFileIterator`, which
provides the iteration that `CornuCopyBuffer` consumes
and also seek support if the underlying file is seekable.
Parameters:
* `f`: the file like object
* `readsize`: an optional preferred read size
* `offset`: a starting position for the data; the file
will seek to this offset, and the buffer will start with this
offset
Other keyword arguments are passed to the buffer constructor.
'''
try:
ftell = f.tell
except AttributeError:
is_seekable = False
foffset = None
else:
try:
foffset = ftell()
except OSError:
is_seekable = False
foffset = None
else:
is_seekable = True
if offset is None:
offset = foffset
if final_offset is None:
final_offset = cls._stat_final_offset(f)
it = (
SeekableFileIterator(f, readsize=readsize, offset=offset)
if is_seekable else FileIterator(f, readsize=readsize, offset=offset)
)
return cls(it, offset=it.offset, final_offset=final_offset, **kw)
@classmethod
def from_filename(cls, filename: str, offset=None, final_offset=None, **kw):
''' Open the file named `filename` and return a new `CornuCopyBuffer`.
If `offset` is provided, skip to that position in the file.
A negative offset skips to a position that far from the end of the file
as determined by its `Stat.st_size`.
Other keyword arguments are passed to the buffer constructor.
'''
f = open(filename, 'rb') # pylint: disable=consider-using-with
if final_offset is None:
final_offset = cls._stat_final_offset(f)
bfr = cls.from_file(f, close=f.close, final_offset=final_offset, **kw)
if offset is not None:
if offset < 0:
S = os.fstat(f.fileno())
offset2 = S.st_size + offset
if offset2 < 0:
raise ValueError(
"offset %s is too far from the end of the file (st_size=%s)" %
(offset, S.st_size),
)
bfr.skipto(offset)
return bfr
@classmethod
def from_cli_filespec(cls, filespec: str, **kw):
''' Return a `CornuCopyBuffer` fed from the supplied command
line file specification `filespec`.
If `filespec` is `"-"` return a buffer using `sys.stdin`,
otherwise treat it as a filename.
Note: the use of `sys.stdin` relies on `sys.stdin.fileno()`
because we need to do binary reads and `sys.stdin` is
normally in text mode.
'''
if filespec == '-':
return cls.from_fd(sys.stdin.fileno(), **kw)
return cls.from_filename(filespec, **kw)
@classmethod
def from_bytes(cls, bs, offset=0, length=None, **kw):
''' Return a `CornuCopyBuffer` fed from the supplied bytes `bs`
starting at `offset` and ending after `length`.
This is handy for callers parsing using buffers but handed bytes.
Parameters:
* `bs`: the bytes
* `offset`: a starting position for the data; the input
data will start this far into the bytes
* `length`: the maximium number of bytes to use; the input
data will be cropped this far past the starting point;
default: the number of bytes in `bs` after `offset`
Other keyword arguments are passed to the buffer constructor.
'''
if offset < 0:
raise ValueError(f'{offset=} should be >= 0')
if offset > len(bs):
raise ValueError(f'{offset=} beyond end of bs ({len(bs)} bytes)')
if length is None:
length = len(bs) - offset
# sanity check supplied length
elif length < 1:
raise ValueError(f'{length=} < 1')
end_offset = offset + length
if end_offset > len(bs):
raise ValueError(f'{offset=}+{length=} > {len(bs)=}')
bs = memoryview(bs)
if offset > 0 or end_offset < len(bs):
bs = bs[offset:end_offset]
return cls([bs], offset=offset, final_offset=end_offset, **kw)
def __str__(self):
return f'{self.__class__.__name__}(offset:{self.offset}:{self.final_offset},buf:{self.buflen}'
def __len__(self):
''' The length is the length of the internal buffer: data available without a fetch.
'''
return self.buflen
def __bool__(self):
return len(self) > 0
__nonzero__ = __bool__
def __getitem__(self, index):
''' Fetch from the internal buffer.
This does not consume data from the internal buffer.
Note that this is an expensive way to access the buffer,
particularly if `index` is a slice.
If `index` is a `slice`, slice the join of the internal subbuffers.
This is quite expensive
and it is probably better to `take` or `takev`
some data from the buffer.
Otherwise `index` should be an `int` and the corresponding
buffered byte is returned.
This is usually not a very useful method;
its primary use case is to probe the buffer to make a parsing decision
instead of taking a byte off and (possibly) pushing it back.
'''
if isinstance(index, slice):
# slice the joined up bufs - expensive
return b''.join(self.bufs)[index]
index0 = index
if index < 0:
index = self.buflen - index
if index < 0:
raise IndexError(f'index {index0} out of range ({self.buflen=})')
if index >= self.buflen:
raise IndexError(f'index {index0} out of range ({self.buflen=})')
buf_offset = 0
for buf in self.bufs:
if index < buf_offset + len(buf):
return buf[index - buf_offset]
buf_offset += len(buf)
raise RuntimeError(
"%s.__getitem__(%s): failed to locate byte in bufs %r" %
(self, index0, [len(buf) for buf in self.bufs]),
)
def __iter__(self):
return self
def __next__(self):
''' Fetch a data chunk from the buffer.
'''
if self.bufs:
chunk = self.bufs.pop(0)
self.buflen -= len(chunk)
else:
chunk = next(self.input_data)
if self.progress is not None:
self.progress += len(chunk)
self.offset += len(chunk)
return chunk
next = __next__
def iter(self, maxlength):
''' Yield chunks from the buffer
up to `maxlength` in total
or until EOF if `maxlength` is `Ellipsis`.
'''
if maxlength is not Ellipsis and maxlength < 1:
raise ValueError(f'{maxlength=} mst be Ellipsis or >=1')
while maxlength is Ellipsis or maxlength > 0:
try:
bs = next(self)
except StopIteration:
break
if maxlength is not Ellipsis:
if maxlength < len(bs):
self.push(bs[maxlength:])
bs = bs[:maxlength]
maxlength -= len(bs)
yield bs
def push(self, bs):
''' Push the chunk `bs` onto the front of the buffered data.
Rewinds the logical `.offset` by the length of `bs`.
'''
blen = len(bs)
if blen > 0:
self.bufs.insert(0, bs)
self.buflen += blen
self.offset -= blen
@property
def end_offset(self):
''' Return the end offset of the input data (in buffer ordinates)
if known, otherwise `None`.
Note that this depends on the computation of the
`input_offset_displacement` which takes place at the buffer
initialisation, which in turn relies on the `input_data.offset`
attribute, which at initialisation is presumed to be 0 if missing.
'''
input_data = self.input_data
try:
input_end_offset = input_data.end_offset
except AttributeError:
return None
return input_end_offset - self.input_offset_displacement
def at_eof(self):
''' Test whether the buffer is at end of input.
*Warning*: this will fetch from the `input_data` if the buffer
is empty and so it may block.
'''
if self.bufs:
return False
self.extend(1, short_ok=True)
return len(self) == 0
def report_offset(self, offset):
''' Report a pertinent offset.
'''
copy_offsets = self.copy_offsets
if copy_offsets is not None:
copy_offsets(offset)
def hint(self, size):
''' Hint that the caller is seeking at least `size` bytes.
If the `input_data` iterator has a `hint` method, this is
passed to it.
'''
try:
self.input_data.hint(size)
except AttributeError:
pass
def extend(self, min_size, short_ok=False):
''' Extend the buffer to at least `min_size` bytes.
If `min_size` is `Ellipsis`, extend the buffer to consume all the input.
This should really only be used with bounded buffers
in order to avoid unconstrained memory consumption.
If there are insufficient data available then an `EOFError`
will be raised unless `short_ok` is true (default `False`)
in which case the updated buffer will be short.
'''
if min_size is Ellipsis:
pass
elif min_size < 1:
raise ValueError(f'{min_size=} must be >= 1')
while min_size is Ellipsis or min_size > self.buflen:
if min_size is not Ellipsis:
self.hint(min_size - self.buflen)
try:
next_chunk = next(self.input_data)
except StopIteration:
# no more input_data
if min_size is Ellipsis or short_ok:
return
# pylint: disable=raise-missing-from
raise EOFError(
"insufficient input data, wanted %d bytes but only found %d" %
(min_size, self.buflen),
)
if next_chunk:
self.bufs.append(next_chunk)
self.buflen += len(next_chunk)
if self.progress is not None:
self.progress += len(next_chunk)
##assert self.buflen >= min_size
##assert self.buflen == sum(len(buf) for buf in self.bufs)
def tail_extend(self, size):
''' Extend method for parsers reading "tail"-like chunk streams,
typically raw reads from a growing file.
This may read 0 bytes at EOF, but a future read may read
more bytes if the file grows.
Such an iterator can be obtained from
``cs.fileutils.read_from(..,tail_mode=True)``.
'''
while size < len(self):
self.extend(size, short_ok=True)
def takev(self, size, short_ok=False) -> List[Buffer]:
''' Return the next `size` bytes as a list of chunks
(because the internal buffering is also a list of chunks).
Other arguments are as for `.extend()`.
See `.take()` to get a flat chunk instead of a list.
'''
if size == 0:
return []
if size is Ellipsis or size > self.buflen:
# extend the buffered data
self.extend(size, short_ok=short_ok)
# post: the buffer is as big as it is going to get for this call
if size is Ellipsis or size >= self.buflen:
# take all the fetched data
# which should be all the data because of the .extend() above
taken = self.bufs
self.bufs = []
else:
# size < self.buflen
# take the leading data from the buffer
taken = []
bufs = self.bufs
while size > 0:
buf0 = bufs[0]
if len(buf0) <= size:
buf = buf0
bufs.pop(0)
else:
# len(buf0) > size: crop from buf0
assert len(buf0) > size
buf = buf0[:size]
bufs[0] = buf0[size:]
taken.append(buf)
size -= len(buf)
# advance offset by the size of the taken data
taken_size = sum(len(buf) for buf in taken)
self.buflen -= taken_size
self.offset += taken_size
return taken
def take(self, size, short_ok=False):
''' Return the next `size` bytes.
Other arguments are as for `.extend()`.
This is a thin wrapper for the `.takev` method.
'''
taken = self.takev(size, short_ok=short_ok)
if not taken:
return b''
if len(taken) == 1:
return bytes(taken[0])
return b''.join(taken)
def readline(self):
r'''Return a binary "line" from `self`, where a line is defined by
its ending `b'\n'` delimiter.
The final line from a buffer might not have a trailing newline;
`b''` is returned at EOF.
Example:
>>> bfr = CornuCopyBuffer([b'abc', b'def\nhij'])
>>> bfr.readline()
b'abcdef\n'
>>> bfr.readline()
b'hij'
>>> bfr.readline()
b''
>>> bfr.readline()
b''
'''
pending = []
for bs in self:
nlpos = bs.find(b'\n')
if nlpos >= 0:
pending.append(bs[:nlpos + 1])
self.push(bs[nlpos + 1:])
break
pending.append(bs)
return b''.join(pending)
def peek(self, size, short_ok=False):
''' Examine the leading bytes of the buffer without consuming them,
a `take` followed by a `push`.
Returns the bytes.
'''
bs = self.take(size, short_ok=short_ok)
self.push(bs)
return bs
def read(self, size, one_fetch=False):
''' Compatibility method to allow using the buffer like a file.
Parameters:
* `size`: the desired data size
* `one_fetch`: do a single data fetch, default `False`
In `one_fetch` mode the read behaves like a POSIX file read,
returning up to to `size` bytes from a single I/O operation.
'''
if size < 1:
raise ValueError(f'{size=} < 1')
if size <= self.buflen:
return self.take(size)
# size > self.buflen
if not one_fetch:
self.extend(size, short_ok=True)
taken = self.takev(min(size, self.buflen))
size -= sum(len(buf) for buf in taken)
if size > 0:
# want more data
if one_fetch:
try:
buf = next(self)
except StopIteration:
pass
else:
if size < len(buf):
# push back the tail of the buffer
self.push(buf[size:])
buf = buf[:size]
taken.append(buf)
if not taken:
return b''
if len(taken) == 1:
return taken[0]
return b''.join(taken)
def read1(self, size):
''' Shorthand method for `self.read(size,one_fetch=True)`.
'''
return self.read(size, one_fetch=True)
def byte0(self):
''' Consume the leading byte and return it as an `int` (`0`..`255`).
'''
byte0, = self.take(1)
return byte0
def tell(self):
''' Compatibility method to allow using the buffer like a file.
'''
return self.offset
def seek(self, offset, whence=None, short_ok=False):
''' Compatibility method to allow using the buffer like a file.
This returns the resulting absolute offset.
Parameters are as for `io.seek` except as noted below:
* `whence`: (default `os.SEEK_SET`). This method only supports
`os.SEEK_SET` and `os.SEEK_CUR`, and does not support seeking to a
lower offset than the current buffer offset.
* `short_ok`: (default `False`). If true, the seek may not reach
the target if there are insufficent `input_data` - the
position will be the end of the `input_data`, and the
`input_data` will have been consumed; the caller must check
the returned offset to check that it is as expected. If
false, a `ValueError` will be raised; however, note that the
`input_data` will still have been consumed.
'''
if whence is None:
whence = SEEK_SET
elif whence == SEEK_SET:
pass
elif whence == SEEK_CUR:
offset += self.offset
else:
raise ValueError(
f'seek: unsupported {whence=}, must be os.SEEK_SET or os.SEEK_CUR'
)
if offset < self.offset:
raise ValueError(
f'seek: target {offset=} < {self.offset=}; may not seek backwards'
)
if offset > self.offset:
self.skipto(offset, short_ok=short_ok)
return self.offset
def skipto(self, new_offset, copy_skip=None, short_ok=False):
''' Advance to position `new_offset`. Return the new offset.
Parameters:
* `new_offset`: the target offset.
* `copy_skip`: callable to receive skipped data.
* `short_ok`: default `False`; if true then skipto may return before
`new_offset` if there are insufficient `input_data`.
Return values:
* `buf`: the new state of `buf`
* `offset`: the final offset; this may be short if `short_ok`.
'''
offset = self.offset
if new_offset < offset:
raise ValueError(f'skipto: {new_offset=} < {offset=}')
return self.skip(
new_offset - offset,
copy_skip=copy_skip,
short_ok=short_ok,
)
def skip(self, toskip, copy_skip=None, short_ok=False):
''' Advance position by `skip_to`. Return the new offset.
Parameters:
* `toskip`: the distance to advance
* `copy_skip`: callable to receive skipped data.
* `short_ok`: default `False`; if true then skip may return before
`skipto` bytes if there are insufficient `input_data`.
'''
# consume buffered bytes in buf before the new offset
bufskip = min(toskip, self.buflen)
if bufskip > 0:
for buf in self.takev(bufskip):
if copy_skip:
copy_skip(buf)
toskip -= len(buf)
assert toskip >= 0
if toskip == 0:
return
# check that we consumed all the buffered data
assert not self.bufs
assert self.buflen == 0
# advance the rest of the way
seekable = False if copy_skip else self.seekable
if seekable is None or seekable:
# should we do a seek?
try:
input_seek = self.input_data.seek
except AttributeError:
if seekable is not None:
print(
f'{self}.skip: warning: {seekable=} but no input_data.seek method,'
" resetting seekable to False",
file=sys.stderr,
)
self.seekable = False
else:
# input_data has a seek method, try to use it
new_offset = self.offset + toskip
input_offset = new_offset + self.input_offset_displacement
try:
input_seek(input_offset)
except OSError as e:
print(
f'{self}.skip: warning: input_data.seek({input_offset}): {e}'
', resetting self.seekable to False',
file=sys.stderr,
)
self.seekable = False
else:
# successful seek, update offset and return
self.offset = new_offset
return
# no seek, consume sufficient chunks
self.hint(toskip)
for buf in self.takev(toskip, short_ok=short_ok):
toskip -= len(buf)
assert toskip == 0
@contextmanager
def subbuffer(self, end_offset):
''' Context manager wrapper for `.bounded`
which calls the `.flush` method automatically
on exiting the context.
Example:
# avoid buffer overrun
with bfr.subbuffer(bfr.offset+128) as subbfr:
id3v1 = ID3V1Frame.parse(subbfr)
# ensure the whole buffer was consumed
assert subbfr.at_eof()
'''
subbfr = self.bounded(end_offset)
try:
yield subbfr
finally:
subbfr.flush()
def bounded(self, end_offset) -> "CornuCopyBuffer":
''' Return a new `CornuCopyBuffer` operating on a bounded view
of this buffer.
This supports parsing of the buffer contents without risk
of consuming past a certain point, such as the known end
of a packet structure.
Parameters:
* `end_offset`: the ending offset of the new buffer.
Note that this is an absolute offset, not a length.
The new buffer starts with the same offset as `self` and
use of the new buffer affects `self`. After a flush both
buffers will again have the same offset and the data consumed
via the new buffer will also have been consumed from `self`.
Here is an example.
* Make a buffer `bfr` with 9 bytes of data in 3 chunks.
* Consume 2 bytes, advancing the offset to 2.
* Make a new bounded buffer `subbfr` extending to offset
5. Its inital offset is also 2.
* Iterate over it, yielding the remaining single byte chunk
from ``b'abc'`` and then the first 2 bytes of ``b'def'``.
The new buffer's offset is now 5.
* Try to take 2 more bytes from the new buffer - this fails.
* Flush the new buffer, synchronising with the original.
The original's offset is now also 5.
* Take 2 bytes from the original buffer, which succeeds.
Example:
>>> bfr = CornuCopyBuffer([b'abc', b'def', b'ghi'])
>>> bfr.offset
0
>>> bfr.take(2)
b'ab'
>>> bfr.offset
2
>>> subbfr = bfr.bounded(5)
>>> subbfr.offset
2
>>> for bs in subbfr:
... print(bs)
...
b'c'
b'de'
>>> subbfr.offset
5
>>> subbfr.take(2)
Traceback (most recent call last):
...
EOFError: insufficient input data, wanted 2 bytes but only found 0
>>> subbfr.flush()
>>> bfr.offset
5
>>> bfr.take(2)
b'fg'
*WARNING*: if the bounded buffer is not completely consumed
then it is critical to call the new `CornuCopyBuffer`'s `.flush`
method to push any unconsumed buffer back into this buffer.
Recommended practice is to always call `.flush` when finished
with the new buffer.
The `CornuCopyBuffer.subbuffer` method returns a context manager
which does this automatically.
Also, because the new buffer may buffer some of the unconsumed
data from this buffer, use of the original buffer should
be suspended.
'''
bfr2 = CornuCopyBuffer(
_BoundedBufferIterator(self, end_offset),
offset=self.offset,
final_offset=end_offset,
)
def flush():
''' Flush the internal buffer of `bfr2` back into `self`'s
internal buffer, adjusting the latter's `.offset` accordingly.
'''
for buf in reversed(bfr2.bufs):