-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandtranslate.py
214 lines (179 loc) · 8.27 KB
/
commandtranslate.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import re
from time import time
from collections import OrderedDict
# TODO: replace exceptions with error codes?
class BotCommandException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
class UnknownCommandException(BotCommandException):
def __init__(self, command_name):
self.command_name = command_name
self.message = "Unrecognized command: %s " % command_name
class WrongValueException(BotCommandException):
def __init__(self, argument_name):
self.argument_name = argument_name
self.message = "Unrecognized value for argument: %s" % argument_name
class UnknownArgumentException(BotCommandException):
def __init__(self, argument_name):
self.argument_name = argument_name
self.message = "Unrecognized argument name: %s" % argument_name
class MissingValueException(BotCommandException):
def __init__(self, argument_name):
self.argument_name = argument_name
self.message = "Argument '%s' requires value!" % argument_name
class MissingArgumentException(BotCommandException):
def __init__(self, argument_name):
self.argument_name = argument_name
self.message = "Command requires '%s' argument! " % argument_name
class BotCommand(object):
def __init__(self, name, command, arguments=None, destination_name=None,origin_name=None):
self.destination_name = destination_name
self.name = name
self.command = command
self.arguments = arguments
self.origin_name = origin_name
self._mandatory_arguments = OrderedDict()
self._optional_arguments = OrderedDict()
def add_argument(self, name, value_regexp=".*"):
# TODO: optional arguments
self._mandatory_arguments[name] = value_regexp
def _parse_argument_list(self, arguments):
# TODO: optional arguments
w = 0
new_arguments = OrderedDict()
while w < len(arguments):
if arguments[w] in self._mandatory_arguments.keys():
argument_name = arguments[w]
value_regexp = self._mandatory_arguments[argument_name]
if value_regexp:
# we expect a value even if empty string
if w < len(arguments) - 1:
value = arguments[w + 1]
if re.match(value_regexp, value):
new_arguments[arguments[w]] = value
w = w + 2
else:
raise WrongValueException(argument_name)
else:
# we do not have a value available!
raise MissingValueException(argument_name)
else:
# value not expected
new_arguments[arguments[w]] = True
w = w + 1
continue
else:
# unknown argument name
# is this a known value?
# trying to match with the first value regexp available
argument_name = None
value = arguments[w]
for argument_name in self._mandatory_arguments.keys():
value_regexp = self._mandatory_arguments[argument_name]
if value_regexp and re.match(value_regexp, arguments[w]):
new_arguments[argument_name] = value
break
if not new_arguments.has_key(argument_name):
raise UnknownArgumentException(argument_name)
w = w + 1
for mandatory_argument in self._mandatory_arguments.keys():
if mandatory_argument not in new_arguments.keys():
raise MissingArgumentException(mandatory_argument)
return new_arguments
def validate(self, arguments):
# TODO: optional arguments
arguments = self._parse_argument_list(arguments)
return arguments
# for debugging purposes
def __str__(self):
return 'from: %s \nto: %s \nname: %s\ncommand: %s\nargs: %s' % (self.origin_name, self.destination_name, self.name, self.command, self.arguments)
class BotCommandTranslator(object):
def __init__(self, modules, conversation_timeout_secs=10):
self._last_current_destination_change = None
self._current_destination = None
self._commands = {}
self._common_commands = {}
self._modules = modules
self._conversation_timeout_secs = conversation_timeout_secs
def end_conversation(self):
self._last_current_destination_change = None
self._current_destination = None
def start_conversation(self, destination):
if self._current_destination:
self.end_conversation()
self._set_current_destination(destination)
def _set_current_destination(self, destination):
self._current_destination = destination
self._last_current_destination_change = time()
def _get_current_destination(self, update=True):
'''
Checks if current_destination is up to date and returns it.
'''
if update and self._last_current_destination_change:
if (time() - self._last_current_destination_change) > self._conversation_timeout_secs :
self.end_conversation()
else:
self._last_current_destination_change = time()
return self._current_destination
def get_current_destination(self, update=False):
'''
Gets current_destination name but does not check if it's up to date
'''
destination = self._get_current_destination(update)
if destination:
return destination
else:
return ''
def _parse_command_line(self, command_line):
'''
Splits command line into "individual expressions"
"Individual expressions" are words or sets of words inside quotes
'''
matches = re.findall(r'([0-9a-zA-Z\.@\-_\+]+)|"(.+?)"', command_line)
words = []
for match in matches:
for word in match:
if word:
words.append(word)
return words
def _validate_destination(self, destination_name):
return self._modules.get_instance(destination_name)
def validate_command(self, line,origin_name=None):
'''
Does basic command validation and validates command destination.
'''
# checking if we are in a conversation
destination = self._get_current_destination()
# split into words / individual expressions
words = self._parse_command_line(line)
#TODO: review this
# workaround to end conversation
# this is not a final solution and must be reviewed
try:
if words[0] == '.':
self.end_conversation()
return True
except KeyError:
pass
# check if first word is a destination
w = 0
new_destination = self._validate_destination(words[w])
if new_destination: # if we have a new destination, we are sending a command directly to a destination
destination = new_destination
if len(words) > 1: # skip first word because it is a destination
w = w + 1
else:
# if there is no more words, we are just starting a conversation with a destination
self.start_conversation(destination)
return True
else:
# we are in the middle of a conversation or:
# TODO: guess destination based on command?
pass
if destination == None: # send command to controller
# TODO: remove this when the controller is migrated to a module ?
return BotCommand(destination_name=None, name=words[w], command=words[w], arguments=words[w + 1:], origin_name=origin_name)
# check if w word is a command accepted by the destination
return destination.validate_command(BotCommand(destination_name=destination.name, name=words[w], command=None, arguments=words[w + 1:], origin_name=origin_name))