Files
Hippolyzer/pyogp/lib/base/message/llsd_builder.py
tao.takashi 6a6dd5fdb1 The big message system renaming:
- renamed all files starting with message_ to a name without message_
- exception: msgdict as dict is a reserved word
- moved all tests into the message/ directory
- changed all tests to use the new names
- changed all modules to use the new names
- shortened imports to not use the full path (pyogp.lib.base. but only the short path)
- removed makepacketdict.py as it's not needed anymore
- moved the data/ directory into message/ as it's local to the msg system

and some small cleanups on the way.

please run the tests!

I haven't adjusted pyogp.interop yet, Enus wanted to look after it.
2008-09-03 22:55:07 +00:00

66 lines
2.2 KiB
Python

#third party
from zope.interface import implements
#local
from interfaces import IMessageBuilder
from template import MsgData, MsgBlockData, MsgVariableData
class LLSDMessageBuilder(object):
implements(IMessageBuilder)
def __init__(self):
self.current_template = None
self.current_msg = None
self.current_block = None
self.cur_msg_name = ''
self.cur_block_name = ''
self.has_been_built = False
def is_built(self):
return self.has_been_built
def build_message(self):
""" this does not serialize it for this type of builder. The message
will be put in standard Python form and will need to be formatted
based on who the target is (xml? something else?) """
msg = {}
for block in self.current_msg.blocks:
#message can have multiple of the same block names, so
#message actually holds a block list
block_list = self.current_msg.blocks[block]
for block_data in block_list:
#set up the block list
if block_data.name not in msg:
msg[block_data.name] = []
#each block in the block list is a map
block = {}
msg[block_data.name].append(block)
#go through the variables for the data
for variable in block_data.vars.values():
#the variable holds the key-value pairs of data
#for the block
block[variable.name] = variable.data
self.has_been_built = True
return msg, len(msg)
def new_message(self, message_name):
self.has_been_built = False
self.current_msg = MsgData(message_name)
self.cur_msg_name = message_name
def next_block(self, block_name):
self.has_been_built = False
block = MsgBlockData(block_name)
self.current_msg.add_block(block)
self.current_block = block
self.cur_block_name = block_name
def add_data(self, var_name, data, data_type):
self.has_been_built = False
var = MsgVariableData(var_name, data)
self.current_block.add_variable(var)