blob: 094e367ef82b21c81ff792b75c92584f7a7550cc (
plain)
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
|
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
|