From a9ca4aec1234d0c21b0a47a8181645f92226738e Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 15 Jun 2022 15:31:08 -0500 Subject: Added command parser code --- src/command_parser.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/command_parser.py (limited to 'src/command_parser.py') diff --git a/src/command_parser.py b/src/command_parser.py new file mode 100644 index 0000000..094e367 --- /dev/null +++ b/src/command_parser.py @@ -0,0 +1,48 @@ +from enum import Enum + +class Commands(Enum): + PLAY = 0 + ADD_TO_PLAYLIST = 1 + CLEAR = 2 + SEARCH_SONG = 3 + SEARCH_ALBUM = 4 + + +class CommandException(Exception): + """ + The user has introduced an invalid command. + """ + pass + + +def parse_command(message): + """ + Parse a mumble message into a possible command. + If it isn't a command (i.e. a plain message intended for + another user) we'll return None. If it is a message (starts with !) + but it is an invalid one, we wil throw an exception. + """ + current = 0 + + if message[current] == '!': + current += 1 + + if message[current:5].tolower() == 'play': + current += 5 + return (Commands.PLAY, message[current:]) + elif message[current:4].tolower() == 'add': + current +=4 + return (Commands.ADD_TO_PLAYLIST, message[current:]) + elif message[current:6].tolower() == 'clear': + current += 6 + return (Commands.CLEAR, message[current:]) + elif message[current:12].tolower() == 'search_song': + current += 13 + return (Commands.SEARCH_SONG, message[current:]) + elif message[current:13].tolower() == 'search_album': + current += 14 + return (Commands.SEARCH_ALBUM, message[current:]) + else: + raise CommandException('Invalid command.') + + return None -- cgit v1.2.3