-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.py
99 lines (84 loc) · 2.69 KB
/
parsers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def get_parser(parser_name):
"""
Try to instantiate a parser from the parser_name
parser_name: The name of the parser to instantiate.
throws: NameError, if the parser is not found
returns: A Parser object.
"""
try:
return globals()[parser_name]()
except KeyError as e:
raise NameError("The parser '"+parser_name+"' was not found. Is it properly defined in parsers.py? Is it correctly spelled?")
class Result(object):
"""
This object is the result of a parse operation
"""
_command_name = ""
_command_arguments = []
@property
def command_name(self):
return self._command_name
@command_name.setter
def command_name(self, value):
self._command_name = value
@property
def command_arguments(self):
return self._command_arguments
@command_arguments.setter
def command_arguments(self, value):
self._command_arguments = value
def __init__(self, command_name="", command_arguments=[]):
self.command_name = command_name
self.command_arguments = command_arguments
class Parser(object):
_trigger_parse_character = '/'
@property
def trigger_parse_character(self):
return self._trigger_parse_character
@trigger_parse_character.setter
def trigger_parse_character(self, value):
self._trigger_parse_character = value
def __init__(self, trigger_parse_character = "/"):
self.trigger_parse_character = trigger_parse_character
def parse(self, message):
"""
Parse a message and return a tuple of the command that should be executed in the module commands and it's arguments.
message: the message to parse
returns: (string to pass to commands.get_command; array of arguments)
"""
if len(message) == 0 or message[0] != self.trigger_parse_character:
return Result("Broadcast", [message])
else:
parts = message.split(' ')
for i, part in enumerate(parts[:]):
if(self._is_part_array(part)):
parts[i] = part.split(",")
return Result(parts[0][1:].title(), parts[1:])
def _is_part_array(self, part):
"""
Determine if part is should be treated as an array or not.
An array have the form of "first,second,last"
"""
if len(part) == 0 or part[0] == "," or part[-1] == "," or "," not in part:
return False
for i in range(1, len(part)-1):
if part[i] == "," and (part[i-1] == ' ' or part[i+1] == ' '):
return False
return True
#
# Example of parser.
#
class Standard(Parser):
def parse(self, message):
if cmd == "leave":
return Result("Exit")
return Result("Broadcast", message)
class Kronos(Parser):
def parse(self, message):
parts = message.split(' ')
if len(parts) != 0:
if parts[0] == "exit":
return Result("Exit")
if parts[0] == "rename" and len(parts) >= 2:
return Result("Rename", [parts[1]])
return Result("Broadcast", message)