<s> import msgtypes <EOL> class MessageTranslator ( object ) : <EOL> """<STR_LIT>""" <EOL> def fromPrimitive ( self , msgPrimitive ) : <EOL> """<STR_LIT>""" <EOL> def toPrimitive ( self , message ) : <EOL> """<STR_LIT>""" <EOL> class DefaultFormat ( MessageTranslator ) : <EOL> """<STR_LIT>""" <EOL> typeRequest , typeResponse , typeError = range ( <NUM_LIT:3> ) <EOL> headerType , headerMsgID , headerNodeID , headerPayload , headerArgs = range ( <NUM_LIT:5> ) <EOL> def fromPrimitive ( self , msgPrimitive ) : <EOL> msgType = msgPrimitive [ self . headerType ] <EOL> if msgType == self . typeRequest : <EOL> msg = msgtypes . RequestMessage ( msgPrimitive [ self . headerNodeID ] , msgPrimitive [ self . headerPayload ] , msgPrimitive [ self . headerArgs ] , msgPrimitive [ self . headerMsgID ] ) <EOL> elif msgType == self . typeResponse : <EOL> msg = msgtypes . ResponseMessage ( msgPrimitive [ self . headerMsgID ] , msgPrimitive [ self . headerNodeID ] , msgPrimitive [ self . headerPayload ] ) <EOL> elif msgType == self . typeError : <EOL> msg = msgtypes . ErrorMessage ( msgPrimitive [ self . headerMsgID ] , msgPrimitive [ self . headerNodeID ] , msgPrimitive [ self . headerPayload ] , msgPrimitive [ self . headerArgs ] ) <EOL> else : <EOL> msg = msgtypes . Message ( msgPrimitive [ self . headerMsgID ] , msgPrimitive [ self . headerNodeID ] ) <EOL> return msg <EOL> def toPrimitive ( self , message ) : <EOL> msg = { self . headerMsgID : message . id , <EOL> self . headerNodeID : message . nodeID } <EOL> if isinstance ( message , msgtypes . RequestMessage ) : <EOL> msg [ self . headerType ] = self . typeRequest <EOL> msg [ self . headerPayload ] = message . request <EOL> msg [ self . headerArgs ] = message . args <EOL> elif isinstance ( message , msgtypes . ErrorMessage ) : <EOL> msg [ self . headerType ] = self . typeError <EOL> msg [ self . headerPayload ] = message . exceptionType <EOL> msg [ self . headerArgs ] = message . response <EOL> elif isinstance ( message , msgtypes . ResponseMessage ) : <EOL> msg [ self . headerType ] = self . typeResponse <EOL> msg [ self . headerPayload ] = message . response <EOL> return msg </s>
