<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import struct <EOL> from binascii import hexlify <EOL> from collections import deque , namedtuple <EOL> import enum <EOL> from ... aid . sixing import * <EOL> from ... aid . odicting import odict <EOL> from ... aid . byting import bytify , unbytify , packify , packifyInto , unpackify <EOL> from ... aid import getConsole <EOL> from . protoing import MixIn <EOL> console = getConsole ( ) <EOL> class Part ( MixIn ) : <EOL> """<STR_LIT>""" <EOL> Size = <NUM_LIT:0> <EOL> def __init__ ( self , size = None , packed = None , ** kwa ) : <EOL> """<STR_LIT>""" <EOL> super ( Part , self ) . __init__ ( ** kwa ) <EOL> if packed is None : <EOL> size = size if size is not None else self . Size <EOL> self . packed = bytearray ( [ <NUM_LIT:0> for i in range ( <NUM_LIT:0> , size ) ] ) <EOL> else : <EOL> self . packed = bytearray ( packed ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . packed ) <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __len__ ( ) <EOL> def show ( self ) : <EOL> """<STR_LIT>""" <EOL> name = self . __class__ . __name__ <EOL> result = ( "<STR_LIT>" . format ( name , <EOL> hexlify ( self . packed ) . decode ( '<STR_LIT:ascii>' ) ) ) <EOL> return result <EOL> class PackerPart ( Part ) : <EOL> """<STR_LIT>""" <EOL> Format = '<STR_LIT:!>' <EOL> def __init__ ( self , <EOL> fmt = None , <EOL> raw = None , <EOL> ** kwa ) : <EOL> """<STR_LIT>""" <EOL> self . fmt = fmt if fmt is not None else self . Format <EOL> self . packer = struct . Struct ( self . fmt ) <EOL> kwa [ '<STR_LIT:size>' ] = self . packer . size <EOL> super ( PackerPart , self ) . __init__ ( ** kwa ) <EOL> if raw is not None : <EOL> self . parse ( raw = raw ) <EOL> def verifySize ( self , raw = bytearray ( b'<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( raw ) >= ( self . size ) ) <EOL> def parse ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> if ( raw is None ) or ( not self . verifySize ( raw ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . size , <EOL> len ( raw ) ) ) <EOL> result = self . packer . unpack_from ( raw ) <EOL> self . packed [ : ] = raw [ <NUM_LIT:0> : self . packer . size ] <EOL> return self . size <EOL> def pack ( self , ** kwa ) : <EOL> """<STR_LIT>""" <EOL> self . packer . pack_into ( self . packed , <NUM_LIT:0> ) <EOL> if self . size != self . packer . size : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . size , self . packer . size ) ) <EOL> return self . packed <EOL> class PackifierPart ( Part ) : <EOL> """<STR_LIT>""" <EOL> Format = '<STR_LIT>' <EOL> def __init__ ( self , <EOL> fmt = None , <EOL> raw = None , <EOL> ** kwa ) : <EOL> """<STR_LIT>""" <EOL> self . fmt = fmt if fmt is not None else self . Format <EOL> tbfl = sum ( ( int ( x ) for x in self . fmt . split ( ) ) ) <EOL> size = ( tbfl // <NUM_LIT:8> ) + <NUM_LIT:1> if tbfl % <NUM_LIT:8> else tbfl // <NUM_LIT:8> <EOL> kwa [ '<STR_LIT:size>' ] = size <EOL> super ( PackifierPart , self ) . __init__ ( ** kwa ) <EOL> if raw is not None : <EOL> self . parse ( raw = raw ) <EOL> def verifySize ( self , raw = bytearray ( b'<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> return ( len ( raw ) >= ( self . size ) ) <EOL> def parse ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> if ( not raw ) or ( not self . verifySize ( raw ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . size , <EOL> len ( raw ) ) ) <EOL> result = unpackify ( self . fmt , raw , boolean = True , size = self . size ) <EOL> self . packed [ : ] = raw [ <NUM_LIT:0> : self . size ] <EOL> return self . size <EOL> def pack ( self , ** kwa ) : <EOL> """<STR_LIT>""" <EOL> size = packifyInto ( self . packed , fmt = self . fmt , fields = ( ) ) <EOL> if self . size != size : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . size , size ) ) <EOL> return self . packed <EOL> def show ( self ) : <EOL> """<STR_LIT>""" <EOL> name = self . __class__ . __name__ <EOL> result = ( "<STR_LIT>" . format ( name , <EOL> hexlify ( self . packed ) . decode ( '<STR_LIT:ascii>' ) ) ) <EOL> return result <EOL> class PacketPart ( Part ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> packet = None , <EOL> ** kwa <EOL> ) : <EOL> """<STR_LIT>""" <EOL> super ( PacketPart , self ) . __init__ ( ** kwa ) <EOL> self . packet = packet <EOL> def show ( self ) : <EOL> """<STR_LIT>""" <EOL> name = self . __class__ . __name__ <EOL> result = ( "<STR_LIT>" . format ( name , <EOL> hexlify ( self . packed ) . decode ( '<STR_LIT:ascii>' ) ) ) <EOL> return result <EOL> class Packet ( Part ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> stack = None , <EOL> ** kwa <EOL> ) : <EOL> """<STR_LIT>""" <EOL> super ( Packet , self ) . __init__ ( ** kwa ) <EOL> self . stack = stack <EOL> def parse ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> return self . size <EOL> def pack ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . packed </s>
