a
    Sicg                     @   s
  d dl mZ d dlmZmZ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
 d dlmZmZ d	d
l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 )   )number_types)UOffsetTFlagsSOffsetTFlagsVOffsetTFlags)encode)packer)compat)
range_func)memoryview_type)import_numpyNumpyRequiredForThisFeature    Nc                   @   s   e Zd ZdZdS )OffsetArithmeticErrorz
    Error 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   O/var/www/html/django/DPS/env/lib/python3.9/site-packages/flatbuffers/builder.pyr   "   s   r   c                   @   s   e Zd ZdZdS )IsNotNestedErrorz]
    Error caused by using a Builder to write Object data when not inside
    an Object.
    Nr   r   r   r   r   r   +   s   r   c                   @   s   e Zd ZdZdS )IsNestedErrorzg
    Error caused by using a Builder to begin an Object when an Object is
    already being built.
    Nr   r   r   r   r   r   3   s   r   c                   @   s   e Zd ZdZdS )StructIsNotInlineErrorzm
    Error caused by using a Builder to write a Struct at a location that
    is not the current Offset.
    Nr   r   r   r   r   r   ;   s   r   c                   @   s   e Zd ZdZdS )BuilderSizeErrorz]
    Error caused by causing a Builder to exceed the hardcoded limit of 2
    gigabytes.
    Nr   r   r   r   r   r   C   s   r   c                   @   s   e Zd ZdZdS )BuilderNotFinishedErrorzG
    Error caused by not calling `Finish` before calling `Output`.
    Nr   r   r   r   r   r   J   s   r   c                   @   s   e Zd ZdZdS )EndVectorLengthMismatchedzm
    The number of elements passed to EndVector does not match the number 
    specified in StartVector.
    Nr   r   r   r   r   r   P   s   r      c                   @   s  e Zd ZdZdZdZd|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dd Zd}d d!Zd~d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zdd2d3Zdd4d5Zdd6d7Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1d`da Z2dbdc Z3ddde Z4dfdg Z5dhdi Z6djdk Z7dldm Z8dndo Z9dpdq Z:drds Z;dtdu Z<dvdw Z=dxdy Z>dzd{ Z?d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vectorNumElemsl           c                 C   sh   d|  krt jks$n d}t|t|| _d| _t|| _d| _	d| _
i | _d| _d| _d| _dS )zgInitializes 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&   )selfZinitialSizemsgr   r   r   __init__}   s    
zBuilder.__init__c                 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   Headr,   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>       z'Builder.StartObject.<locals>.<listcomp>TN)assertNotNestedr	   r   Offsetr"   r$   )r,   Z	numfieldsr   r   r   StartObject   s    
zBuilder.StartObjectc              	   C   s  |  d |  }g }d}t| jD ]*}|dkr8|rDq$n|| }d}|| q$t|}| j|}|du rft| jd }d}d}|dkrd}| j| }|d8 }|dkr|r|d7 }qn|| }d}| 	| qt
|| j }	| 	t|	 t| j| t }
|
tjj9 }
| 	t|
 tt| j| }ttj| j|t|  |  |  | j|< nBt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   )PrependSOffsetTRelativer7   reversedr   appendtupler#   getlenPrependVOffsetTr   r+   r"   r   VtableMetadataFieldsN	bytewidthr   r   r   Writer   soffsetr    r/   )r,   ZobjectOffsetZvtKeytrimelemZ	vt2OffsetitrailingoffZ
objectSizeZvBytesobjectStartr   r   r   WriteVtable   sZ    


zBuilder.WriteVtablec                 C   s   |    d| _|  S )z>EndObject writes data necessary to finish object construction.F)assertNestedr$   rK   r0   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)r>   r   r   r)   r   minr*   )r,   r-   ZnewSizeZbytes2r   r   r   growByteBuffer  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    r0   r   r   r   r/   )  s    zBuilder.Headc                 C   s   t t| j|   S )z)Offset relative to the end of the buffer.)r   r+   r>   r   r/   r0   r   r   r   r7   4  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	   PlacerA   
Uint8Flags)r,   nrG   r   r   r   Pad8  s    zBuilder.Padc                 C   s   || j kr|| _ t| j|   |  d }||d M }|  || | k rt| j}|   | jt| j | }t|| _q8| | dS )aP  
        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!   r>   r   r/   rO   r    r   r+   rS   )r,   sizeZadditionalBytesZ	alignSizeZ
oldBufSizeZupdated_headr   r   r   Prep=  s    


zBuilder.Prepc                 C   sJ   |  tjjd ||  ks(d}t||  | tjj }| | dS )zm
        PrependSOffsetTRelative prepends an SOffsetT, relative to where it
        will be written.
        r   %flatbuffers: Offset arithmetic error.N)rU   rA   r   rB   r7   r   PlaceSOffsetTr,   rI   r-   Zoff2r   r   r   r9   W  s    zBuilder.PrependSOffsetTRelativec                 C   sJ   |  tjjd ||  ks(d}t||  | tjj }| | dS )zdPrepends an unsigned offset into vector data, relative to where it
        will be written.
        r   rV   N)rU   rA   r   rB   r7   r   PlaceUOffsetTrX   r   r   r   PrependUOffsetTRelativef  s    zBuilder.PrependUOffsetTRelativec                 C   s@   |    d| _|| _| tjj||  | |||  |  S )z
        StartVector 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)r6   r$   r'   rU   rA   Uint32FlagsrB   r7   )r,   ZelemSizenumElems	alignmentr   r   r   StartVectort  s    	zBuilder.StartVectorNc                 C   sL   |    d| _|r2tjdtdd || jkr2t | | j d| _|  S )z>EndVector writes data necessary to finish vector construction.FznumElems is deprecated.r   )
stacklevelN)	rL   r$   warningswarnDeprecationWarningr'   r   rY   r7   )r,   r\   r   r   r   	EndVector  s    
zBuilder.EndVectorutf-8strictc                 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   )r6   r$   
isinstancer   string_typesr   binary_types	TypeErrorrU   rA   r   rB   r>   rQ   rP   r+   r/   r    r   r'   rc   )r,   sencodingerrorsxlr   r   r   CreateString  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)r6   r$   rf   r   rh   ri   rU   rA   r   rB   r>   rQ   r+   r/   r    r   r'   rc   )r,   rm   rn   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r8td|jdkrJtd| |j	|j
|jj |jjd d	krv|}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)brG   ufz4numpy-ndarray holds elements of unsupported datatyper   z4multidimensional-ndarray passed to CreateNumpyVectorr   <F)inplaceC)order)npr   rf   ndarrayri   dtypekindndimr^   itemsizerT   r]   strbyteswapr   r+   r/   r    tobytesr   r'   rc   )r,   rm   Zx_lendrn   r   r   r   CreateNumpyVector  s"    
"zBuilder.CreateNumpyVectorc                 C   s   | j st dS )zI
        Check that we are in the process of building an object.
        N)r$   r   r0   r   r   r   rL     s    zBuilder.assertNestedc                 C   s   | j rt dS )z{
        Check that no other objects are being built while making this
        object. If not, raise an exception.
        N)r$   r   r0   r   r   r   r6     s    zBuilder.assertNotNestedc                 C   s*   t |t j ||  kr&d}t|dS )z
        Structs 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)rA   enforce_numberr   r7   r   )r,   objr-   r   r   r   assertStructIsInline  s    zBuilder.assertStructIsInlinec                 C   s   |    |  | j|< dS )zd
        Slot sets the vtable key `voffset` to the current location in the
        buffer.

        N)rL   r7   r   )r,   Zslotnumr   r   r   Slot  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r| t jjtj t j	
d|}ttjd ddD ]}| || t j q| | |rt| j|   }t |t j | | d| _|  S )=Finish finalizes a buffer, pointing to the given `rootTable`.Nz>BBBBr   T)rA   r   r   rB   
Int32FlagsrU   r!   r   ZFILE_IDENTIFIER_LENGTHstructunpackrangerP   rQ   rZ   r>   r   r/   PrependInt32r&   )r,   	rootTableZ
sizePrefixfile_identifierZprepSizerG   rT   r   r   r   Z__Finish  s&    

zBuilder.__Finishc                 C   s   | j |d|dS )r   Fr   _Builder__Finishr,   r   r   r   r   r   Finish4  s    zBuilder.Finishc                 C   s   | j |d|dS )zo
        Finish finalizes a buffer, pointing to the given `rootTable`,
        with the size prefixed.
        Tr   r   r   r   r   r   FinishSizePrefixed8  s    zBuilder.FinishSizePrefixedc                 C   s   |  |jd | || d S )Nr   )rU   rB   rP   )r,   flagsrI   r   r   r   Prepend@  s    zBuilder.Prependc                 C   sX   |d urt || |d ur(t || ||ks>| jrT|d urT| || | | d S N)rA   r   r%   r   r   )r,   r   orm   dr   r   r   PrependSlotD  s    zBuilder.PrependSlotc                 G   s   | j tjg|R   d S r   )r   rA   	BoolFlagsr,   argsr   r   r   PrependBoolSlotM  r5   zBuilder.PrependBoolSlotc                 G   s   | j tjg|R   d S r   r   rA   rQ   r   r   r   r   PrependByteSlotO  r5   zBuilder.PrependByteSlotc                 G   s   | j tjg|R   d S r   r   r   r   r   r   PrependUint8SlotQ  r5   zBuilder.PrependUint8Slotc                 G   s   | j tjg|R   d S r   )r   rA   Uint16Flagsr   r   r   r   PrependUint16SlotS  r5   zBuilder.PrependUint16Slotc                 G   s   | j tjg|R   d S r   )r   rA   r[   r   r   r   r   PrependUint32SlotU  r5   zBuilder.PrependUint32Slotc                 G   s   | j tjg|R   d S r   )r   rA   Uint64Flagsr   r   r   r   PrependUint64SlotW  r5   zBuilder.PrependUint64Slotc                 G   s   | j tjg|R   d S r   )r   rA   	Int8Flagsr   r   r   r   PrependInt8SlotY  r5   zBuilder.PrependInt8Slotc                 G   s   | j tjg|R   d S r   )r   rA   
Int16Flagsr   r   r   r   PrependInt16Slot[  r5   zBuilder.PrependInt16Slotc                 G   s   | j tjg|R   d S r   )r   rA   r   r   r   r   r   PrependInt32Slot]  r5   zBuilder.PrependInt32Slotc                 G   s   | j tjg|R   d S r   )r   rA   
Int64Flagsr   r   r   r   PrependInt64Slot_  r5   zBuilder.PrependInt64Slotc                 G   s   | j tjg|R   d S r   )r   rA   Float32Flagsr   r   r   r   PrependFloat32Slota  s   
zBuilder.PrependFloat32Slotc                 G   s   | j tjg|R   d S r   )r   rA   Float64Flagsr   r   r   r   PrependFloat64Slotd  s   
zBuilder.PrependFloat64Slotc                 C   s&   ||ks| j r"| | | | dS )z
        PrependUOffsetTRelativeSlot 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%   rZ   r   )r,   r   rm   r   r   r   r   PrependUOffsetTRelativeSlotg  s    
z#Builder.PrependUOffsetTRelativeSlotc                 C   s.   t |t j ||kr*| | | | dS )z
        PrependStructSlot 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)rA   r   r   r   r   )r,   vrm   r   r   r   r   PrependStructSlotr  s    
zBuilder.PrependStructSlotc                 C   s   |  tj| dS )z\Prepend a `bool` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r,   rm   r   r   r   PrependBool  s    zBuilder.PrependBoolc                 C   s   |  tj| dS )z\Prepend a `byte` to the Builder buffer.

        Note: aligns and checks for space.
        Nr   rA   rQ   r   r   r   r   PrependByte  s    zBuilder.PrependBytec                 C   s   |  tj| dS )z^Prepend an `uint8` to the Builder buffer.

        Note: aligns and checks for space.
        Nr   r   r   r   r   PrependUint8  s    zBuilder.PrependUint8c                 C   s   |  tj| dS )z_Prepend an `uint16` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependUint16  s    zBuilder.PrependUint16c                 C   s   |  tj| dS )z_Prepend an `uint32` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r[   r   r   r   r   PrependUint32  s    zBuilder.PrependUint32c                 C   s   |  tj| dS )z_Prepend an `uint64` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependUint64  s    zBuilder.PrependUint64c                 C   s   |  tj| dS )z]Prepend an `int8` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependInt8  s    zBuilder.PrependInt8c                 C   s   |  tj| dS )z^Prepend an `int16` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependInt16  s    zBuilder.PrependInt16c                 C   s   |  tj| dS )z^Prepend an `int32` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   r     s    zBuilder.PrependInt32c                 C   s   |  tj| dS )z^Prepend an `int64` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependInt64  s    zBuilder.PrependInt64c                 C   s   |  tj| dS )z_Prepend a `float32` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependFloat32  s    zBuilder.PrependFloat32c                 C   s   |  tj| dS )z_Prepend a `float64` to the Builder buffer.

        Note: aligns and checks for space.
        N)r   rA   r   r   r   r   r   PrependFloat64  s    zBuilder.PrependFloat64c                 C   s
   || _ dS )a  
        In 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   rA   r   r   r   r   r   r?     r5   zBuilder.PrependVOffsetTc                 C   s6   t || | j|j | _t|j| j|  | dS )z{
        Place prepends a value specified by `flags` to the Builder,
        without checking for available space.
        N)	rA   r   r    rB   r   rC   Zpacker_typer   r/   )r,   rm   r   r   r   r   rP     s    zBuilder.Placec                 C   s:   t |t j | jt jj | _ttj| j	| 
 | dS )z^PlaceVOffsetT prepends a VOffsetT to the Builder, without checking
        for space.
        N)rA   r   r   r    rB   r   rC   r   voffsetr   r/   r   r   r   r   PlaceVOffsetT  s    zBuilder.PlaceVOffsetTc                 C   s:   t |t j | jt jj | _ttj| j	| 
 | dS )z^PlaceSOffsetT prepends a SOffsetT to the Builder, without checking
        for space.
        N)rA   r   r   r    rB   r   rC   r   rD   r   r/   r   r   r   r   rW     s    zBuilder.PlaceSOffsetTc                 C   s:   t |t j | jt jj | _ttj| j	| 
 | dS )z^PlaceUOffsetT prepends a UOffsetT to the Builder, without checking
        for space.
        N)rA   r   r   r    rB   r   rC   r   Zuoffsetr   r/   r   r   r   r   rY     s    zBuilder.PlaceUOffsetT)r(   )N)rd   re   )N)N)N)@r   r   r   r   	__slots__r)   r.   r1   r8   rK   rM   rO   r/   r7   rS   rU   r9   rZ   r^   rc   ro   rp   r   rL   r6   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?   rP   r   rW   rY   r   r   r   r   r   \   sz   

e

%	



	
r   c                 C   s   t |t j t| t jj t|kr*dS t| D ]H\}}tt	j
||t jj }|dkrd|dkrdq2|| }||kr2 dS q2dS )z=vtableEqual compares an unwritten vtable to a written vtable.Fr   T)rA   r   r   r>   r   rB   	enumerater   Getr   r   )arJ   rq   rG   rF   rm   yr   r   r   vtableEqual  s    r   ) r   rA   r   r   r   r   r   r   r	   r
   r   r   r`   rx   RuntimeErrorr   r   r   r   r   r   r   r@   objectr   r   r   r   r   r   <module>   s0   		     /