<s> from __future__ import absolute_import <EOL> import msgpack <EOL> import logging <EOL> import lz4 <EOL> from . constants import FAILURE_STATUS <EOL> activity_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> errors_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class MessageFormatError ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return repr ( self . value ) <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , raw_message ) : <EOL> self . message = msgpack . unpackb ( raw_message ) <EOL> self . meta = self . message . get ( '<STR_LIT>' , { } ) <EOL> activity_logger . debug ( '<STR_LIT>' + str ( self . message ) + '<STR_LIT:>>' ) <EOL> try : <EOL> self . db_uid = self . message . get ( '<STR_LIT>' ) <EOL> self . command = self . message [ '<STR_LIT>' ] <EOL> self . data = self . message [ '<STR_LIT:args>' ] <EOL> except KeyError : <EOL> errors_logger . exception ( "<STR_LIT>" % self . message ) <EOL> raise MessageFormatError ( "<STR_LIT>" % self . message ) <EOL> class ResponseContent ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> response = { <EOL> '<STR_LIT>' : cls . _format_datas ( kwargs [ '<STR_LIT>' ] ) , <EOL> } <EOL> activity_logger . debug ( '<STR_LIT>' + str ( response [ '<STR_LIT>' ] ) + '<STR_LIT:>>' ) <EOL> msg = msgpack . packb ( response ) <EOL> if kwargs . pop ( '<STR_LIT>' , False ) is True : <EOL> msg = lz4 . dumps ( msg ) <EOL> return msg <EOL> @ classmethod <EOL> def _format_datas ( cls , datas ) : <EOL> if datas and not isinstance ( datas , ( tuple , list ) ) : <EOL> datas = [ datas ] <EOL> return datas <EOL> class ResponseHeader ( dict ) : <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> header = { <EOL> '<STR_LIT:status>' : kwargs . pop ( '<STR_LIT:status>' ) , <EOL> '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : kwargs . pop ( '<STR_LIT>' , False ) <EOL> } <EOL> activity_logger . debug ( '<STR_LIT>' + str ( header ) + '<STR_LIT:>>' ) <EOL> for key , value in kwargs . iteritems ( ) : <EOL> header . update ( { key : value } ) <EOL> return msgpack . packb ( header ) </s>
