3
1V$                 @   s  d dl mZ yd dlmZmZ W n$ ek
rD   d dlmZmZ Y nX dZdZdZ	dZ
y d dlmZ d dlmZ eZW n$ ek
r   d dlmZ eZY nX d d	lmZ d d
lZd d
lZejdZG dd deZG dd deZe ZefddZdddZG dd deZdd Z d
S )    )unicode_literals)MappingSequenceu    Stefan Kögl <stefan@skoegl.net>z1.10z2https://github.com/stefankoegl/python-json-pointerzModified BSD License)unquote)izip)teeNz0|[1-9][0-9]*$c               @   s   e Zd ZdS )JsonPointerExceptionN)__name__
__module____qualname__ r   r   -/usr/lib/python3/dist-packages/jsonpointer.pyr   B   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 list c             C   s
   || _ d S )N)list_)selfr   r   r   r   __init__I   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__M   s    
zEndOfList.__repr__N)r	   r
   r   __doc__r   r   r   r   r   r   r   F   s   r   c             C   s   t |}|j| |S )an  
    Resolves pointer against doc and returns the referenced object

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

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

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

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

    >>> resolve_pointer(obj, '/foo/another%20prop/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

    )JsonPointerresolve)docpointerdefaultr   r   r   resolve_pointerU   s    r   Tc             C   s   t |}|j| ||S )aV  
    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%20another%20prop', 'added prop') ==     {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}}
    True

    )r   set)r   r   valueinplacer   r   r   set_pointerr   s    r!   c               @   s   e Zd Z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 document c             C   sP   |j d}|jddkr tdtt|}dd |D }dd |D }|| _d S )N/r    zlocation must starts with /c             S   s   g | ]}|j d dqS )z~1r"   )replace).0partr   r   r   
<listcomp>   s    z(JsonPointer.__init__.<locals>.<listcomp>c             S   s   g | ]}|j d dqS )z~0~)r$   )r%   r&   r   r   r   r'      s    )splitpopr   mapr   parts)r   r   r,   r   r   r   r      s    

zJsonPointer.__init__c             C   sH   | j s|dfS x"| j dd D ]}| j||}qW || j|| j d fS )z@ Resolves ptr until the last step, returns (sub-doc, last-step) N   r.   )r,   walkget_part)r   r   r&   r   r   r   to_last   s
    zJsonPointer.to_lastc             C   sJ   xD| j D ]:}y| j||}W q tk
r@   |tkr8 n|S Y qX qW |S )zBResolves the pointer against doc and returns the referenced object)r,   r/   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j|}| j|\}}|||< |S )zH Resolve the pointer against the doc and replace the target with value. r   zcannot set root in place)lenr,   r   copydeepcopyr1   )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 tjt|s@td|f 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 list index__getitem__zQDocument '%s' does not support indexing, must be dict/list or support __getitem__N)

isinstancer   r   RE_ARRAY_INDEXmatchstrr   inthasattrtype)r   r   r&   r   r   r   r0      s    


zJsonPointer.get_partc             C   s   | j ||}t|ttfks8t|ds8tdt|f t|trry|| S  tk
rn   t	d||f Y qX nPt|t
r|dkrt|S y|| S  tk
r   t	d|f Y qX n|| S dS )z7 Walks one step in doc and returns the referenced part r8   zinvalid document type %szmember '%s' not found in %sr7   zindex '%s' is out of boundsN)r0   r?   dictlistr>   AssertionErrorr9   r   KeyErrorr   r   r   
IndexError)r   r   r&   r   r   r   r/      s    ,

zJsonPointer.walkc             C   s   | j dt|j  |j kS )z+Returns True if self contains the given ptrN)r,   r3   )r   ptrr   r   r   contains   s    zJsonPointer.containsc             C   s
   | j |S )z+Returns True if self contains the given ptr)rF   )r   itemr   r   r   __contains__   s    zJsonPointer.__contains__c             C   s2   dd | j D }dd |D }djdd |D S )zx Returns the string representation of the pointer

        >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
        c             S   s   g | ]}|j d dqS )r(   z~0)r$   )r%   r&   r   r   r   r'   	  s    z$JsonPointer.path.<locals>.<listcomp>c             S   s   g | ]}|j d dqS )r"   z~1)r$   )r%   r&   r   r   r   r'   
  s    r#   c             s   s   | ]}d | V  qdS )r"   Nr   )r%   r&   r   r   r   	<genexpr>  s    z#JsonPointer.path.<locals>.<genexpr>)r,   join)r   r,   r   r   r   path  s    zJsonPointer.pathc             C   s   t |tsdS | j|jkS )z 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)r9   r   r,   )r   otherr   r   r   __eq__  s    
zJsonPointer.__eq__c             C   s   t t| jS )N)hashtupler,   )r   r   r   r   __hash__  s    zJsonPointer.__hash__c             C   sF   dd |D }dd |D }dd |D }| dj 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 |qS r   )r<   )r%   r&   r   r   r   r'   $  s    z*JsonPointer.from_parts.<locals>.<listcomp>c             S   s   g | ]}|j d dqS )r(   z~0)r$   )r%   r&   r   r   r   r'   %  s    c             S   s   g | ]}|j d dqS )r"   z~1)r$   )r%   r&   r   r   r   r'   &  s    r#   c             s   s   | ]}d | V  qdS )r"   Nr   )r%   r&   r   r   r   rI   '  s    z)JsonPointer.from_parts.<locals>.<genexpr>)rJ   )r   r,   rE   r   r   r   
from_parts  s
    zJsonPointer.from_partsN)T)r	   r
   r   r   r   r1   r2   r   getr   r0   r/   rF   rH   propertyrK   rM   rP   classmethodrQ   r   r   r   r   r      s   

r   c             C   s&   t | \}}x|D ]}P qW t||S )z 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    
rY   )T)!Z
__future__r   collections.abcr   r   ImportErrorcollections
__author____version__Z__website__Z__license__Zurllibr   	itertoolsr   Zunicoder<   Zurllib.parsezipr   rer4   compiler:   	Exceptionr   objectr   r2   r   r!   r   rY   r   r   r   r   <module>!   s6   


 $