a
    ]gRt                     @   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
yf   ddlm	Z	 Y n0 zddlmZ W n e
y   eZY n0 ddlmZmZ dZdZzddlmZmZ W n& e
y   ddlmZmZ eZY n0 d	Zd
ZdZdZejdkre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+Z0G d*d+ d+e+Z1G d,d- d-e*Z2G d.d/ d/e*Z3d0d1 Z4dS )2z Apply JSON-Patches (RFC 6902)     )unicode_literalsN)Sequence)MappingProxyType)JsonPointerJsonPointerException   )MutableMappingMutableSequenceu    Stefan Kögl <stefan@skoegl.net>z1.33z0https://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   W/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/jsonpatch.pyr   P   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   T   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   X   s   r   c                   @   s   e Zd ZdZdS )JsonPatchTestFailedz A Test operation failed Nr   r   r   r   r   r   a   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>l   s   zmultidict.<locals>.<genexpr>)collectionsdefaultdictlistappenddictitems)Zordered_pairsZmdictr   valuer   r   r   	multidicte   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_patchx   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    r2   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 | _nJ|d | _z|  | j| _W n, ty } ztdW Y d }~n
d }~0 0 || _d S )Npathz#Operation must have a 'path' memberzInvalid 'path')	r%   __contains__r   r&   r4   locationpointer	TypeError	operation)selfr9   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)r:   objr   r   r   r*      s    zPatchOperation.applyc                 C   s   t t| j S N)hash	frozensetr9   r    r:   r   r   r   __hash__   s    zPatchOperation.__hash__c                 C   s   t |tsdS | j|jkS NF)r&   r3   r9   r:   otherr   r   r   __eq__   s    
zPatchOperation.__eq__c                 C   s
   | |k S r?   r   rE   r   r   r   __ne__   s    zPatchOperation.__ne__c                 C   s   d | jjd d S )N/)joinr7   partsrB   r   r   r   r4      s    zPatchOperation.pathc                 C   s6   zt | jjd W S  ty0   | jjd  Y S 0 d S )NrJ   )intr7   rL   
ValueErrorrB   r   r   r   r      s    zPatchOperation.keyc                 C   s*   t || jjd< | jj| _| j| jd< d S )NrJ   r4   )strr7   rL   r4   r6   r9   )r:   r!   r   r   r   r      s    
N)r   r   r   r   r   r<   r*   rC   rG   rH   propertyr4   r   setterr   r   r   r   r3      s   

r3   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   s|   | j |\}}t|tr2t|ts2td|z
||= W n: ttfyv } zd|}t	|W Y d }~n
d }~0 0 |S )Nzinvalid array index '{0}'z(can't remove a non-existent object '{0}')
r7   to_lastr&   r   rM   r   formatKeyError
IndexErrorr   )r:   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   r4   r   r:   r4   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 rZ   r[   r\   r   r   r   _on_undo_add  s
    

zRemoveOperation._on_undo_addNr   r   r   r   r*   r]   r^   r   r   r   r   rR      s   rR   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y: } ztdW Y d }~n
d }~0 0 | j|\}}t|tr|dkrj|| q|t|ks~|dk rt	dq|
|| nPt|tr|d u r|}q|||< n.|d u 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})r9   rU   r   r7   rS   r&   r	   r   r   r   insertr   r8   rT   typer6   )r:   r>   r!   r;   rW   rX   r   r   r   r*     s*    



zAddOperation.applyc                 C   s0   | j |kr,| j|kr$|  jd7  _n|d7 }|S rZ   r[   r\   r   r   r   r]   3  s
    

zAddOperation._on_undo_removec                 C   s0   | j |kr,| j|kr$|  jd8  _n|d7 }|S rZ   r[   r\   r   r   r   r^   ;  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y: } ztdW Y d }~n
d }~0 0 | j|\}}|d u rX|S |dkrhtdt|tr|t|ks|dk rtdnTt|t	r||vrd
|}t|n.|d u rtd
t|ntd	
| j||||< |S )
Nr!   ra   rb   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}'rc   rd   )r9   rU   r   r7   rS   r&   r	   r   r   r   rT   r8   rf   r6   )r:   r>   r!   r;   rW   rX   rY   r   r   r   r*   G  s.    




zReplaceOperation.applyc                 C   s   |S r?   r   r\   r   r   r   r]   g  s    z ReplaceOperation._on_undo_removec                 C   s   |S r?   r   r\   r   r   r   r^   j  s    zReplaceOperation._on_undo_addNr_   r   r   r   r   rg   D  s    rg   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y^ } ztdW Y d }~n
d }~0 0 ||\}}z|| }W n4 ttfy } ztt|W Y d }~n
d }~0 0 | 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opr4   r$   addrm   r4   r!   )r&   r9   r%   rU   r   rS   rV   r   rO   r7   r   containsrR   r*   r`   r6   r:   r>   from_ptrr;   rW   rX   r!   r   r   r   r*   q  sJ    "


zMoveOperation.applyc                 C   s$   |  | jd }d|jd d S )Nri   rI   rJ   )r%   r9   rK   rL   r:   rr   r   r   r   	from_path  s    zMoveOperation.from_pathc                 C   sB   |  | jd }zt|jd W S  ty<   |jd  Y S 0 d S Nri   rJ   )r%   r9   rM   rL   r8   rs   r   r   r   from_key  s
    zMoveOperation.from_keyc                 C   s.   |  | jd }t||jd< |j| jd< d S ru   )r%   r9   rO   rL   r4   )r:   r!   rr   r   r   r   rv     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 rZ   rt   rv   r4   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 rZ   rw   r\   r   r   r   r^     s    



zMoveOperation._on_undo_addN)r   r   r   r   r*   rP   rt   rv   rQ   r]   r^   r   r   r   r   rh   n  s   %


rh   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 u r |}n| j ||}W n0 ty` } ztt|W Y d }~n
d }~0 0 z| jd }W n, ty } ztdW Y d }~n
d }~0 0 ||krd}t|	|t
||t
||S )Nr!   ra   z0{0} ({1}) is not equal to tested value {2} ({3}))r7   rS   walkr   r   rO   r9   rU   r   rT   rf   )r:   r>   rW   rX   valr;   r!   rY   r   r   r   r*     s&    "zTestOperation.applyNr   r   r   r   r*   r   r   r   r   rx     s   rx   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y@ } ztdW Y d }~n
d }~0 0 ||\}}zt|| }W n4 ttfy } ztt	|W Y d }~n
d }~0 0 t
d| j|d| j d|}|S )Nri   rj   rn   ro   r$   )r%   r9   rU   r   rS   copydeepcopyrV   r   rO   r`   r6   r*   rq   r   r   r   r*     s*    "zCopyOperation.applyNr{   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(   )rk   rn   replacemovetestr}   c                 C   s8   || _ || _| j D ] }t|tr(td| | qd S )NzMDocument is expected to be sequence of operations, got a sequence of strings.)r,   r%   r&   r'   r   _get_operation)r:   r,   r%   rm   r   r   r   r<   2  s    


zJsonPatch.__init__c                 C   s   |   S )zstr(self) -> self.to_string())	to_stringrB   r   r   r   __str__J  s    zJsonPatch.__str__c                 C   s
   t | jS r?   )boolr,   rB   r   r   r   __bool__N  s    zJsonPatch.__bool__c                 C   s
   t | jS r?   )iterr,   rB   r   r   r   __iter__S  s    zJsonPatch.__iter__c                 C   s   t t| jS r?   )r@   tuple_opsrB   r   r   r   rC   V  s    zJsonPatch.__hash__c                 C   s   t |tsdS | j|jkS rD   )r&   r(   r   rE   r   r   r   rG   Y  s    
zJsonPatch.__eq__c                 C   s
   | |k S r?   r   rE   r   r   r   rH   ^  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)   a  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   r0   r1   optimizationdumpsr%   r   Zbuilderopsr   r   r   r/   u  s
    
zJsonPatch.from_diffc                 C   s   |p| j }|| jS )z!Returns patch set as JSON string.)r   r,   )r:   r   r   r   r   r   r     s    
zJsonPatch.to_stringc                 C   s   t t| j| jS r?   )r   mapr   r,   rB   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*   )r:   r>   r-   r9   r   r   r   r*     s
    

zJsonPatch.applyc                 C   sZ   d|vrt d|d }t|ts*t d|| jvrBt d|| j| }||| jdS )Nrm   z&Operation does not contain 'op' memberzOperation's op must be a stringzUnknown operation {0!r}r$   )r   r&   r'   
operationsrT   r%   )r:   r9   rm   r   r   r   r   r     s    


zJsonPatch._get_operation)N)F)"r   r   r   staticmethodjsonr   r   
_jsonloadsr   r   rR   r`   rg   rh   rx   r|   r   r   r<   r   r   __nonzero__r   rC   rG   rH   classmethodr)   r/   r   rP   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)r:   r   r   r   r%   rootr   r   r   r<     s    


zDiffBuilder.__init__c                 C   sr   |t |f}z:| j| }||}|d u r6|g||< n|| | W n& tyl   | j| ||f Y n0 d S r?   )rf   r   getr   r8   r   )r:   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y   | j| }tt|d ddD ]*}|| d |krZ||d    Y S qZY n0 d S )Nr   rJ   r   )rf   r   r   popr8   r   ranger   )r:   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   )r:   rm   r   lastr   r   r   re     s    zDiffBuilder.insertc                 C   s*   |\}}}||d< ||d< g |d d < d S )Nr   r   r   )r:   r   Z	link_prevZ	link_next_r   r   r   rk     s    
zDiffBuilder.removec                 c   s.   | j }|d }||ur*|d V  |d }qd S Nr      r   )r:   startr   currr   r   r   	iter_from  s
    
zDiffBuilder.iter_fromc                 c   s.   | j }|d }||ur*|d V  |d }qd S r   r   )r:   r   r   r   r   r   r     s
    
zDiffBuilder.__iter__c                 c   s   | j }|d }||ur|d |u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!   ro   r$   )r   r6   rf   rR   r`   rg   r9   r%   )r:   r   r   Zop_firstZ	op_secondr   r   r   r     s*    


zDiffBuilder.executec           	      C   s   |  |t}|d u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   rm   ri   r4   r$   rn   ro   )r   
_ST_REMOVErf   r   rM   r   r]   r4   rk   r6   
_path_joinrh   r%   re   r`   r   _ST_ADD)	r:   r4   r   itemr   rm   vnew_op	new_indexr   r   r   _item_added  s2    

zDiffBuilder._item_addedc           
      C   s   t dt||d| jd}| |t}| |}|d u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 )Nrk   rl   r$   r   r   r   r   )rR   r   r%   r   r   re   r7   rS   r   rf   r   r   r^   r4   r   rk   r6   rh   r   r   )
r:   r4   r   r   r   r   r   rm   Z
added_itemr   r   r   r   _item_removed5  s4    


zDiffBuilder._item_removedc                 C   s&   |  tdt|||d| jd d S )Nr   ro   r$   )re   rg   r   r%   )r:   r4   r   r   r   r   r   _item_replacedV  s    zDiffBuilder._item_replacedc           	      C   s   t | }t | }|| }|| }|D ]}| |t|||  q,|D ]}| |t|||  qL||@ D ]}| |||| ||  qpd S r?   )setkeysr   rO   r   r   )	r:   r4   r0   r1   Zsrc_keysZdst_keysZ
added_keysZremoved_keysr   r   r   r   _compare_dicts]  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   )r:   r4   r0   r1   Zlen_srcZlen_dstmax_lenZmin_lenr   oldnewr   r   r   r   l  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   )r:   r4   r   r0   r1   r   r   r   r     s    

	zDiffBuilder._compare_valuesN)r   r   r   r   r   r   r<   r   r   re   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
!r   c                 C   s,   |d u r| S | d t |dddd S )NrI   ~z~0z~1)rO   r   r[   r   r   r   r     s    r   )5r   
__future__r   r   r}   	functoolsr   syscollections.abcr   ImportErrortypesr   r   Zjsonpointerr   r   r   r   r   r	   unicoderO   
__author____version__Z__website____license__version_infobytesr'   	Exceptionr   r   r   AssertionErrorr   r"   partialr   r   r.   r2   objectr3   rR   r`   rg   rh   rx   r|   r(   r   r   r   r   r   r   <module>!   s`   

	(6"4*V Q X