HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/__pycache__/jsonpatch.cpython-310.pyc
o

���gRt�@sdZddlmZddlZddlZddlZddlZddlZzddlm	Z	Wne
y3ddlm	Z	YnwzddlmZWne
yGe
ZYnwddlmZmZdZdZz
ddlmZmZWne
yqddlmZmZeZYnwd	Zd
ZdZdZejd
kr�eefZGdd�de�ZGdd�de�Z Gdd�de�Z!Gdd�dee"�Z#dd�Z$ej%ej&e$d�Z'defdd�Z(efdd�Z)Gdd�de*�Z+Gd d!�d!e+�Z,Gd"d#�d#e+�Z-Gd$d%�d%e+�Z.Gd&d'�d'e+�Z/Gd(d)�d)e+�Z0Gd*d+�d+e+�Z1Gd,d-�d-e*�Z2Gd.d/�d/e*�Z3d0d1�Z4dS)2z Apply JSON-Patches (RFC 6902) �)�unicode_literalsN)�Sequence)�MappingProxyType)�JsonPointer�JsonPointerException�)�MutableMapping�MutableSequenceu Stefan Kögl <stefan@skoegl.net>z1.33z0https://github.com/stefankoegl/python-json-patchzModified BSD License)�rc@�eZdZdZdS)�JsonPatchExceptionzBase Json Patch exceptionN��__name__�
__module__�__qualname__�__doc__�rr�4/usr/local/lib/python3.10/dist-packages/jsonpatch.pyrP�rc@r)�InvalidJsonPatchz, Raised if an invalid JSON Patch is created Nr
rrrrrTrrc@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
rrrrrXrrc@r)�JsonPatchTestFailedz A Test operation failed Nr
rrrrrarrcCs<t�t�}|D]\}}||�|�qtdd�|��D��S)z'Convert duplicate keys values to lists.css0�|]\}}|t|�dkr|dn|fVqdS)rrN)�len)�.0�key�valuesrrr�	<genexpr>ls
��
�zmultidict.<locals>.<genexpr>)�collections�defaultdict�list�append�dict�items)�
ordered_pairs�mdictr�valuerrr�	multidictes
�r&)�object_pairs_hookFcCs2t|t�r
tj||d�}nt||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)rrr�apply_patchxs
!r2cCstj|||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)rrr�
make_patch�sr6c@sbeZdZdZefdd�Zdd�Zdd�Zdd	�Zd
d�Z	e
dd
��Ze
dd��Zej
dd��ZdS)�PatchOperationz'A single operation inside a JSON Patch.c
Cs�||_|�d�std��t|d|j�r |dj|_|d|_n|d|_z	|�|j�|_Wnty>}ztd��d}~ww||_dS)N�pathz#Operation must have a 'path' memberzInvalid 'path')	r)�__contains__rr*r8�location�pointer�	TypeError�	operation)�selfr=r)�exrrr�__init__�s

��
zPatchOperation.__init__cCstd��)zGAbstract method that applies a patch operation to the specified object.z%should implement the patch operation.)�NotImplementedError)r>�objrrrr.��zPatchOperation.applycCstt|j����S�N)�hash�	frozensetr=r"�r>rrr�__hash__�szPatchOperation.__hash__cC�t|t�sdS|j|jkS�NF)r*r7r=�r>�otherrrr�__eq__��
zPatchOperation.__eq__cC�
||kSrDrrKrrr�__ne__��
zPatchOperation.__ne__cCsd�|jjdd��S)N�/���)�joinr;�partsrGrrrr8�szPatchOperation.pathcCs2z	t|jjd�WSty|jjdYSw)NrS)�intr;rU�
ValueErrorrGrrrr�s
�zPatchOperation.keycCs*t|�|jjd<|jj|_|j|jd<dS)NrSr8)�strr;rUr8r:r=)r>r%rrrr�s
N)rrrrrr@r.rHrMrP�propertyr8r�setterrrrrr7�s

r7c@�(eZdZdZdd�Zdd�Zdd�ZdS)	�RemoveOperationz/Removes an object property or an array element.c
Csn|j�|�\}}t|t�rt|t�std�|���z||=W|Sttfy6}z	d�|�}t	|��d}~ww)Nzinvalid array index '{0}'z(can't remove a non-existent object '{0}')
r;�to_lastr*rrVr�format�KeyError�
IndexErrorr)r>rB�subobj�partr?�msgrrrr.�s�
��zRemoveOperation.applycCs2|j|kr|j|kr|jd7_|S|d8}|S�Nr�r8r�r>r8rrrr�_on_undo_remove��

�zRemoveOperation._on_undo_removecCs2|j|kr|j|kr|jd8_|S|d8}|Srdrerfrrr�_on_undo_addrhzRemoveOperation._on_undo_addN�rrrrr.rgrirrrrr\�s
r\c@r[)	�AddOperationz,Adds an object property or an array element.c
Cs�z|jd}Wnty}ztd��d}~ww|j�|�\}}t|t�rF|dkr0|�|�|S|t|�ks:|dkr>t	d��|�
||�|St|t�rY|durS|}|S|||<|S|durftd�
t|����t	d�
|j|���)Nr%�/The operation does not contain a 'value' member�-rzcan't insert outside of list�invalid document type {0}�2unable to fully resolve json pointer {0}, part {1})r=r_rr;r]r*r	r rr�insertrr<r^�typer:)r>rBr%r?rarbrrrr.s4���

�
�	��zAddOperation.applycCs2|j|kr|j|kr|jd7_|S|d7}|Srdrerfrrrrg3rhzAddOperation._on_undo_removecCs2|j|kr|j|kr|jd8_|S|d7}|Srdrerfrrrri;rhzAddOperation._on_undo_addNrjrrrrrks
 rkc@r[)	�ReplaceOperationz?Replaces an object property or an array element by a new value.c
Cs�z|jd}Wnty}ztd��d}~ww|j�|�\}}|dur&|S|dkr.td��t|t�rB|t|�ks=|dkrAtd��n)t|t	�rU||vrTd�
|�}t|��n|durbtd�
t|����td	�
|j
|���|||<|S)
Nr%rlrmz7'path' with '-' can't be applied to 'replace' operationrzcan't replace outside of listz)can't replace a non-existent object '{0}'rnro)r=r_rr;r]r*r	rrrr^r<rqr:)r>rBr%r?rarbrcrrrr.Gs6���
�

�zReplaceOperation.applycC�|SrDrrfrrrrgg�z ReplaceOperation._on_undo_removecCrsrDrrfrrrrijrtzReplaceOperation._on_undo_addNrjrrrrrrDs
 rrc@sNeZdZdZdd�Zedd��Zedd��Zejdd��Zd	d
�Z	dd�Z
d
S)�
MoveOperationz?Moves an object property or an array element to a new location.c
Cszt|jd|j�r|jd}n|�|jd�}Wnty)}ztd��d}~ww|�|�\}}z||}WnttfyK}ztt|���d}~ww|j	|krS|St|t
�rb|j	�|�rbtd��td|jdd�|jd��
|�}td|j|d�|jd��
|�}|S)	N�from�.The operation does not contain a 'from' memberz*Cannot move values into their own children�remove��opr8r(�add�rzr8r%)r*r=r)r_rr]r`rrXr;r�containsr\r.rkr:�r>rB�from_ptrr?rarbr%rrrr.qsT������


�������zMoveOperation.applycCs$|�|jd�}d�|jdd��S)NrvrRrS)r)r=rTrU�r>rrrr�	from_path�szMoveOperation.from_pathcCs>|�|jd�}zt|jd�WSty|jdYSw�NrvrS)r)r=rVrUr<r�rrr�from_key�s�zMoveOperation.from_keycCs.|�|jd�}t|�|jd<|j|jd<dSr�)r)r=rXrUr8)r>r%rrrrr��scCs^|j|kr|j|kr|jd7_n|d8}|j|kr-|j|kr)|jd7_|S|d7}|Srd�r�r�r8rrfrrrrg��



�zMoveOperation._on_undo_removecCs^|j|kr|j|kr|jd8_n|d8}|j|kr-|j|kr)|jd8_|S|d7}|Srdr�rfrrrri�r�zMoveOperation._on_undo_addN)rrrrr.rYr�r�rZrgrirrrrruns%



ruc@�eZdZdZdd�ZdS)�
TestOperationz!Test value by specified location.c
Cs�z|j�|�\}}|dur|}n|j�||�}Wnty*}ztt|���d}~wwz|jd}WntyB}ztd��d}~ww||krWd}t|�	|t
|�|t
|����|S)Nr%rlz0{0} ({1}) is not equal to tested value {2} ({3}))r;r]�walkrrrXr=r_rr^rq)r>rBrarb�valr?r%rcrrrr.�s0�������zTestOperation.applyN�rrrrr.rrrrr���r�c@r�)�
CopyOperationzA Copies an object property or an array element to a new location c
Cs�z
|�|jd�}Wnty}ztd��d}~ww|�|�\}}z	t�||�}Wnttfy?}ztt	|���d}~wwt
d|j|d�|jd��|�}|S)Nrvrwr{r|r()
r)r=r_rr]�copy�deepcopyr`rrXrkr:r.r~rrrr.�s2��������zCopyOperation.applyNr�rrrrr��r�r�c@s�eZdZeej�Zee�Ze	e
eee
eed��Z	efdd�Zdd�Zdd�ZeZdd	�Zd
d�Zdd
�Zdd�Zedefdd��Zeddefdd��Zddd�Zedd��Zd dd�Z dd�Z!dS)!r,)rxr{�replace�move�testr�cCs8||_||_|jD]}t|t�rtd��|�|�q	dS)NzMDocument is expected to be sequence of operations, got a sequence of strings.)r0r)r*r+r�_get_operation)r>r0r)rzrrrr@2s


�zJsonPatch.__init__cCs|��S)zstr(self) -> self.to_string())�	to_stringrGrrr�__str__JrCzJsonPatch.__str__cC�
t|j�SrD)�boolr0rGrrr�__bool__NrQzJsonPatch.__bool__cCr�rD)�iterr0rGrrr�__iter__SrQzJsonPatch.__iter__cCstt|j��SrD)rE�tuple�_opsrGrrrrHVszJsonPatch.__hash__cCrIrJ)r*r,r�rKrrrrMYrNzJsonPatch.__eq__cCrOrDrrKrrrrP^rQzJsonPatch.__ne__NcCs|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�r0rrrr-as
zJsonPatch.from_stringTc	CsB|p|j}t||||d�}|�dd||�t|���}|||d�S)aCCreates 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�r4r5�optimization�dumpsr)r��builder�opsrrrr3us

zJsonPatch.from_diffcCs|p|j}||j�S)z!Returns patch set as JSON string.)r�r0)r>r�r�rrrr��s

zJsonPatch.to_stringcCstt|j|j��SrD)r��mapr�r0rGrrrr��szJsonPatch._opsFcCs(|st�|�}|jD]}|�|�}q
|S)a5Applies 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>rBr1r=rrrr.�s


zJsonPatch.applycCsZd|vrtd��|d}t|t�std��||jvr!td�|���|j|}|||jd�S)Nrzz&Operation does not contain 'op' memberzOperation's op must be a stringzUnknown operation {0!r}r()rr*r+�
operationsr^r))r>r=rzr�rrrr��s


zJsonPatch._get_operationrD)F)"rrr�staticmethod�jsonr�r��
_jsonloadsr�rr\rkrrrur�r�r�rr@r�r��__nonzero__r�rHrMrP�classmethodr-r3r�rYr�r.r�rrrrr,�s>
�	-�
$

r,c@s�eZdZejefdd�Zdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)r�cCsL||_||_iig|_ggg|_g|_}||_||_||dg|dd�<dSrD)r�r)�
index_storage�index_storage2�_DiffBuilder__root�src_doc�dst_doc)r>r�r�r�r)�rootrrrr@�s


zDiffBuilder.__init__cCsv|t|�f}z |j|}|�|�}|dur|g||<WdS||�|�WdSty:|j|�||f�YdSwrD)rqr��getr r<r�)r>r%�index�st�	typed_key�storage�storedrrr�store_index�s

�zDiffBuilder.store_indexcCs�|t|�f}z|j|�|�}|r|��WSWdStyG|j|}tt|�ddd�D]}||d|krC|�|�dYSq.YdSw)NrrSr)rqr�r��popr<r��ranger)r>r%r�r�r�r��irrr�
take_index�s
�
���zDiffBuilder.take_indexcCs,|j}|d}|||g|d<|d<|dS)Nrr�r�)r>rzr��lastrrrrp�szDiffBuilder.insertcCs*|\}}}||d<||d<g|dd�<dS)Nrrr)r>r��	link_prev�	link_next�_rrrrx�s
zDiffBuilder.removeccs:�|j}|d}||ur|dV|d}||usdSdS�Nr�r�)r>�startr��currrrr�	iter_from���
�zDiffBuilder.iter_fromccs:�|j}|d}||ur|dV|d}||usdSdSr�r�)r>r�r�rrrr�r�zDiffBuilder.__iter__ccs��|j}|d}||urW|d|urG|d|dd}}|j|jkrGt|�tkrGt|�tkrGtd|j|jdd�|jd�jV|dd}q|djV|d}||usdSdS)Nrr�r�r%r|r()r�r:rqr\rkrrr=r))r>r�r��op_first�	op_secondrrrr�s.�����zDiffBuilder.executec	Cs�|�|t�}|durQ|d}t|j�tkr,t|�tkr,|�|�D]}|�|j|j�|_q |�|�|j	t
||�krOtd|j	t
||�d�|jd�}|�
|�dSdStdt
||�|d�|jd�}|�
|�}|�||t�dS)Nr�r��rzrvr8r(r{r|)r��
_ST_REMOVErqrrVr�rgr8rxr:�
_path_joinrur)rprkr��_ST_ADD)	r>r8r�itemr�rz�v�new_op�	new_indexrrr�_item_addeds4
�����
zDiffBuilder._item_addedc
Cs�tdt||�d�|jd�}|�|t�}|�|�}|dure|d}|j�|j�d}t	|�t
kr@|�|�D]}	|	�|j
|j�|_q4|�|�|j|jkr^td|j|jd�|jd�}||d<dS|�|�dS|�||t�dS)Nrxryr(r�rr�r�)r\r�r)r�r�rpr;r]r�rqrr�rir8rrxr:rur�r�)
r>r8rr�r�r�r�rz�
added_itemr�rrr�
_item_removed5s4��

��zDiffBuilder._item_removedcCs&|�tdt||�|d�|jd��dS)Nr�r|r()rprrr�r))r>r8rr�rrr�_item_replacedVs��zDiffBuilder._item_replacedc	Cs�t|���}t|���}||}||}|D]
}|�|t|�||�q|D]
}|�|t|�||�q&||@D]}|�||||||�q8dSrD)�set�keysr�rXr�r�)	r>r8r4r5�src_keys�dst_keys�
added_keys�removed_keysrrrr�_compare_dicts]s�zDiffBuilder._compare_dictscCs�t|�t|�}}t||�}t||�}t|�D]d}||krd||||}	}
|	|
kr+qt|	t�r@t|
t�r@|�t||�|	|
�qt|	t�rUt|
t�rU|�	t||�|	|
�q|�
|||	�|�|||
�q||krr|�
||||�q|�||||�qdSrD)r�max�minr�r*rr�r�r	�_compare_listsr�r�)r>r8r4r5�len_src�len_dst�max_len�min_lenr�old�newrrrr�ls,


�
��zDiffBuilder._compare_listscCs�t|t�rt|t�r|�t||�||�dSt|t�r,t|t�r,|�t||�||�dS|�|�|�|�kr8dS|�|||�dSrD)r*rr�r�r	r�r�r�)r>r8rr4r5rrrr��s
�
�	zDiffBuilder._compare_valuesN)rrrr�r�rr@r�r�rprxr�r�r�r�r�r�r�r�r�rrrrr��s


!r�cCs,|dur|S|dt|��dd��dd�S)NrR�~z~0z~1)rXr�rerrrr��s r�)5r�
__future__rrr��	functoolsr��sys�collections.abcr�ImportError�typesrr!�jsonpointerrrr�r�rr	�unicoderX�
__author__�__version__�__website__�__license__�version_info�bytesr+�	Exceptionrrr�AssertionErrorrr&�partialr�r�r2r6�objectr7r\rkrrrur�r�r,r�r�rrrr�<module>sh ���
	(6"4*VQX