%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core18/current/usr/lib/python3/dist-packages/__pycache__/
Upload File :
Create Path :
Current File : //snap/core18/current/usr/lib/python3/dist-packages/__pycache__/jsonpatch.cpython-36.pyc

3

��BY�h�
@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	yddl
mZmZWn$e
k
r�ddlmZmZYnXddlmZmZdZdZdZd	Ze	jdBkr�eefZGdd�de�ZGd
d�de�ZGdd�de�ZGdd�dee�Zdd�Zdd�Z e �e_!dCdd�Z"dd�Z#Gdd�de$�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&�Z+Gd*d+�d+e&�Z,dDd-d.�Z-d/d0�Z.dId2d3�Z/d4d5�Z0d6d7�Z1d8d9�Z2d:d;�Z3d<d=�Z4d>d?�Z5d@dA�Z6dS)Jz Apply JSON-Patches (RFC 6902) �)�unicode_literalsN)�MutableMapping�MutableSequence)�JsonPointer�JsonPointerExceptionu Stefan Kögl <stefan@skoegl.net>z1.16z0https://github.com/stefankoegl/python-json-patchzModified BSD License�c@seZdZdZdS)�JsonPatchExceptionzBase Json Patch exceptionN)�__name__�
__module__�__qualname__�__doc__�r
r
�+/usr/lib/python3/dist-packages/jsonpatch.pyr@src@seZdZdZdS)�InvalidJsonPatchz, Raised if an invalid JSON Patch is created N)r	r
rrr
r
r
rrDsrc@seZdZdZdS)�JsonPatchConflictaRaised if patch could not be applied due to conflict situation such as:
    - attempt to add object key then it already exists;
    - attempt to operate with nonexistence object key;
    - attempt to insert value to array at position beyond of it size;
    - etc.
    N)r	r
rrr
r
r
rrHsrc@seZdZdZdS)�JsonPatchTestFailedz A Test operation failed N)r	r
rrr
r
r
rrQsrcCs@tjt�}x|D]\}}||j|�qWtdd�|j�D��S)z'Convert duplicate keys values to lists.css.|]&\}}|t|�dkr |dn|fVqdS)�rN)�len)�.0�key�valuesr
r
r�	<genexpr>^szmultidict.<locals>.<genexpr>)�collections�defaultdict�list�append�dict�items)Z
ordered_pairsZmdictr�valuer
r
r�	multidictUs
rcCsFtjdkrtjtj�j}ntjtj�j}d|kr6tjSt	j
tjtd�S)a� adds the object_pairs_hook parameter to json.load when possible

    The "object_pairs_hook" parameter is used to handle duplicate keys when
    loading a JSON object. This parameter does not exist in Python 2.6. This
    methods returns an unmodified json.load for Python 2.6 and a partial
    function with object_pairs_hook set to multidict for Python versions that
    support the parameter. r�object_pairs_hook)r )rr)�sys�version_info�inspectZ	signature�json�loadZ
parametersZ
getargspec�args�	functools�partialr)r&r
r
r�get_loadjsoncs	
r)FcCs*t|t�rtj|�}nt|�}|j||�S)aOApply 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

    :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
    )�
isinstance�
basestring�	JsonPatch�from_string�apply)�doc�patch�in_placer
r
r�apply_patchxs
r2cCs8tj||�}|j|�}||kr,tj||d�Stj||�S)a�Generates patch by comparing of 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

    >>> 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
    F)r,�	from_diffr.)�src�dstr0�newr
r
r�
make_patch�s

r7c@s�eZdZdZdd�Zdd�Zdd�ZeZdd	�Zd
d�Z	dd
�Z
dd�Zedd��Z
eddd��Zdd�Zedd��Zd dd�Zdd�ZdS)!r,agA JSON Patch is a list of Patch Operations.

    >>> patch = JsonPatch([
    ...     {'op': 'add', 'path': '/foo', 'value': 'bar'},
    ...     {'op': 'add', 'path': '/baz', 'value': [1, 2, 3]},
    ...     {'op': 'remove', 'path': '/baz/1'},
    ...     {'op': 'test', 'path': '/baz', 'value': [1, 3]},
    ...     {'op': 'replace', 'path': '/baz/0', 'value': 42},
    ...     {'op': 'remove', 'path': '/baz/1'},
    ... ])
    >>> doc = {}
    >>> result = patch.apply(doc)
    >>> expected = {'foo': 'bar', 'baz': [42]}
    >>> result == expected
    True

    JsonPatch object is iterable, so you could easily access to each patch
    statement in loop:

    >>> lpatch = list(patch)
    >>> expected = {'op': 'add', 'path': '/foo', 'value': 'bar'}
    >>> lpatch[0] == expected
    True
    >>> lpatch == patch.patch
    True

    Also JsonPatch could be converted directly to :class:`bool` if it contains
    any operation statements:

    >>> bool(patch)
    True
    >>> bool(JsonPatch([]))
    False

    This behavior is very handy with :func:`make_patch` to write more readable
    code:

    >>> old = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
    >>> new = {'baz': 'qux', 'numbers': [1, 4, 7]}
    >>> patch = make_patch(old, new)
    >>> if patch:
    ...     # document have changed, do something useful
    ...     patch.apply(old)    #doctest: +ELLIPSIS
    {...}
    cCs||_ttttttd�|_dS)N)�remove�add�replace�moveZtest�copy)r0�RemoveOperation�AddOperation�ReplaceOperation�
MoveOperation�
TestOperation�
CopyOperation�
operations)�selfr0r
r
r�__init__�szJsonPatch.__init__cCs|j�S)zstr(self) -> self.to_string())�	to_string)rDr
r
r�__str__�szJsonPatch.__str__cCs
t|j�S)N)�boolr0)rDr
r
r�__bool__�szJsonPatch.__bool__cCs
t|j�S)N)�iterr0)rDr
r
r�__iter__�szJsonPatch.__iter__cCstt|j��S)N)�hash�tuple�_ops)rDr
r
r�__hash__�szJsonPatch.__hash__cCst|t�sdS|j|jkS)NF)r*r,rN)rD�otherr
r
r�__eq__s
zJsonPatch.__eq__cCs
||kS)Nr
)rDrPr
r
r�__ne__szJsonPatch.__ne__cCstj|�}||�S)z�Creates JsonPatch instance from string source.

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

        :return: :class:`JsonPatch` instance.
        )r$�loads)�clsZ	patch_strr0r
r
rr-
s	
zJsonPatch.from_stringTcs:��fdd���fdd���fdd��|t�g||���S)aOCreates JsonPatch instance based on comparing 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

        :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
        c3s�||krdSt|t�r>t|t�r>xh�|||�D]
}|Vq.WnLt|t�rpt|t�rpx6�|||�D]
}|Vq`Wntj|�}d|j|d�VdS)Nr:)�op�pathr)r*rrr�
from_partsrV)rVrrP�	operation�ptr)�
compare_dicts�
compare_listsr
r�compare_values+s




z+JsonPatch.from_diff.<locals>.compare_valuesc3s�xb|D]Z}||kr2tj||g�}d|jd�Vq||g}x"�|||||�D]
}|VqRWqWx8|D]0}||krjtj||g�}d|j||d�VqjWdS)Nr8)rUrVr9)rUrVr)rrWrV)rVr4r5rrYZcurrentrX)r\r
rrZ:s


z*JsonPatch.from_diff.<locals>.compare_dictscst|||�d�S)N)�optimization)�_compare_lists)rVr4r5)r]r
rr[Jsz*JsonPatch.from_diff.<locals>.compare_lists)r)rTr4r5r]r
)rZr[r\r]rr3szJsonPatch.from_diffcCstj|j�S)z!Returns patch set as JSON string.)r$�dumpsr0)rDr
r
rrFOszJsonPatch.to_stringcCstt|j|j��S)N)rM�map�_get_operationr0)rDr
r
rrNSszJsonPatch._opsFcCs,|stj|�}x|jD]}|j|�}qW|S)a/Applies the patch to given object.

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

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

        :return: Modified `obj`.
        )r<�deepcopyrNr.)rD�objr1rXr
r
rr.Ws


zJsonPatch.applycCsTd|krtd��|d}t|t�s*td��||jkrBtdj|���|j|}||�S)NrUz&Operation does not contain 'op' memberzOperation must be a stringzUnknown operation {0!r})rr*r+rC�format)rDrXrUrTr
r
rrals


zJsonPatch._get_operationN)T)F)r	r
rrrErGrIZ__nonzero__rKrOrQrR�classmethodr-r3rF�propertyrNr.rar
r
r
rr,�s -8
r,c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�PatchOperationz'A single operation inside a JSON Patch.cCs |d|_t|j�|_||_dS)NrV)�locationr�pointerrX)rDrXr
r
rrEs
zPatchOperation.__init__cCstd��dS)zAAbstract method that applies patch operation to specified object.z!should implement patch operation.N)�NotImplementedError)rDrcr
r
rr.�szPatchOperation.applycCstt|jj���S)N)rL�	frozensetrXr)rDr
r
rrO�szPatchOperation.__hash__cCst|t�sdS|j|jkS)NF)r*rgrX)rDrPr
r
rrQ�s
zPatchOperation.__eq__cCs
||kS)Nr
)rDrPr
r
rrR�szPatchOperation.__ne__N)	r	r
rrrEr.rOrQrRr
r
r
rrg|srgc@seZdZdZdd�ZdS)r=z/Removes an object property or an array element.cCsX|jj|�\}}y
||=Wn8ttfk
rR}zdj|�}t|��WYdd}~XnX|S)Nz&can't remove non-existent object '{0}')ri�to_last�KeyError�
IndexErrorrdr)rDrc�subobj�part�ex�msgr
r
rr.�s

zRemoveOperation.applyN)r	r
rrr.r
r
r
rr=�sr=c@seZdZdZdd�ZdS)r>z,Adds an object property or an array element.cCs�y|jd}Wn*tk
r8}ztd��WYdd}~XnX|jj|�\}}t|t�r�|dkrh|j|�q�|t|�ks||dkr�t	d��q�|j
||�n4t|t�r�|dkr�|}q�|||<ntdj
t|����|S)Nrz/The operation does not contain a 'value' member�-rzcan't insert outside of listzinvalid document type {0})rXrmrrirlr*rrrr�insertr�	TypeErrorrd�type)rDrcrrqrorpr
r
rr.�s$



zAddOperation.applyN)r	r
rrr.r
r
r
rr>�sr>c@seZdZdZdd�ZdS)r?z=Replaces an object property or an array element by new value.cCs�y|jd}Wn*tk
r8}ztd��WYdd}~XnX|jj|�\}}|dkrV|St|t�r~|t|�kst|dkr�td��n8t|t	�r�||kr�dj
|�}t|��ntdj
t|����|||<|S)Nrz/The operation does not contain a 'value' memberrzcan't replace outside of listz'can't replace non-existent object '{0}'zinvalid document type {0})
rXrmrrirlr*rrrrrdrurv)rDrcrrqrorprrr
r
rr.�s$




zReplaceOperation.applyN)r	r
rrr.r
r
r
rr?�sr?c@seZdZdZdd�ZdS)r@z=Moves an object property or an array element to new location.c!Cs�yt|jd�}Wn*tk
r<}ztd��WYdd}~XnX|j|�\}}y||}Wn2ttfk
r�}ztt|���WYdd}~XnX|j|kr�|St	|t
�r�|jj|�r�td��td|jdd��j
|�}td|j|d��j
|�}|S)N�fromz.The operation does not contain a 'from' memberz(Cannot move values into its own childrenr8)rUrVr9)rUrVr)rrXrmrrlrnr�strrir*r�containsr=r.r>rh)rDrc�from_ptrrqrorprr
r
rr.�s2


zMoveOperation.applyN)r	r
rrr.r
r
r
rr@�sr@c@seZdZdZdd�ZdS)rAz!Test value by specified location.c#Cs�y0|jj|�\}}|dkr |}n|jj||�}Wn.tk
r^}ztt|���WYdd}~XnXy|jd}Wn*tk
r�}ztd��WYdd}~XnX||kr�d}t|j	|t
|�|t
|����|S)Nrz/The operation does not contain a 'value' memberz0{0} ({1}) is not equal to tested value {2} ({3}))rirl�walkrrrxrXrmrrdrv)rDrcrorp�valrqrrrr
r
rr.s"zTestOperation.applyN)r	r
rrr.r
r
r
rrAsrAc@seZdZdZdd�ZdS)rBzA Copies an object property or an array element to a new location c!Cs�yt|jd�}Wn*tk
r<}ztd��WYdd}~XnX|j|�\}}ytj||�}Wn2ttfk
r�}ztt	|���WYdd}~XnXt
d|j|d��j|�}|S)Nrwz.The operation does not contain a 'from' memberr9)rUrVr)
rrXrmrrlr<rbrnrrxr>rhr.)rDrcrzrqrorprr
r
rr.&s 
zCopyOperation.applyN)r	r
rrr.r
r
r
rrB#srBTcCs0tt|||ft||����}|r,tt|��S|S)z7Compares two lists objects and return JSON patch about.)r�_compare�_split_by_common_seq�	_optimize)rVr4r5r]r0r
r
rr^<sr^c
st|�t|�}�tt���}�fdd�t|�D�}d}d\}}x�tjt|�|�D]�\}}	||||	k�r|dks||	dkr�d|||	<n ||d|	dd|||	<|||	|kr�|||	}|||	|k�r||d|df}|	|d|	df}qRd|||	<qRW||fS)a�Returns pair of ranges of longest common subsequence for the `src`
    and `dst` lists.

    >>> src = [1, 2, 3, 4]
    >>> dst = [0, 1, 2, 3, 5]
    >>> # The longest common subsequence for these lists is [1, 2, 3]
    ... # which is located at (0, 3) index range for src list and (1, 4) for
    ... # dst one. Tuple of these ranges we should get back.
    ... assert ((0, 3), (1, 4)) == _longest_common_subseq(src, dst)
    csg|]}dg��qS)rr
)r�_)�ldstr
r�
<listcomp>Qsz*_longest_common_subseq.<locals>.<listcomp>rNr)NN)rr�range�	itertools�product)
r4r5ZlsrcZdrangeZmatrix�zZ	range_srcZ	range_dst�i�jr
)r�r�_longest_common_subseqDs" r�rc	Cs|d|dkr|nd}|d|dkr,|nd}|s<d|gS|sH|dgSt||�\}}|dksf|dkrn||gSt|d|d�|d|d�|d|d|df|d|d|df�t||dd�||dd�|d|d|dt|�f|d|d|dt|�f�gS)a�Recursively splits the `dst` list onto two parts: left and right.
    The left part contains differences on left from common subsequence,
    same as the right part by for other side.

    To easily understand the process let's take two lists: [0, 1, 2, 3] as
    `src` and [1, 2, 4, 5] for `dst`. If we've tried to generate the binary tree
    where nodes are common subsequence for both lists, leaves on the left
    side are subsequence for `src` list and leaves on the right one for `dst`,
    our tree would looks like::

        [1, 2]
       /         [0]       []
             /            [3]   [4, 5]

    This function generate the similar structure as flat tree, but without
    nodes with common subsequences - since we're don't need them - only with
    left and right leaves::

        []
       /     [0]  []
        /      [3]  [4, 5]

    The `bx` is the absolute range for currently processed subsequence of
    `src` list.  The `by` means the same, but for the `dst` list.
    rrN)r�r~r)r4r5ZbxZby�x�yr
r
rr~dsr~ccs*x$t|||||d�D]\}}|VqWdS)zESame as :func:`_compare_with_shift` but strips emitted `shift` value.rN)�_compare_with_shift)rVr4r5�left�rightrUr�r
r
rr}�sr}ccs�t|t�r<x^t|||f|�d|i�D]\}}||fVq$Wn.|dk	rjx$t||||�D]\}}||fVqTWt|t�r�x^t|||f|�d|i�D]\}}||fVq�Wn.|dk	r�x$t||||�D]\}}||fVq�WdS)aRecursively compares differences from `left` and `right` sides
    from common subsequences.

    The `shift` parameter is used to store index shift which caused
    by ``add`` and ``remove`` operations.

    Yields JSON patch operations and list index shift.
    �shiftN)r*rr��
_compare_left�_compare_right)rVr4r5r�r�r��itemr
r
rr��s	

r�ccsx|\}}|dkrt|�}xZtt||||��D]@}tj|t|�g�}d||||jd�|dfV|d8}q0WdS)z`Yields JSON patch ``remove`` operations for elements that are only
    exists in the `src` list.rr8)rUrrVN���)r�reversedr�rrWrxrV)rVr4r�r��start�end�idxrYr
r
rr��s
r�ccsh|\}}|dkrt|�}xJt||�D]<}tj|t|�g�}d|j||d�|dfV|d7}q$WdS)z\Yields JSON patch ``add`` operations for elements that are only
    exists in the `dst` listrr9)rUrVrNr�)rr�rrWrxrV)rVr5r�r�r�r�r�rYr
r
rr��sr�ccsg}i}i}tddg�}x�|D]�}t|dttf�}|d|krVt||d|�q|r�|d|kr�||d}t|d|dg�|kr�t||�|j|d�q|j|�|||d<|r|||d<qW|j�|j�x(|D] }|ddkr�|jd�|Vq�WdS)arOptimizes operations which was produced by lists comparison.

    Actually it does two kinds of optimizations:

    1. Seeks pair of ``remove`` and ``add`` operations against the same path
       and replaces them with ``replace`` operation.
    2. Seeks pair of ``remove`` and ``add`` operations for the same value
       and replaces them with ``move`` operation.
    r9r8rrVrUN)	�setr*rr�_optimize_using_replace�_optimize_using_move�popr�clear)rC�resultZops_by_pathZops_by_valueZ
add_remover�Zhashable_value�	prev_itemr
r
rr�s4





rcCs$d|d<|ddk�r t|d|d�}t|dt�r�t|dt�r�t|dj��dkr�t|dj��}t|dj��}||@|kr�t|d|d�}t|j�dko�|jdddko�|jddo�|jddjd	�d|dk�r|d|jdd|d<|jdd|d<n|d|d<d
S)z�Optimises by replacing ``add``/``remove`` with ``replace`` on same path

    For nested strucures, tries to recurse replacement, see #36 r:rUr9rrrr8rV�/N)r7r*rr�keysr�r0�split)�prevZcurr0Zprev_setZcur_setr
r
rr�s00r�cCs�d|d<|d|df|d|dfg|ddk\}}|ddkrV||d<||d<n4|jdd�\}}t|�d}|d||d<||d<d	S)
z�Optimises JSON patch by using ``move`` operation instead of
    ``remove` and ``add`` against the different paths but for the same value.r;rUrVr9rwr�rz/%dN)�rsplit�int)r�r�Z	move_fromZmove_to�headr
r
rr� s
r�)rr)F)Tr��rr�r��rr�)r�r�)7rZ
__future__rrr<r'r#r�r$r!�collections.abcrr�ImportErrorZjsonpointerrr�
__author__�__version__Z__website__Z__license__r"�bytesrxr+�	Exceptionrrr�AssertionErrorrrr)r%r2r7�objectr,rgr=r>r?r@rArBr^r�r~r}r�r�r�rr�r�r
r
r
r�<module>!s^
	
%D"&
 
5*

Zerion Mini Shell 1.0