# Path: python/tHome/util/Data.py
# class Data:
#    def __init__( self, dict=None, **kwargs ):
#       if dict:
#          self.__dict__.update( dict )
#       if kwargs:
#          self.__dict__.update( kwargs )
#       
#    #--------------------------------------------------------------------------
#    def keys( self ):
#       return self.__dict__.keys()
#    
#    #--------------------------------------------------------------------------
#    def update( self, rhs ):
#       return self.__dict__.update( rhs.__dict__ )
#    
#    #--------------------------------------------------------------------------
#    def __setitem__( self, key, value ):
#       self.__dict__[key] = value
#       
#    #--------------------------------------------------------------------------
#    def __getitem__( self, key ):
#       return self.__dict__[key]
#    
#    #--------------------------------------------------------------------------
#    def __contains__( self, key ):
#       return key in self.__dict__
#    
#    #--------------------------------------------------------------------------
#    def __str__( self ):
#       out = StringIO.StringIO()
#       self._formatValue( self, out, 3 )
#       return out.getvalue()
# 
#    #--------------------------------------------------------------------------
#    def __repr__( self ):
#       return self.__str__()
#       
#    #--------------------------------------------------------------------------
#    def _formatValue( self, value, out, indent ):
#       if isinstance( value, Data ):
#          out.write( "%s(\n" % self.__class__.__name__ )
#          for k, v in sorted( value.__dict__.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s%s" % ( indent, '', k ) )
#             out.write( " = " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s)" % ( indent, '' ) )
#          
#       elif isinstance( value, dict ):
#          out.write( "{\n" )
#          for k, v in sorted( value.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( k, out, 0 )
#             out.write( " : " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s}" % ( indent, '' ) )
# 
#       elif isinstance( value, list ):
#          out.write( "[\n" )
#          for i in value:
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( i, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s]" % ( indent, '' ) )
#             
#       elif isinstance( value, str ):
#          out.write( "'%s'" % ( value ) )
#          
#       else:
#          out.write( "%s" % ( value ) )


import struct
from .Data import Data
   def __init__( self, endian, elems ):
      """Constructr
      
      endian == BIG_ENDIAN or LITTLE_ENDIAN
      elems = [ ( struct_format_code, name ), ... ]
      """
      assert( endian == "BIG_ENDIAN" or endian == "LITTLE_ENDIAN" )
      
      if endian == "BIG_ENDIAN":
         self.format = ">"
      elif endian == "LITTLE_ENDIAN":
         self.format = "<"

      self.format += "".join( [ i[0] for i in elems ] )
      self.names = [ i[1] for i in elems ]

      self.struct = struct.Struct( self.format )

   #---------------------------------------------------------------------------
   def __len__( self ):
      return self.struct.size

   #---------------------------------------------------------------------------
   def pack( self, obj ):
      data = [ getattr( obj, i ) for i in self.names ]
      return self.struct.pack( *data )

   #---------------------------------------------------------------------------
   def unpack( self, obj, bytes, offset=0 ):
      if obj is None:

# Path: python/tHome/util/Data.py
# class Data:
#    def __init__( self, dict=None, **kwargs ):
#       if dict:
#          self.__dict__.update( dict )
#       if kwargs:
#          self.__dict__.update( kwargs )
#       
#    #--------------------------------------------------------------------------
#    def keys( self ):
#       return self.__dict__.keys()
#    
#    #--------------------------------------------------------------------------
#    def update( self, rhs ):
#       return self.__dict__.update( rhs.__dict__ )
#    
#    #--------------------------------------------------------------------------
#    def __setitem__( self, key, value ):
#       self.__dict__[key] = value
#       
#    #--------------------------------------------------------------------------
#    def __getitem__( self, key ):
#       return self.__dict__[key]
#    
#    #--------------------------------------------------------------------------
#    def __contains__( self, key ):
#       return key in self.__dict__
#    
#    #--------------------------------------------------------------------------
#    def __str__( self ):
#       out = StringIO.StringIO()
#       self._formatValue( self, out, 3 )
#       return out.getvalue()
# 
#    #--------------------------------------------------------------------------
#    def __repr__( self ):
#       return self.__str__()
#       
#    #--------------------------------------------------------------------------
#    def _formatValue( self, value, out, indent ):
#       if isinstance( value, Data ):
#          out.write( "%s(\n" % self.__class__.__name__ )
#          for k, v in sorted( value.__dict__.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s%s" % ( indent, '', k ) )
#             out.write( " = " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s)" % ( indent, '' ) )
#          
#       elif isinstance( value, dict ):
#          out.write( "{\n" )
#          for k, v in sorted( value.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( k, out, 0 )
#             out.write( " : " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s}" % ( indent, '' ) )
# 
#       elif isinstance( value, list ):
#          out.write( "[\n" )
#          for i in value:
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( i, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s]" % ( indent, '' ) )
#             
#       elif isinstance( value, str ):
#          out.write( "'%s'" % ( value ) )
#          
#       else:
#          out.write( "%s" % ( value ) )


# Path: python/tHome/util/NamedStruct.py
import struct
from .Data import Data

   def __init__( self, endian, elems ):
      """Constructr
      
      endian == BIG_ENDIAN or LITTLE_ENDIAN
      elems = [ ( struct_format_code, name ), ... ]
      """
      assert( endian == "BIG_ENDIAN" or endian == "LITTLE_ENDIAN" )
      
      if endian == "BIG_ENDIAN":
         self.format = ">"
      elif endian == "LITTLE_ENDIAN":
         self.format = "<"

      self.format += "".join( [ i[0] for i in elems ] )
      self.names = [ i[1] for i in elems ]

      self.struct = struct.Struct( self.format )

   #---------------------------------------------------------------------------
   def __len__( self ):
      return self.struct.size

   #---------------------------------------------------------------------------
   def pack( self, obj ):
      data = [ getattr( obj, i ) for i in self.names ]
      return self.struct.pack( *data )

   #---------------------------------------------------------------------------
   def unpack( self, obj, bytes, offset=0 ):
      if obj is None:

         obj = Data()