a
    d&                     @   sn   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dZG d	d de	Z
ed
krjdd Ze  dS )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              	   c   sN  z|p
| j }W n ty$   d}Y n0 d}| }z| } W n ttjfyR   Y n0 d\}}}|rp| dtj |}|  }	d|	k r.t	||	}
|	|
8 }	| |	tj
 | |
}|| }| }t|dk s||d |krq||dd |kr|r|n|V  |ddd D ]}|r||n|V  q|d }q||rJ|rD||n|V  dS )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.

    Nzutf-8)       
 r      )encodingAttributeErrordetachioUnsupportedOperationseekosSEEK_ENDtellminSEEK_SETread
splitlineslendecode)file_obj	blocksizepreseekr   Zorig_objZempty_bytesZnewline_bytesZ
empty_textZbuffcur_pos	read_sizecurlinesline r"   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/boltons/jsonutils.pyr   6   s>    





c                   @   sJ   e Zd ZdZdddZedd Zdd	 Zd
d Zdd Z	dd Z
e
ZdS )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/
    FNc                 C   s   t || _|| _|| _|d u r(|r\d}n4d|  k r<dk sLn td| n|dk r\d| }|| _d| _|d urx|   | jrt| j| jdd| _	nt
| j| _	d S )N      ?g      z8'rel_seek' expected a float between -1.0 and 1.0, not %rr   r   F)r   r   )boolZ_reverse	_file_objignore_errors
ValueError	_rel_seek
_blocksize_init_rel_seekr   
_line_iteriter)selfr   r'   reverseZrel_seekr"   r"   r#   __init__   s,    

zJSONLIterator.__init__c                 C   s
   | j  S )zBA property representing where in the file the iterator is reading.)r&   r   r.   r"   r"   r#   cur_byte_pos   s    zJSONLIterator.cur_byte_posc                 C   sx   | j | j }}d\}}| }d|vr:||}||7 }qz|d| | }W n tyd    Y n0 |||  dS )z6Aligns the file object's position to the next newline.)r   r   
N)r&   r*   r   r   indexr(   r   )r.   foZbsizer   Z
total_readr   Znewline_offsetr"   r"   r#   _align_to_newline   s    

zJSONLIterator._align_to_newlinec                 C   s|   | j | j }}|dkr&|dtj nR|dtj | }|dkrL|| _n,t|| }||tj | 	  | | _dS )zCSets the file object's position to the relative location set above.g        r   r$   N)
r)   r&   r   r   r   r   r   Z_cur_posintr6   )r.   rsr5   sizetargetr"   r"   r#   r+      s    zJSONLIterator._init_rel_seekc                 C   s   | S )Nr"   r1   r"   r"   r#   __iter__   s    zJSONLIterator.__iter__c                 C   sF   t | j }|sq zt|}W n ty@   | js8 Y q Y n0 |S )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``.
        N)nextr,   lstripjsonloads	Exceptionr'   )r.   r!   objr"   r"   r#   r<      s    
zJSONLIterator.next)FFN)__name__
__module____qualname____doc__r0   propertyr2   r6   r+   r;   r<   __next__r"   r"   r"   r#   r   ~   s    

__main__c            	   
   C   sz  dd l } d| jv sd| jv r(td d S d}d| jv s@d| jv rDd}d	\}}| jd
d  }|D  ]}|dv rnq^|d
7 }t|d}t|}d}zt| W nP ty   td|d
 |j|f  Y W d     d S  ty   Y q6Y n0 |d
7 }|d
7 }|r|r|d dkr| j	
d |d r| j	
d|  qW d    q^1 sL0    Y  q^|rvtd|  td|  d S )Nr   -h--helpz7loads one or more JSON Line files for basic validation.F-v	--verboseT)r   r      )rI   rJ   rK   rL   rbz-error reading object #%s around byte %s in %sd   .i'  z%s
zfiles checked: %szobjects loaded: %s)sysargvprintopenr   r<   r(   r2   StopIterationstdoutwrite)	rQ   verboseZ
file_countZ	obj_count	filenamesfilenamer   iteratorZcur_obj_countr"   r"   r#   _main   sF    
4r\   )rE   
__future__r   r   r   r>   ZDEFAULT_BLOCKSIZE__all__r   objectr   rB   r\   r"   r"   r"   r#   <module>!   s   Hj%