hfutils.utils.binary_proxy
BinaryProxyIO module provides a proxy implementation for binary I/O operations.
This module defines a class that wraps a binary stream and implements the BinaryIO interface, primarily focusing on write operations while providing stubs for other operations. It can be used as a base class for implementing custom binary I/O handlers that need to intercept or modify write operations.
BinaryProxyIO
- class hfutils.utils.binary_proxy.BinaryProxyIO(stream: BinaryIO)[source]
A proxy class that implements the BinaryIO interface, wrapping another binary stream.
This class primarily supports write operations, while other operations raise
io.UnsupportedOperation
exceptions by default. It can be extended by overriding the_on_write
and_after_close
methods to implement custom behavior.- Parameters:
stream (BinaryIO) – The binary stream to wrap
- __enter__()[source]
Enter the runtime context related to this object.
- Returns:
Self
- Return type:
- Raises:
ValueError – If the file is already closed
- __exit__(_BinaryProxyIO__t, _BinaryProxyIO__value, _BinaryProxyIO__traceback)[source]
Exit the runtime context related to this object.
- Parameters:
__t – Exception type
__value – Exception value
__traceback – Exception traceback
- __iter__()[source]
Return self as an iterator.
- Returns:
Self
- Return type:
- Raises:
ValueError – If the file is closed
- close()[source]
Close the file.
This method sets the internal closed flag to True and calls _after_close.
- fileno()[source]
Return the underlying file descriptor.
- Raises:
UnsupportedOperation – Always, as this operation is not supported
- flush()[source]
Flush the write buffers.
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- isatty()[source]
Return whether this is an ‘interactive’ stream.
- Returns:
Always False
- Return type:
bool
- Raises:
ValueError – If the file is closed
- read(_BinaryProxyIO__n=Ellipsis)[source]
Read up to n bytes from the object and return them.
- Parameters:
__n (int, optional) – Number of bytes to read
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- readable()[source]
Return whether the file is readable.
- Returns:
Always False
- Return type:
bool
- Raises:
ValueError – If the file is closed
- readline(_BinaryProxyIO__limit=Ellipsis)[source]
Read and return one line from the stream.
- Parameters:
__limit (int, optional) – Maximum number of bytes to read
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- readlines(_BinaryProxyIO__hint=Ellipsis)[source]
Return a list of lines from the stream.
- Parameters:
__hint (int, optional) – Maximum number of bytes to read
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- seek(_BinaryProxyIO__offset, _BinaryProxyIO__whence=Ellipsis)[source]
Change the stream position.
- Parameters:
__offset (int) – Offset relative to position indicated by whence
__whence (int, optional) – Position from which offset is applied
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- seekable()[source]
Return whether the file supports seeking.
- Returns:
Always False
- Return type:
bool
- Raises:
ValueError – If the file is closed
- tell()[source]
Return the current stream position.
- Returns:
Current position in the file
- Return type:
int
- Raises:
ValueError – If the file is closed
- truncate(_BinaryProxyIO__size=Ellipsis)[source]
Truncate the file to at most size bytes.
- Parameters:
__size (int, optional) – Size to truncate to
- Raises:
ValueError – If the file is closed
UnsupportedOperation – Always, as this operation is not supported
- writable()[source]
Return whether the file is writable.
- Returns:
Always True
- Return type:
bool
- Raises:
ValueError – If the file is closed
- write(_BinaryProxyIO__s)[source]
Write bytes to the file.
This method writes the bytes to the wrapped stream, updates the position, calls the _on_write hook, and returns the number of bytes written.
- Parameters:
__s (bytes) – Bytes to write
- Returns:
Number of bytes written
- Return type:
int
- Raises:
ValueError – If the file is closed