
    gVk                     ,   d Z ddlmZmZ ddlZddlmZ ddlmZm	Z	 ddl
Z
ddlZddlZddlmZmZmZ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 m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z- d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d Z8d Z9d Z:d Z;d  Z<i d!e.d"e/d#e5d$e0d%e1d&e2d'e3d(e4d)e7d*e8d+e9d,e:d-e;d.e;d/e<d0e<Z=d1 Z>d2 Z?d3 Z@d4 ZAd5 ZBd6 ZCe@eAeBeCd7ZDd8 ZEd9 ZF	 dd:lGmHZH eHj                  ZJeFeDd;<   d> ZN	 ddlOZOeNeDd?<   d@ ZP	 ddlQZRePeDdA<    G dB dCe      ZS G dD dEeS      ZT G dF dGeS      ZUd!dHe z  ddIdJdfdIdIdIdKdLeeef   dMe,dNee   dOeVdPeWdQeeeVeVf      dReXdSeYdTeeW   dUeXdVeXdWeXfdXZZdIdIdIdKdLedMe,d/edUeXdVeXdWeXfdYZ[y# eK$ r: 	 ddlHZHeHj                  ZJ ed<eM       eFeDd;<   n# eK$ r  eEd;d=      eDd;<   Y nw xY wY w xY w# eK$ r  eEd?d?      eDd?<   Y w xY w# eK$ r  eEdAdA      eDdA<   Y w xY w)Zz"Python code for writing AVRO files    )ABCabstractmethodN)BytesIO)urandomSEEK_SET)UnionIOIterableAnyOptionalDict)warn   )NAMED_TYPES)BinaryEncoder)AvroJSONEncoder)	_validate)HEADER_SCHEMA	SYNC_SIZEMAGICreader)LOGICAL_WRITERS)extract_record_typeextract_logical_typeparse_schema)_is_appendable)SchemaNamedSchemasc                 $    | j                          y)znull is written as zero bytesN)
write_nullencoderdatumschemanamed_schemasfnameoptionss         G/var/www/openai/venv/lib/python3.12/site-packages/fastavro/_write_py.pyr    r       s        c                 &    | j                  |       y)zVA boolean is written as a single byte whose value is either 0 (false) or
    1 (true).N)write_booleanr!   s         r(   r+   r+   !   s     % r)   c                 &    | j                  |       yzFint and long values are written using variable-length, zig-zag coding.N)	write_intr!   s         r(   r.   r.   '   s    er)   c                 &    | j                  |       yr-   )
write_longr!   s         r(   r0   r0   ,   s    ur)   c                 &    | j                  |       y)zA float is written as 4 bytes.  The float is converted into a 32-bit
    integer using a method equivalent to Java's floatToIntBits and then encoded
    in little-endian format.N)write_floatr!   s         r(   r2   r2   1   s     r)   c                 &    | j                  |       y)zA double is written as 8 bytes.  The double is converted into a 64-bit
    integer using a method equivalent to Java's doubleToLongBits and then
    encoded in little-endian format.N)write_doubler!   s         r(   r4   r4   8   s     r)   c                 &    | j                  |       y)z@Bytes are encoded as a long followed by that many bytes of data.N)write_bytesr!   s         r(   r6   r6   ?       r)   c                 &    | j                  |       y)z^A string is encoded as a long followed by that many bytes of UTF-8
    encoded character data.N)
write_utf8r!   s         r(   r9   r9   D   s     ur)   c                 &    | j                  |       y)z#A 4-byte, big-endian CRC32 checksumN)write_crc32)r"   r#   s     r(   r;   r;   J   r7   r)   c                 |    t        |      |d   k7  rt        dt        |       d|       | j                  |       y)zQFixed instances are encoded using the number of bytes declared in the
    schema.sizezdata of length z does not match schema size: N)len
ValueErrorwrite_fixedr!   s         r(   r@   r@   O   sF     5zVF^#c%j\)FvhO
 	
 r)   c                 N    |d   j                  |      }| j                  |       y)zbAn enum is encoded by a int, representing the zero-based position of
    the symbol in the schema.symbolsN)index
write_enum)r"   r#   r$   r%   r&   r'   rC   s          r(   rD   rD   Y   s'     9##E*Eur)   c           	          | j                          t        |      dkD  rF| j                  t        |             |d   }|D ]"  }t        | |||||       | j	                          $ | j                          y)a  Arrays are encoded as a series of blocks.

    Each block consists of a long count value, followed by that many array
    items.  A block with count zero indicates the end of the array.  Each item
    is encoded per the array's item schema.

    If a block's count is negative, then the count is followed immediately by a
    long block size, indicating the number of bytes in the block.  The actual
    count in this case is the absolute value of the count written.r   itemsN)write_array_startr>   write_item_count
write_dataend_itemwrite_array_end)r"   r#   r$   r%   r&   r'   dtypeitems           r(   write_arrayrN   `   sl     
5zA~  U,wDwe]E7K  r)   c           	         | j                          t        |      dkD  rX| j                  t        |             |d   }|j                         D ]&  \  }}| j	                  |       t        | |||||       ( | j                          y)a  Maps are encoded as a series of blocks.

    Each block consists of a long count value, followed by that many key/value
    pairs.  A block with count zero indicates the end of the map.  Each item is
    encoded per the map's value schema.

    If a block's count is negative, then the count is followed immediately by a
    long block size, indicating the number of bytes in the block. The actual
    count in this case is the absolute value of the count written.r   valuesN)write_map_startr>   rH   rF   r9   rI   write_map_end)	r"   r#   r$   r%   r&   r'   vtypekeyvals	            r(   	write_maprV   t   sy     
5zA~  U,x HCs#wUM5'J & r)   c           	         d}t        |t              rx|j                  d      sg|\  }}t        |      D ])  \  }}	t	        |	      }
|
t
        v r|	d   }n|
}||k(  s'|} n |dk(  r"|rd| nd}d| d| d| z   }t        |      |}nt        |      }d}d	}t        |      D ]  \  }}	|rt	        |	      d
k(  r|} nt        ||	|d	d|      s-t	        |	      }||v r||   }	t	        |	      }|dk(  rrt        |	      }|r t        j                  |      }|r	 |||	      }t        d |	d   D              }t        |      }t        |j                  |            }||kD  s|}|}|dk(  r|}d}|} n |dk(  r(|rd| nd}t        t        |       d| d| d|       |}| j                  |||          t!        | |||   |||       y)zA union is encoded by first writing a long value indicating the
    zero-based position within the union of the schema of its value. The value
    is then encoded per the indicated schema within the union.disable_tuple_notationnamez	on field  zprovided union type name z not found in schema  Fdouble)raise_errorsfieldr'   recordc              3   &   K   | ]	  }|d      ywrZ   N ).0fs     r(   	<genexpr>zwrite_union.<locals>.<genexpr>   s     *R>Q1V9>Q   fieldsfloatTz (type z) do not match N)
isinstancetupleget	enumerater   r   r?   typer   r   r   setr>   intersectionreprwrite_indexrI   )r"   r#   r$   r%   r&   r'   best_match_indexrZ   rC   	candidateextracted_typeschema_namer_   msgpytypemost_fieldscould_be_floatrecord_typelogical_typepreparecandidate_fieldsdatum_fieldsrh   s                          r(   write_unionr      s\   
 %4L(Mu )& 1E90;N,'/,{"#(  !2 r!+0iw'bE+D61FGHAeW%&  S/! e  )& 1E9&y1X=',$ " 2)<-/ -k :I"5i"@K(*#7	#BL#"1"5"5l"C"$+E9$=E'**Ri>Q*R'R$#&u:L !1!>!>|!LMF++0(&, G+',$ &*N',$W !2X r!+0iw'bE;-wvhofXQugN  ! ve}-wve}mUGLr)   c           	         t        |      t        d |d   D              z
  }|j                  d      s|j                  d      r|rt        ddj                  |             |d   D ]  }|d   }|d   }	||vrK|j                  d      s|j                  d      rd	|vrt        d
| d      d	|vrd|	vrt        d|       |j                  ||j                  d	            }
|	dk(  s|	dk(  rt	        |
      }
t        | |
|	|||        y)zA record is encoded by encoding the values of its fields in the order
    that they are declared. In other words, a record is encoded as just the
    concatenation of the encodings of its fields.  Field values are encoded per
    their schema.c              3   &   K   | ]	  }|d      ywrb   rc   )rd   r_   s     r(   rf   zwrite_record.<locals>.<genexpr>   s     J9IeFm9Irg   rh   strictstrict_allow_defaultz7record contains more fields than the schema specifies: z, rZ   rn   defaultzField z7 is specified in the schema but missing from the recordnullzno value and no default for ri   r]   N)ro   rl   r?   joinri   rI   )r"   r#   r$   r%   r&   r'   extrasr_   rZ   
field_typedatum_values              r(   write_recordr      s3   
 Z#J9IJJJFH-C!D&EdiiPVFWEXY
 	
 !V}6]
u{{8$23	8N TF"YZ  %'F*,D #?v!FGGiieii	&:; J($:,K	
! "r)   r   booleanstringintlongri   r]   bytesfixedenumarraymapunionerror_unionr`   errorc                 *   t        |      }t        |      }t        j                  |      }|r0|r t	        j                  |      }	|	r	 |	||      }	  || |||||      S t        | |||   |d|      S # t
        $ r}
|rt        |
 d|        d}
~
ww xY w)a&  Write a datum of data to output stream.

    Parameters
    ----------
    encoder: encoder
        Type of encoder (e.g. binary or json)
    datum: object
        Data to write
    schema: dict
        Schema to use
    named_schemas: dict
        Mapping of fullname to schema definition
    z
 on field Nr[   )r   r   WRITERSrl   r   	TypeErrorrI   )r"   r#   r$   r%   r&   r'   r{   r|   fnr}   exs              r(   rI   rI   #  s     &f-K'/L	[	!B	%)),7Gv.	gufmUGLL UM+6r7
 	
  	2$j 899	s   A1 1	B:BBc                     t         |j                         D ci c]  \  }}||j                          c}}|d}t        | |t        i di        y c c}}w )N)magicmetasyncr[   )r   rF   encoderI   r   )r"   metadatasync_markerrT   valueheaders         r(   write_headerr   G  sS    7?~~7GH7Geelln$7GHF
 wr2r: Is   Ac                 n    | j                  t        |             | j                  j                  |       y)zWrite block in "null" codec.N)r0   r>   _fowrite)r"   block_bytescompression_levels      r(   null_write_blockr   P  s'    s;'(KKk"r)   c                     |t        j                  ||      dd }nt        j                  |      dd }| j                  t        |             | j                  j                  |       y)zWrite block in "deflate" codec.N   rX   )zlibcompressr0   r>   r   r   r"   r   r   datas       r(   deflate_write_blockr   V  s[     $}}[*;<QrB}}[)!B/s4y!KKdr)   c                     t        j                  |      }| j                  t        |             | j                  j                  |       y)zWrite block in "bzip2" codec.N)bz2r   r0   r>   r   r   r   s       r(   bzip2_write_blockr   b  s4    <<$Ds4y!KKdr)   c                     t        j                  |      }| j                  t        |             | j                  j                  |       y)zWrite block in "xz" codec.N)lzmar   r0   r>   r   r   r   s       r(   xz_write_blockr   i  s4    ==%Ds4y!KKdr)   )r   deflatebzip2xzc                       fd}|S )Nc                 ,    t         dd z         )Nz7 codec is supported but you need to install one of the zfollowing libraries: )r?   )r"   r   r   codec	librariess      r(   missingz#_missing_codec_lib.<locals>.missingy  s*    gLM%i[12
 	
r)   rc   )r   r   r   s   `` r(   _missing_codec_libr   x  s    
 Nr)   c                     t        |      }| j                  t        |      dz          | j                  j	                  |       | j                  |       y)zWrite block in "snappy" codec.   N)snappy_compressr0   r>   r   r   r;   r   s       r(   snappy_write_blockr     sB    ;'Ds4y1}%KKd$r)   )snappyr   zbSnappy compression will use `cramjam` in the future. Please make sure you have `cramjam` installedcramjamc                    |&t        j                  |      j                  |      }n#t        j                         j                  |      }| j                  t	        |             | j
                  j                  |       y)z!Write block in "zstandard" codec.N)level)	zstandardZstdCompressorr   r0   r>   r   r   r   s       r(   zstandard_write_blockr     sa    $''.?@II+V'')22;?s4y!KKdr)   r   c                     t         j                  j                  |      }| j                  t	        |             | j
                  j                  |       y)zWrite block in "lz4" codec.N)lz4blockr   r0   r>   r   r   r   s       r(   lz4_write_blockr     s:    99k*Ds4y!KKdr)   r   c                   :    e Zd Zddi fdZed        Zed        Zy)GenericWriterNc           	      B   i | _         |rt        nd | _        |xs i | _        || _        |t        || j                         | _        t        |t              r(|j                         D ci c]  \  }}|dvr|| }}}ntt        |t              rdg }|D ][  }t        |t              r8|j                  |j                         D ci c]  \  }}|dvr|| c}}       K|j                  |       ] |}t        j                  |      | j                  d<   y c c}}w c c}}w )N)__fastavro_parsed__named_schemaszavro.schema)_named_schemasr   validate_fnr   r'   r   r$   rj   dictrF   listappendjsondumps)	selfr$   r   	validatorr'   rT   r   schemasss	            r(   __init__zGenericWriter.__init__  s&    (19t B &vt/B/BCDKfd# #),,."0JCFF U
"0  
 %Ga&NN /0ggi.7
U"$   J.7
 NN1%  F'+zz&'9m$1s   )DDc                      y Nrc   r   r`   s     r(   r   zGenericWriter.write      r)   c                      y r   rc   r   s    r(   flushzGenericWriter.flush  r   r)   )__name__
__module____qualname__r   r   r   r   rc   r)   r(   r   r     s7    (,b $:L    r)   r   c                        e Zd Zddez  ddddi fdeeef   deded	e	d
e
eeef      dedede
e	   deeef   f fdZd Zd Zd Zd Z xZS )Writerr     NFr)   for$   r   sync_intervalr   r   r   r   r'   c
                    t         |   ||||	       || j                  d<   t        |t              r|| _        nt	        |      | _        t	        t                     | _        d| _        || _	        || _
        t        | j
                  j                        r| j
                  j                  j                  d       t        | j
                  j                        }
|
j                  }i | _        t#        |
j$                  | j                         | _        |
j                  j)                  dd      }|d   | _        | j
                  j                  j                  dd       t,        |   | _        y |xs t1        t2              | _        	 t,        |   | _        t9        | j
                  | j                  | j*                         y # t4        $ r t7        d|       w xY w)Nz
avro.codecr   r   r   r   zunrecognized codec: )superr   r   rj   r   r"   r   ioblock_countr   r   r   r   seekr   _headerr   r   writer_schemar$   rl   r   BLOCK_WRITERSblock_writerr   r   KeyErrorr?   r   )r   r   r$   r   r   r   r   r   r   r'   avro_readerr   	__class__s               r(   r   zWriter.__init__  s    	9g>&+l#b-(DL(,DL	**!2$,,**+LL!!!$ !1!12K ((F"$D&{'@'@$BUBUVDK((,,\6BE%f~D LL!!!Q' -e 4D*@gi.@DA$1%$8! t}}d6F6FG  A #7w!?@@As   G Gc                    | j                   j                  | j                         | j                  | j                   | j                  j
                  j                         | j                         | j                   j
                  j                  | j                         | j                  j
                  j                  d       | j                  j
                  j                  dt               d| _        y Nr   )r"   r0   r   r   r   r   getvaluer   r   r   truncater   r   r   s    r(   dumpzWriter.dump$  s     0 01$,,(<(<(>@V@VWt//0QH%r)   c                    | j                   r4| j                  || j                  | j                  dd| j                         t	        | j
                  || j                  | j                  d| j                         | xj                  dz  c_        | j
                  j                  j                         | j                  k\  r| j                          y y )Nr[   Tr   )r   r$   r   r'   rI   r   r   r   tellr   r  r   s     r(   r   zWriter.write,  s    T%8%8"dDLL 	477FDKK1D1Db$,,WA77;;!3!33IIK 4r)   c                    | j                   j                  j                         s| j                  dkD  r| j	                          | j
                  j                  |j                         | j                  | j
                  |j                  j                         | j                         | j
                  j                  j                  | j                         y r   )r   r   r  r   r  r"   r0   num_recordsr   bytes_r   r   r   r   )r   r   s     r(   write_blockzWriter.write_block6  s    77;;!1!1A!5IIK 1 12$,,(=(=(?AWAWXt//0r)   c                     | j                   j                  j                         s| j                  dkD  r| j	                          | j
                  j                  j                          y r   )r   r   r  r   r  r"   r   r   s    r(   r   zWriter.flush>  sB    77;;!1!1A!5IIK r)   )r   r   r   r   r   r	   r   r   strr   r   r   boolr   r   r  r   r  r   __classcell__r   s   @r(   r   r     s    
 !I--1 +/#%1H"m#$1H 1H 	1H
 1H 4S>*1H 1H 1H $C=1H c4i1Hf1!r)   r   c                        e Zd Zddez  ddddi fdededed	ed
ee	eef      de
dedee   de	ee
f   f fdZd Zd Z xZS )
JSONWriterr   r   NFr)   r   r$   r   r   r   r   r   codec_compression_levelr'   c
                     t         
|   ||||	       || _        | j                  j                  | j                  | j
                         y r   )r   r   r"   	configurer$   r   )r   r   r$   r   r   r   r   r   r  r'   r   s             r(   r   zJSONWriter.__init__E  s=     	9g>t{{D,?,?@r)   c                     | j                   r4| j                  || j                  | j                  dd| j                         t	        | j
                  || j                  | j                  d| j                         y )Nr[   T)r   r$   r   r'   rI   r"   r   s     r(   r   zJSONWriter.writeV  s]    T%8%8"dDLL 	LL&$++t/B/BB	
r)   c                 8    | j                   j                          y r   )r"   r   r   s    r(   r   zJSONWriter.flush_  s    r)   )r   r   r   r   r   r   r
  r   r   r   r  r   r   r   r   r  r  s   @r(   r  r  D  s    
 !I--1 15#%AA A 	A
 A 4S>*A A A "*#A c4iA"
r)   r  r   Fr)   r   r   rY   r   r$   recordsr   r   r   r   r   r  r   r   rY   c	                   t        |t              rt        d      t        | t              rt	        | ||||||||	|
|d	      }n!t        t        |       ||||||||	|
|d	      }|D ]  }|j                  |        |j                          y)av  Write records to fo (stream) according to schema

    Parameters
    ----------
    fo
        Output stream
    schema
        Writer schema
    records
        Records to write. This is commonly a list of the dictionary
        representation of the records, but it can be any iterable
    codec
        Compression codec, can be 'null', 'deflate' or 'snappy' (if installed)
    sync_interval
        Size of sync interval
    metadata
        Header metadata
    validator
        If true, validation will be done on the records
    sync_marker
        A byte string used as the avro sync marker. If not provided, a random
        byte string will be used.
    codec_compression_level
        Compression level to use with the specified codec (if the codec
        supports it)
    strict
        If set to True, an error will be raised if records do not contain
        exactly the same fields that the schema states
    strict_allow_default
        If set to True, an error will be raised if records do not contain
        exactly the same fields that the schema states unless it is a missing
        field that has a default value in the schema
    disable_tuple_notation
        If set to True, tuples will not be treated as a special case. Therefore,
        using a tuple to indicate the type of a record will not work


    Example::

        from fastavro import writer, parse_schema

        schema = {
            'doc': 'A weather reading.',
            'name': 'Weather',
            'namespace': 'test',
            'type': 'record',
            'fields': [
                {'name': 'station', 'type': 'string'},
                {'name': 'time', 'type': 'long'},
                {'name': 'temp', 'type': 'int'},
            ],
        }
        parsed_schema = parse_schema(schema)

        records = [
            {u'station': u'011990-99999', u'temp': 0, u'time': 1433269388},
            {u'station': u'011990-99999', u'temp': 22, u'time': 1433270389},
            {u'station': u'011990-99999', u'temp': -11, u'time': 1433273379},
            {u'station': u'012650-99999', u'temp': 111, u'time': 1433275478},
        ]

        with open('weather.avro', 'wb') as out:
            writer(out, parsed_schema, records)

    The `fo` argument is a file-like object so another common example usage
    would use an `io.BytesIO` object like so::

        from io import BytesIO
        from fastavro import writer

        fo = BytesIO()
        writer(fo, schema, records)

    Given an existing avro file, it's possible to append to it by re-opening
    the file in `a+b` mode. If the file is only opened in `ab` mode, we aren't
    able to read some of the existing header information and an error will be
    raised. For example::

        # Write initial records
        with open('weather.avro', 'wb') as out:
            writer(out, parsed_schema, records)

        # Write some more records
        with open('weather.avro', 'a+b') as out:
            writer(out, None, more_records)

    Note: When appending, any schema provided will be ignored since the schema
    in the avro file will be re-used. Therefore it is convenient to just use
    None as the schema.
    z2"records" argument should be an iterable, not dictr  )r'   N)	rj   r   r?   r   r  r   r   r   r   )r   r$   r  r   r   r   r   r   r  r   r   rY   outputr`   s                 r(   writerr  c  s    V '4 MNN "o&# (<*@
  "# (<*@
  V 
LLNr)   c                ~    i }t        ||      }t        |       }t        ||||d|||d       |j                          y)a  Write a single record without the schema or header information

    Parameters
    ----------
    fo
        Output file
    schema
        Schema
    record
        Record to write
    strict
        If set to True, an error will be raised if records do not contain
        exactly the same fields that the schema states
    strict_allow_default
        If set to True, an error will be raised if records do not contain
        exactly the same fields that the schema states unless it is a missing
        field that has a default value in the schema
    disable_tuple_notation
        If set to True, tuples will not be treated as a special case. Therefore,
        using a tuple to indicate the type of a record will not work


    Example::

        parsed_schema = fastavro.parse_schema(schema)
        with open('file', 'wb') as fp:
            fastavro.schemaless_writer(fp, parsed_schema, record)

    Note: The ``schemaless_writer`` can only write a single record.
    r[   r  N)r   r   rI   r   )r   r$   r`   r   r   rY   r%   r"   s           r(   schemaless_writerr    sP    N #%M&-0FBG
$8&<	
 MMOr)   )\__doc__abcr   r   r   r   r   osr   r   r   r   r   typingr   r	   r
   r   r   r   warningsr   constr   io.binary_encoderr   io.json_encoderr   
validationr   readr   r   r   r   logical_writersr   r$   r   r   r   _write_commonr   typesr   r   r    r+   r.   r0   r2   r4   r6   r9   r;   r@   rD   rN   rV   r   r   r   rI   r   r   r   r   r   r   r   r   r   r   compress_rawr   ImportErrorr   DeprecationWarningr   r   r   	lz4.blockr   r   r   r  r
  r   r  r   r  r  rc   r)   r(   <module>r-     s   ( $     
   ; ;   , , ! 9 9 , K K ) '
!

 

((`MF!
H
J} j 
9	
 J [ l [ [ J [ 
9 [ ; l  \!(!
H;#	 "
	%1))O 1M(7 "7M++ +M%-C -`Q!] Q!h F 	))--1R !&#(Rb/!"RR c]R 	R
 R tCH~&R R R &c]R R R !Rt !&#(7
77 7
 7 7 !7U  55 //p	
 #5h  J"4Xy"IhJ5:  N!3K!MM+N  <-eU;M%<sZ    F& G( (G? &G%,GG%GG%GG%$G%(G<;G<?HH