Skip to content

Commit

Permalink
remove comments, update versions and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Georgios Hadjiharalambous committed Oct 2, 2024
1 parent 472e83c commit 9ed03bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
52 changes: 28 additions & 24 deletions speechmatics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
logging.getLogger("websockets.protocol").setLevel(logging.INFO)
HIDDEN_MSG_PREFIX = "Hidden_"


class WebsocketClient:
"""
Manage a transcription session with the server.
Expand Down Expand Up @@ -275,24 +276,27 @@ async def _consumer_handler(self):
raise ex
self._consumer(message)

# async def _request_spkrids(self, interval = 5):
# LOGGER.warning("waiting for recognisnier")
# await self._recognition_started.wait()
# LOGGER.warning("done waiting for recognisnier started")

# while self.session_running:
# await asyncio.sleep(interval)
# assert self.websocket
# await self.websocket.send(json.dumps({"message": "Hidden_GetSpeakerIds"}))
# # self._call_middleware({"message": "Hidden_GetSpeakerIds"})

async def _send_message(self, msg):
# LOGGER.warning("waiting for recognisnier")
# await self._recognition_started.wait()
# LOGGER.warning("done waiting for recognisnier started")
"""
Sends a message to the server. A dict/json like object is expected as the msg param.
"""
if self.session_running:
assert self.websocket
await self.websocket.send(json.dumps(msg))
assert self.websocket
try:
await self.websocket.send(json.dumps(msg))
except TypeError as ex:
LOGGER.info(
f"Cannot send this type of object {msg=} as a message. Exception occured:%s",
repr(ex),
)
return
except websockets.exceptions.ConnectionClosedOK:
# Can occur if a timeout has closed the connection.
LOGGER.info("Cannot send from a closed websocket.")
return
except websockets.exceptions.ConnectionClosedError:
LOGGER.info("Disconnected while sending a message().")
return

async def _producer_handler(self, stream, audio_chunk_size):
"""
Expand Down Expand Up @@ -363,9 +367,10 @@ def add_event_handler(self, event_name, event_handler):
:raises ValueError: If the given event name is not valid.
"""
if event_name.startswith(
HIDDEN_MSG_PREFIX
)and event_name not in self.event_handlers:
if (
event_name.startswith(HIDDEN_MSG_PREFIX)
and event_name not in self.event_handlers
):
self.event_handlers[event_name] = []
if event_name == "all":
for name in self.event_handlers.keys():
Expand Down Expand Up @@ -400,9 +405,10 @@ def add_middleware(self, event_name, middleware):
:raises ValueError: If the given event name is not valid.
"""
if event_name.startswith(
HIDDEN_MSG_PREFIX
) and event_name not in self.middlewares:
if (
event_name.startswith(HIDDEN_MSG_PREFIX)
and event_name not in self.middlewares
):
self.middlewares[event_name] = []
if event_name == "all":
for name in self.middlewares.keys():
Expand Down Expand Up @@ -430,8 +436,6 @@ async def _communicate(self, stream, audio_settings):
await self.websocket.send(start_recognition_msg)

consumer_task = asyncio.create_task(self._consumer_handler())
# request_speaker_ids_task = asyncio.create_task(self._request_spkrids())

producer_task = asyncio.create_task(
self._producer_handler(stream, audio_settings.chunk_size)
)
Expand Down

0 comments on commit 9ed03bf

Please sign in to comment.