本文整理汇总了Python中typing.BinaryIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python BinaryIO.read方法的具体用法?Python BinaryIO.read怎么用?Python BinaryIO.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类typing.BinaryIO
的用法示例。
在下文中一共展示了BinaryIO.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def load(file_handle: typing.BinaryIO) -> TSerializable:
"""load(file) -> object
This function reads a tnetstring from a file and parses it into a
python object. The file must support the read() method, and this
function promises not to read more data than necessary.
"""
# Read the length prefix one char at a time.
# Note that the netstring spec explicitly forbids padding zeros.
c = file_handle.read(1)
if c == b"": # we want to detect this special case.
raise ValueError("not a tnetstring: empty file")
data_length = b""
while c.isdigit():
data_length += c
if len(data_length) > 9:
raise ValueError("not a tnetstring: absurdly large length prefix")
c = file_handle.read(1)
if c != b":":
raise ValueError("not a tnetstring: missing or invalid length prefix")
data = file_handle.read(int(data_length))
data_type = file_handle.read(1)[0]
return parse(data_type, data)
示例2: parse_header
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def parse_header(source: BinaryIO) -> Tuple[OFXHeaderType, str]:
"""
Consume source; feed to appropriate class constructor which performs
validation/type conversion on OFX header.
Using header, locate/read/decode (but do not parse) OFX data body.
Returns a 2-tuple of:
* instance of OFXHeaderV1/OFXHeaderV2 containing parsed data, and
* decoded text of OFX data body
"""
# Skip any empty lines at the beginning
while True:
# OFX header is read by nice clean machines, not meatbags -
# should not contain emoji, 漢字, or what have you.
line = source.readline().decode("ascii")
if line.strip():
break
# If the first non-empty line contains an XML declaration, it's OFX v2
xml_match = XML_REGEX.match(line)
if xml_match:
# OFXv2 spec doesn't require newlines between XML declaration,
# OFX declaration, and data elements; `line` may or may not
# contain the latter two.
#
# Just rewind, read the whole file (it must be UTF-8 encoded per
# the spec) and slice the OFX data body from the end of the
# OFX declaration
source.seek(0)
decoded_source = source.read().decode(OFXHeaderV2.codec)
header, header_end_index = OFXHeaderV2.parse(decoded_source)
message = decoded_source[header_end_index:]
else:
# OFX v1
rawheader = line + "\n"
# First line is OFXHEADER; need to read next 8 lines for a fixed
# total of 9 fields required by OFX v1 spec.
for n in range(8):
rawheader += source.readline().decode("ascii")
header, header_end_index = OFXHeaderV1.parse(rawheader)
# Input source stream position has advanced to the beginning of
# the OFX body tag soup, which is where subsequent calls
# to read()/readlines() will pick up.
#
# Decode the OFX data body according to the encoding declared
# in the OFX header
message = source.read().decode(header.codec)
return header, message.strip()
示例3: read_delimited_chunks
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def read_delimited_chunks(infile: BinaryIO, chunk_size: int) -> Generator[bytes, None, None]:
"""Yield the contents of infile in chunk_size pieces ending at newlines.
The individual pieces, except for the last one, end in newlines and
are smaller than chunk_size if possible.
Params:
infile: stream to read from
chunk_size: maximum size of each chunk
Yields:
chunk: chunk with maximum size of chunk_size if possible
"""
leftover = b""
while True:
new_chunk = infile.read(chunk_size)
chunks = split_chunks(leftover + new_chunk, chunk_size)
leftover = b""
# the last item in chunks has to be combined with the next chunk
# read from the file because it may not actually stop at a
# newline and to avoid very small chunks.
if chunks:
leftover = chunks[-1]
chunks = chunks[:-1]
for chunk in chunks:
yield chunk
if not new_chunk:
if leftover:
yield leftover
break
示例4: add_member_stream
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def add_member_stream( self,
path: PurePosixPath, mtime: ArchiveMTime,
content_stream: BinaryIO,
) -> None:
content = content_stream.read()
assert isinstance(content, bytes), type(content)
return self.add_member_bytes(path, mtime, content)
示例5: get_index
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def get_index(
self, archive: BinaryIO, version: Optional[Version] = None
) -> Dict[str, ComplexIndexEntry]:
if not version:
version = self.version() if self.version else self.detect_version()
offset = 0
key: Optional[int] = None
if self.offset_and_key:
offset, key = self.offset_and_key
else:
offset, key = version.find_offset_and_key(archive)
archive.seek(offset)
index: Dict[bytes, IndexEntry] = pickle.loads(
zlib.decompress(archive.read()), encoding="bytes"
)
if key is not None:
normal_index = UnRPA.deobfuscate_index(key, index)
else:
normal_index = UnRPA.normalise_index(index)
return {
UnRPA.ensure_str_path(path).replace("/", os.sep): data
for path, data in normal_index.items()
}
示例6: put
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def put(
self, namespace: str, metadata: Dict[str, Any], bytes_io: BinaryIO,
) -> None:
"""Store a file."""
subset = dict_subset(metadata, lambda k, v: k in (
# We are not storing the 'file_name'
'image_width', 'image_height', 'original_id', 'version'))
self._convert_values_to_str(subset)
if hasattr(bytes_io, 'seekable') and bytes_io.seekable():
bytes_io.seek(0)
# When botocore.response.StreamingBody is passed in as bytes_io,
# the bucket.put_object() call below fails with
# "AttributeError: 'StreamingBody' object has no attribute 'tell'"
# so we have to read the stream, getting the bytes:
if not hasattr(bytes_io, 'tell'):
bytes_io = bytes_io.read() # type: ignore
result = self.bucket.put_object(
Key=self._get_path(namespace, metadata),
# done automatically by botocore: ContentMD5=encoded_md5,
ContentType=metadata['mime_type'],
ContentLength=metadata['length'], Body=bytes_io, Metadata=subset)
# print(result)
return result
示例7: _rewrite_ownership_v0
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def _rewrite_ownership_v0(
input_file: BinaryIO, new_file: BinaryIO, header: MdvHeader, uid: int, gid: int
) -> None:
entries_processed = 0
entry_size = InodeMetadataV0.FORMAT.size
for _ in range(header.entry_count):
entries_processed += 1
entry_data = input_file.read(entry_size)
if len(entry_data) != entry_size:
raise Exception("inode metadata table appears truncated")
entry = InodeMetadataV0.parse(entry_data)
entry.uid = uid
entry.gid = gid
new_file.write(entry.serialize())
# Copy the remaining file contents as is. This is normally all 0-filled data
# that provides space for new entries to be written in the future.
padding = input_file.read()
new_file.write(padding)
示例8: read_nullstr
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def read_nullstr(file: BinaryIO, pos: int=None):
"""Read a null-terminated string from the file."""
if pos is not None:
if pos == 0:
return ''
file.seek(pos)
text = []
while True:
char = file.read(1)
if char == b'\0':
return b''.join(text).decode('ascii')
if not char:
raise ValueError('Fell off end of file!')
text.append(char)
示例9: generate_reports
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def generate_reports(report_template: Report, infile: BinaryIO, chunk_size: Optional[int],
copy_header_line: bool) -> Generator[Report, None, None]:
"""Generate reports from a template and input file, optionally split into chunks.
If chunk_size is None, a single report is generated with the entire
contents of infile as the raw data. Otherwise chunk_size should be
an integer giving the maximum number of bytes in a chunk. The data
read from infile is then split into chunks of this size at newline
characters (see read_delimited_chunks). For each of the chunks, this
function yields a copy of the report_template with that chunk as the
value of the raw attribute.
When splitting the data into chunks, if copy_header_line is true,
the first line the file is read before chunking and then prepended
to each of the chunks. This is particularly useful when splitting
CSV files.
The infile should be a file-like object. generate_reports uses only
two methods, readline and read, with readline only called once and
only if copy_header_line is true. Both methods should return bytes
objects.
Params:
report_template: report used as template for all yielded copies
infile: stream to read from
chunk_size: maximum size of each chunk
copy_header_line: copy the first line of the infile to each chunk
Yields:
report: a Report object holding the chunk in the raw field
"""
if chunk_size is None:
report = report_template.copy()
data = infile.read()
if data:
report.add("raw", data, overwrite=True)
yield report
else:
header = b""
if copy_header_line:
header = infile.readline()
for chunk in read_delimited_chunks(infile, chunk_size):
report = report_template.copy()
report.add("raw", header + chunk, overwrite=True)
yield report
示例10: put
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def put(
self, namespace: str, metadata: Dict[str, Any], bytes_io: BinaryIO,
) -> None:
"""Store a file (``bytes_io``) inside ``namespace``."""
if bytes_io.tell():
bytes_io.seek(0)
outdir = self._dir_of(namespace)
if not outdir.exists():
outdir.mkdir(parents=True) # Create namespace directory as needed
outfile = outdir / self._get_filename(metadata)
with open(str(outfile), mode='wb', buffering=MEGABYTE) as writer:
while True:
chunk = bytes_io.read(MEGABYTE)
if chunk:
writer.write(chunk)
else:
break
assert outfile.lstat().st_size == metadata['length']
示例11: _compute_md5
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def _compute_md5(
self, bytes_io: BinaryIO, metadata: Dict[str, Any],
) -> None:
from hashlib import md5
two_megabytes = 1048576 * 2
the_hash = md5()
the_length = 0
bytes_io.seek(0)
while True:
segment = bytes_io.read(two_megabytes)
if segment == b'':
break
the_length += len(segment)
the_hash.update(segment)
metadata['md5'] = the_hash.hexdigest()
previous_length = metadata.get('length')
if previous_length is None:
metadata['length'] = the_length
else:
assert previous_length == the_length, "Bug? File lengths {}, {} " \
"don't match.".format(previous_length, the_length)
bytes_io.seek(0) # ...so it can be read again
示例12: iter_nullstr
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def iter_nullstr(file: BinaryIO):
"""Read a null-terminated ASCII string from the file.
This continuously yields strings, with empty strings
indicting the end of a section.
"""
chars = bytearray()
while True:
char = file.read(1)
if char == b'\x00':
string = chars.decode('ascii')
chars.clear()
if string == ' ': # Blank strings are saved as ' '
yield ''
elif string == '':
return # Actual blanks end the array.
else:
yield string
elif char == b'':
raise Exception('Reached EOF without null-terminator in {}!'.format(bytes(chars)))
else:
chars.extend(char)
示例13: read
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def read(cls: Type["MdvHeader"], input_file: BinaryIO) -> "MdvHeader":
data = input_file.read(cls.FORMAT.size)
return cls.parse(data)
示例14: _load
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def _load(self, f: BinaryIO):
"""Read data from the MDL file."""
assert f.tell() == 0, "Doesn't begin at start?"
if f.read(4) != b'IDST':
raise ValueError('Not a model!')
(
self.version,
name,
file_len,
# 4 bytes are unknown...
) = str_read('i 4x 64s i', f)
if not 44 <= self.version <= 49:
raise ValueError('Unknown MDL version {}!'.format(self.version))
self.name = name.rstrip(b'\0').decode('ascii')
self.eye_pos = str_readvec(f)
self.illum_pos = str_readvec(f)
# Approx dimensions
self.hull_min = str_readvec(f)
self.hull_max = str_readvec(f)
self.view_min = str_readvec(f)
self.view_max = str_readvec(f)
# Break up the reading a bit to limit the stack size.
(
flags,
bone_count,
bone_off,
bone_controller_count, bone_controller_off,
hitbox_count, hitbox_off,
anim_count, anim_off,
sequence_count, sequence_off,
) = str_read('11I', f)
self.flags = Flags(flags)
(
activitylistversion, eventsindexed,
texture_count, texture_offset,
cdmat_count, cdmat_offset,
skinref_count, skinref_ind, skinfamily_count,
bodypart_count, bodypart_offset,
attachment_count, attachment_offset,
) = str_read('13i', f)
(
localnode_count,
localnode_index,
localnode_name_index,
# mstudioflexdesc_t
flexdesc_count,
flexdesc_index,
# mstudioflexcontroller_t
flexcontroller_count,
flexcontroller_index,
# mstudioflexrule_t
flexrules_count,
flexrules_index,
# IK probably refers to inverse kinematics
# mstudioikchain_t
ikchain_count,
ikchain_index,
# Information about any "mouth" on the model for speech animation
# More than one sounds pretty creepy.
# mstudiomouth_t
mouths_count,
mouths_index,
# mstudioposeparamdesc_t
localposeparam_count,
localposeparam_index,
) = str_read('15I', f)
# VDC:
# For anyone trying to follow along, as of this writing,
# the next "surfaceprop_index" value is at position 0x0134 (308)
# from the start of the file.
assert f.tell() == 308, 'Offset wrong? {} != 308 {}'.format(f.tell(), f)
(
# Surface property value (single null-terminated string)
surfaceprop_index,
# Unusual: In this one index comes first, then count.
# Key-value data is a series of strings. If you can't find
# what you're interested in, check the associated PHY file as well.
keyvalue_index,
#.........这里部分代码省略.........
示例15: str_readvec
# 需要导入模块: from typing import BinaryIO [as 别名]
# 或者: from typing.BinaryIO import read [as 别名]
def str_readvec(file: BinaryIO) -> Vec:
"""Read a vector from a file."""
return Vec(ST_VEC.unpack(file.read(ST_VEC.size)))