Improved the IMC2 packet lexxer to handle single quotes as regular tokenizable characters.

This commit is contained in:
Greg Taylor 2009-04-12 08:36:10 +00:00
parent 6f11a1a62c
commit dd8a05c40a

View file

@ -3,7 +3,23 @@ IMC2 packets. These are pretty well documented at:
http://www.mudbytes.net/index.php?a=articles&s=imc2_protocol http://www.mudbytes.net/index.php?a=articles&s=imc2_protocol
""" """
import shlex import shlex
from django.conf import settings if __name__ == "__main__":
class settings:
IMC2_MUDNAME = "BLAH"
else:
from django.conf import settings
class Lexxer(shlex.shlex):
"""
A lexical parser for interpreting IMC2 packets.
"""
def __init__(self, packet_str, posix=True):
shlex.shlex.__init__(self, packet_str, posix=True)
# Single-quotes are notably not present. This is important!
self.quotes = '"'
self.commenters = ''
# This helps denote what constitutes a continuous token.
self.wordchars += "=-@*!:'"
class IMC2Packet(object): class IMC2Packet(object):
""" """
@ -29,29 +45,42 @@ class IMC2Packet(object):
self.imc2_protocol = None self.imc2_protocol = None
if packet_str: if packet_str:
split_packet = shlex.split(packet_str) # The lexxer handles the double quotes correctly, unlike just
# splitting. Spaces throw things off, so shlex handles it
# gracefully, ala POSIX shell-style parsing.
lex = Lexxer(packet_str)
# Get values for the sender and origin attributes. # Token counter.
sender_origin = split_packet[0] counter = 0
split_sender_origin = sender_origin.split('@') for token in lex:
self.sender = split_sender_origin[0].strip() if counter == 0:
self.origin = split_sender_origin[1] # This is the sender@origin token.
sender_origin = token
self.sequence = split_packet[1] print token
self.route = split_packet[2] split_sender_origin = sender_origin.split('@')
self.packet_type = split_packet[3] self.sender = split_sender_origin[0].strip()
self.origin = split_sender_origin[1]
# Get values for the target and destination attributes. elif counter == 1:
target_destination = split_packet[4] # Numeric time-based sequence.
split_target_destination = target_destination.split('@') self.sequence = token
self.target = split_target_destination[0] elif counter == 2:
self.destination = split_sender_origin[1] # Packet routing info.
self.route = token
# Populate optional data. elif counter == 3:
data_list = split_packet[5:] # Packet type string.
for pair in data_list: self.packet_type = token
key, value = pair.split('=', 1) elif counter == 4:
self.optional_data.append((key, value)) # Get values for the target and destination attributes.
target_destination = token
split_target_destination = target_destination.split('@')
self.target = split_target_destination[0]
self.destination = split_target_destination[1]
elif counter > 4:
# Populate optional data.
key, value = token.split('=', 1)
self.optional_data.append((key, value))
# Increment and continue to the next token (if applicable)
counter += 1
def __str__(self): def __str__(self):
retval = """ retval = """
@ -674,3 +703,7 @@ class IMC2PacketCloseNotify(IMC2Packet):
*@Hub2 1234567890 Hub2!Hub1 close-notify *@* host=DisconnMUD *@Hub2 1234567890 Hub2!Hub1 close-notify *@* host=DisconnMUD
""" """
pass pass
if __name__ == "__main__":
packstr = "Kayle@MW 1234567 MW!Server02!Server01 ice-msg-b *@* channel=Server01:ichat text=\"*they're going woot\" emote=0 echo=1"
print IMC2Packet(packstr)