U
    Â^ZD&  ã                   @   s  d Z ddlmZ dZdZdZdZzddlmZ e	Z
W n ek
rL   eZY nX zddlmZmZ W n$ ek
r†   ddlmZmZ Y nX dd	lmZ dd
lZdd
l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) é    )Úunicode_literalsu    Stefan KÃ¶gl <stefan@skoegl.net>z2.0z2https://github.com/stefankoegl/python-json-pointerzModified BSD License)Úizip)ÚMappingÚSequence)ÚteeNTc                 C   s   t |ƒ}| | ||¡S )a¼  Resolves 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   ú-/usr/lib/python3/dist-packages/jsonpointer.pyÚset_pointer?   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_pointerY   s    $r   c                 C   s$   t | ƒ\}}|D ]} qq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   r   )ÚiterableÚaÚbÚ_r   r   r   Úpairwise�   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__œ   s    zEndOfList.__init__c                 C   s   dj | jjt| jƒd�S )Nz{cls}({lst}))ÚclsZ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dd„ Zdd„ Zdd„ Zdd„ Ze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 an JSON documentz0|[1-9][0-9]*$z(~[^01]|~$)c                 C   sZ   | j  |¡}|r"td | ¡ ¡ƒ‚| d¡}| d¡dkrBtdƒ‚dd„ |D ƒ}|| _d S )NzFound invalid escape {}ú/r   Ú zlocation must starts with /c                 S   s   g | ]}t |ƒ‘qS r   )Úunescape©Ú.0Úpartr   r   r   Ú
<listcomp>¸   s     z(JsonPointer.__init__.<locals>.<listcomp>)Ú_RE_INVALID_ESCAPEÚsearchr   r"   ÚgroupÚsplitÚpopÚparts)r   r
   Zinvalid_escaper4   r   r   r   r    ¬   s    ÿ
zJsonPointer.__init__c                 C   sD   | j s|dfS | j dd… D ]}|  ||¡}q||  || j d ¡fS )z>Resolves ptr until the last step, returns (sub-doc, last-step)Néÿÿÿÿ)r4   ÚwalkÚget_part©r   r	   r-   r   r   r   Úto_last»   s
    zJsonPointer.to_lastc              	   C   sN   | j D ]B}z|  ||¡}W q tk
rF   |tkr6‚ n| Y   S Y qX q|S )zBResolves the pointer against doc and returns the referenced object)r4   r6   r   Ú_nothing)r   r	   r   r-   r   r   r   r   Æ   s    
zJsonPointer.resolveTc                 C   sF   t | jƒdkr|rtdƒ‚|S |s,t |¡}|  |¡\}}|||< |S )zFResolve the pointer against the doc and replace the target with value.r   zcannot set root in place)Úlenr4   r   ÚcopyÚdeepcopyr9   )r   r	   r   r   Úparentr-   r   r   r   r   ×   s    
zJsonPointer.setc                 C   sj   t |tƒr|S t |tƒrH|dkr$|S | j t|ƒ¡s@td| ƒ‚t|ƒS t|dƒrV|S tdt	|ƒ ƒ‚dS )z)Returns the next step in the correct typeú-z"'%s' is not a valid sequence indexÚ__getitem__zXDocument '%s' does not support indexing, must be mapping/sequence or support __getitem__N)
Ú
isinstancer   r   Ú_RE_ARRAY_INDEXÚmatchÚstrr   ÚintÚhasattrÚtyper8   r   r   r   r7   ç   s    


ÿzJsonPointer.get_partc                 C   s¤   |   ||¡}t|dƒs(tdt|ƒf ƒ‚t|tƒrp|dkrBt|ƒS z
|| W S  tk
rn   td|f ƒ‚Y nX z
|| W S  t	k
rž   td||f ƒ‚Y nX dS )z7 Walks one step in doc and returns the referenced part r@   zinvalid document type %sr?   zindex '%s' is out of boundszmember '%s' not found in %sN)
r7   rF   ÚAssertionErrorrG   rA   r   r   Ú
IndexErrorr   ÚKeyErrorr8   r   r   r   r6     s    


zJsonPointer.walkc                 C   s   | j dt|j ƒ… |j kS )ú- Returns True if self contains the given ptr N)r4   r;   )r   Úptrr   r   r   Úcontains  s    zJsonPointer.containsc                 C   s
   |   |¡S )rK   )rM   )r   Úitemr   r   r   Ú__contains__  s    zJsonPointer.__contains__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   s   g | ]}t |ƒ‘qS r   )Úescaper+   r   r   r   r.   (  s     z$JsonPointer.path.<locals>.<listcomp>r)   c                 s   s   | ]}d | V  qdS ©r(   Nr   r+   r   r   r   Ú	<genexpr>)  s     z#JsonPointer.path.<locals>.<genexpr>)r4   Újoin)r   r4   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)rA   r   r4   )r   Úotherr   r   r   Ú__eq__+  s    
zJsonPointer.__eq__c                 C   s   t t| jƒƒS r   )ÚhashÚtupler4   r%   r   r   r   Ú__hash__8  s    zJsonPointer.__hash__c                 C   s*   dd„ |D ƒ}| d  dd„ |D ƒ¡ƒ}|S )zœConstructs 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   )rP   rD   r+   r   r   r   r.   B  s     z*JsonPointer.from_parts.<locals>.<listcomp>r)   c                 s   s   | ]}d | V  qdS rQ   r   r+   r   r   r   rR   C  s     z)JsonPointer.from_parts.<locals>.<genexpr>)rS   )r!   r4   rL   r   r   r   Ú
from_parts;  s    zJsonPointer.from_partsN)T)r   r   r   r'   ÚreÚcompilerB   r/   r    r9   r:   r   Úgetr   r7   r6   rM   rO   ÚpropertyrT   rV   rY   ÚclassmethodrZ   r   r   r   r   r   ¤   s$   



r   c                 C   s   |   dd¡  dd¡S )Nú~ú~0r(   ú~1©Úreplace©Úsr   r   r   rP   G  s    rP   c                 C   s   |   dd¡  dd¡S )Nrb   r(   ra   r`   rc   re   r   r   r   r*   J  s    r*   )T)r'   Z
__future__r   Ú
__author__Ú__version__Z__website__Z__license__Ú	itertoolsr   ZunicoderD   ÚImportErrorÚzipZcollections.abcr   r   Úcollectionsr   r[   r<   Úobjectr:   r   r   r   Ú	Exceptionr   r   r   rP   r*   r   r   r   r   Ú<module>!   s6   

( $