openssh_key.pascal_style_byte_stream.PascalStyleByteStream

class openssh_key.pascal_style_byte_stream.PascalStyleByteStream(initial_bytes=b'')

Bases: _io.BytesIO

Methods on io.BytesIO that allow reading and writing values either as struct values, or as Pascal-style values: variable-length bytes, str, or variable-precision int values prefixed by the length of such variable-length value.

__init__(*args, **kwargs)

Methods

__init__(*args, **kwargs)

check_dict_matches_format_instructions_dict(...)

Checks whether a given set of values can validly be passed to write_from_format_instructions_dict for given format instructions.

close()

Disable all I/O operations.

detach()

Disconnect this buffer from its underlying raw stream and return it.

fileno()

Returns underlying file descriptor if one exists.

flush()

Does nothing.

get_openssh_default_string_length_size()

The value 4, the size in bytes used by OpenSSH for the int preceding a variable-length value that indicates the length of the latter.

getbuffer()

Get a read-write view over the contents of the BytesIO object.

getvalue()

Retrieve the entire contents of the BytesIO object.

isatty()

Always returns False.

read([size])

Read at most size bytes, returned as a bytes object.

read1([size])

Read at most size bytes, returned as a bytes object.

read_fixed_bytes(num_bytes)

Reads a fixed number of bytes from the underlying bytestream.

read_from_format_instruction(format_instruction)

Reads a value from the underlying bytestream according to a format instruction.

read_from_format_instructions_dict(...)

Reads values from the underlying bytestream according to a typing.Mapping of format instructions.

read_pascal_bytes(string_length_size)

Reads a Pascal-style byte string from the underlying bytestream, given the size of the length prefix.

read_repeatedly_from_format_instructions_dict(...)

Reads values repeatedly as per read_from_format_instructions_dict until the stream is exhausted.

readable()

Returns True if the IO object can be read.

readinto(buffer, /)

Read bytes into buffer.

readinto1(buffer, /)

readline([size])

Next line from the file, as a bytes object.

readlines([size])

List of bytes objects, each a line from the file.

seek(pos[, whence])

Change stream position.

seekable()

Returns True if the IO object can be seeked.

tell()

Current file position, an integer.

truncate([size])

Truncate the file to at most size bytes.

writable()

Returns True if the IO object can be written.

write(b, /)

Write bytes to file.

write_from_format_instruction(...[, ...])

Writes a value to the underlying bytestream according to a format instruction.

write_from_format_instructions_dict(...)

Writes values to the underlying bytestream according to a typing.Mapping of format instructions.

write_repeatedly_from_format_instructions_dict(...)

Writes a list of values to the underlying bytestream as per write_from_format_instructions_dict.

writelines(lines, /)

Write lines to the file.

Attributes

OPENSSH_DEFAULT_STRING_LENGTH_SIZE

The value 4, the size in bytes used by OpenSSH for the int preceding a variable-length value that indicates the length of the latter.

closed

True if the file is closed.

OPENSSH_DEFAULT_STRING_LENGTH_SIZE = 4

The value 4, the size in bytes used by OpenSSH for the int preceding a variable-length value that indicates the length of the latter.

static check_dict_matches_format_instructions_dict(target_dict, format_instructions_dict)

Checks whether a given set of values can validly be passed to write_from_format_instructions_dict for given format instructions.

Parameters
Raises

UserWarning – A key is missing from target_dict that is present in format_instructions_dict, or the type or struct size of a value for a key in target_dict does not match that proscribed for that key in format_instructions_dict.

Return type

None

close()

Disable all I/O operations.

closed

True if the file is closed.

detach()

Disconnect this buffer from its underlying raw stream and return it.

After the raw stream has been detached, the buffer is in an unusable state.

fileno()

Returns underlying file descriptor if one exists.

OSError is raised if the IO object does not use a file descriptor.

flush()

Does nothing.

static get_openssh_default_string_length_size()

The value 4, the size in bytes used by OpenSSH for the int preceding a variable-length value that indicates the length of the latter.

Return type

int

getbuffer()

Get a read-write view over the contents of the BytesIO object.

getvalue()

Retrieve the entire contents of the BytesIO object.

isatty()

Always returns False.

BytesIO objects are not connected to a TTY-like device.

read(size=- 1, /)

Read at most size bytes, returned as a bytes object.

If the size argument is negative, read until EOF is reached. Return an empty bytes object at EOF.

read1(size=- 1, /)

Read at most size bytes, returned as a bytes object.

If the size argument is negative or omitted, read until EOF is reached. Return an empty bytes object at EOF.

read_fixed_bytes(num_bytes)

Reads a fixed number of bytes from the underlying bytestream.

Parameters

num_bytes (int) – The number of bytes to read.

Return type

bytes

Returns

The read bytes.

Raises

EOFError – Fewer than num_bytes bytes remained in the underlying bytestream.

read_from_format_instruction(format_instruction, string_length_size=None)

Reads a value from the underlying bytestream according to a format instruction.

Parameters
Return type

Any

Returns

The read value. If format_instruction is a struct format string, the value unpacked using struct.unpack(); if format_instruction is a PascalStyleFormatInstruction, the value converted to the corresponding class.

Raises
  • EOFError – The underlying bytestream does not contain enough bytes to read a complete value according to format_instruction.

  • ValueErrorstring_length_size is nonpositive.

read_from_format_instructions_dict(format_instructions_dict)

Reads values from the underlying bytestream according to a typing.Mapping of format instructions.

Parameters

format_instructions_dict (Mapping[str, Union[str, PascalStyleFormatInstruction, PascalStyleFormatInstructionStringLengthSize]]) – A typing.Mapping of value names to format instructions.

Return type

Mapping[str, Any]

Returns

A typing.Mapping of value names to read values, as per read_from_format_instruction.

Raises

EOFError – The underlying bytestream does not contain enough bytes to read a complete value for one of the format instructions in format_instructions_dict.

read_pascal_bytes(string_length_size)

Reads a Pascal-style byte string from the underlying bytestream, given the size of the length prefix.

Parameters

string_length_size (int) – The size of the big-endian unsigned int prefix that indicates the length of the byte string to read.

Return type

bytes

Returns

The read byte string.

Raises
  • EOFError – Fewer than string_length_size bytes remained in the underlying bytestream, or the length prefix exceeds the number of bytes remaining in the underlying bytestream.

  • ValueErrorstring_length_size is nonpositive.

read_repeatedly_from_format_instructions_dict(format_instructions_dict)

Reads values repeatedly as per read_from_format_instructions_dict until the stream is exhausted.

Parameters

format_instructions_dict (Mapping[str, Union[str, PascalStyleFormatInstruction, PascalStyleFormatInstructionStringLengthSize]]) – A typing.Mapping of value names to format instructions.

Return type

List[Any]

Returns

A typing.List of typing.Mapping of value names to read values.

Raises

EOFError – The underlying bytestream does not contain enough bytes to read a complete value for one of the format instructions in format_instructions_dict.

readable()

Returns True if the IO object can be read.

readinto(buffer, /)

Read bytes into buffer.

Returns number of bytes read (0 for EOF), or None if the object is set not to block and has no data to read.

readline(size=- 1, /)

Next line from the file, as a bytes object.

Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty bytes object at EOF.

readlines(size=None, /)

List of bytes objects, each a line from the file.

Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned.

seek(pos, whence=0, /)

Change stream position.

Seek to byte offset pos relative to position indicated by whence:

0 Start of stream (the default). pos should be >= 0; 1 Current position - pos may be negative; 2 End of stream - pos usually negative.

Returns the new absolute position.

seekable()

Returns True if the IO object can be seeked.

tell()

Current file position, an integer.

truncate(size=None, /)

Truncate the file to at most size bytes.

Size defaults to the current file position, as returned by tell(). The current file position is unchanged. Returns the new size.

writable()

Returns True if the IO object can be written.

write(b, /)

Write bytes to file.

Return the number of bytes written.

write_from_format_instruction(format_instruction, value, string_length_size=None)

Writes a value to the underlying bytestream according to a format instruction.

Parameters
Return type

None

write_from_format_instructions_dict(format_instructions_dict, values_dict)

Writes values to the underlying bytestream according to a typing.Mapping of format instructions.

Parameters
Raises

KeyErrorvalues_dict does not contain a key that is contained in format_instructions_dict.

Return type

None

write_repeatedly_from_format_instructions_dict(format_instructions_dict, values_dicts)

Writes a list of values to the underlying bytestream as per write_from_format_instructions_dict.

Parameters
Raises

KeyError – One of the values_dicts does not contain a key that is contained in format_instructions_dict.

Return type

None

writelines(lines, /)

Write lines to the file.

Note that newlines are not added. lines can be any iterable object producing bytes-like objects. This is equivalent to calling write() for each element.