11import ipaddress
22import asyncio
33import logging
4- from typing import Union
4+ from typing import Union , List , AsyncIterator
55
66from pyasic .network .net_range import MinerNetworkRange
77from pyasic .miners .miner_factory import MinerFactory , AnyMiner
@@ -40,7 +40,11 @@ def __repr__(self):
4040 return str (self .network )
4141
4242 def get_network (self ) -> ipaddress .ip_network :
43- """Get the network using the information passed to the MinerNetwork or from cache."""
43+ """Get the network using the information passed to the MinerNetwork or from cache.
44+
45+ Returns:
46+ The proper network to be able to scan.
47+ """
4448 # if we have a network cached already, use that
4549 if self .network :
4650 return self .network
@@ -68,13 +72,19 @@ def get_network(self) -> ipaddress.ip_network:
6872 self .network = ipaddress .ip_network (
6973 f"{ default_gateway } /{ subnet_mask } " , strict = False
7074 )
75+
76+ logging .debug (f"Setting MinerNetwork: { self .network } " )
7177 return self .network
7278
73- async def scan_network_for_miners (self ) -> None or list :
74- """Scan the network for miners, and return found miners as a list."""
79+ async def scan_network_for_miners (self ) -> List [AnyMiner ]:
80+ """Scan the network for miners, and return found miners as a list.
81+
82+ Returns:
83+ A list of found miners.
84+ """
7585 # get the network
7686 local_network = self .get_network ()
77- print (f"Scanning { local_network } for miners... " )
87+ logging . debug (f"Scanning { local_network } for miners" )
7888
7989 # clear cached miners
8090 MinerFactory ().clear_cached_miners ()
@@ -103,16 +113,17 @@ async def scan_network_for_miners(self) -> None or list:
103113
104114 # remove all None from the miner list
105115 miners = list (filter (None , miners ))
106- print (f"Found { len (miners )} connected miners... " )
116+ logging . debug (f"Found { len (miners )} connected miners" )
107117
108118 # return the miner objects
109119 return miners
110120
111- async def scan_network_generator (self ):
121+ async def scan_network_generator (self ) -> AsyncIterator [ AnyMiner ] :
112122 """
113123 Scan the network for miners using an async generator.
114124
115- Returns an asynchronous generator containing found miners.
125+ Returns:
126+ An asynchronous generator containing found miners.
116127 """
117128 # get the current event loop
118129 loop = asyncio .get_event_loop ()
@@ -145,7 +156,7 @@ async def scan_network_generator(self):
145156 yield await miner
146157
147158 @staticmethod
148- async def ping_miner (ip : ipaddress .ip_address ) -> None or ipaddress .ip_address :
159+ async def ping_miner (ip : ipaddress .ip_address ) -> Union [ None , ipaddress .ip_address ] :
149160 tasks = [ping_miner (ip , port = port ) for port in [4028 , 4029 , 8889 ]]
150161 for miner in asyncio .as_completed (tasks ):
151162 miner = await miner
@@ -155,7 +166,7 @@ async def ping_miner(ip: ipaddress.ip_address) -> None or ipaddress.ip_address:
155166 @staticmethod
156167 async def ping_and_get_miner (
157168 ip : ipaddress .ip_address ,
158- ) -> None or AnyMiner :
169+ ) -> Union [ None , AnyMiner ] :
159170 tasks = [ping_and_get_miner (ip , port = port ) for port in [4028 , 4029 , 8889 ]]
160171 for miner in asyncio .as_completed (tasks ):
161172 miner = await miner
@@ -165,7 +176,7 @@ async def ping_and_get_miner(
165176
166177async def ping_miner (
167178 ip : ipaddress .ip_address , port = 4028
168- ) -> None or ipaddress .ip_address :
179+ ) -> Union [ None , ipaddress .ip_address ] :
169180 for i in range (PyasicSettings ().network_ping_retries ):
170181 connection_fut = asyncio .open_connection (str (ip ), port )
171182 try :
@@ -192,7 +203,9 @@ async def ping_miner(
192203 return
193204
194205
195- async def ping_and_get_miner (ip : ipaddress .ip_address , port = 4028 ) -> None or AnyMiner :
206+ async def ping_and_get_miner (
207+ ip : ipaddress .ip_address , port = 4028
208+ ) -> Union [None , AnyMiner ]:
196209 for i in range (PyasicSettings ().network_ping_retries ):
197210 connection_fut = asyncio .open_connection (str (ip ), port )
198211 try :
0 commit comments