11"""The middleman between Plover and the server."""
22
3- import os
43from datetime import datetime
54from json import loads
65from operator import itemgetter
7- from typing import List
6+ from pathlib import Path
87
98from aiohttp .web import WebSocketResponse
109from jsonpickle import encode
2322 ERROR_NO_SERVER ,
2423 ERROR_SERVER_RUNNING ,
2524)
26- from plover_websocket_server .server import EngineServer , ServerStatus
27- from plover_websocket_server .websocket .server import WebSocketServer
25+ from plover_websocket_server .websocket .server import ServerStatus , WebSocketServer
2826
2927SERVER_CONFIG_FILE = "plover_websocket_server_config.json"
3028
3129
3230class EngineServerManager :
3331 """Manages a server that exposes the Plover engine."""
3432
35- _server : EngineServer
33+ _server : WebSocketServer
3634 _tape_model : TapeModel
3735
3836 def __init__ (self , engine : StenoEngine , test_mode : bool = False ) -> None :
3937 self ._server = None
4038 self ._engine : StenoEngine = engine
41- self ._config_path : str = os . path . join (CONFIG_DIR , SERVER_CONFIG_FILE )
39+ self ._config_path = Path (CONFIG_DIR ). joinpath ( SERVER_CONFIG_FILE )
4240 if self .get_server_status () != ServerStatus .Stopped :
4341 raise AssertionError (ERROR_SERVER_RUNNING )
4442
45- self ._config = ServerConfig (
46- self ._config_path
47- ) # reload the configuration when the server is restarted
43+ self ._config = ServerConfig (self ._config_path ) # reload the configuration when the server is restarted
4844
4945 self ._server = WebSocketServer (
5046 self ._config .host ,
@@ -66,6 +62,7 @@ def start(self) -> None:
6662 Raises:
6763 AssertionError: The server failed to start.
6864 IOError: The server failed to start.
65+
6966 """
7067 self ._server .start ()
7168
@@ -78,8 +75,8 @@ def raw_stop(self) -> None:
7875
7976 Raises:
8077 AssertionError: The server failed to stop.
81- """
8278
79+ """
8380 if self .get_server_status () != ServerStatus .Running :
8481 raise AssertionError (ERROR_NO_SERVER )
8582
@@ -97,8 +94,8 @@ def stop(self) -> None:
9794
9895 Raises:
9996 AssertionError: The server failed to stop.
100- """
10197
98+ """
10299 self .raw_stop ()
103100 log .info ("Joining server thread..." )
104101 self ._server .join ()
@@ -116,8 +113,8 @@ def get_server_status(self) -> ServerStatus:
116113
117114 Returns:
118115 The status of the server.
119- """
120116
117+ """
121118 return self ._server .listened .status if self ._server else ServerStatus .Stopped
122119
123120 def join (self ) -> None :
@@ -157,9 +154,7 @@ async def _on_message(self, data: dict) -> None:
157154 from plover .steno import Stroke
158155 from plover .translation import Translation , _mapping_to_macro
159156
160- stroke = Stroke (
161- []
162- ) # required, because otherwise Plover will try to merge the outlines together
157+ stroke = Stroke ([]) # required, because otherwise Plover will try to merge the outlines together
163158 # and the outline [] (instead of [Stroke([])]) can be merged to anything
164159 macro = _mapping_to_macro (mapping , stroke )
165160 if macro is not None :
@@ -181,7 +176,6 @@ async def _on_message(self, data: dict) -> None:
181176
182177 def _connect_hooks (self ):
183178 """Creates hooks into all of Plover's events."""
184-
185179 if not self ._engine :
186180 raise AssertionError (ERROR_MISSING_ENGINE )
187181
@@ -194,7 +188,6 @@ def _connect_hooks(self):
194188
195189 def _disconnect_hooks (self ):
196190 """Removes hooks from all of Plover's events."""
197-
198191 self ._server .data .stop_listening ()
199192 if not self ._engine :
200193 raise AssertionError (ERROR_MISSING_ENGINE )
@@ -211,6 +204,7 @@ def _on_stroked(self, stroke: Stroke):
211204
212205 Args:
213206 stroke: The stroke that was just performed.
207+
214208 """
215209 stroke_json = encode (stroke , unpicklable = False )
216210 paper = self ._tape_model ._paper_format (stroke )
@@ -223,14 +217,14 @@ def _on_stroked(self, stroke: Stroke):
223217 }
224218 self ._server .queue_message (data )
225219
226- def _on_translated (self , old : List [_Action ], new : List [_Action ]):
220+ def _on_translated (self , old : list [_Action ], new : list [_Action ]):
227221 """Broadcasts when a new translation occurs.
228222
229223 Args:
230224 old: A list of the previous actions for the current translation.
231225 new: A list of the new actions for the current translation.
232- """
233226
227+ """
234228 old_json = encode (old , unpicklable = False )
235229 new_json = encode (new , unpicklable = False )
236230
@@ -244,8 +238,8 @@ def _on_machine_state_changed(self, machine_type: str, machine_state: str):
244238 machine_type: The name of the active machine.
245239 machine_state: The new machine state. This should be one of the
246240 state constants listed in plover.machine.base.
247- """
248241
242+ """
249243 data = {
250244 "machine_state_changed" : {
251245 "machine_type" : machine_type ,
@@ -259,8 +253,8 @@ def _on_output_changed(self, enabled: bool):
259253
260254 Args:
261255 enabled: If the output is now enabled or not.
262- """
263256
257+ """
264258 data = {"output_changed" : enabled }
265259 self ._server .queue_message (data )
266260
@@ -270,8 +264,8 @@ def _on_config_changed(self, config_update: Config):
270264 Args:
271265 config_update: An object containing the full configuration or a
272266 part of the configuration that was updated.
273- """
274267
268+ """
275269 config_json = encode (config_update , unpicklable = False )
276270
277271 data = {"config_changed" : loads (config_json )}
@@ -282,8 +276,8 @@ def _on_dictionaries_loaded(self, dictionaries: StenoDictionaryCollection):
282276
283277 Args:
284278 dictionaries: A collection of the dictionaries that loaded.
285- """
286279
280+ """
287281 data = {"dictionaries_loaded" : "0" }
288282 self ._server .queue_message (data )
289283
@@ -292,8 +286,8 @@ def _on_send_string(self, text: str):
292286
293287 Args:
294288 text: The string that was output.
295- """
296289
290+ """
297291 data = {"send_string" : text }
298292 self ._server .queue_message (data )
299293
@@ -302,8 +296,8 @@ def _on_send_backspaces(self, count: int):
302296
303297 Args:
304298 count: The number of backspaces that were output.
305- """
306299
300+ """
307301 data = {"send_backspaces" : count }
308302 self ._server .queue_message (data )
309303
@@ -314,38 +308,33 @@ def _on_send_key_combination(self, combination: str):
314308 combination: A string representing a sequence of key combinations.
315309 Keys are represented by their names based on the OS-specific
316310 keyboard implementations in plover.oslayer.
317- """
318311
312+ """
319313 data = {"send_key_combination" : combination }
320314 self ._server .queue_message (data )
321315
322316 def _on_add_translation (self ):
323317 """Broadcasts when the add translation tool is opened via a command."""
324-
325318 data = {"add_translation" : True }
326319 self ._server .queue_message (data )
327320
328321 def _on_focus (self ):
329322 """Broadcasts when the main window is focused via a command."""
330-
331323 data = {"focus" : True }
332324 self ._server .queue_message (data )
333325
334326 def _on_configure (self ):
335327 """Broadcasts when the configuration tool is opened via a command."""
336-
337328 data = {"configure" : True }
338329 self ._server .queue_message (data )
339330
340331 def _on_lookup (self ):
341332 """Broadcasts when the lookup tool is opened via a command."""
342-
343333 data = {"lookup" : True }
344334 self ._server .queue_message (data )
345335
346336 def _on_suggestions (self ):
347337 """Broadcasts when the suggestions tool is opened via a command."""
348-
349338 data = {"suggestions" : True }
350339 self ._server .queue_message (data )
351340
@@ -354,6 +343,5 @@ def _on_quit(self):
354343
355344 Can be either a full quit or a restart.
356345 """
357-
358346 data = {"quit" : True }
359347 self ._server .queue_message (data )
0 commit comments