Skip to content

Commit bb21671

Browse files
Refactor exception handling and remove unused file
Replaced bare except clauses with 'except Exception' for better error handling in several protocol files. Removed the unused 'opengsq/responses/flatout2.py' file. Minor formatting and whitespace adjustments were made for consistency.
1 parent 5e0240b commit bb21671

File tree

12 files changed

+17
-45
lines changed

12 files changed

+17
-45
lines changed

opengsq/binary_reader.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@ def read_pascal_string(self, encoding='utf-8', errors='ignore'):
7373
length = self.read_byte()
7474
pascal_string = str(self.read_bytes(length - 1), encoding=encoding, errors=errors)
7575
return pascal_string
76-

opengsq/protocol_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def __init__(self, host: str, port: int, timeout: float = 5.0):
1111
self._host = host
1212
self._port = port
1313
self._timeout = timeout
14-
self._allow_broadcast = False
14+
self._allow_broadcast = False

opengsq/protocol_socket.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ async def communicate(protocol: ProtocolBase, data: bytes, source_port: int = No
152152
if source_port:
153153
udpClient.bind_port(source_port)
154154
udpClient.settimeout(protocol._timeout)
155-
155+
156156
loop = asyncio.get_running_loop()
157157
transport, protocol_instance = await loop.create_datagram_endpoint(
158158
lambda: Socket.Protocol(protocol._timeout), # Use public Protocol class
159159
local_addr=('0.0.0.0', source_port if source_port else 0),
160160
allow_broadcast=protocol._allow_broadcast
161161
)
162-
162+
163163
try:
164164
transport.sendto(data, (protocol._host, protocol._port))
165165
return await protocol_instance.recv()
@@ -194,4 +194,4 @@ async def test_socket_async():
194194

195195
loop = asyncio.get_event_loop()
196196
loop.run_until_complete(test_socket_async())
197-
loop.close()
197+
loop.close()

opengsq/protocols/eldewrito.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,4 @@ async def discover_servers(self, broadcast_address: str = "255.255.255.255") ->
172172
except Exception as e:
173173
self.logger.error(f"Error during server discovery: {e}")
174174

175-
return discovered_servers
175+
return discovered_servers

opengsq/protocols/flatout2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ def _parse_response(self, br: BinaryReader, original_data: bytes) -> Status:
610610
try:
611611
config_data = br.read_bytes(min(remaining_bytes, 12))
612612
info["config"] = config_data.hex()
613-
except:
613+
except Exception:
614614
info["config"] = ""
615615
else:
616616
info["config"] = ""

opengsq/protocols/trackmania_nations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _deserialize_string(self, data: bytes, offset: int) -> Tuple[Optional[str],
283283
string_data = data[offset+4:offset+4+length]
284284
string = string_data.decode('utf-8', errors='replace')
285285
return string, 4 + length
286-
except:
286+
except Exception:
287287
return None, 0
288288

289289
def _extract_all_strings(self, data: bytes) -> List[str]:
@@ -307,7 +307,7 @@ def _extract_all_strings(self, data: bytes) -> List[str]:
307307
try:
308308
string = current_string.decode('ascii')
309309
strings.append(string)
310-
except:
310+
except Exception:
311311
pass
312312
current_string = bytearray()
313313

@@ -316,7 +316,7 @@ def _extract_all_strings(self, data: bytes) -> List[str]:
316316
try:
317317
string = current_string.decode('ascii')
318318
strings.append(string)
319-
except:
319+
except Exception:
320320
pass
321321

322322
return strings
@@ -634,7 +634,7 @@ def _extract_challenge_without_prefix(self, data: bytes, srv_pos: int) -> Option
634634
if self._is_valid_challenge_name(string):
635635
return string
636636
found_strings.append(string)
637-
except:
637+
except Exception:
638638
pass
639639
current_string = bytearray()
640640

@@ -645,7 +645,7 @@ def _extract_challenge_without_prefix(self, data: bytes, srv_pos: int) -> Option
645645
if self._is_valid_challenge_name(string):
646646
return string
647647
found_strings.append(string)
648-
except:
648+
except Exception:
649649
pass
650650

651651
# Fallback: Erste gültige Challenge aus gefundenen Strings

opengsq/protocols/vcmp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async def __send_and_receive(self, data: bytes):
7979
f"Packet header mismatch. Received: {header}. Expected: {self._response_header}."
8080
)
8181

82-
return response[len(self._response_header) + len(packet_header) :]
82+
return response[len(self._response_header) + len(packet_header):]
8383

8484
def __read_string(self, br: BinaryReader, read_offset=1):
8585
"""

opengsq/protocols/w40kdow.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ def _parse_broadcast(self, data: bytes, addr: tuple) -> Status:
226226

227227

228228
if __name__ == "__main__":
229-
import asyncio
230-
231229
async def main_async():
232230
# Test with the provided server
233231
w4kdow = W40kDow(host="172.29.100.29", port=6112, timeout=10.0)
@@ -236,7 +234,7 @@ async def main_async():
236234
print("Listening for Dawn of War server broadcasts...")
237235
status = await w4kdow.get_status()
238236
print(f"\n{'='*60}")
239-
print(f"Server Status:")
237+
print("Server Status:")
240238
print(f"{'='*60}")
241239
print(f"GUID: {status.guid}")
242240
print(f"Hostname: {status.hostname}")
@@ -249,7 +247,7 @@ async def main_async():
249247
print(f"Build: {status.build_number}")
250248
print(f"Magic: {status.magic_marker}")
251249
print(f"\nFaction Codes: {', '.join(status.faction_codes)}")
252-
print(f"\nMap Features:")
250+
print("\nMap Features:")
253251
for i, feature in enumerate(status.map_features, 1):
254252
print(f" {i}. {feature}")
255253

opengsq/rcon_protocols/source_rcon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __get_packets(self, packet_bytes: bytes):
151151
size = br.read_long()
152152

153153
if br.stream_position + size > len(packet_bytes):
154-
return packets, packet_bytes[br.stream_position - 4 :]
154+
return packets, packet_bytes[br.stream_position - 4:]
155155

156156
id = br.read_long()
157157
type = br.read_long()

opengsq/responses/flatout2.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)