Skip to content

Commit

Permalink
finish replacing str.format() with f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
engn33r committed Dec 3, 2023
1 parent 2f51937 commit 3eabc6e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 29 deletions.
4 changes: 2 additions & 2 deletions compliance/test_fuzzingclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

case = 0
for case in range(1, count + 1):
url = SERVER + '/runCase?case={0}&agent={1}'.format(case, AGENT)
url = f'{SERVER}/runCase?case={case}&agent={AGENT}'
status = websocket.STATUS_NORMAL
try:
ws = websocket.create_connection(url)
Expand All @@ -57,5 +57,5 @@
ws.close(status)

print(f"Ran {case} test cases.")
url = SERVER + '/updateReports?agent={0}'.format(AGENT)
url = f'{SERVER}/updateReports?agent={AGENT}'
ws = websocket.create_connection(url)
28 changes: 9 additions & 19 deletions websocket/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ def timeout(self, seconds: int, callback: Callable) -> None:
def reconnect(self, seconds: int, reconnector: Callable) -> None:
try:
_logging.info(
"reconnect() - retrying in {seconds_count} seconds [{frame_count} frames in stack]".format(
seconds_count=seconds, frame_count=len(inspect.stack())
)
f"reconnect() - retrying in {seconds} seconds [{len(inspect.stack())} frames in stack]"
)
time.sleep(seconds)
reconnector(reconnecting=True)
except KeyboardInterrupt as e:
_logging.info("User exited {err}".format(err=e))
_logging.info(f"User exited {e}")
raise e


Expand Down Expand Up @@ -347,7 +345,7 @@ def _send_ping(self) -> None:
_logging.debug("Sending ping")
self.sock.ping(self.ping_payload)
except Exception as e:
_logging.debug("Failed to send ping: {err}".format(err=e))
_logging.debug(f"Failed to send ping: {e}")

def run_forever(
self,
Expand Down Expand Up @@ -596,16 +594,14 @@ def handleDisconnect(e: Exception, reconnecting: bool = False) -> bool:
raise

if reconnect:
_logging.info("{err} - reconnect".format(err=e))
_logging.info(f"{e} - reconnect")
if custom_dispatcher:
_logging.debug(
"Calling custom dispatcher reconnect [{frame_count} frames in stack]".format(
frame_count=len(inspect.stack())
)
f"Calling custom dispatcher reconnect [{len(inspect.stack())} frames in stack]"
)
dispatcher.reconnect(reconnect, setSock)
else:
_logging.error("{err} - goodbye".format(err=e))
_logging.error(f"{e} - goodbye")
teardown()

custom_dispatcher = bool(dispatcher)
Expand All @@ -618,13 +614,11 @@ def handleDisconnect(e: Exception, reconnecting: bool = False) -> bool:
if not custom_dispatcher and reconnect:
while self.keep_running:
_logging.debug(
"Calling dispatcher reconnect [{frame_count} frames in stack]".format(
frame_count=len(inspect.stack())
)
f"Calling dispatcher reconnect [{len(inspect.stack())} frames in stack]"
)
dispatcher.reconnect(reconnect, setSock)
except (KeyboardInterrupt, Exception) as e:
_logging.info("tearing down on exception {err}".format(err=e))
_logging.info(f"tearing down on exception {e}")
teardown()
finally:
if not custom_dispatcher:
Expand Down Expand Up @@ -671,10 +665,6 @@ def _callback(self, callback, *args) -> None:
callback(self, *args)

except Exception as e:
_logging.error(
"error from callback {callback}: {err}".format(
callback=callback, err=e
)
)
_logging.error(f"error from callback {callback}: {e}")
if self.on_error:
self.on_error(self, e)
3 changes: 1 addition & 2 deletions websocket/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ def recv_data_frame(self, control_frame: bool = False):
if not frame:
# handle error:
# 'NoneType' object has no attribute 'opcode'
raise WebSocketProtocolException(
"Not a valid frame {frame}".format(frame=frame))
raise WebSocketProtocolException(f"Not a valid frame {frame}")
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
self.cont_frame.validate(frame)
self.cont_frame.add(frame)
Expand Down
2 changes: 1 addition & 1 deletion websocket/_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _get_resp_headers(sock, success_statuses: tuple = SUCCESS_STATUSES) -> tuple
response_body = sock.recv(int(content_len)) # read the body of the HTTP error message response and include it in the exception
else:
response_body = None
raise WebSocketBadStatusException("Handshake status {status} {message} -+-+- {headers} -+-+- {body}".format(status=status, message=status_message, headers=resp_headers, body=response_body), status, status_message, resp_headers, response_body)
raise WebSocketBadStatusException(f"Handshake status {status} {status_message} -+-+- {resp_headers} -+-+- {response_body}", status, status_message, resp_headers, response_body)
return status, resp_headers


Expand Down
5 changes: 2 additions & 3 deletions websocket/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _tunnel(sock, host, port, auth):
if auth[1]:
auth_str += f":{auth[1]}"
encoded_str = base64encode(auth_str.encode()).strip().decode().replace('\n', '')
connect_header += "Proxy-Authorization: Basic {str}\r\n".format(str=encoded_str)
connect_header += f"Proxy-Authorization: Basic {encoded_str}\r\n".
connect_header += "\r\n"
dump("request header", connect_header)

Expand All @@ -300,8 +300,7 @@ def _tunnel(sock, host, port, auth):
raise WebSocketProxyException(str(e))

if status != 200:
raise WebSocketProxyException(
"failed CONNECT via proxy status: {status}".format(status=status))
raise WebSocketProxyException(f"failed CONNECT via proxy status: {status}")

return sock

Expand Down
4 changes: 2 additions & 2 deletions websocket/_wsdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def recv() -> tuple:
except websocket.WebSocketException:
return websocket.ABNF.OPCODE_CLOSE, ""
if not frame:
raise websocket.WebSocketException("Not a valid frame {frame}".format(frame=frame))
raise websocket.WebSocketException(f"Not a valid frame {frame}"
elif frame.opcode in OPCODE_DATA:
return frame.opcode, frame.data
elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
Expand Down Expand Up @@ -193,7 +193,7 @@ def recv_ws() -> None:
data = repr(data)

if args.verbose:
msg = "{opcode}: {data}".format(opcode=websocket.ABNF.OPCODE_MAP.get(opcode), data=data)
msg = f"{websocket.ABNF.OPCODE_MAP.get(opcode)}: {data}"
else:
msg = data

Expand Down

0 comments on commit 3eabc6e

Please sign in to comment.