openssh_key.pascal_style_byte_stream.PascalStyleByteStream¶
- class openssh_key.pascal_style_byte_stream.PascalStyleByteStream(initial_bytes=b'')¶
Bases:
_io.BytesIOMethods on
io.BytesIOthat allow reading and writing values either asstructvalues, or as Pascal-style values: variable-lengthbytes,str, or variable-precisionintvalues prefixed by the length of such variable-length value.- __init__(*args, **kwargs)¶
Methods
__init__(*args, **kwargs)Checks whether a given set of values can validly be passed to
write_from_format_instructions_dictfor 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.
The value 4, the size in bytes used by OpenSSH for the
intpreceding a variable-length value that indicates the length of the latter.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.
Reads values from the underlying bytestream according to a
typing.Mappingof 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.
Reads values repeatedly as per
read_from_format_instructions_dictuntil 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.
Writes values to the underlying bytestream according to a
typing.Mappingof format instructions.Writes a list of values to the underlying bytestream as per
write_from_format_instructions_dict.writelines(lines, /)Write lines to the file.
Attributes
The value 4, the size in bytes used by OpenSSH for the
intpreceding a variable-length value that indicates the length of the latter.True if the file is closed.
- OPENSSH_DEFAULT_STRING_LENGTH_SIZE = 4¶
The value 4, the size in bytes used by OpenSSH for the
intpreceding 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_dictfor given format instructions.- Parameters
target_dict (
Mapping[str,Any]) – Atyping.Mappingof value names to values to be checked.format_instructions_dict (
Mapping[str,Union[str,PascalStyleFormatInstruction,PascalStyleFormatInstructionStringLengthSize]]) – Atyping.Mappingof value names to format instructions.
- Raises
UserWarning – A key is missing from
target_dictthat is present informat_instructions_dict, or the type or struct size of a value for a key intarget_dictdoes not match that proscribed for that key informat_instructions_dict.- Return type
- 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
intpreceding a variable-length value that indicates the length of the latter.- Return type
- 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.
- read_from_format_instruction(format_instruction, string_length_size=None)¶
Reads a value from the underlying bytestream according to a format instruction.
- Parameters
format_instruction (
Union[str,PascalStyleFormatInstruction]) – A format instruction; either astructformat string, or aPascalStyleFormatInstruction.string_length_size (
Optional[int]) – Ifformat_instructionis aPascalStyleFormatInstruction, the size in bytes of theintpreceding the variable-length value that indicates the length of the latter. Ignored otherwise. The default is 4, which OpenSSH uses for encoding keys.
- Return type
- Returns
The read value. If
format_instructionis astructformat string, the value unpacked usingstruct.unpack(); ifformat_instructionis aPascalStyleFormatInstruction, 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.ValueError –
string_length_sizeis nonpositive.
- read_from_format_instructions_dict(format_instructions_dict)¶
Reads values from the underlying bytestream according to a
typing.Mappingof format instructions.- Parameters
format_instructions_dict (
Mapping[str,Union[str,PascalStyleFormatInstruction,PascalStyleFormatInstructionStringLengthSize]]) – Atyping.Mappingof value names to format instructions.- Return type
- Returns
A
typing.Mappingof value names to read values, as perread_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 unsignedintprefix that indicates the length of the byte string to read.- Return type
- Returns
The read byte string.
- Raises
EOFError – Fewer than
string_length_sizebytes remained in the underlying bytestream, or the length prefix exceeds the number of bytes remaining in the underlying bytestream.ValueError –
string_length_sizeis nonpositive.
- read_repeatedly_from_format_instructions_dict(format_instructions_dict)¶
Reads values repeatedly as per
read_from_format_instructions_dictuntil the stream is exhausted.- Parameters
format_instructions_dict (
Mapping[str,Union[str,PascalStyleFormatInstruction,PascalStyleFormatInstructionStringLengthSize]]) – Atyping.Mappingof value names to format instructions.- Return type
- Returns
A
typing.Listoftyping.Mappingof 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
format_instruction (
Union[str,PascalStyleFormatInstruction]) –A format instruction; either a
structformat string, or aPascalStyleFormatInstruction.value (
Any) – The value to write.string_length_size (
Optional[int]) – Ifformat_instructionis aPascalStyleFormatInstruction, the size in bytes of theintpreceding the variable-length value that indicates the length of the latter. Ignored otherwise. The default is 4, which OpenSSH uses for encoding keys.
- Return type
- write_from_format_instructions_dict(format_instructions_dict, values_dict)¶
Writes values to the underlying bytestream according to a
typing.Mappingof format instructions.- Parameters
format_instructions_dict (
Mapping[str,Union[str,PascalStyleFormatInstruction,PascalStyleFormatInstructionStringLengthSize]]) – Atyping.Mappingof value names to format instructions.values_dict (
Mapping[str,Any]) – Atyping.Mappingof value names to values to be written.
- Raises
KeyError –
values_dictdoes not contain a key that is contained informat_instructions_dict.- Return type
- 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
format_instructions_dict (
Mapping[str,Union[str,PascalStyleFormatInstruction,PascalStyleFormatInstructionStringLengthSize]]) – Atyping.Mappingof value names to format instructions.values_dicts (
Sequence[Mapping[str,Any]]) – Atyping.Listoftyping.Mappingof value names to values to be written.
- Raises
KeyError – One of the
values_dictsdoes not contain a key that is contained informat_instructions_dict.- Return type
- 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.