-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebsocket_commands.py
66 lines (46 loc) · 1.73 KB
/
websocket_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Contains the websocket command structure
__author__ = "Matteo Golin"
# Imports
from enum import StrEnum, Enum
from typing import Type
# Classes
class WebsocketCommandNotFound(Exception):
"""Raised when there is no matching command."""
def __init__(self, command: str):
self.command = command
self.message = f"The websocket command '{command}' does not exist."
super().__init__(self.message)
class ReplayCommands(StrEnum):
"""Contains the structure for the replay subcommands."""
PLAY = "play replay"
PAUSE = "pause replay"
RESUME = "resume replay"
SPEED = "speed replay"
STOP = "stop replay"
class RecordCommands(StrEnum):
"""Contains the structure for the record subcommands."""
START = "start recording"
STOP = "stop recording"
class WebsocketCommand(Enum):
"""Contains the structure for the telemetry commands."""
UPDATE = "update"
RECORD = RecordCommands
REPLAY = ReplayCommands
# Parsing functions
def split_command_string(command: str) -> list[str]:
"""Splits a websocket command on the spaces."""
return command.split(" ")
def parse(websocket_command: list[str], enum: Type[Enum] = WebsocketCommand) -> Enum:
"""
Returns the websocket command as the matching command enum variable. Any remaining parameters will be left
inside the websocket_command list parameter.
"""
sub_command = websocket_command.pop(0) # Get the highest level command
try:
next_enum = enum[sub_command.upper()]
except KeyError:
raise WebsocketCommandNotFound(sub_command)
if websocket_command and type(next_enum.value) is not str:
return parse(websocket_command, enum=next_enum.value)
else:
return next_enum