U
    [ep                     @   s  d Z ddlmZ ddlZddlZddlZddlZddlZzddlm	Z	 W n e
k
r`   eZ	Y nX ddlmZmZ dZdZzddlmZmZ W n( e
k
r   ddlmZmZ eZY nX dZd	Zd
ZdZejdkreefZG dd deZG dd deZG dd deZ G dd dee!Z"dd Z#ej$ej%e#dZ&defddZ'efddZ(G dd de)Z*G dd  d e*Z+G d!d" d"e*Z,G d#d$ d$e*Z-G d%d& d&e*Z.G d'd( d(e*Z/G d)d* d*e*Z0G d+d, d,e)Z1G d-d. d.e)Z2d/d0 Z3dS )1z Apply JSON-Patches (RFC 6902)     )unicode_literalsN)MappingProxyType)JsonPointerJsonPointerException   )MutableMappingMutableSequenceu    Stefan Kögl <stefan@skoegl.net>z1.32z0https://github.com/stefankoegl/python-json-patchzModified BSD License)   r   c                   @   s   e Zd ZdZdS )JsonPatchExceptionzBase Json Patch exceptionN__name__
__module____qualname____doc__ r   r   K/var/www/html/services/stratfitenv/lib/python3.8/site-packages/jsonpatch.pyr
   J   s   r
   c                   @   s   e Zd ZdZdS )InvalidJsonPatchz, Raised if an invalid JSON Patch is created Nr   r   r   r   r   r   N   s   r   c                   @   s   e Zd ZdZdS )JsonPatchConflicta
  Raised if patch could not be applied due to conflict situation such as:
    - attempt to add object key when it already exists;
    - attempt to operate with nonexistence object key;
    - attempt to insert value to array at position beyond its size;
    - etc.
    Nr   r   r   r   r   r   R   s   r   c                   @   s   e Zd ZdZdS )JsonPatchTestFailedz A Test operation failed Nr   r   r   r   r   r   [   s   r   c                 C   s<   t t}| D ]\}}|| | qtdd | D S )z'Convert duplicate keys values to lists.c                 s   s.   | ]&\}}|t |d kr |d n|fV  qdS )r   r   N)len).0keyvaluesr   r   r   	<genexpr>f   s   zmultidict.<locals>.<genexpr>)collectionsdefaultdictlistappenddictitems)Zordered_pairsZmdictr   valuer   r   r   	multidict_   s    
r!   )object_pairs_hookFc                 C   s2   t |trtj||d}nt||d}|| |S )a  Apply list of patches to specified json document.

    :param doc: Document object.
    :type doc: dict

    :param patch: JSON patch as list of dicts or raw JSON-encoded string.
    :type patch: list or str

    :param in_place: While :const:`True` patch will modify target document.
                     By default patch will be applied to document copy.
    :type in_place: bool

    :param pointer_cls: JSON pointer class to use.
    :type pointer_cls: Type[JsonPointer]

    :return: Patched document object.
    :rtype: dict

    >>> doc = {'foo': 'bar'}
    >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}]
    >>> other = apply_patch(doc, patch)
    >>> doc is not other
    True
    >>> other == {'foo': 'bar', 'baz': 'qux'}
    True
    >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}]
    >>> apply_patch(doc, patch, in_place=True) == {'foo': 'bar', 'baz': 'qux'}
    True
    >>> doc == other
    True
    pointer_cls)
isinstance
basestring	JsonPatchfrom_stringapply)docpatchin_placer$   r   r   r   apply_patchr   s    !
r-   c                 C   s   t j| ||dS )a!  Generates patch by comparing two document objects. Actually is
    a proxy to :meth:`JsonPatch.from_diff` method.

    :param src: Data source document object.
    :type src: dict

    :param dst: Data source document object.
    :type dst: dict

    :param pointer_cls: JSON pointer class to use.
    :type pointer_cls: Type[JsonPointer]

    >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
    >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
    >>> patch = make_patch(src, dst)
    >>> new = patch.apply(src)
    >>> new == dst
    True
    r#   )r'   	from_diff)srcdstr$   r   r   r   
make_patch   s    r1   c                   @   sb   e Zd ZdZefddZdd Zdd Zdd	 Zd
d Z	e
dd Ze
dd Zejdd ZdS )PatchOperationz'A single operation inside a JSON Patch.c              
   C   s   || _ |dstdt|d | j r@|d j| _|d | _nH|d | _z|  | j| _W n* tk
r } ztdW 5 d }~X Y nX || _d S )Npathz#Operation must have a 'path' memberzInvalid 'path')	r$   __contains__r   r%   r3   locationpointer	TypeError	operation)selfr8   r$   exr   r   r   __init__   s    

zPatchOperation.__init__c                 C   s   t ddS )zGAbstract method that applies a patch operation to the specified object.z%should implement the patch operation.N)NotImplementedError)r9   objr   r   r   r)      s    zPatchOperation.applyc                 C   s   t t| j S N)hash	frozensetr8   r   r9   r   r   r   __hash__   s    zPatchOperation.__hash__c                 C   s   t |tsdS | j|jkS NF)r%   r2   r8   r9   otherr   r   r   __eq__   s    
zPatchOperation.__eq__c                 C   s
   | |k S r>   r   rD   r   r   r   __ne__   s    zPatchOperation.__ne__c                 C   s   d | jjd d S )N/)joinr6   partsrA   r   r   r   r3      s    zPatchOperation.pathc                 C   s8   zt | jjd W S  tk
r2   | jjd  Y S X d S )NrI   )intr6   rK   
ValueErrorrA   r   r   r   r      s    zPatchOperation.keyc                 C   s*   t || jjd< | jj| _| j| jd< d S )NrI   r3   )strr6   rK   r3   r5   r8   )r9   r    r   r   r   r      s    
N)r   r   r   r   r   r;   r)   rB   rF   rG   propertyr3   r   setterr   r   r   r   r2      s   

r2   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RemoveOperationz/Removes an object property or an array element.c              
   C   sX   | j |\}}z
||= W n8 ttfk
rR } zd|}t|W 5 d }~X Y nX |S )Nz(can't remove a non-existent object '{0}')r6   to_lastKeyError
IndexErrorformatr   )r9   r=   subobjpartr:   msgr   r   r   r)      s    

zRemoveOperation.applyc                 C   s0   | j |kr,| j|kr$|  jd7  _n|d8 }|S Nr   r3   r   r9   r3   r   r   r   r   _on_undo_remove   s
    

zRemoveOperation._on_undo_removec                 C   s0   | j |kr,| j|kr$|  jd8  _n|d8 }|S rY   rZ   r[   r   r   r   _on_undo_add   s
    

zRemoveOperation._on_undo_addNr   r   r   r   r)   r\   r]   r   r   r   r   rQ      s   
rQ   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AddOperationz,Adds an object property or an array element.c              
   C   s   z| j d }W n* tk
r8 } ztdW 5 d }~X Y nX | j|\}}t|tr|dkrh|| q|t|ks||dk rt	dq|
|| nPt|tr|d kr|}q|||< n.|d krtdt|nt	d| j||S )Nr    /The operation does not contain a 'value' member-r   zcan't insert outside of listinvalid document type {0}2unable to fully resolve json pointer {0}, part {1})r8   rS   r   r6   rR   r%   r   r   r   r   insertr   r7   rU   typer5   )r9   r=   r    r:   rV   rW   r   r   r   r)   	  s*    



zAddOperation.applyc                 C   s0   | j |kr,| j|kr$|  jd7  _n|d7 }|S rY   rZ   r[   r   r   r   r\   )  s
    

zAddOperation._on_undo_removec                 C   s0   | j |kr,| j|kr$|  jd8  _n|d7 }|S rY   rZ   r[   r   r   r   r]   1  s
    

zAddOperation._on_undo_addNr^   r   r   r   r   r_     s    r_   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ReplaceOperationz?Replaces an object property or an array element by a new value.c              
   C   s   z| j d }W n* tk
r8 } ztdW 5 d }~X Y nX | j|\}}|d krV|S |dkrftdt|tr|t|ks|dk rtdnTt|t	r||krd
|}t|n.|d krtd
t|ntd	
| j||||< |S )
Nr    r`   ra   z7'path' with '-' can't be applied to 'replace' operationr   zcan't replace outside of listz)can't replace a non-existent object '{0}'rb   rc   )r8   rS   r   r6   rR   r%   r   r   r   r   rU   r7   re   r5   )r9   r=   r    r:   rV   rW   rX   r   r   r   r)   =  s.    




zReplaceOperation.applyc                 C   s   |S r>   r   r[   r   r   r   r\   ]  s    z ReplaceOperation._on_undo_removec                 C   s   |S r>   r   r[   r   r   r   r]   `  s    zReplaceOperation._on_undo_addNr^   r   r   r   r   rf   :  s    rf   c                   @   sN   e Zd ZdZdd Zedd Zedd Zejdd Zd	d
 Z	dd Z
dS )MoveOperationz?Moves an object property or an array element to a new location.c              
   C   s  z2t | jd | jr | jd }n| | jd }W n* tk
r\ } ztdW 5 d }~X Y nX ||\}}z|| }W n2 ttfk
r } ztt|W 5 d }~X Y nX | j	|kr|S t |t
r| j	|rtdtd| jd d| jd|}td| j|d| jd|}|S )	Nfrom.The operation does not contain a 'from' memberz*Cannot move values into their own childrenremoveopr3   r#   addrl   r3   r    )r%   r8   r$   rS   r   rR   rT   r   rN   r6   r   containsrQ   r)   r_   r5   r9   r=   from_ptrr:   rV   rW   r    r   r   r   r)   g  sJ    


zMoveOperation.applyc                 C   s$   |  | jd }d|jd d S )Nrh   rH   rI   )r$   r8   rJ   rK   r9   rq   r   r   r   	from_path  s    zMoveOperation.from_pathc                 C   sD   |  | jd }zt|jd W S  tk
r>   |jd  Y S X d S Nrh   rI   )r$   r8   rL   rK   r7   rr   r   r   r   from_key  s
    zMoveOperation.from_keyc                 C   s.   |  | jd }t||jd< |j| jd< d S rt   )r$   r8   rN   rK   r3   )r9   r    rq   r   r   r   ru     s    c                 C   s\   | j |kr,| j|kr$|  jd7  _n|d8 }| j|krX| j|krP|  jd7  _n|d7 }|S rY   rs   ru   r3   r   r[   r   r   r   r\     s    



zMoveOperation._on_undo_removec                 C   s\   | j |kr,| j|kr$|  jd8  _n|d8 }| j|krX| j|krP|  jd8  _n|d7 }|S rY   rv   r[   r   r   r   r]     s    



zMoveOperation._on_undo_addN)r   r   r   r   r)   rO   rs   ru   rP   r\   r]   r   r   r   r   rg   d  s   %


rg   c                   @   s   e Zd ZdZdd ZdS )TestOperationz!Test value by specified location.c              
   C   s   z0| j |\}}|d kr |}n| j ||}W n. tk
r^ } ztt|W 5 d }~X Y nX z| jd }W n* tk
r } ztdW 5 d }~X Y nX ||krd}t|	|t
||t
||S )Nr    r`   z0{0} ({1}) is not equal to tested value {2} ({3}))r6   rR   walkr   r   rN   r8   rS   r   rU   re   )r9   r=   rV   rW   valr:   r    rX   r   r   r   r)     s(     zTestOperation.applyNr   r   r   r   r)   r   r   r   r   rw     s   rw   c                   @   s   e Zd ZdZdd ZdS )CopyOperationzA Copies an object property or an array element to a new location c              
   C   s   z|  | jd }W n* tk
r> } ztdW 5 d }~X Y nX ||\}}zt|| }W n2 ttfk
r } ztt	|W 5 d }~X Y nX t
d| j|d| j d|}|S )Nrh   ri   rm   rn   r#   )r$   r8   rS   r   rR   copydeepcopyrT   r   rN   r_   r5   r)   rp   r   r   r   r)     s*    zCopyOperation.applyNrz   r   r   r   r   r{     s   r{   c                   @   s   e Zd ZeejZeeZe	e
eeeeedZefddZdd Zdd ZeZdd	 Zd
d Zdd Zdd ZedefddZeddefddZdddZedd Zd ddZ dd Z!dS )!r'   )rj   rm   replacemovetestr|   c                 C   s&   || _ || _| j D ]}| | qd S r>   )r+   r$   _get_operation)r9   r+   r$   rl   r   r   r   r;   (  s    
zJsonPatch.__init__c                 C   s   |   S )zstr(self) -> self.to_string())	to_stringrA   r   r   r   __str__3  s    zJsonPatch.__str__c                 C   s
   t | jS r>   )boolr+   rA   r   r   r   __bool__7  s    zJsonPatch.__bool__c                 C   s
   t | jS r>   )iterr+   rA   r   r   r   __iter__<  s    zJsonPatch.__iter__c                 C   s   t t| jS r>   )r?   tuple_opsrA   r   r   r   rB   ?  s    zJsonPatch.__hash__c                 C   s   t |tsdS | j|jkS rC   )r%   r'   r   rD   r   r   r   rF   B  s    
zJsonPatch.__eq__c                 C   s
   | |k S r>   r   rD   r   r   r   rG   G  s    zJsonPatch.__ne__Nc                 C   s   |p| j }||}| ||dS )a  Creates JsonPatch instance from string source.

        :param patch_str: JSON patch as raw string.
        :type patch_str: str

        :param loads: A function of one argument that loads a serialized
                      JSON string.
        :type loads: function

        :param pointer_cls: JSON pointer class to use.
        :type pointer_cls: Type[JsonPointer]

        :return: :class:`JsonPatch` instance.
        r#   )json_loader)clsZ	patch_strloadsr$   r   r+   r   r   r   r(   J  s    
zJsonPatch.from_stringTc           	      C   sB   |p| j }t||||d}|dd|| t| }| ||dS )aC  Creates JsonPatch instance based on comparison of two document
        objects. Json patch would be created for `src` argument against `dst`
        one.

        :param src: Data source document object.
        :type src: dict

        :param dst: Data source document object.
        :type dst: dict

        :param dumps: A function of one argument that produces a serialized
                      JSON string.
        :type dumps: function

        :param pointer_cls: JSON pointer class to use.
        :type pointer_cls: Type[JsonPointer]

        :return: :class:`JsonPatch` instance.

        >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
        >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
        >>> patch = JsonPatch.from_diff(src, dst)
        >>> new = patch.apply(src)
        >>> new == dst
        True
        r#    N)json_dumperDiffBuilder_compare_valuesr   execute)	r   r/   r0   optimizationdumpsr$   r   ZbuilderZopsr   r   r   r.   ^  s
    
zJsonPatch.from_diffc                 C   s   |p| j }|| jS )z!Returns patch set as JSON string.)r   r+   )r9   r   r   r   r   r   r     s    
zJsonPatch.to_stringc                 C   s   t t| j| jS r>   )r   mapr   r+   rA   r   r   r   r     s    zJsonPatch._opsFc                 C   s(   |st |}| jD ]}||}q|S )a5  Applies the patch to a given object.

        :param obj: Document object.
        :type obj: dict

        :param in_place: Tweaks the way how patch would be applied - directly to
                         specified `obj` or to its copy.
        :type in_place: bool

        :return: Modified `obj`.
        )r|   r}   r   r)   )r9   r=   r,   r8   r   r   r   r)     s
    

zJsonPatch.applyc                 C   sZ   d|krt d|d }t|ts*t d|| jkrBt d|| j| }||| jdS )Nrl   z&Operation does not contain 'op' memberzOperation must be a stringzUnknown operation {0!r}r#   )r   r%   r&   
operationsrU   r$   )r9   r8   rl   r   r   r   r   r     s    


zJsonPatch._get_operation)N)F)"r   r   r   staticmethodjsonr   r   
_jsonloadsr   r   rQ   r_   rf   rg   rw   r{   r   r   r;   r   r   __nonzero__r   rB   rF   rG   classmethodr(   r.   r   rO   r   r)   r   r   r   r   r   r'     s<   
6 $


r'   c                   @   s   e Zd ZejefddZdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )r   c                 C   sL   || _ || _i i g| _g g g| _g  | _}|| _|| _||d g|d d < d S r>   )r   r$   index_storageindex_storage2_DiffBuilder__rootsrc_docdst_doc)r9   r   r   r   r$   rootr   r   r   r;     s    


zDiffBuilder.__init__c                 C   st   |t |f}z:| j| }||}|d kr6|g||< n|| | W n( tk
rn   | j| ||f Y nX d S r>   )re   r   getr   r7   r   )r9   r    indexst	typed_keystoragestoredr   r   r   store_index  s    

zDiffBuilder.store_indexc                 C   s   |t |f}z"| j| |}|r,| W S W n^ tk
r   | j| }tt|d ddD ]*}|| d |kr\||d    Y S q\Y nX d S )Nr   rI   r   )re   r   r   popr7   r   ranger   )r9   r    r   r   r   r   ir   r   r   
take_index  s    
zDiffBuilder.take_indexc                 C   s,   | j }|d }|||g |d< |d< |d S )Nr   r   r   )r9   rl   r   lastr   r   r   rd     s    zDiffBuilder.insertc                 C   s*   |\}}}||d< ||d< g |d d < d S )Nr   r   r   )r9   r   Z	link_prevZ	link_next_r   r   r   rj     s    
zDiffBuilder.removec                 c   s.   | j }|d }||k	r*|d V  |d }qd S Nr      r   )r9   startr   currr   r   r   	iter_from  s
    
zDiffBuilder.iter_fromc                 c   s.   | j }|d }||k	r*|d V  |d }qd S r   r   )r9   r   r   r   r   r   r     s
    
zDiffBuilder.__iter__c                 c   s   | j }|d }||k	r|d |k	r|d |d d  }}|j|jkrt|tkrt|tkrtd|j|jd d| jdjV  |d d }q|d jV  |d }qd S )Nr   r   r~   r    rn   r#   )r   r5   re   rQ   r_   rf   r8   r$   )r9   r   r   Zop_firstZ	op_secondr   r   r   r     s*    


zDiffBuilder.executec           	      C   s   |  |t}|d k	r|d }t|jtkrXt|tkrX| |D ]}||j|j|_q@| | |j	t
||krtd|j	t
||d| jd}| | n4tdt
|||d| jd}| |}| ||t d S )Nr   r   rl   rh   r3   r#   rm   rn   )r   
_ST_REMOVEre   r   rL   r   r\   r3   rj   r5   
_path_joinrg   r$   rd   r_   r   _ST_ADD)	r9   r3   r   itemr   rl   vnew_op	new_indexr   r   r   _item_added  s2    

zDiffBuilder._item_addedc           
      C   s   t dt||d| jd}| |t}| |}|d k	r|d }|j| jd }t	|t
kr| |D ]}	|	|j|j|_qh| | |j|jkrtd|j|jd| jd}||d< q| | n| ||t d S )Nrj   rk   r#   r   r   r   r   )rQ   r   r$   r   r   rd   r6   rR   r   re   r   r   r]   r3   r   rj   r5   rg   r   r   )
r9   r3   r   r   r   r   r   rl   Z
added_itemr   r   r   r   _item_removed  s4    


zDiffBuilder._item_removedc                 C   s&   |  tdt|||d| jd d S )Nr~   rn   r#   )rd   rf   r   r$   )r9   r3   r   r   r   r   r   _item_replaced?  s    zDiffBuilder._item_replacedc           	      C   s   t | }t | }|| }|| }|D ]}| |t|||  q,|D ]}| |t|||  qL||@ D ]}| |||| ||  qpd S r>   )setkeysr   rN   r   r   )	r9   r3   r/   r0   Zsrc_keysZdst_keysZ
added_keysZremoved_keysr   r   r   r   _compare_dictsF  s    zDiffBuilder._compare_dictsc                 C   s   t |t | }}t||}t||}t|D ]}||k r|| ||  }	}
|	|
krXq.qt|	trt|
tr| t|||	|
 qt|	trt|
tr| 	t|||	|
 q| 
|||	 | |||
 q.||kr| 
||||  q.| ||||  q.d S r>   )r   maxminr   r%   r   r   r   r   _compare_listsr   r   )r9   r3   r/   r0   Zlen_srcZlen_dstmax_lenZmin_lenr   oldnewr   r   r   r   U  s*    



zDiffBuilder._compare_listsc                 C   s~   t |tr*t |tr*| t|||| nPt |trTt |trT| t|||| n&| || |krld S | ||| d S r>   )r%   r   r   r   r   r   r   r   )r9   r3   r   r/   r0   r   r   r   r   q  s    

	zDiffBuilder._compare_valuesN)r   r   r   r   r   r   r;   r   r   rd   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
!r   c                 C   s,   |d kr| S | d t |dddd S )NrH   ~z~0z~1)rN   r~   rZ   r   r   r   r     s    r   )4r   
__future__r   r   r|   	functoolsr   systypesr   ImportErrorr   Zjsonpointerr   r   r   r   collections.abcr   r   unicoderN   
__author____version__Z__website____license__version_infobytesr&   	Exceptionr
   r   r   AssertionErrorr   r!   partialr   r   r-   r1   objectr2   rQ   r_   rf   rg   rw   r{   r'   r   r   r   r   r   r   <module>!   sX   


	(64*V D X