
    c&                     ~    d Z ddlmZ ddlZddlZddlZdZddgZeddfdZ	  G d	 de	      Z
ed
k(  rd Z e        yy)z``jsonutils`` aims to provide various helpers for working with
JSON. Currently it focuses on providing a reliable and intuitive means
of working with `JSON Lines`_-formatted files.

.. _JSON Lines: http://jsonlines.org/

    )print_functionN   JSONLIteratorreverse_iter_linesTc              #     K   	 |xs | j                   }d}| }	 |j                         } d\  }}}|r | j                  dt        j                         |}| j                         }	d|	k  rt        ||	      }
|	|
z  }	| j                  |	t        j                         | j                  |
      }||z   }|j                         }t        |      dk  s|d   |k(  rs|dd |k(  r|r|n| |ddd   D ]  }|r|j                  |      n|  |d   }d|	k  r|r|r|j                  |      n| yy# t        $ r d}Y (w xY w# t        t        j                  f$ r Y 5w xY ww)a;  Returns an iterator over the lines from a file object, in
    reverse order, i.e., last line first, first line last. Uses the
    :meth:`file.seek` method of file objects, and is tested compatible with
    :class:`file` objects, as well as :class:`StringIO.StringIO`.

    Args:
        file_obj (file): An open file object. Note that
            ``reverse_iter_lines`` mutably reads from the file and
            other functions should not mutably interact with the file
            object after being passed. Files can be opened in bytes or
            text mode.
        blocksize (int): The block size to pass to
          :meth:`file.read()`. Warning: keep this a fairly large
          multiple of 2, defaults to 4096.
        preseek (bool): Tells the function whether or not to automatically
            seek to the end of the file. Defaults to ``True``.
            ``preseek=False`` is useful in cases when the
            file cursor is already in position, either at the end of
            the file or in the middle for relative reverse line
            generation.

    zutf-8N)       
 r      )encodingAttributeErrordetachioUnsupportedOperationseekosSEEK_ENDtellminSEEK_SETread
splitlineslendecode)file_obj	blocksizepreseekr   orig_objempty_bytesnewline_bytes
empty_textbuffcur_pos	read_sizecurlineslines                 1lib/python3.12/site-packages/boltons/jsonutils.pyr   r   6   s    00x00
  H??$ .=*K
a%DmmoG
g+	7+	9gr{{+mmI&Tz!u:>U1X49% (*k9%1R%L 	>D+3$++h'=	>Qx g+ '/dkk(#T9 E   B334 sP   E"D0 E"E C+E"E"0D?;E">D??E"EE"EE"c                   F    e Zd ZdZ	 d	dZed        Zd Zd Zd Z	d Z
e
Zy)
r   a  The ``JSONLIterator`` is used to iterate over JSON-encoded objects
    stored in the `JSON Lines format`_ (one object per line).

    Most notably it has the ability to efficiently read from the
    bottom of files, making it very effective for reading in simple
    append-only JSONL use cases. It also has the ability to start from
    anywhere in the file and ignore corrupted lines.

    Args:
        file_obj (file): An open file object.
        ignore_errors (bool): Whether to skip over lines that raise an error on
            deserialization (:func:`json.loads`).
        reverse (bool): Controls the direction of the iteration.
            Defaults to ``False``. If set to ``True`` and *rel_seek*
            is unset, seeks to the end of the file before iteration
            begins.
        rel_seek (float): Used to preseek the start position of
            iteration. Set to 0.0 for the start of the file, 1.0 for the
            end, and anything in between.

    .. _JSON Lines format: http://jsonlines.org/
    Nc                 t   t        |      | _        || _        || _        ||r(d}n%d|cxk  rdk  sn t	        d|z        |dk  rd|z
  }|| _        d| _        || j                          | j                  r(t        | j                  | j                  d      | _	        y t        | j                        | _	        y )N      ?g      z8'rel_seek' expected a float between -1.0 and 1.0, not %rr   r   F)r   r   )bool_reverse	_file_objignore_errors
ValueError	_rel_seek
_blocksize_init_rel_seekr   
_line_iteriter)selfr   r0   reverserel_seeks        r)   __init__zJSONLIterator.__init__   s    W!*&3& 57?@ A A\X~H!!==0;???9>@DO #4>>2DOr   c                 6    | j                   j                         S )zBA property representing where in the file the iterator is reading.)r/   r   r7   s    r)   cur_byte_poszJSONLIterator.cur_byte_pos   s     ~~""$$r   c                    | j                   | j                  }}d\  }}|j                         }d|vr|j                  |      }||z  }d|vr	 |j	                  d      |z   |z
  }|j                  ||z          y# t
        $ r  w xY w)z6Aligns the file object's position to the next newline.)r
   r   
N)r/   r3   r   r   indexr1   r   )r7   fobsizer&   
total_readr$   newline_offsets          r)   _align_to_newlinezJSONLIterator._align_to_newline   s    NNDOOEZ'')#o''%.C%J #o	 YYt_z9EAN 	.()  		s   A: :Bc                    | j                   | j                  }}|dk(  r!|j                  dt        j                         y|j                  dt        j
                         |j                         }|dk(  r|| _        yt        ||z        }|j                  |t        j                         | j                          |j                         | _        y)zCSets the file object's position to the relative location set above.g        r   r,   N)
r2   r/   r   r   r   r   r   _cur_posintrE   )r7   rsrA   sizetargets        r)   r4   zJSONLIterator._init_rel_seek   s    B9GGAr{{#GGAr{{#779DSy $TBY,&&( "	r   c                     | S )N r<   s    r)   __iter__zJSONLIterator.__iter__   s    r   c                     	 t        | j                        j                         }|s'	 t        j                  |      }|S # t
        $ r | j                  s Y Ww xY w)zYields one :class:`dict` loaded with :func:`json.loads`, advancing
        the file object by one line. Raises :exc:`StopIteration` upon reaching
        the end of the file (or beginning, if ``reverse`` was set to ``True``.
        )nextr5   lstripjsonloads	Exceptionr0   )r7   r(   objs      r)   rP   zJSONLIterator.next   s^    
 (//1Djj&
 J	  ))s   A   AA)FFN)__name__
__module____qualname____doc__r:   propertyr=   rE   r4   rN   rP   __next__rM   r   r)   r   r   ~   sA    . ?C32 % %** " Hr   __main__c                     dd l } d| j                  v sd| j                  v rt        d       y d}d| j                  v sd| j                  v rd}d	\  }}| j                  d
d  }|D ]  }|dv r|d
z  }t        |d      5 }t	        |      }d}	 	 t        |       |d
z  }|d
z  }|rH|rF|dz  dk(  r>| j                  j                  d       |dz  r| j                  j                  d|z         a |rt        d|z         t        d|z         y # t        $ r- t        d|d
z   d|j                  d|       Y d d d         y t        $ r Y nw xY w	 d d d        # 1 sw Y   xY w)Nr   -h--helpz7loads one or more JSON Line files for basic validation.F-v	--verboseT)r   r      )r^   r_   r`   ra   rbzerror reading object #z around byte z in d   .i'  z%s
zfiles checked: %szobjects loaded: %s)sysargvprintopenr   rP   r1   r=   StopIterationstdoutwrite)	rf   verbose
file_count	obj_count	filenamesfilenamer   iteratorcur_obj_counts	            r)   _mainrt      s   388x3883KL388{chh6G $
IHHQRL	! 	AH>>!OJh% A(2 !X NI!Q&M9SA1E

((-$u,JJ,,Vi-?@ 	A, %
23&23 & !.!2H4I4I8U VA A ) A A As=   <EDAE*E6EE	EEEE"	)rY   
__future__r   r   r   rR   DEFAULT_BLOCKSIZE__all__r   objectr   rV   rt   rM   r   r)   <module>ry      so   B & 	 	    0
1 ,=dUY =:BgF gT z#J 
GM r   