-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
210 lines (185 loc) · 7.51 KB
/
parser.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
import re
class Header:
def __init__(self, header):
self.header = header
def text(self):
return self.header
class Container:
def append(self, matcher):
raise Exception('Not implemented.')
class Action:
def __init__(self, client, matcher):
self.client = client
self.matcher = matcher
def execute(self):
raise Exception('Not implemented.')
class MessageAction(Action):
pass
class ResultMessageAction(MessageAction):
def __init__(self, client, valid):
MessageAction.__init__(self, client, None)
self.valid = valid
RESULT_OK = ResultMessageAction(None, True)
RESULT_NOK = ResultMessageAction(None, False)
class SampleMessageAction(MessageAction,Container):
def __init__(self, client, matcher):
MessageAction.__init__(self, client, matcher)
self.nSamples,samples = self.convert(matcher)
self.samples = []
self.samples.append(samples)
def execute(self):
watchers = self.caller.getWatchers(self.client.id)
self.caller.message(self.client.id, RESULT_OK)
for watcher in watchers:
self.caller.message(watcher, self)
def text(self):
fmt = '! {0} {2} {1} '+' '.join(['{'+str(i+3)+'}' for i in range(self.nSamples) ])
return fmt.format(self.client.id, self.nSamples, *self.samples)
def convert(self, matcher):
nSamples = int(self.matcher.group(2))
samples = [ int(self.matcher.group(1)) ]
for i in self.matcher.group(3).split():
samples.append(float(i))
if len(samples)-1 != nSamples:
raise ValueError('Inconsistent packet count.')
return (nSamples, tuple(samples))
def append(self, matcher):
nSamples,samples = self.convert(matcher)
if nSamples != self.nSamples:
raise ValueError("Wrong value")
self.samples.append(samples)
class RoleMessageAction(MessageAction):
def __init__(self, client, role):
MessageAction.__init__(self, client, None)
self.role = role
def text(self):
return str(self.role)
class HeaderMessageAction(MessageAction):
def __init__(self, client, header):
MessageAction.__init__(self, client, None)
self.header = Header(header)
def text(self):
return self.header.text()
class StatusMessageAction(MessageAction):
def __init__(self, client, clients):
MessageAction.__init__(self, client, None)
self.clients = clients
def text(self):
nClients = len(self.clients)
retarr = [ '{0} clients connected'.format(nClients) ]
for key,val in self.clients.items():
retarr.append('{0}:{1}'.format(key, val.getRole()))
return '\r\n'.join(retarr)
class CommandAction(Action):
pass
class ResultCommandAction(CommandAction):
def __init__(self, client, status):
CommandAction.__init__(self, client, False)
self.status = status
def execute(self):
self.caller.message(self.client.id, self.status)
class StatusCommandAction(CommandAction):
def execute(self):
clients = self.caller.getClients()
status = StatusMessageAction(self.client, clients)
self.caller.message(self.client.id, [RESULT_OK, status])
class GetheaderCommandAction(CommandAction):
def __init__(self, client, matcher):
CommandAction.__init__(self, client, matcher)
self.id = int(self.matcher.group(1))
def execute(self):
header = HeaderMessageAction(self.client, client.getHeader(self.id))
self.caller.message(self.client.id, [ RESULT_OK, header ])
class SetheaderCommandAction(CommandAction):
def __init__(self, client, matcher):
CommandAction.__init__(self, client, matcher)
self.header = Header(self.matcher.group(1))
def execute(self):
self.client.setHeader(self.header)
self.caller.message(self.client.id, RESULT_OK)
class EEGCommandAction(CommandAction):
def execute(self):
self.client.setRole('EEG')
self.caller.message(self.client.id, RESULT_OK)
class DisplayCommandAction(CommandAction):
def execute(self):
self.client.setRole('Display')
self.caller.message(self.client.id, RESULT_OK)
class RoleCommandAction(CommandAction):
def execute(self):
role = RoleMessageAction(self.client, self.client.getRole())
self.caller.message(self.client.id, [ RESULT_OK, role ])
class WatchCommandAction(CommandAction):
def __init__(self, client, matcher):
CommandAction.__init__(self, client, matcher)
self.id = int(self.matcher.group(1))
def execute(self):
try:
if self.caller.getRole(self.id) != 'EEG':
raise Exception('Role "EEG" is required.')
self.client.setWatch(self.id)
self.caller.message(self.client.id, RESULT_OK)
except:
self.caller.message(self.client.id, RESULT_NOK)
raise
class UnwatchCommandAction(CommandAction):
def __init__(self, client, matcher):
CommandAction.__init__(self, client, matcher)
self.id = int(self.matcher.group(1))
def execute(self):
try:
if self.caller.getRole(self.id) != 'EEG':
raise Exception('Role "EEG" is required.')
self.client.setUnwatch(self.id)
self.caller.message(self.client.id, RESULT_OK)
except:
self.caller.message(self.client.id, RESULT_NOK)
raise
class WatchallCommandAction(CommandAction):
def execute(self):
try:
if self.caller.getRole(self.id) != 'EEG':
raise Exception('Role "EEG" is required.')
self.client.setWatch()
self.caller.message(self.client.id, RESULT_OK)
except:
self.caller.message(self.client.id, RESULT_NOK)
raise
class UnwatchallCommandAction(CommandAction):
def execute(self):
try:
if self.caller.getRole(self.id) != 'EEG':
raise Exception('Role "EEG" is required.')
self.client.setUnwatch()
self.caller.message(self.client.id, RESULT_OK)
except:
self.caller.message(self.client.id, RESULT_NOK)
raise
map = [
(re.compile(r'^!\s+([0-9]+)\s+([0-9]+)\s+(.*)$'), SampleMessageAction),
(re.compile(r'^status$'), StatusCommandAction),
(re.compile(r'^getheader\s+([0-9]+)$'), GetheaderCommandAction),
(re.compile(r'^setheader\s(.+)$'), SetheaderCommandAction),
(re.compile(r'^eeg$'), EEGCommandAction),
(re.compile(r'^display$'), DisplayCommandAction),
(re.compile(r'^role$'), RoleCommandAction),
(re.compile(r'^watch\s([0-9]+)$'), WatchCommandAction),
(re.compile(r'^unwatch\s([0-9]+)$'), UnwatchCommandAction),
(re.compile(r'^watch\s+$'), WatchallCommandAction),
(re.compile(r'^unwatch\s+$'), UnwatchallCommandAction)
]
class Parser(object):
def __init__(self, caller):
self.caller = caller
def parse(msg, client = None, container = None):
for pattern, Class in map:
matcher = pattern.match(msg)
if matcher is not None:
if container is not None and isinstance(container, Container):
container.append(matcher)
return container
else:
if client is None:
raise ValueError('One of the "client" or "container" is supposed to be present.')
return Class(client, matcher)
return ResultCommandAction(client, RESULT_NOK)