o
    2ha                     @   s
  d dl Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZm	Z	 dd	lm
Z
 dd
lmZmZmZ e	 ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdZG dd deZdd ZdS )    N   )compat)encode)number_types)packer)memoryview_type)NumpyRequiredForThisFeatureimport_numpy)
range_func)SOffsetTFlagsUOffsetTFlagsVOffsetTFlagsc                   @      e Zd ZdZdS )OffsetArithmeticErrorzError caused by an Offset arithmetic error.

  Probably caused by bad writing of fields. This is considered an unreachable
  situation in normal circumstances.
  N__name__
__module____qualname____doc__ r   r   N/var/www/html/chatgem/venv/lib/python3.10/site-packages/flatbuffers/builder.pyr   !   s    r   c                   @   r   )IsNotNestedErrorzUError caused by using a Builder to write Object data when not inside

  an Object.
  Nr   r   r   r   r   r   +       r   c                   @   r   )IsNestedErrorz_Error caused by using a Builder to begin an Object when an Object is

  already being built.
  Nr   r   r   r   r   r   4   r   r   c                   @   r   )StructIsNotInlineErrorzeError caused by using a Builder to write a Struct at a location that

  is not the current Offset.
  Nr   r   r   r   r   r   =   r   r   c                   @   r   )BuilderSizeErrorzUError caused by causing a Builder to exceed the hardcoded limit of 2

  gigabytes.
  Nr   r   r   r   r   r   F   r   r   c                   @   r   )BuilderNotFinishedErrorz=Error caused by not calling `Finish` before calling `Output`.Nr   r   r   r   r   r   O   s    r   c                   @   r   )EndVectorLengthMismatchedzdThe number of elements passed to EndVector does not match the number

  specified in StartVector.
  Nr   r   r   r   r   r   U   r   r      c                   @   s
  e Zd ZdZdZ	 dZdddZdd	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zdd#d$Zdd'd(Zdd)d*Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zdd7d8Zdd9d:Zdd;d<Zd=d> Z d?d@ Z!dAdB Z"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)dQdR Z*dSdT Z+dUdV Z,dWdX Z-dYdZ Z.d[d\ Z/d]d^ Z0d_d` Z1dadb Z2dcdd Z3dedf Z4dgdh Z5didj Z6dkdl Z7dmdn Z8dodp Z9dqdr Z:dsdt Z;dudv Z<dwdx Z=dydz Z>d{d| Z?d}d~ Z@dd ZAdS )Buildera|  A Builder is used to construct one or more FlatBuffers.

  Typically, Builder objects will be used from code generated by the `flatc`
  compiler.

  A Builder constructs byte buffers in a last-first manner for simplicity and
  performance during reading.

  Internally, a Builder is a state machine for creating FlatBuffer objects.

  It holds the following internal state:
      - Bytes: an array of bytes.
      - current_vtable: a list of integers.
      - vtables: a hash of vtable entries.

  Attributes:
    Bytes: The internal `bytearray` for the Builder.
    finished: A boolean determining if the Builder has been finalized.
  )Bytescurrent_vtableheadminalign	objectEndvtablesnestedforceDefaultsfinishedvectorNumElemssharedStringsl           c                 C   sn   d|  krt jksn d}t|t|| _d| _t|| _d| _	d| _
i | _d| _d| _i | _d| _dS )z_Initializes a Builder of size `initial_size`.

    The internal buffer is grown as needed.
    r   z;flatbuffers: Cannot create Builder larger than 2 gigabytes.Nr   F)r   MAX_BUFFER_SIZEr   	bytearrayr    r!   r   py_typer"   r#   r$   r%   r&   r'   r*   r(   )selfinitialSizemsgr   r   r   __init__   s   

zBuilder.__init__returnNc                 C   sL   d | _ tt| j| _d| _d | _i | _d| _	d| _
i | _d | _d| _d S )Nr   F)r!   r   r.   lenr    r"   r#   r$   r%   r&   r'   r*   r)   r(   r/   r   r   r   Clear   s   
zBuilder.Clearc                 C   s   | j st | j|  d S )a  Return the portion of the buffer that has been used for writing data.

    This is the typical way to access the FlatBuffer data inside the
    builder. If you try to access `Builder.Bytes` directly, you would need
    to manually index it with `Head()`, since the buffer is constructed
    backwards.

    It raises BuilderNotFinishedError if the buffer has not been finished
    with `Finish`.
    N)r(   r   r    Headr5   r   r   r   Output   s   zBuilder.Outputc                 C   s0   |    dd t|D | _|  | _d| _dS )z=StartObject initializes bookkeeping for writing a new object.c                 S   s   g | ]}d qS )r   r   ).0_r   r   r   
<listcomp>   s    z'Builder.StartObject.<locals>.<listcomp>TN)assertNotNestedr
   r!   Offsetr$   r&   )r/   	numfieldsr   r   r   StartObject   s   

zBuilder.StartObjectc              	   C   s  |  d |  }g }d}t| jD ]}|dkr|rqn|| }d}|| qt|}| j|}|du rt| jd }d}d}|dkrkd}| j| }|d8 }|dkr\|r[|d7 }qAn|| }d}| 	| |dksEt
|| j }	| 	t|	 t| j| t }
|
tjj9 }
| 	t|
 tt| j| }ttj| j|t|  |  |  | j|< n!tt| j| }t
|| _ttj| j|  t||  d| _|S )a  WriteVtable serializes the vtable for the current object, if needed.

    Before writing out the vtable, this checks pre-existing vtables for
    equality to this one. If an equal vtable is found, point the object to
    the existing vtable and return.

    Because vtable values are sensitive to alignment of object data, not
    all logically-equal vtables will be deduplicated.

    A vtable has the following format:
      <VOffsetT: size of the vtable in bytes, including this value>
      <VOffsetT: size of the object in bytes, including the vtable offset>
      <VOffsetT: offset for a field> * N, where N is the number of fields
                 in the schema for this type. Includes deprecated fields.
    Thus, a vtable is made of 2 + N elements, each VOffsetT bytes wide.

    An object has the following format:
      <SOffsetT: offset to this object's vtable (may be negative)>
      <byte: data>+
    r   TFNr   )PrependSOffsetTRelativer=   reversedr!   appendtupler%   getr4   PrependVOffsetTr   r.   r$   r   VtableMetadataFieldsN	bytewidthr   r    r   Writer   soffsetr"   r7   )r/   objectOffsetvtKeytrimelem	vt2Offsetitrailingoff
objectSizevBytesobjectStartr   r   r   WriteVtable   sl   


	zBuilder.WriteVtablec                 C   s   |    d| _|  S )z>EndObject writes data necessary to finish object construction.F)assertNestedr&   rV   r5   r   r   r   	EndObject<  s   zBuilder.EndObjectc                 C   sh   t | jtjkrd}t|tt | jd tj}|dkrd}t|}| j||t | j d< || _dS )zDoubles the size of the byteslice, and copies the old data towards

    the end of the new buffer (since we build the buffer backwards).
    z2flatbuffers: cannot grow buffer beyond 2 gigabytesr   r   r   N)r4   r    r   r,   r   minr-   )r/   r1   newSizebytes2r   r   r   growByteBufferB  s   
zBuilder.growByteBufferc                 C   s   | j S )zGet the start of useful data in the underlying byte buffer.

    Note: unlike other functions, this value is interpreted as from the
    left.
    )r"   r5   r   r   r   r7   T  s   zBuilder.Headc                 C   s   t t| j|   S )z)Offset relative to the end of the buffer.)r   r.   r4   r    r7   r5   r   r   r   r=   _  s   zBuilder.Offsetc                 C   s    t |D ]	}| dtj qdS )z'Pad places zeros at the current offset.r   N)r
   PlacerG   
Uint8Flags)r/   nrP   r   r   r   Padc  s   zBuilder.Padc                 C   s   || j kr|| _ t| j|   |  d }||d M }|  || | k rIt| j}|   | jt| j | }t|| _|  || | k s&| | dS )a4  Prep prepares to write an element of `size` after `additional_bytes`

    have been written, e.g. if you write a string, you need to align
    such the int length field is aligned to SizeInt32, and the string
    data follows it directly.
    If all you need to do is align, `additionalBytes` will be 0.
    r   N)	r#   r4   r    r7   r\   r"   r   r.   r`   )r/   sizeadditionalBytes	alignSize
oldBufSizeupdated_headr   r   r   Preph  s   


zBuilder.Prepc                 C   J   |  tjjd ||  ksd}t||  | tjj }| | dS )z]PrependSOffsetTRelative prepends an SOffsetT, relative to where it

    will be written.
    r   %flatbuffers: Offset arithmetic error.N)rf   rG   r   rH   r=   r   PlaceSOffsetTr/   rR   r1   off2r   r   r   r@        zBuilder.PrependSOffsetTRelativec                 C   rg   )z]Prepends an unsigned offset into vector data, relative to where it

    will be written.
    r   rh   N)rf   rG   r   rH   r=   r   PlaceUOffsetTrj   r   r   r   PrependUOffsetTRelative  rl   zBuilder.PrependUOffsetTRelativec                 C   s@   |    d| _|| _| tjj||  | |||  |  S )zStartVector initializes bookkeeping for writing a new vector.

    A vector has the following format:
      - <UOffsetT: number of elements in this vector>
      - <T: data>+, where T is the type of elements of this vector.
    T)r<   r&   r)   rf   rG   Uint32FlagsrH   r=   )r/   elemSizenumElems	alignmentr   r   r   StartVector  s   zBuilder.StartVectorc                 C   sL   |    d| _|rtjdtdd || jkrt | | j d| _|  S )z>EndVector writes data necessary to finish vector construction.FznumElems is deprecated.r   )
stacklevelN)	rW   r&   warningswarnDeprecationWarningr)   r   rm   r=   )r/   rq   r   r   r   	EndVector  s   
zBuilder.EndVectorutf-8strictc                 C   s0   || j v r
| j | S | |||}|| j |< |S )zoCreateSharedString checks if the string is already written to the buffer

    before calling CreateString.
    )r*   CreateString)r/   sencodingerrorsrR   r   r   r   CreateSharedString  s
   


zBuilder.CreateSharedStringc                 C   s   |    d| _t|tjr|||}nt|tjr|}ntd| t	j
jt|d t	jj  | dt	j t
t|}t
|  | | _|| j|  |  | < t|| _|  S )z>CreateString writes a null-terminated byte string as a vector.Tz!non-string passed to CreateStringr   r   )r<   r&   
isinstancer   string_typesr   binary_types	TypeErrorrf   rG   r   rH   r4   r^   r]   r.   r7   r"   r    r)   rx   )r/   r|   r}   r~   xlr   r   r   r{     s    
zBuilder.CreateStringc                 C   s   |    d| _t|tjstd| tjj	t
|tjj	  tt
|}t|  | | _|| j|  |  | < t
|| _|  S )z"CreateString writes a byte vector.Tz*non-byte vector passed to CreateByteVector)r<   r&   r   r   r   r   rf   rG   r   rH   r4   r^   r.   r7   r"   r    r)   rx   )r/   r   r   r   r   r   CreateByteVector  s   
zBuilder.CreateByteVectorc                 C   s   t du rtdt|t jstd|jjdvrtd|jdkr%td| |j	|j
|jj |jjd d	kr;|}n|jd
d}t|j	|j
 }t|  | | _|jdd| j|  |  | < |j
| _|  S )z7CreateNumpyVector writes a numpy array into the buffer.NzNumpy was not found.z-non-numpy-ndarray passed to CreateNumpyVector)brP   ufz4numpy-ndarray holds elements of unsupported datatyper   z4multidimensional-ndarray passed to CreateNumpyVectorr   <F)inplaceC)order)npr   r   ndarrayr   dtypekindndimrs   itemsizera   rr   strbyteswapr   r.   r7   r"   tobytesr    r)   rx   )r/   r   x_lendr   r   r   r   CreateNumpyVector  s"   
"zBuilder.CreateNumpyVectorc                 C   s   | j st dS )z7Check that we are in the process of building an object.N)r&   r   r5   r   r   r   rW   (  s   zBuilder.assertNestedc                 C   s   | j rt dS )zkCheck that no other objects are being built while making this object.

    If not, raise an exception.
    N)r&   r   r5   r   r   r   r<   .  s   zBuilder.assertNotNestedc                 C   s*   t |t j ||  krd}t|dS )zStructs are always stored inline, so need to be created right

    where they are used. You'll get this error if you created it
    elsewhere.
    zkflatbuffers: Tried to write a Struct at an Offset that is different from the current Offset of the Builder.N)rG   enforce_numberr   r=   r   )r/   objr1   r   r   r   assertStructIsInline7  s   zBuilder.assertStructIsInlinec                 C   s   |    |  | j|< dS )zSSlot sets the vtable key `voffset` to the current location in the

    buffer.
    N)rW   r=   r!   )r/   slotnumr   r   r   SlotF  s   zBuilder.Slotc                 C   s   t |t j t jj}|dur|t jj7 }|r|t jj7 }| | j| |durM| t jjtj t j	
d|}ttjd ddD ]}| || t j qA| | |rit| j|   }t |t j | | d| _|  S )=Finish finalizes a buffer, pointing to the given `rootTable`.Nz>BBBBr   T)rG   r   r   rH   
Int32Flagsrf   r#   r   FILE_IDENTIFIER_LENGTHstructunpackranger]   r^   rn   r4   r    r7   PrependInt32r(   )r/   	rootTable
sizePrefixfile_identifierprepSizerP   ra   r   r   r   __FinishP  s&   

zBuilder.__Finishc                 C      | j |d|dS )r   Fr   _Builder__Finishr/   r   r   r   r   r   Finishn  s   zBuilder.Finishc                 C   r   )z_Finish finalizes a buffer, pointing to the given `rootTable`,

    with the size prefixed.
    Tr   r   r   r   r   r   FinishSizePrefixedr  s   zBuilder.FinishSizePrefixedc                 C   s   |  |jd | || d S )Nr   )rf   rH   r]   )r/   flagsrR   r   r   r   Prependz  s   zBuilder.Prependc                 C   s`   |d ur
t || |d urt || ||ks| jr,|d ur.| || | | d S d S d S N)rG   r   r'   r   r   )r/   r   or   dr   r   r   PrependSlot~  s   zBuilder.PrependSlotc                 G      | j tjg|R   d S r   )r   rG   	BoolFlagsr/   argsr   r   r   PrependBoolSlot     zBuilder.PrependBoolSlotc                 G   r   r   r   rG   r^   r   r   r   r   PrependByteSlot  r   zBuilder.PrependByteSlotc                 G   r   r   r   r   r   r   r   PrependUint8Slot  r   zBuilder.PrependUint8Slotc                 G   r   r   )r   rG   Uint16Flagsr   r   r   r   PrependUint16Slot  r   zBuilder.PrependUint16Slotc                 G   r   r   )r   rG   ro   r   r   r   r   PrependUint32Slot  r   zBuilder.PrependUint32Slotc                 G   r   r   )r   rG   Uint64Flagsr   r   r   r   PrependUint64Slot  r   zBuilder.PrependUint64Slotc                 G   r   r   )r   rG   	Int8Flagsr   r   r   r   PrependInt8Slot  r   zBuilder.PrependInt8Slotc                 G   r   r   )r   rG   
Int16Flagsr   r   r   r   PrependInt16Slot  r   zBuilder.PrependInt16Slotc                 G   r   r   )r   rG   r   r   r   r   r   PrependInt32Slot  r   zBuilder.PrependInt32Slotc                 G   r   r   )r   rG   
Int64Flagsr   r   r   r   PrependInt64Slot  r   zBuilder.PrependInt64Slotc                 G   r   r   )r   rG   Float32Flagsr   r   r   r   PrependFloat32Slot  r   zBuilder.PrependFloat32Slotc                 G   r   r   )r   rG   Float64Flagsr   r   r   r   PrependFloat64Slot  r   zBuilder.PrependFloat64Slotc                 C   s*   ||ks| j r| | | | dS dS )zPrependUOffsetTRelativeSlot prepends an UOffsetT onto the object at

    vtable slot `o`. If value `x` equals default `d`, then the slot will
    be set to zero and no other data will be written.
    N)r'   rn   r   )r/   r   r   r   r   r   r   PrependUOffsetTRelativeSlot  s   
z#Builder.PrependUOffsetTRelativeSlotc                 C   s2   t |t j ||kr| | | | dS dS )zPrependStructSlot prepends a struct onto the object at vtable slot `o`.

    Structs are stored inline, so nothing additional is being added. In
    generated code, `d` is always 0.
    N)rG   r   r   r   r   )r/   vr   r   r   r   r   PrependStructSlot  s
   
zBuilder.PrependStructSlotc                 C      |  tj| dS )zTPrepend a `bool` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r/   r   r   r   r   PrependBool     zBuilder.PrependBoolc                 C   r   )zTPrepend a `byte` to the Builder buffer.

    Note: aligns and checks for space.
    Nr   rG   r^   r   r   r   r   PrependByte  r   zBuilder.PrependBytec                 C   r   )zVPrepend an `uint8` to the Builder buffer.

    Note: aligns and checks for space.
    Nr   r   r   r   r   PrependUint8  r   zBuilder.PrependUint8c                 C   r   )zWPrepend an `uint16` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependUint16  r   zBuilder.PrependUint16c                 C   r   )zWPrepend an `uint32` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   ro   r   r   r   r   PrependUint32  r   zBuilder.PrependUint32c                 C   r   )zWPrepend an `uint64` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependUint64  r   zBuilder.PrependUint64c                 C   r   )zUPrepend an `int8` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependInt8  r   zBuilder.PrependInt8c                 C   r   )zVPrepend an `int16` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependInt16  r   zBuilder.PrependInt16c                 C   r   )zVPrepend an `int32` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   r     r   zBuilder.PrependInt32c                 C   r   )zVPrepend an `int64` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependInt64  r   zBuilder.PrependInt64c                 C   r   )zWPrepend a `float32` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependFloat32
  r   zBuilder.PrependFloat32c                 C   r   )zWPrepend a `float64` to the Builder buffer.

    Note: aligns and checks for space.
    N)r   rG   r   r   r   r   r   PrependFloat64  r   zBuilder.PrependFloat64c                 C   s
   || _ dS )zIn order to save space, fields that are set to their default value

    don't get serialized into the buffer. Forcing defaults provides a
    way to manually disable this optimization. When set to `True`, will
    always serialize default values.
    N)r'   )r/   r'   r   r   r   ForceDefaults  s   
zBuilder.ForceDefaultsc                 C   s   |  tj| d S r   )r   rG   r   r   r   r   r   rE   $  s   zBuilder.PrependVOffsetTc                 C   s6   t || | j|j | _t|j| j|  | dS )zkPlace prepends a value specified by `flags` to the Builder,

    without checking for available space.
    N)	rG   r   r"   rH   r   rI   packer_typer    r7   )r/   r   r   r   r   r   r]   '  s   zBuilder.Placec                 C   :   t |t j | jt jj | _ttj| j	| 
 | dS )zWPlaceVOffsetT prepends a VOffsetT to the Builder, without checking

    for space.
    N)rG   r   r   r"   rH   r   rI   r   voffsetr    r7   r   r   r   r   PlaceVOffsetT1     zBuilder.PlaceVOffsetTc                 C   r   )zWPlaceSOffsetT prepends a SOffsetT to the Builder, without checking

    for space.
    N)rG   r   r   r"   rH   r   rI   r   rJ   r    r7   r   r   r   r   ri   :  r   zBuilder.PlaceSOffsetTc                 C   r   )zWPlaceUOffsetT prepends a UOffsetT to the Builder, without checking

    for space.
    N)rG   r   r   r"   rH   r   rI   r   uoffsetr    r7   r   r   r   r   rm   C  r   zBuilder.PlaceUOffsetT)r+   )r3   Nr   )ry   rz   )Br   r   r   r   	__slots__r,   r2   r6   r8   r?   rV   rX   r\   r7   r=   r`   rf   r@   rn   rs   rx   r   r{   r   r   rW   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r]   r   ri   rm   r   r   r   r   r   c   s    


l


%	



	
		r   c                 C   s   t |t j t| t jj t|krdS t| D ]$\}}tt	j
||t jj }|dkr2|dkr2q|| }||kr= dS qdS )z=vtableEqual compares an unwritten vtable to a written vtable.Fr   T)rG   r   r   r4   r   rH   	enumerater   Getr   r   )arU   r   rP   rN   r   yr   r   r   vtableEqualP  s   r   )ru    r   r   r   rG   r   r   r   r	   r
   r   r   r   r   RuntimeErrorr   r   r   r   r   r   r   rF   objectr   r   r   r   r   r   <module>   s2   
				
     r