<s> import logging <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . setLevel ( logging . ERROR ) <EOL> log . addHandler ( NullHandler ( ) ) <EOL> import coapUtils as u <EOL> import coapException as e <EOL> import coapDefines as d <EOL> class coapOption ( object ) : <EOL> def __init__ ( self , optionNumber ) : <EOL> self . optionNumber = optionNumber <EOL> self . length = <NUM_LIT:0> <EOL> def getPayloadBytes ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def toBytes ( self , lastOptionNum ) : <EOL> payload = self . getPayloadBytes ( ) <EOL> delta = self . optionNumber - lastOptionNum <EOL> if delta <= <NUM_LIT:12> : <EOL> optionDelta = delta <EOL> optionDeltaExt = u . int2buf ( delta , <NUM_LIT:0> ) <EOL> elif delta <= ( <NUM_LIT> + <NUM_LIT> ) : <EOL> optionDelta = <NUM_LIT> <EOL> optionDeltaExt = u . int2buf ( delta - <NUM_LIT> , <NUM_LIT:1> ) <EOL> elif delta <= ( <NUM_LIT> + <NUM_LIT> ) : <EOL> optionDelta = <NUM_LIT> <EOL> optionDeltaExt = u . int2buf ( delta - <NUM_LIT> , <NUM_LIT:2> ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( delta ) ) <EOL> if len ( payload ) <= <NUM_LIT:12> : <EOL> optionLength = len ( payload ) <EOL> optionLengthExt = u . int2buf ( len ( payload ) , <NUM_LIT:0> ) <EOL> elif len ( payload ) <= ( <NUM_LIT> + <NUM_LIT> ) : <EOL> optionLength = <NUM_LIT> <EOL> optionLengthExt = u . int2buf ( len ( payload ) - <NUM_LIT> , <NUM_LIT:1> ) <EOL> elif len ( payload ) <= ( <NUM_LIT> + <NUM_LIT> ) : <EOL> optionLength = <NUM_LIT> <EOL> optionLengthExt = u . int2buf ( len ( payload ) - <NUM_LIT> , <NUM_LIT:2> ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( len ( payload ) ) ) <EOL> returnVal = [ ] <EOL> returnVal += [ optionDelta << <NUM_LIT:4> | optionLength ] <EOL> returnVal += optionDeltaExt <EOL> returnVal += optionLengthExt <EOL> returnVal += payload <EOL> return returnVal <EOL> class UriPath ( coapOption ) : <EOL> def __init__ ( self , path ) : <EOL> coapOption . __init__ ( self , d . OPTION_NUM_URIPATH ) <EOL> self . path = path <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . path ) <EOL> def getPayloadBytes ( self ) : <EOL> return [ ord ( b ) for b in self . path ] <EOL> class ContentFormat ( coapOption ) : <EOL> def __init__ ( self , cformat ) : <EOL> assert len ( cformat ) == <NUM_LIT:1> <EOL> assert cformat [ <NUM_LIT:0> ] in d . FORMAT_ALL <EOL> coapOption . __init__ ( self , d . OPTION_NUM_CONTENTFORMAT ) <EOL> self . format = cformat [ <NUM_LIT:0> ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . format ) <EOL> def getPayloadBytes ( self ) : <EOL> return [ self . format ] <EOL> class Block2 ( coapOption ) : <EOL> def __init__ ( self , num = None , m = None , szx = None , rawbytes = [ ] ) : <EOL> if rawbytes : <EOL> assert num == None <EOL> assert m == None <EOL> assert szx == None <EOL> else : <EOL> assert num != None <EOL> assert m != None <EOL> assert szx != None <EOL> coapOption . __init__ ( self , d . OPTION_NUM_BLOCK2 ) <EOL> if num : <EOL> self . num = num <EOL> self . m = m <EOL> self . szx = szx <EOL> else : <EOL> if len ( rawbytes ) == <NUM_LIT:1> : <EOL> self . num = ( rawbytes [ <NUM_LIT:0> ] >> <NUM_LIT:4> ) & <NUM_LIT> <EOL> self . m = ( rawbytes [ <NUM_LIT:0> ] >> <NUM_LIT:3> ) & <NUM_LIT> <EOL> self . szx = ( rawbytes [ <NUM_LIT:0> ] >> <NUM_LIT:0> ) & <NUM_LIT> <EOL> elif len ( rawbytes ) == <NUM_LIT:2> : <EOL> self . num = rawbytes [ <NUM_LIT:0> ] << <NUM_LIT:8> | ( rawbytes [ <NUM_LIT:1> ] >> <NUM_LIT:4> ) & <NUM_LIT> <EOL> self . m = ( rawbytes [ <NUM_LIT:1> ] >> <NUM_LIT:3> ) & <NUM_LIT> <EOL> self . szx = ( rawbytes [ <NUM_LIT:1> ] >> <NUM_LIT:0> ) & <NUM_LIT> <EOL> elif len ( rawbytes ) == <NUM_LIT:3> : <EOL> self . num = rawbytes [ <NUM_LIT:0> ] << <NUM_LIT:16> | rawbytes [ <NUM_LIT:1> ] << <NUM_LIT:8> | ( rawbytes [ <NUM_LIT:2> ] >> <NUM_LIT:4> ) & <NUM_LIT> <EOL> self . m = ( rawbytes [ <NUM_LIT:2> ] >> <NUM_LIT:3> ) & <NUM_LIT> <EOL> self . szx = ( rawbytes [ <NUM_LIT:2> ] >> <NUM_LIT:0> ) & <NUM_LIT> <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( len ( rawbytes ) ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . num , self . m , self . szx ) <EOL> def getPayloadBytes ( self ) : <EOL> return NotImplementedError ( ) <EOL> def parseOption ( message , previousOptionNumber ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> u . formatBuf ( message ) , <EOL> previousOptionNumber , <EOL> ) <EOL> ) <EOL> if len ( message ) == <NUM_LIT:0> : <EOL> message = message [ <NUM_LIT:1> : ] <EOL> return ( None , message ) <EOL> if message [ <NUM_LIT:0> ] == d . COAP_PAYLOAD_MARKER : <EOL> message = message [ <NUM_LIT:1> : ] <EOL> return ( None , message ) <EOL> optionDelta = ( message [ <NUM_LIT:0> ] >> <NUM_LIT:4> ) & <NUM_LIT> <EOL> optionLength = ( message [ <NUM_LIT:0> ] >> <NUM_LIT:0> ) & <NUM_LIT> <EOL> message = message [ <NUM_LIT:1> : ] <EOL> if optionDelta <= <NUM_LIT:12> : <EOL> pass <EOL> elif optionDelta == <NUM_LIT> : <EOL> if len ( message ) < <NUM_LIT:1> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> optionDelta = u . buf2int ( message [ <NUM_LIT:0> ] ) + <NUM_LIT> <EOL> message = message [ <NUM_LIT:1> : ] <EOL> elif optionDelta == <NUM_LIT> : <EOL> if len ( message ) < <NUM_LIT:2> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> optionDelta = u . buf2int ( message [ <NUM_LIT:0> : <NUM_LIT:1> ] ) + <NUM_LIT> <EOL> message = message [ <NUM_LIT:2> : ] <EOL> else : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( optionDelta ) ) <EOL> log . debug ( '<STR_LIT>' . format ( optionDelta ) ) <EOL> if optionLength <= <NUM_LIT:12> : <EOL> pass <EOL> elif optionLength == <NUM_LIT> : <EOL> if len ( message ) < <NUM_LIT:1> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> optionLength = u . buf2int ( message [ <NUM_LIT:0> ] ) + <NUM_LIT> <EOL> message = message [ <NUM_LIT:1> : ] <EOL> elif optionLength == <NUM_LIT> : <EOL> if len ( message ) < <NUM_LIT:2> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> optionLength = u . buf2int ( message [ <NUM_LIT:0> : <NUM_LIT:1> ] ) + <NUM_LIT> <EOL> message = message [ <NUM_LIT:2> : ] <EOL> else : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( optionLength ) ) <EOL> log . debug ( '<STR_LIT>' . format ( optionLength ) ) <EOL> if len ( message ) < optionLength : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> optionValue = message [ : optionLength ] <EOL> message = message [ optionLength : ] <EOL> log . debug ( '<STR_LIT>' . format ( u . formatBuf ( optionValue ) ) ) <EOL> optionNumber = previousOptionNumber + optionDelta <EOL> log . debug ( '<STR_LIT>' . format ( optionNumber ) ) <EOL> if optionNumber not in d . OPTION_NUM_ALL : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( optionNumber ) ) <EOL> if optionNumber == d . OPTION_NUM_URIPATH : <EOL> option = UriPath ( path = '<STR_LIT>' . join ( [ chr ( b ) for b in optionValue ] ) ) <EOL> elif optionNumber == d . OPTION_NUM_CONTENTFORMAT : <EOL> option = ContentFormat ( cformat = optionValue ) <EOL> elif optionNumber == d . OPTION_NUM_BLOCK2 : <EOL> option = Block2 ( rawbytes = optionValue ) <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' . format ( optionNumber ) ) <EOL> return ( option , message ) </s>
