
    Vi&                         S r SSKJr  SSKrSSKrSSKrSrSS/r\SS4S jr  " S	 S\	5      r
\S
:X  a  S r\" 5         gg)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              #     #     U=(       d    U R                   nSnU n UR                  5       n Su  pVnU(       a   U R                  S[        R                  5        UnU R                  5       n	SU	:  a  [        X5      n
X-  n	U R                  U	[        R                  5        U R                  U
5      nX-   nUR                  5       n[        U5      S:  d	  US   U:X  a  Mt  USS U:X  a  U(       a  UOUv   USSS2    H  nU(       a  UR                  U5      OUv   M!     US   nSU	:  a  M  U(       a  U(       a  UR                  U5      OUv   gg! [         a    Sn GNGf = f! [        [        R                  4 a     GNUf = f7f)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                 d/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/boltons/jsonutils.pyr   r   6   sq    00x00
  H??$ .=*K
a%DmmoG
g+	+	gr{{+mmI&z!u:>U1X49% (*k9%1R%LD+3$++h'= !Qx g+ '/dkk(#T9 E   B334 sP   FE FE& C>F.%FE#F"E##F&F FFFc                   P    \ rS rSrSr SS jr\S 5       rS rS r	S r
S	 r\rS
rg)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                    [        U5      U l        Xl        X l        Uc
  U(       a  SnO(SUs=:  a  S:  d  O  [	        SU-  5      eUS:  a  SU-
  nX@l        SU l        Ub  U R                  5         U R                  (       a%  [        U R                  U R                  SS9U l	        g [        U R                  5      U l	        g )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   r1   reverserel_seeks        r)   __init__JSONLIterator.__init__   s    W!*&3& 57?@ A A\X~H!!==0;???9>@DO #4>>2DOr   c                 6    U R                   R                  5       $ )zBA property representing where in the file the iterator is reading.)r0   r   r8   s    r)   cur_byte_posJSONLIterator.cur_byte_pos   s     ~~""$$r   c                    U R                   U R                  p!Su  p4UR                  5       nSU;  a  UR                  U5      nXB-  nSU;  a  M   UR	                  S5      U-   U-
  nUR                  XV-   5        g! [
         a    e f = f)z6Aligns the file object's position to the next newline.)r
   r   
N)r0   r4   r   r   indexr2   r   )r8   fobsizer&   
total_readr$   newline_offsets          r)   _align_to_newlineJSONLIterator._align_to_newline   s    NNDOOE'')#o''%.CJ #o	 YYt_z9EAN 	()  		s   A; ;Bc                    U R                   U R                  p!US:X  a!  UR                  S[        R                  5        gUR                  S[        R
                  5        UR                  5       nUS:X  a  X0l        g[        X1-  5      nUR                  U[        R                  5        U R                  5         UR                  5       U l        g)zCSets the file object's position to the relative location set above.g        r   r-   N)
r3   r0   r   r   r   r   r   _cur_posintrH   )r8   rsrD   sizetargets        r)   r5   JSONLIterator._init_rel_seek   s    B9GGAr{{#GGAr{{#779DSy $TY,&&( "	r   c                     U $ )N r>   s    r)   __iter__JSONLIterator.__iter__   s    r   c                      [        U R                  5      R                  5       nU(       d  M-   [        R                  " U5      nU$ ! [
         a    U R                  (       d  e  Me  f = f)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``.
)nextr6   lstripjsonloads	Exceptionr1   )r8   r(   objs      r)   rV   JSONLIterator.next   s^    
 (//1Djj&
 J	  ))s   A A'&A')r4   rK   r0   r6   r3   r/   r1   )FFN)__name__
__module____qualname____firstlineno____doc__r;   propertyr?   rH   r5   rS   rV   __next____static_attributes__rR   r   r)   r   r   ~   sA    . ?C32 % %** " Hr   __main__c                  &   SS K n SU R                  ;   d  SU R                  ;   a  [        S5        g SnSU R                  ;   d  SU R                  ;   a  SnS	u  p#U R                  S
S  nU H  nUS;   a  M  US
-  n[        US5       n[	        U5      nSn  [        U5        US
-  nUS
-  nU(       aS  U(       aL  US-  S:X  aC  U R                  R                  S5        US-  (       a  U R                  R                  SU-  5        Mr     U(       a  [        SU-  5        [        SU-  5        g ! [         a0    [        SUS
-   < SUR                  < SU< 35         S S S 5          g [         a     Of = fS S S 5        GM  ! , (       d  f       GM&  = f)Nr   -h--helpz7loads one or more JSON Line files for basic validation.F-v	--verboseT)r   r      )rg   rh   ri   rj   rbzerror reading object #z around byte z in d   .i'  z%s
zfiles checked: %szobjects loaded: %s)sysargvprintopenr   rV   r2   r?   StopIterationstdoutwrite)	ro   verbose
file_count	obj_count	filenamesfilenamer   iteratorcur_obj_counts	            r)   _mainr}      s   388x3883KL388{chh6G $
HHQRL	!H>>!OJh%(2 !X NI!Q&M9SA1E

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