o
    Ggi)                     @   s   d Z dZdZdZdZddlZddlZddlmZm	Z	 ddl
mZmZ e Zdd
dZefddZdd ZG dd deZG dd deZG dd deZdd Zdd ZdS )z7 Identify specific nodes in a JSON document (RFC 6901) u    Stefan Kögl <stefan@skoegl.net>z3.0.0z2https://github.com/stefankoegl/python-json-pointerzModified BSD License    N)MappingSequence)teechainTc                 C   s   t |}|| ||S )a  Resolves a pointer against doc and sets the value of the target within doc.

    With inplace set to true, doc is modified as long as pointer is not the
    root.

    >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}}

    >>> set_pointer(obj, '/foo/anArray/0/prop', 55) ==     {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
    True

    >>> set_pointer(obj, '/foo/yet another prop', 'added prop') ==     {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}}
    True

    >>> obj = {'foo': {}}
    >>> set_pointer(obj, '/foo/a%20b', 'x') ==     {'foo': {'a%20b': 'x' }}
    True
    )JsonPointerset)docpointervalueinplace r   N/var/www/html/AyurvedaChatbot/venv/lib/python3.10/site-packages/jsonpointer.pyset_pointer1   s   r   c                 C   s   t |}|| |S )aa   Resolves pointer against doc and returns the referenced object

    >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}, 'a%20b': 1, 'c d': 2}

    >>> resolve_pointer(obj, '') == obj
    True

    >>> resolve_pointer(obj, '/foo') == obj['foo']
    True

    >>> resolve_pointer(obj, '/foo/another prop') == obj['foo']['another prop']
    True

    >>> resolve_pointer(obj, '/foo/another prop/baz') == obj['foo']['another prop']['baz']
    True

    >>> resolve_pointer(obj, '/foo/anArray/0') == obj['foo']['anArray'][0]
    True

    >>> resolve_pointer(obj, '/some/path', None) == None
    True

    >>> resolve_pointer(obj, '/a b', None) == None
    True

    >>> resolve_pointer(obj, '/a%20b') == 1
    True

    >>> resolve_pointer(obj, '/c d') == 2
    True

    >>> resolve_pointer(obj, '/c%20d', None) == None
    True
    )r   resolve)r   r	   defaultr   r   r   resolve_pointerK   s   $r   c                 C   s    t | \}}|D ]} t||S )z Transforms a list to a list of tuples of adjacent items

    s -> (s0,s1), (s1,s2), (s2, s3), ...

    >>> list(pairwise([]))
    []

    >>> list(pairwise([1]))
    []

    >>> list(pairwise([1, 2, 3, 4]))
    [(1, 2), (2, 3), (3, 4)]
    )r   zip)iterableab_r   r   r   pairwises   s   
r   c                   @   s   e Zd ZdS )JsonPointerExceptionN)__name__
__module____qualname__r   r   r   r   r      s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )	EndOfListz)Result of accessing element "-" of a listc                 C   s
   || _ d S N)list_)selfr   r   r   r   __init__      
zEndOfList.__init__c                 C   s   dj | jjt| jdS )Nz{cls}({lst}))clslst)format	__class__r   reprr   r   r   r   r   __repr__   s   
zEndOfList.__repr__N)r   r   r   __doc__r    r(   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZedZedZdd Zdd Z	e
fdd	ZeZd(ddZedd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zdd Zdd  Zd!d" Zd#d$ Zed%d& Zd'S ))r   z:A JSON Pointer that can reference parts of a JSON documentz0|[1-9][0-9]*$z(~[^01]|~$)c                 C   sZ   | j |}|rtd| |d}|ddkr!tddd |D }|| _d S )NzFound invalid escape {}/r    zLocation must start with /c                 S      g | ]}t |qS r   )unescape.0partr   r   r   
<listcomp>       z(JsonPointer.__init__.<locals>.<listcomp>)_RE_INVALID_ESCAPEsearchr   r$   groupsplitpopparts)r   r	   invalid_escaper8   r   r   r   r       s   

zJsonPointer.__init__c                 C   sD   | j s|dfS | j dd D ]}| ||}q|t|| j d fS )z>Resolves ptr until the last step, returns (sub-doc, last-step)N)r8   walkr   get_partr   r   r0   r   r   r   to_last   s
   zJsonPointer.to_lastc              	   C   sD   | j D ]}z| ||}W q ty   |tu r | Y   S w |S )zBResolves the pointer against doc and returns the referenced object)r8   r;   r   _nothing)r   r   r   r0   r   r   r   r      s   
zJsonPointer.resolveTc                 C   sf   t | jdkr|rtd|S |st|}| |\}}t|tr-|dkr-|| |S |||< |S )zFResolve the pointer against the doc and replace the target with value.r   zCannot set root in place-)	lenr8   r   copydeepcopyr>   
isinstancer   append)r   r   r
   r   parentr0   r   r   r   r      s   

zJsonPointer.setc                 C   sf   t |tr|S t |tr$|dkr|S tjt|s td| t|S t	|dr+|S tdt
| )z)Returns the next step in the correct typer@   z"'%s' is not a valid sequence index__getitem__zXDocument '%s' does not support indexing, must be mapping/sequence or support __getitem__)rD   r   r   r   _RE_ARRAY_INDEXmatchstrr   inthasattrtype)r"   r   r0   r   r   r   r<      s   


zJsonPointer.get_partc                 C      | j S )zYReturns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b'])r8   r'   r   r   r   	get_parts   s   zJsonPointer.get_partsc                 C   s   t ||}t|dsJ dt|f t|tr5|dkr!t|S z|| W S  ty4   td|f w z|| W S  t	yI   td||f w )z7 Walks one step in doc and returns the referenced part rG   zinvalid document type %sr@   zindex '%s' is out of boundszmember '%s' not found in %s)
r   r<   rL   rM   rD   r   r   
IndexErrorr   KeyErrorr=   r   r   r   r;      s   


zJsonPointer.walkc                 C   s   | j dt|j  |j kS )- Returns True if self contains the given ptr N)r8   rA   )r   ptrr   r   r   contains  s   zJsonPointer.containsc                 C   
   |  |S )rR   )rT   )r   itemr   r   r   __contains__  s   
zJsonPointer.__contains__c                 C   sP   t |tr	|j}nt |trt|j}n|}z
tt| j|W S    td)zD Returns a new JsonPointer with the given suffix append to this ptr zInvalid suffix)rD   r   r8   rJ   
from_partsr   r   )r   suffixsuffix_partsr   r   r   join  s   

zJsonPointer.joinc                 C   rU   r   r[   )r   rY   r   r   r   __truediv__)  r!   zJsonPointer.__truediv__c                 C   s$   dd | j D }ddd |D S )zwReturns the string representation of the pointer

        >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
        c                 S   r,   r   )escaper.   r   r   r   r1   2  r2   z$JsonPointer.path.<locals>.<listcomp>r+   c                 s       | ]}d | V  qdS r*   Nr   r.   r   r   r   	<genexpr>3      z#JsonPointer.path.<locals>.<genexpr>)r8   r[   )r   r8   r   r   r   path,  s   zJsonPointer.pathc                 C   s   t |tsdS | j|jkS )a  Compares a pointer to another object

        Pointers can be compared by comparing their strings (or splitted
        strings), because no two different parts can point to the same
        structure in an object (eg no different number representations)
        F)rD   r   r8   )r   otherr   r   r   __eq__5  s   
zJsonPointer.__eq__c                 C   s   t t| jS r   )hashtupler8   r'   r   r   r   __hash__B  s   zJsonPointer.__hash__c                 C   rN   r   )rc   r'   r   r   r   __str__E  s   zJsonPointer.__str__c                 C   s   t | jd t| j d S )N())rM   r   r&   rc   r'   r   r   r   r(   H  s   zJsonPointer.__repr__c                 C   s*   dd |D }| d dd |D }|S )zConstructs a JsonPointer from a list of (unescaped) paths

        >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0'
        True
        c                 S   s   g | ]}t t|qS r   )r^   rJ   r.   r   r   r   r1   R  s    z*JsonPointer.from_parts.<locals>.<listcomp>r+   c                 s   r_   r`   r   r.   r   r   r   ra   S  rb   z)JsonPointer.from_parts.<locals>.<genexpr>r\   )r"   r8   rS   r   r   r   rX   K  s   zJsonPointer.from_partsNT)r   r   r   r)   recompilerH   r3   r    r>   r?   r   getr   classmethodr<   rO   r;   rT   rW   r[   r]   propertyrc   re   rh   ri   r(   rX   r   r   r   r   r      s2    




r   c                 C      |  dd ddS )N~~0r*   ~1replacesr   r   r   r^   W     r^   c                 C   rr   )Nru   r*   rt   rs   rv   rx   r   r   r   r-   [  rz   r-   rl   )r)   
__author____version____website____license__rB   rm   collections.abcr   r   	itertoolsr   r   objectr?   r   r   r   	Exceptionr   r   r   r^   r-   r   r   r   r   <module>   s&    
( B