o
    GgRt                     @   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
y3   ddlm	Z	 Y nw zddlmZ W n e
yG   eZY nw ddlmZmZ dZdZz
ddlmZmZ W n e
yq   ddlmZmZ eZY nw 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+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                   @      e Zd ZdZdS )JsonPatchExceptionzBase Json Patch exceptionN__name__
__module____qualname____doc__ r   r   L/var/www/html/AyurvedaChatbot/venv/lib/python3.10/site-packages/jsonpatch.pyr   P       r   c                   @   r   )InvalidJsonPatchz, Raised if an invalid JSON Patch is created Nr   r   r   r   r   r   T   r   r   c                   @   r   )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   r   r   c                   @   r   )JsonPatchTestFailedz A Test operation failed Nr   r   r   r   r   r   a   r   r   c                 C   s<   t t}| D ]\}}|| | qtdd | D S )z'Convert duplicate keys values to lists.c                 s   s0    | ]\}}|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)ordered_pairs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   
!r2   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   r6   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 | _n|d | _z	|  | j| _W n ty> } ztdd }~ww || _d S )Npathz#Operation must have a 'path' memberzInvalid 'path')	r)   __contains__r   r*   r8   locationpointer	TypeError	operation)selfr=   r)   exr   r   r   __init__   s   


zPatchOperation.__init__c                 C   s   t d)zGAbstract method that applies a patch operation to the specified object.z%should implement the patch operation.)NotImplementedError)r>   objr   r   r   r.         zPatchOperation.applyc                 C   s   t t| j S N)hash	frozensetr=   r"   r>   r   r   r   __hash__   s   zPatchOperation.__hash__c                 C      t |tsdS | j|jkS NF)r*   r7   r=   r>   otherr   r   r   __eq__      
zPatchOperation.__eq__c                 C   
   | |k S rD   r   rK   r   r   r   __ne__      
zPatchOperation.__ne__c                 C   s   d | jjd d S )N/)joinr;   partsrG   r   r   r   r8      s   zPatchOperation.pathc                 C   s2   z	t | jjd W S  ty   | jjd  Y S w )NrS   )intr;   rU   
ValueErrorrG   r   r   r   r      s
   zPatchOperation.keyc                 C   s*   t || jjd< | jj| _| j| jd< d S )NrS   r8   )strr;   rU   r8   r:   r=   )r>   r%   r   r   r   r      s   
N)r   r   r   r   r   r@   r.   rH   rM   rP   propertyr8   r   setterr   r   r   r   r7      s    

r7   c                   @   (   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   sn   | j |\}}t|trt|tstd|z||= W |S  ttfy6 } z	d|}t	|d }~ww )Nzinvalid array index '{0}'z(can't remove a non-existent object '{0}')
r;   to_lastr*   r   rV   r   formatKeyError
IndexErrorr   )r>   rB   subobjpartr?   msgr   r   r   r.      s   
zRemoveOperation.applyc                 C   s2   | j |kr| j|kr|  jd7  _|S |d8 }|S Nr   r8   r   r>   r8   r   r   r   r   _on_undo_remove      

zRemoveOperation._on_undo_removec                 C   s2   | j |kr| j|kr|  jd8  _|S |d8 }|S rd   re   rf   r   r   r   _on_undo_add  rh   zRemoveOperation._on_undo_addNr   r   r   r   r.   rg   ri   r   r   r   r   r\      s
    r\   c                   @   r[   )	AddOperationz,Adds an object property or an array element.c              
   C   s   z| j d }W n ty } ztdd }~ww | j|\}}t|trF|dkr0|| |S |t|ks:|dk r>t	d|
|| |S t|trY|d u rS|}|S |||< |S |d u rftdt|t	d| j|)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})r=   r_   r   r;   r]   r*   r	   r    r   r   insertr   r<   r^   typer:   )r>   rB   r%   r?   ra   rb   r   r   r   r.     s4   


	zAddOperation.applyc                 C   s2   | j |kr| j|kr|  jd7  _|S |d7 }|S rd   re   rf   r   r   r   rg   3  rh   zAddOperation._on_undo_removec                 C   s2   | j |kr| j|kr|  jd8  _|S |d7 }|S rd   re   rf   r   r   r   ri   ;  rh   zAddOperation._on_undo_addNrj   r   r   r   r   rk     s
     rk   c                   @   r[   )	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d }~ww | j|\}}|d u r&|S |dkr.tdt|trB|t|ks=|dk rAtdn)t|t	rU||vrTd
|}t|n|d u rbtd
t|td	
| j||||< |S )
Nr%   rl   rm   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}'rn   ro   )r=   r_   r   r;   r]   r*   r	   r   r   r   r^   r<   rq   r:   )r>   rB   r%   r?   ra   rb   rc   r   r   r   r.   G  s6   


zReplaceOperation.applyc                 C      |S rD   r   rf   r   r   r   rg   g     z ReplaceOperation._on_undo_removec                 C   rs   rD   r   rf   r   r   r   ri   j  rt   zReplaceOperation._on_undo_addNrj   r   r   r   r   rr   D  s
     rr   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  zt | jd | jr| jd }n| | jd }W n ty) } ztdd }~ww ||\}}z|| }W n ttfyK } ztt|d }~ww | j	|krS|S t |t
rb| j	|rbt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opr8   r(   addrz   r8   r%   )r*   r=   r)   r_   r   r]   r`   r   rX   r;   r   containsr\   r.   rk   r:   r>   rB   from_ptrr?   ra   rb   r%   r   r   r   r.   q  sT   


zMoveOperation.applyc                 C   s$   |  | jd }d|jd d S )Nrv   rR   rS   )r)   r=   rT   rU   r>   r   r   r   r   	from_path  s   zMoveOperation.from_pathc                 C   s>   |  | jd }zt|jd W S  ty   |jd  Y S w Nrv   rS   )r)   r=   rV   rU   r<   r   r   r   r   from_key  s   zMoveOperation.from_keyc                 C   s.   |  | jd }t||jd< |j| jd< d S r   )r)   r=   rX   rU   r8   )r>   r%   r   r   r   r   r     s   c                 C   s^   | j |kr| j|kr|  jd7  _n|d8 }| j|kr-| j|kr)|  jd7  _|S |d7 }|S rd   r   r   r8   r   rf   r   r   r   rg        



zMoveOperation._on_undo_removec                 C   s^   | j |kr| j|kr|  jd8  _n|d8 }| j|kr-| j|kr)|  jd8  _|S |d7 }|S rd   r   rf   r   r   r   ri     r   zMoveOperation._on_undo_addN)r   r   r   r   r.   rY   r   r   rZ   rg   ri   r   r   r   r   ru   n  s    %


ru   c                   @      e Zd ZdZdd ZdS )TestOperationz!Test value by specified location.c              
   C   s   z| j |\}}|d u r|}n| j ||}W n ty* } ztt|d }~ww z| jd }W n tyB } ztdd }~ww ||krWd}t|	|t
||t
||S )Nr%   rl   z0{0} ({1}) is not equal to tested value {2} ({3}))r;   r]   walkr   r   rX   r=   r_   r   r^   rq   )r>   rB   ra   rb   valr?   r%   rc   r   r   r   r.     s0   zTestOperation.applyNr   r   r   r   r.   r   r   r   r   r         r   c                   @   r   )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d }~ww ||\}}z	t|| }W n ttfy? } ztt	|d }~ww t
d| j|d| j d|}|S )Nrv   rw   r{   r|   r(   )r)   r=   r_   r   r]   copydeepcopyr`   r   rX   rk   r:   r.   r~   r   r   r   r.     s2   zCopyOperation.applyNr   r   r   r   r   r     r   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,   )rx   r{   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.)r0   r)   r*   r+   r   _get_operation)r>   r0   r)   rz   r   r   r   r@   2  s   


zJsonPatch.__init__c                 C   s   |   S )zstr(self) -> self.to_string())	to_stringrG   r   r   r   __str__J  rC   zJsonPatch.__str__c                 C   
   t | jS rD   )boolr0   rG   r   r   r   __bool__N  rQ   zJsonPatch.__bool__c                 C   r   rD   )iterr0   rG   r   r   r   __iter__S  rQ   zJsonPatch.__iter__c                 C   s   t t| jS rD   )rE   tuple_opsrG   r   r   r   rH   V  s   zJsonPatch.__hash__c                 C   rI   rJ   )r*   r,   r   rK   r   r   r   rM   Y  rN   zJsonPatch.__eq__c                 C   rO   rD   r   rK   r   r   r   rP   ^  rQ   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)cls	patch_strloadsr)   r   r0   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   r4   r5   optimizationdumpsr)   r   builderopsr   r   r   r3   u  s
   
zJsonPatch.from_diffc                 C   s   |p| j }|| jS )z!Returns patch set as JSON string.)r   r0   )r>   r   r   r   r   r   r     s   

zJsonPatch.to_stringc                 C   s   t t| j| jS rD   )r   mapr   r0   rG   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>   rB   r1   r=   r   r   r   r.     s
   

zJsonPatch.applyc                 C   sZ   d|vrt d|d }t|tst d|| jvr!t d|| j| }||| jdS )Nrz   z&Operation does not contain 'op' memberzOperation's op must be a stringzUnknown operation {0!r}r(   )r   r*   r+   
operationsr^   r)   )r>   r=   rz   r   r   r   r   r     s   


zJsonPatch._get_operationrD   )F)"r   r   r   staticmethodjsonr   r   
_jsonloadsr   r   r\   rk   rr   ru   r   r   r   r   r@   r   r   __nonzero__r   rH   rM   rP   classmethodr-   r3   r   rY   r   r.   r   r   r   r   r   r,     s>    
	-
$

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 rD   )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   sv   |t |f}z | j| }||}|d u r|g||< W d S || | W d S  ty:   | j| ||f Y d S w rD   )rq   r   getr    r<   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 d S  tyG   | j| }tt|d ddD ]}|| d |krC||d    Y S q.Y d S w )Nr   rS   r   )rq   r   r   popr<   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>   rz   r   lastr   r   r   rp     s   zDiffBuilder.insertc                 C   s*   |\}}}||d< ||d< g |d d < d S )Nr   r   r   )r>   r   	link_prev	link_next_r   r   r   rx     s   
zDiffBuilder.removec                 c   s:    | j }|d }||ur|d V  |d }||usd S d S Nr      r   )r>   startr   currr   r   r   	iter_from     
zDiffBuilder.iter_fromc                 c   s:    | j }|d }||ur|d V  |d }||usd S d S r   r   )r>   r   r   r   r   r   r     r   zDiffBuilder.__iter__c                 c   s    | j }|d }||urW|d |urG|d |d d }}|j|jkrGt|tkrGt|tkrGtd|j|jd d| jdjV  |d d }q|d jV  |d }||usd S d S )Nr   r   r   r%   r|   r(   )r   r:   rq   r\   rk   rr   r=   r)   )r>   r   r   op_first	op_secondr   r   r   r     s.   zDiffBuilder.executec           	      C   s   |  |t}|d urQ|d }t|jtkr,t|tkr,| |D ]}||j|j|_q | | |j	t
||krOtd|j	t
||d| jd}| | d S d S tdt
|||d| jd}| |}| ||t d S )Nr   r   rz   rv   r8   r(   r{   r|   )r   
_ST_REMOVErq   r   rV   r   rg   r8   rx   r:   
_path_joinru   r)   rp   rk   r   _ST_ADD)	r>   r8   r   itemr   rz   vnew_op	new_indexr   r   r   _item_added  s4   

zDiffBuilder._item_addedc           
      C   s   t dt||d| jd}| |t}| |}|d ure|d }|j| jd }t	|t
kr@| |D ]}	|	|j|j|_q4| | |j|jkr^td|j|jd| jd}||d< d S | | d S | ||t d S )Nrx   ry   r(   r   r   r   r   )r\   r   r)   r   r   rp   r;   r]   r   rq   r   r   ri   r8   r   rx   r:   ru   r   r   )
r>   r8   r   r   r   r   r   rz   
added_itemr   r   r   r   _item_removed5  s4   

zDiffBuilder._item_removedc                 C   s&   |  tdt|||d| jd d S )Nr   r|   r(   )rp   rr   r   r)   )r>   r8   r   r   r   r   r   _item_replacedV  s   zDiffBuilder._item_replacedc           	      C   s   t | }t | }|| }|| }|D ]}| |t|||  q|D ]}| |t|||  q&||@ D ]}| |||| ||  q8d S rD   )setkeysr   rX   r   r   )	r>   r8   r4   r5   src_keysdst_keys
added_keysremoved_keysr   r   r   r   _compare_dicts]  s   zDiffBuilder._compare_dictsc                 C   s   t |t |}}t||}t||}t|D ]d}||k rd|| || }	}
|	|
kr+qt|	tr@t|
tr@| t|||	|
 qt|	trUt|
trU| 	t|||	|
 q| 
|||	 | |||
 q||krr| 
||||  q| ||||  qd S rD   )r   maxminr   r*   r   r   r   r	   _compare_listsr   r   )r>   r8   r4   r5   len_srclen_dstmax_lenmin_lenr   oldnewr   r   r   r   l  s,   



zDiffBuilder._compare_listsc                 C   s   t |trt |tr| t|||| d S t |tr,t |tr,| t|||| d S | || |kr8d S | ||| d S rD   )r*   r   r   r   r	   r   r   r   )r>   r8   r   r4   r5   r   r   r   r     s   

	zDiffBuilder._compare_valuesN)r   r   r   r   r   r   r@   r   r   rp   rx   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 )NrR   ~z~0z~1)rX   r   re   r   r   r   r     s    r   )5r   
__future__r   r   r   	functoolsr   syscollections.abcr   ImportErrortypesr   r!   jsonpointerr   r   r   r   r   r	   unicoderX   
__author____version____website____license__version_infobytesr+   	Exceptionr   r   r   AssertionErrorr   r&   partialr   r   r2   r6   objectr7   r\   rk   rr   ru   r   r   r,   r   r   r   r   r   r   <module>   sh    
	(6"4*V Q X