Files
Hippolyzer/pyogp/lib/base/message/template_dict.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

92 lines
3.6 KiB
Python

from types import MsgFrequency
from interfaces import ITemplateDictionary
import grokcore.component as grok
from grokcore.component import GlobalUtility
from data import msg_tmpl, msg_details
from template_parser import MessageTemplateParser
from data_packer import DataPacker
from types import MsgType, EndianType
class TemplateDictionary(GlobalUtility):
"""the dictionary with all known templates"""
grok.implements(ITemplateDictionary)
def __init__(self, template_list=None):
if template_list == None:
parser = MessageTemplateParser(msg_tmpl)
template_list = parser.message_templates
template_dict = TemplateDictionary(template_list)
# maps name to template
self.message_templates = {}
# maps (freq,num) to template
self.message_dict = {}
self.build_dictionaries(template_list)
self.build_message_ids()
def build_dictionaries(self, template_list):
for template in template_list:
self.message_templates[template.name] = template
#do a mapping of type to a string for easier reference
frequency_str = ''
if template.frequency == MsgFrequency.FIXED_FREQUENCY_MESSAGE:
frequency_str = "Fixed"
elif template.frequency == MsgFrequency.LOW_FREQUENCY_MESSAGE:
frequency_str = "Low"
elif template.frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE:
frequency_str = "Medium"
elif template.frequency == MsgFrequency.HIGH_FREQUENCY_MESSAGE:
frequency_str = "High"
self.message_dict[(frequency_str, \
template.msg_num)] = template
def build_message_ids(self):
packer = DataPacker()
for template in self.message_templates.values():
frequency = template.frequency
if frequency == MsgFrequency.FIXED_FREQUENCY_MESSAGE:
#have to do this because Fixed messages are stored as a long in the template
template.msg_num_hex = '\xff\xff\xff' + \
packer.pack_data(template.msg_num, \
MsgType.MVT_U8)
elif frequency == MsgFrequency.LOW_FREQUENCY_MESSAGE:
template.msg_num_hex = '\xff\xff' + \
packer.pack_data(template.msg_num, \
MsgType.MVT_U16, \
EndianType.BIG)
elif frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE:
template.msg_num_hex = '\xff' + \
packer.pack_data(template.msg_num, \
MsgType.MVT_U8, \
EndianType.BIG)
elif frequency == MsgFrequency.HIGH_FREQUENCY_MESSAGE:
template.msg_num_hex = packer.pack_data(template.msg_num, \
MsgType.MVT_U8, \
EndianType.BIG)
def get_template(self, template_name):
if template_name in self.message_templates:
return self.message_templates[template_name]
return None
def get_template_by_pair(self, frequency, num):
if (frequency, num) in self.message_dict:
return self.message_dict[(frequency, num)]
return None
def __getitem__(self, i):
return self.get_template(i)