parameters: (input_file)
return_statement: return xpt
docstring: Read a typelib from |input_file| and return
          the constructed Typelib object. |input_file| can be a filename
          or a file-like object.
docstring_summary: Read a typelib from |input_file| and return
          the constructed Typelib object. |input_file| can be a filename
          or a file-like object.
docstring_tokens: Read a typelib from |input_file| and return the constructed Typelib object . |input_file| can be a filename or a file - like object .
function: def read(input_file):
          """
          Read a typelib from |input_file| and return
          the constructed Typelib object. |input_file| can be a filename
          or a file-like object.
  
          """
          filename = ""
          data = None
          expected_size = None
          if isinstance(input_file, basestring):
              filename = input_file
              with open(input_file, "rb") as f:
                  st = os.fstat(f.fileno())
                  data = f.read(st.st_size)
                  expected_size = st.st_size
          else:
              data = input_file.read()
  
          (magic,
           major_ver,
           minor_ver,
           num_interfaces,
           file_length,
           interface_directory_offset,
           data_pool_offset) = Typelib._header.unpack_from(data)
          if magic != XPT_MAGIC:
              raise FileFormatError, "Bad magic: %s" % magic
          xpt = Typelib((major_ver, minor_ver))
          xpt.filename = filename
          if expected_size and file_length != expected_size:
              raise FileFormatError, "File is of wrong length, got %d bytes, expected %d" % (expected_size, file_length)
          #XXX: by spec this is a zero-based file offset. however,
          # the xpt_xdr code always subtracts 1 from data offsets
          # (because that's what you do in the data pool) so it
          # winds up accidentally treating this as 1-based.
          # Filed as: https://bugzilla.mozilla.org/show_bug.cgi?id=575343
          interface_directory_offset -= 1
          # make a half-hearted attempt to read Annotations,
          # since XPIDL doesn't produce any anyway.
          start = Typelib._header.size
          (anno, ) = struct.unpack_from(">B", data, start)
          islast = anno & 0x80
          tag = anno & 0x7F
          if tag == 0: # EmptyAnnotation
              xpt.annotations.append(None)
          # We don't bother handling PrivateAnnotations or anything
  
          for i in range(num_interfaces):
              # iid, name, namespace, interface_descriptor
              start = interface_directory_offset + i * Interface._direntry.size
              ide = Interface._direntry.unpack_from(data, start)
              iid = Typelib.iid_to_string(ide[0])
              name = Typelib.read_string(data, data_pool_offset, ide[1])
              namespace = Typelib.read_string(data, data_pool_offset, ide[2])
              iface = Interface(name, iid, namespace)
              iface._descriptor_offset = ide[3]
              iface.xpt_filename = xpt.filename
              xpt.interfaces.append(iface)
          for iface in xpt.interfaces:
              iface.read_descriptor(xpt, data, data_pool_offset)
          return xpt
function_tokens: def read ( input_file ) : filename = "" data = None expected_size = None if isinstance ( input_file , basestring ) : filename = input_file with open ( input_file , "rb" ) as f : st = os . fstat ( f . fileno ( ) ) data = f . read ( st . st_size ) expected_size = st . st_size else : data = input_file . read ( ) ( magic , major_ver , minor_ver , num_interfaces , file_length , interface_directory_offset , data_pool_offset ) = Typelib . _header . unpack_from ( data ) if magic != XPT_MAGIC : raise FileFormatError , "Bad magic: %s" % magic xpt = Typelib ( ( major_ver , minor_ver ) ) xpt . filename = filename if expected_size and file_length != expected_size : raise FileFormatError , "File is of wrong length, got %d bytes, expected %d" % ( expected_size , file_length ) #XXX: by spec this is a zero-based file offset. however, # the xpt_xdr code always subtracts 1 from data offsets # (because that's what you do in the data pool) so it # winds up accidentally treating this as 1-based. # Filed as: https://bugzilla.mozilla.org/show_bug.cgi?id=575343 interface_directory_offset -= 1 # make a half-hearted attempt to read Annotations, # since XPIDL doesn't produce any anyway. start = Typelib . _header . size ( anno , ) = struct . unpack_from ( ">B" , data , start ) islast = anno & 0x80 tag = anno & 0x7F if tag == 0 : # EmptyAnnotation xpt . annotations . append ( None ) # We don't bother handling PrivateAnnotations or anything for i in range ( num_interfaces ) : # iid, name, namespace, interface_descriptor start = interface_directory_offset + i * Interface . _direntry . size ide = Interface . _direntry . unpack_from ( data , start ) iid = Typelib . iid_to_string ( ide [ 0 ] ) name = Typelib . read_string ( data , data_pool_offset , ide [ 1 ] ) namespace = Typelib . read_string ( data , data_pool_offset , ide [ 2 ] ) iface = Interface ( name , iid , namespace ) iface . _descriptor_offset = ide [ 3 ] iface . xpt_filename = xpt . filename xpt . interfaces . append ( iface ) for iface in xpt . interfaces : iface . read_descriptor ( xpt , data , data_pool_offset ) return xpt