summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/command_parser.py48
1 files changed, 48 insertions, 0 deletions
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