async def handler(next_handler: Callable, message: str) -> None: try: event, kwargs = unpack_command(message) client.trigger(event, **kwargs) except ValueError: rfc2812_log.debug("Failed to parse line >>> {}".format(message)) await next_handler(message)
def test_usermode(): """ MODE command """ command = "USERMODE" expected_kwargs = {"nick": "nck", "host": "", "modes": "+x"} message = "MODE nck +x" assert (command, expected_kwargs) == unpack_command(message) expected_kwargs['user'] = '******' assert set(expected_kwargs) == set(parameters(command))
def test_channelmode_no_params(): """ MODE command """ command = "CHANNELMODE" expected_kwargs = {"channel": "#ch", "host": "", "modes": "+m", "params": []} message = "MODE #ch +m" assert (command, expected_kwargs) == unpack_command(message) expected_kwargs['user'] = '******' expected_kwargs['nick'] = 'nck' assert set(expected_kwargs) == set(parameters(command))
def test_channelmode_plus(): """ MODE command """ command = "CHANNELMODE" expected_kwargs = {"channel": "+ch", "host": "", "modes": "+o", "params": ['trget', 'trget2']} message = "MODE +ch +o trget trget2" assert (command, expected_kwargs) == unpack_command(message) expected_kwargs['user'] = '******' expected_kwargs['nick'] = 'nck' assert set(expected_kwargs) == set(parameters(command))
def test_notice(): """ NOTICE command """ command = "NOTICE" message = ":n!u@h NOTICE #t :m" expected_kwargs = {"nick": "n", "user": "******", "host": "h", "message": "m", "target": "#t"} validate(command, message, expected_kwargs) # server notice - can't use validate since not all params are defined message = ":some.host.edu NOTICE #t :m" expected_kwargs = {"host": "some.host.edu", "message": "m", "target": "#t"} assert (command, expected_kwargs) == unpack_command(message)
def data_received(self, data): self.buffer += data # All but the last result of split should be pushed into the # client. The last will be b"" if the buffer ends on b"\n" *lines, self.buffer = self.buffer.split(DELIM_COMPAT) for line in lines: message = line.decode(self.client.encoding, "ignore").strip() try: event, kwargs = unpack_command(message) self.client.trigger(event, **kwargs) except ValueError: log.debug("Failed to parse line >>> {}".format(message))
def test_channelmode_no_params(): """ MODE command """ command = "CHANNELMODE" expected_kwargs = { "channel": "#ch", "host": "", "modes": "+m", "params": [] } message = "MODE #ch +m" assert (command, expected_kwargs) == unpack_command(message) expected_kwargs['user'] = '******' expected_kwargs['nick'] = 'nck' assert set(expected_kwargs) == set(parameters(command))
def test_channelmode_plus(): """ MODE command """ command = "CHANNELMODE" expected_kwargs = { "channel": "+ch", "host": "", "modes": "+o", "params": ['trget', 'trget2'] } message = "MODE +ch +o trget trget2" assert (command, expected_kwargs) == unpack_command(message) expected_kwargs['user'] = '******' expected_kwargs['nick'] = 'nck' assert set(expected_kwargs) == set(parameters(command))
def test_unknown_command(): """ raise when command isn't known """ with pytest.raises(ValueError): unpack_command("unknown_command") with pytest.raises(ValueError): parameters("unknown_command")
def test_no_command(): ''' raise when command is None or empty ''' with pytest.raises(AttributeError): unpack_command(None) with pytest.raises(ValueError): unpack_command("")
def validate(command, message, expected_kwargs): ''' Basic case - expected_kwargs expects all parameters of the command ''' assert (command, expected_kwargs) == unpack_command(message) assert set(expected_kwargs) == set(parameters(command))
def test_ignore_case(): ''' input case doesn't matter ''' assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
def test_unknown_command(): ''' raise when command isn't known ''' with pytest.raises(ValueError): unpack_command("unknown_command") with pytest.raises(ValueError): parameters("unknown_command")
def test_bad_command(): ''' raise when command is incorrectly formatted ''' with pytest.raises(ValueError): unpack_command(":prefix_only")
def test_no_command(): """ raise when command is None or empty """ with pytest.raises(AttributeError): unpack_command(None) with pytest.raises(ValueError): unpack_command("")
def validate(command, message, expected_kwargs): """ Basic case - expected_kwargs expects all parameters of the command """ assert (command, expected_kwargs) == unpack_command(message) assert set(expected_kwargs) == set(parameters(command))
def test_ignore_case(): """ input case doesn't matter """ assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
def test_bad_command(): """ raise when command is incorrectly formatted """ with pytest.raises(ValueError): unpack_command(":prefix_only")