def handle_message(self, msg): """ Takes a raw message direct from the server, parses it, and stores its data in the world and body model objects given at init. Returns the type of message received. """ # get all the expressions contained in the given message parsed = message_parser.parse(msg) if PRINT_SERVER_MESSAGES: print parsed[0] + ":", parsed[1:], "\n" # this is the name of the function that should be used to handle # this message type. we pull it from this object dynamically to # avoid having a huge if/elif/.../else statement. msg_func = "_handle_%s" % parsed[0] if hasattr(self, msg_func): # call the appropriate function with this message getattr(self, msg_func).__call__(parsed) # throw an exception if we don't know about the given message type else: m = "Can't handle message type '%s', function '%s' not found." raise sp_exceptions.MessageTypeError(m % (parsed[0], msg_func)) # return the type of message received return parsed[0]
def handle_message(self, msg): """ Takes a raw message direct from the server, parses it, and stores its data in the world and body model objects given at init. Returns the type of message received. """ # get all the expressions contained in the given message parsed = message_parser.parse(msg) if PRINT_SERVER_MESSAGES: print parsed, "\n" # this is the name of the function that should be used to handle # this message type. we pull it from this object dynamically to # avoid having a huge if/elif/.../else statement. msg_func = "_handle_%s" % parsed[0] if hasattr(self, msg_func): # call the appropriate function with this message getattr(self, msg_func).__call__(parsed) # throw an exception if we don't know about the given message type else: m = "Can't handle message type '%s', function '%s' not found." raise sp_exceptions.MessageTypeError(m % (parsed[0], msg_func)) # return the type of message received return parsed[0]
def load_train_set(self, message: str) -> None: parsed = message_parser.parse(message, ';') self.train_number = parsed[0] self.train_type = parsed[1] self.railway = parsed[2] self.start_station = parsed[3] self.final_station = parsed[4] self.arrival_time = parsed[5] if len(parsed) > 5 else '' self.departure_time = parsed[6] if len(parsed) > 6 else ''
def _process_message(self, message: str) -> None: parsed = message_parser.parse(message, ';') if len(parsed) < 2: return parsed[1] = parsed[1].upper() if parsed[1] == 'HELLO': self._process_hello(parsed) assert self.device_info.area is not None, 'Area cannot be empty!' assert self.rm.soundset.name is not None, \ 'Soundset cannot have empty name!' self._send(self.device_info.area + ';SH;REGISTER;' + self.rm.soundset.name + ';1.0') elif (parsed[1] == 'PING' and len(parsed) > 2 and parsed[2].upper() == 'REQ-RESP'): if len(parsed) > 3: self._send('-;PONG;{0}'.format(parsed[3])) else: self._send('-;PONG') if parsed[1] != 'SH' or parsed[0] != self.device_info.area: return parsed[2] = parsed[2].upper() if parsed[2] == 'REGISTER-RESPONSE': self._process_register_response(parsed) elif parsed[2] == 'SYNC': self._process_sync(parsed) elif parsed[2] == 'CHANGE-SET': self._process_change_set(parsed) elif parsed[2] == 'SETS-LIST': self._process_sets_list() elif (parsed[2] == 'PRIJEDE' or parsed[2] == 'ODJEDE' or parsed[2] == 'PROJEDE' or parsed[2] == 'SPEC'): if not self.gong_played and self.rm.soundset.play_gong: self.rm.play_raw_report([ os.path.join('gong', 'gong_start'), ]) self.gong_played = True if parsed[2] == 'SPEC': self.rm.process_spec_message(parsed[3].upper()) else: self.rm.process_trainset_message(parsed)
def do_POST(self): content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) try: if self.path == '/message': if len(post_data) > 140: raise Exception('Message is too long (max 140 chars allowed)') pattern = message_parser.parse(post_data) else: x_parser.parse(post_data) pattern = post_data f = open('pattern', 'w') f.write(pattern) f.close() self.respond(200, "OK") except Exception as e: self.respond(400, "Not OK: " + e.message)
def do_POST(self): content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) try: if self.path == '/message': if len(post_data) > 140: raise Exception( 'Message is too long (max 140 chars allowed)') pattern = message_parser.parse(post_data) else: x_parser.parse(post_data) pattern = post_data f = open('pattern', 'w') f.write(pattern) f.close() self.respond(200, "OK") except Exception as e: self.respond(400, "Not OK: " + e.message)
def new_message(event: Event): if event.from_me: return if event.from_group: return answer = None try: res = message_parser.parse(event.text) if res is not None: action, args = res answer = action(event, *args) except ValueError as e: answer = 'Error at parsing: {0}'.format(e) except Exception as e: answer = str(e) if answer is None: return vk.messages.send(peer_id=event.peer_id, message=answer)