Skip to content

Commit

Permalink
Added comments to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ForceFledgling committed Nov 26, 2023
1 parent 9b369df commit 4f9e38b
Showing 1 changed file with 77 additions and 4 deletions.
81 changes: 77 additions & 4 deletions asyncio_telnet/asyncio_telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@
class AsyncTelnet:

def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
"""
Initializes the Telnet connection with optional timeout settings.
Args:
timeout: Timeout value for the Telnet connection.
"""
self.debuglevel = 0
self.timeout = timeout
self.fast_read_timeout = 0.1
self.reader = None
self.writer = None

async def open(self, host, port=0):
"""
Opens a connection to the specified host and port.
Args:
host: The host to connect to.
port: The port to connect to (default is 0, which uses the default TELNET_PORT).
"""
self.eof = 0
if not port:
port = TELNET_PORT
Expand All @@ -47,20 +60,38 @@ async def open(self, host, port=0):
raise f"Error connecting to {host}:{port}: {e}"

async def write(self, buffer):
"""
Writes data to the Telnet connection.
Args:
buffer: The data to be written.
"""
buffer = buffer.replace(IAC, IAC + IAC)
self.writer.write(buffer)
await self.writer.drain()

async def read(self, num_bytes=1):
"""
Reads data from the Telnet connection.
Args:
num_bytes: The number of bytes to read.
Returns:
The read data.
"""
data = await self.reader.read(num_bytes)
return data

async def read_until_eof(self, fast_mode=True):
"""
Performs reading data until the end of the file (EOF).
Typically used in the context of file
or stream handling to read and extract data from a
source until the end of the file is reached.
Reads data from the Telnet connection until the end of the stream.
Args:
fast_mode: If True, uses a fast read timeout; otherwise, uses the standard timeout.
Returns:
The read data until the end of the stream.
"""
timeout = self.fast_read_timeout if fast_mode else self.timeout
response = b''
Expand Down Expand Up @@ -121,10 +152,22 @@ async def filter_telnet_data(self, data):


class AsyncToSyncWrapper:
"""
Wraps an asynchronous class instance, allowing synchronous access to its methods.
"""
def __init__(self, async_class_instance):
self.async_class_instance = async_class_instance

def __getattr__(self, name):
"""
Retrieves attributes from the asynchronous class instance and converts asynchronous methods to synchronous.
Args:
name: The name of the attribute.
Returns:
The attribute or a synchronous wrapper for an asynchronous method.
"""
if hasattr(self.async_class_instance, name):
attr = getattr(self.async_class_instance, name)
if asyncio.iscoroutinefunction(attr):
Expand All @@ -135,16 +178,46 @@ def __getattr__(self, name):
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

def _sync_method(self, async_method, *args, **kwargs):
"""
Invokes an asynchronous method synchronously.
Args:
async_method: The asynchronous method to be invoked.
*args: Positional arguments for the method.
**kwargs: Keyword arguments for the method.
Returns:
The result of the asynchronous method.
"""
loop = asyncio.get_event_loop()
return loop.run_until_complete(async_method(*args, **kwargs))


class Telnet:
"""
Wrapper for synchronous or asynchronous Telnet communication.
"""
def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, sync_mode=False):
"""
Initializes the Telnet wrapper.
Args:
timeout: Timeout value for the Telnet connection.
sync_mode: If True, operates in synchronous mode using AsyncToSyncWrapper.
"""
if sync_mode:
self._instance = AsyncToSyncWrapper(AsyncTelnet(timeout))
else:
self._instance = AsyncTelnet(timeout)

def __getattr__(self, name):
"""
Retrieves attributes from the underlying Telnet instance.
Args:
name: The name of the attribute.
Returns:
The attribute from the underlying Telnet instance.
"""
return getattr(self._instance, name)

0 comments on commit 4f9e38b

Please sign in to comment.