관리-도구
편집 파일: jsonpatch.cpython-39.opt-1.pyc
a ����d�GZ�_����������������������@���s���d�Z�ddlmZ�ddlZddlZddlZddlZddlZddlZddl Z ddl mZmZ�dZ dZzddlmZmZ�W�n&�ey����ddlmZmZ�eZY�n0�dZdZd Zd Ze jdkr�eefZG�dd ��d e�ZG�dd��de�ZG�dd��de�ZG�dd��dee �Z!dd��Z"ej#ej$e"d�Z%d0dd�Z&dd��Z'G�dd��de(�Z)G�dd��de(�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(�Z1d.d/��Z2dS�)1z Apply JSON-Patches (RFC 6902) �����)�unicode_literalsN)�JsonPointer�JsonPointerException����)�MutableMapping�MutableSequenceu ���Stefan Kögl <stefan@skoegl.net>z1.21z0https://github.com/stefankoegl/python-json-patchzModified BSD License)����r���c�������������������@���s���e�Zd�ZdZdS�)�JsonPatchExceptionzBase Json Patch exceptionN��__name__� __module__�__qualname__�__doc__��r���r����-/usr/lib/python3.9/site-packages/jsonpatch.pyr ���G���s���r ���c�������������������@���s���e�Zd�ZdZdS�)�InvalidJsonPatchz, Raised if an invalid JSON Patch is created Nr ���r���r���r���r���r���K���s���r���c�������������������@���s���e�Zd�ZdZdS�)�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. Nr ���r���r���r���r���r���O���s���r���c�������������������@���s���e�Zd�ZdZdS�)�JsonPatchTestFailedz A Test operation failed Nr ���r���r���r���r���r���X���s���r���c�����������������C���s<���t��t�}|�D�]\}}||��|��qtdd��|���D���S�)z'Convert duplicate keys values to lists.c�����������������s���s.���|�]&\}}|t�|�d�kr |d�n|fV��qdS�)r���r���N)�len)�.0�key�valuesr���r���r���� <genexpr>c���s����zmultidict.<locals>.<genexpr>)�collections�defaultdict�list�append�dict�items)Z ordered_pairsZmdictr����valuer���r���r���� multidict\���s���� �r ���)Zobject_pairs_hookFc�����������������C���s*���t�|t�rt�|�}nt|�}|�|�|�S�)aO��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 :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_patcho���s���� r)���c�����������������C���s���t��|�|�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 )r#���� from_diff)�src�dstr���r���r���� make_patch����s����r-���c�������������������@���s����e�Zd�ZdZdd��Zdd��Zdd��ZeZdd ��Zd d��Z dd ��Z dd��Zedd���Z eddd��Zdd��Zedd���Zd dd�Zdd��ZdS�)!r#���ag��A 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 {...} c�����������������C���s���||�_�ttttttd�|�_d�S�)N)�remove�add�replace�move�test�copy)r'����RemoveOperation�AddOperation�ReplaceOperation� MoveOperation� TestOperation� CopyOperation� operations)�selfr'���r���r���r����__init__����s�����zJsonPatch.__init__c�����������������C���s���|�����S�)zstr(self) -> self.to_string())� to_string�r;���r���r���r����__str__����s����zJsonPatch.__str__c�����������������C���s ���t�|�j�S��N)�boolr'���r>���r���r���r����__bool__����s����zJsonPatch.__bool__c�����������������C���s ���t�|�j�S�r@���)�iterr'���r>���r���r���r����__iter__����s����zJsonPatch.__iter__c�����������������C���s���t�t|�j��S�r@���)�hash�tuple�_opsr>���r���r���r����__hash__����s����zJsonPatch.__hash__c�����������������C���s���t�|t�sdS�|�j|jkS��NF)r!���r#���rG����r;����otherr���r���r����__eq__����s���� zJsonPatch.__eq__c�����������������C���s ���|�|k�S�r@���r���rJ���r���r���r����__ne__����s����zJsonPatch.__ne__c�����������������C���s���t�|�}|�|�S�)z�Creates JsonPatch instance from string source. :param patch_str: JSON patch as raw string. :type patch_str: str :return: :class:`JsonPatch` instance. )� _jsonloads)�clsZ patch_strr'���r���r���r���r$�������s���� zJsonPatch.from_stringTc�����������������C���s*���t���}|�dd||��t|����}|�|�S�)aO��Creates 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 ��N)�DiffBuilder�_compare_valuesr����execute)rO���r+���r,����optimizationZbuilder�opsr���r���r���r*�����s����zJsonPatch.from_diffc�����������������C���s���t��|�j�S�)z!Returns patch set as JSON string.)�json�dumpsr'���r>���r���r���r���r=���!��s����zJsonPatch.to_stringc�����������������C���s���t�t|�j|�j��S�r@���)rF����map�_get_operationr'���r>���r���r���r���rG���%��s����zJsonPatch._opsFc�����������������C���s(���|st��|�}|�jD�]}|�|�}q|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`. )r3����deepcopyrG���r%���)r;����objr(���� operationr���r���r���r%���)��s ���� zJsonPatch.applyc�����������������C���sT���d|vrt�d��|d�}t|t�s*t�d��||�jvrBt�d�|���|�j|�}||�S�)N�opz&Operation does not contain 'op' memberzOperation must be a stringzUnknown operation {0!r})r���r!���r"���r:����format)r;���r\���r]���rO���r���r���r���rY���>��s���� zJsonPatch._get_operationN)T)F)r���r���r ���r���r<���r?���rB���Z__nonzero__rD���rH���rL���rM����classmethodr$���r*���r=����propertyrG���r%���rY���r���r���r���r���r#�������s$���- r#���c�������������������@���s^���e�Zd�ZdZdd��Zdd��Zdd��Zdd ��Zd d��Ze dd ���Z e dd���Zejdd���ZdS�)�PatchOperationz'A single operation inside a JSON Patch.c�����������������C���s ���|d�|�_�t|�j��|�_||�_d�S�)N�path)�locationr����pointerr\���)r;���r\���r���r���r���r<���Q��s���� zPatchOperation.__init__c�����������������C���s���t�d��dS�)zAAbstract method that applies patch operation to specified object.z!should implement patch operation.N)�NotImplementedError)r;���r[���r���r���r���r%���V��s����zPatchOperation.applyc�����������������C���s���t�t|�j�����S�r@���)rE���� frozensetr\���r���r>���r���r���r���rH���Z��s����zPatchOperation.__hash__c�����������������C���s���t�|t�sdS�|�j|jkS�rI���)r!���ra���r\���rJ���r���r���r���rL���]��s���� zPatchOperation.__eq__c�����������������C���s ���|�|k�S�r@���r���rJ���r���r���r���rM���b��s����zPatchOperation.__ne__c�����������������C���s���d��|�jjd�d���S�)N�/���)�joinrd����partsr>���r���r���r���rb���e��s����zPatchOperation.pathc�����������������C���s6���zt�|�jjd��W�S��ty0���|�jjd��Y�S�0�d�S�)Nrh���)�intrd���rj���� ValueErrorr>���r���r���r���r���i��s����zPatchOperation.keyc�����������������C���s*���t�|�|�jjd<�|�jj|�_|�j|�jd<�d�S�)Nrh���rb���)�strrd���rj���rb���rc���r\���)r;���r���r���r���r���r���p��s���� N) r���r���r ���r���r<���r%���rH���rL���rM���r`���rb���r����setterr���r���r���r���ra���N��s��� ra���c�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�) r4���z/Removes an object property or an array element.c�������������� ���C���sZ���|�j��|�\}}z ||=�W�n:�ttfyT�}�zd�|�}t|��W�Y�d�}~n d�}~0�0�|S�)Nz&can't remove non-existent object '{0}')rd����to_last�KeyError� IndexErrorr^���r���)r;���r[����subobj�part�ex�msgr���r���r���r%���z��s���� zRemoveOperation.applyc�����������������C���s0���|�j�|kr,|�j|kr$|��jd7��_n|d8�}|S��Nr����rb���r����r;���rb���r���r���r���r����_on_undo_remove���s ���� zRemoveOperation._on_undo_removec�����������������C���s0���|�j�|kr,|�j|kr$|��jd8��_n|d8�}|S�rv���rw���rx���r���r���r����_on_undo_add���s ���� zRemoveOperation._on_undo_addN�r���r���r ���r���r%���ry���rz���r���r���r���r���r4���w��s��� r4���c�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�) r5���z,Adds an object property or an array element.c�������������� ���C���s����z|�j�d�}W�n,�ty:�}�ztd��W�Y�d�}~n d�}~0�0�|�j�|�\}}t|t�r�|dkrj|�|��q�|t|�ks~|dk�r�t d��q�|� ||��n4t|t�r�|d�u�r�|}q�|||<�ntd� t|����|S�)Nr����/The operation does not contain a 'value' member�-r���zcan't insert outside of list�invalid document type {0})r\���rp���r���rd���ro���r!���r���r���r���r����insertr���� TypeErrorr^����type)r;���r[���r���rt���rr���rs���r���r���r���r%������s&����� zAddOperation.applyc�����������������C���s0���|�j�|kr,|�j|kr$|��jd7��_n|d7�}|S�rv���rw���rx���r���r���r���ry������s ���� zAddOperation._on_undo_removec�����������������C���s0���|�j�|kr,|�j|kr$|��jd8��_n|d7�}|S�rv���rw���rx���r���r���r���rz������s ���� zAddOperation._on_undo_addNr{���r���r���r���r���r5������s���r5���c�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�) r6���z=Replaces an object property or an array element by new value.c�������������� ���C���s����z|�j�d�}W�n,�ty:�}�ztd��W�Y�d�}~n d�}~0�0�|�j�|�\}}|d�u�rX|S�t|t�r�|t|�ksv|dk�r�td��n8t|t �r�||vr�d� |�}t|��ntd� t|����|||<�|S�)Nr���r|���r���zcan't replace outside of listz'can't replace non-existent object '{0}'r~���) r\���rp���r���rd���ro���r!���r���r���r���r���r^���r����r����)r;���r[���r���rt���rr���rs���ru���r���r���r���r%������s&����� zReplaceOperation.applyc�����������������C���s���|S�r@���r���rx���r���r���r���ry������s����z ReplaceOperation._on_undo_removec�����������������C���s���|S�r@���r���rx���r���r���r���rz������s����zReplaceOperation._on_undo_addNr{���r���r���r���r���r6������s���r6���c�������������������@���sN���e�Zd�ZdZdd��Zedd���Zedd���Zejdd���Zd d ��Z dd��Z d S�)r7���z=Moves an object property or an array element to new location.c�������������� ���C���s����zt�|�jd��}W�n,�ty>�}�ztd��W�Y�d�}~n d�}~0�0�|�|�\}}z||�}W�n4�ttfy��}�ztt|���W�Y�d�}~n d�}~0�0�|�j|kr�|S�t |t �r�|�j�|�r�td��td|�jd�d��� |�}td|�j|d��� |�}|S�)N�from�.The operation does not contain a 'from' memberz(Cannot move values into its own childrenr.����r]���rb���r/����r]���rb���r���)r���r\���rp���r���ro���rq���r���rm���rd���r!���r����containsr4���r%���r5���rc����r;���r[����from_ptrrt���rr���rs���r���r���r���r���r%������s>�����" �����zMoveOperation.applyc�����������������C���s"���t�|�jd��}d�|jd�d���S�)Nr����rg���rh���)r���r\���ri���rj����r;���r����r���r���r���� from_path��s����zMoveOperation.from_pathc�����������������C���s@���t�|�jd��}zt|jd��W�S��ty:���|jd��Y�S�0�d�S��Nr����rh���)r���r\���rk���rj���r����r����r���r���r����from_key��s ����zMoveOperation.from_keyc�����������������C���s,���t�|�jd��}t|�|jd<�|j|�jd<�d�S�r����)r���r\���rm���rj���rb���)r;���r���r����r���r���r���r������s����c�����������������C���s\���|�j�|kr,|�j|kr$|��jd7��_n|d8�}|�j|krX|�j|krP|��jd7��_n|d7�}|S�rv����r����r����rb���r���rx���r���r���r���ry���#��s���� zMoveOperation._on_undo_removec�����������������C���s\���|�j�|kr,|�j|kr$|��jd8��_n|d8�}|�j|krX|�j|krP|��jd8��_n|d7�}|S�rv���r����rx���r���r���r���rz���0��s���� zMoveOperation._on_undo_addN)r���r���r ���r���r%���r`���r����r����rn���ry���rz���r���r���r���r���r7������s���" r7���c�������������������@���s���e�Zd�ZdZdd��ZdS�)r8���z!Test value by specified location.c�������������� ���C���s����z0|�j��|�\}}|d�u�r |}n|�j��||�}W�n0�ty`�}�ztt|���W�Y�d�}~n d�}~0�0�z|�jd�}W�n,�ty��}�ztd��W�Y�d�}~n d�}~0�0�||kr�d}t|� |t |�|t |����|S�)Nr���r|���z0{0} ({1}) is not equal to tested value {2} ({3}))rd���ro����walkr���r���rm���r\���rp���r���r^���r����)r;���r[���rr���rs����valrt���r���ru���r���r���r���r%���A��s&����"��zTestOperation.applyN�r���r���r ���r���r%���r���r���r���r���r8���>��s���r8���c�������������������@���s���e�Zd�ZdZdd��ZdS�)r9���zA Copies an object property or an array element to a new location c�������������� ���C���s����zt�|�jd��}W�n,�ty>�}�ztd��W�Y�d�}~n d�}~0�0�|�|�\}}zt�||��}W�n4�ttfy��}�ztt |���W�Y�d�}~n d�}~0�0�t d|�j|d���|�}|S�)Nr����r����r/���r����) r���r\���rp���r���ro���r3���rZ���rq���r���rm���r5���rc���r%���r����r���r���r���r%���\��s&�����"��zCopyOperation.applyNr����r���r���r���r���r9���Y��s���r9���c�������������������@���s|���e�Zd�Zdd��Zdd��Zdd��Zdd��Zd d ��Zdd��Zd d��Z dd��Z dd��Zdd��Zdd��Z dd��Zdd��Zdd��ZdS�)rQ���c�����������������C���s4���i�i�g|�_�g�g�g|�_g��|�_}||d�g|d�d��<�d�S�r@���)� index_storage�index_storage2�_DiffBuilder__root)r;����rootr���r���r���r<���t��s���� zDiffBuilder.__init__c�����������������C���sf���z:|�j�|�}|�|�}|d�u�r*|g||<�n||��|��W�n&�ty`���|�j|��||f��Y�n0�d�S�r@���)r�����getr���r����r����)r;���r����index�st�storage�storedr���r���r����store_indexz��s���� zDiffBuilder.store_indexc�����������������C���s����z"|�j�|��|�}|r |���W�S�W�n\�ty~���|�j|�}tt|�d�dd�D�]*}||�d�|krN|�|�d����Y�S�qNY�n0�d�S�)Nr���rh���r���)r����r�����popr����r�����ranger���)r;���r���r����r����r�����ir���r���r���� take_index���s���� zDiffBuilder.take_indexc�����������������C���s,���|�j�}|d�}|||g�|d<�|d<�|d�S�)Nr���r����r����)r;���r]���r����Zlastr���r���r���r������s����zDiffBuilder.insertc�����������������C���s*���|\}}}||d<�||d<�g�|d�d��<�d�S�)Nr���r���r���)r;���r����Z link_prevZ link_next�_r���r���r���r.������s���� zDiffBuilder.removec�����������������c���s.���|�j�}|d�}||ur*|d�V��|d�}qd�S��Nr�������r����)r;����startr�����currr���r���r���� iter_from���s ���� zDiffBuilder.iter_fromc�����������������c���s.���|�j�}|d�}||ur*|d�V��|d�}qd�S�r����r����)r;���r����r����r���r���r���rD������s ���� zDiffBuilder.__iter__c�����������������c���s����|�j�}|d�}||ur�|d�|ur�|d�|d�d��}}|j|jkr�t|�tkr�t|�tkr�td|j|jd�d��jV��|d�d�}q|d�jV��|d�}qd�S�)Nr���r����r0���r���r����)r����rc���r����r4���r5���r6���r\���)r;���r����r����Zop_firstZ op_secondr���r���r���rS������s&���� � ��zDiffBuilder.executec����������� ������C���s����|���|t�}|d�ur�|d�}t|j�tkrL|��|�D�]}|�|j|j�|_q4|��|��|j t ||�kr�td|j t ||�d��}|��|��n.t dt ||�|d��}|��|�}|��||t��d�S�)Nr����r1����r]���r����rb���r/���r����)r����� _ST_REMOVEr����r���rk���r����ry���rb���r.���rc���� _path_joinr7���r���r5���r�����_ST_ADD) r;���rb���r����itemr����r]����v�new_op� new_indexr���r���r����_item_added���s*���� �� zDiffBuilder._item_addedc����������� ������C���s����t�dt||�d��}|��|t�}|��|�}|d�ur�|d�}t|j�tkrj|��|�D�]}|� |j |j�|_qR|��|��|j|jkr�t d|j|jd��}||d<�q�|��|��n|��||t��d�S�)Nr.���r����r����r1���r����)r4���r����r����r����r���r����r���rk���r����rz���rb���r.���rc���r7���r����r����) r;���rb���r���r����r����r����r����r]���r����r���r���r���� _item_removed���s*����� � zDiffBuilder._item_removedc�����������������C���s ���|���tdt||�|d����d�S�)Nr0���r����)r���r6���r����)r;���rb���r���r����r���r���r����_item_replaced���s �����zDiffBuilder._item_replacedc����������� ������C���s����t�|����}t�|����}||�}||�}|D�]}|��|t|�||���q,|D�]}|��|t|�||���qL||@�D�]}|��||||�||���qpd�S�r@���)�set�keysr����rm���r����rR���) r;���rb���r+���r,���Zsrc_keysZdst_keysZ added_keysZremoved_keysr���r���r���r����_compare_dicts���s����zDiffBuilder._compare_dictsc�����������������C���s����t�|�t�|��}}t||�}t||�}t|�D�]�}||k�r�||�||��} } | | krXq.q�t| t�r�t| t�r�|��t||�| | ��q�t| t�r�t| t�r�|�� t||�| | ��q�|�� ||| ��|��||| ��q.||kr�|�� ||||���q.|��||||���q.d�S�r@���)r����max�minr����r!���r���r����r����r����_compare_listsr����r����)r;���rb���r+���r,���Zlen_srcZlen_dstZmax_lenZmin_lenr����old�newr���r���r���r������s*���� � �zDiffBuilder._compare_listsc�����������������C���sr���||krd�S�t�|t�r6t�|t�r6|��t||�||��n8t�|t�r`t�|t�r`|��t||�||��n|��|||��d�S�r@���)r!���r���r����r����r���r����r����)r;���rb���r���r+���r,���r���r���r���rR���'��s���� � �zDiffBuilder._compare_valuesN)r���r���r ���r<���r����r����r���r.���r����rD���rS���r����r����r����r����r����rR���r���r���r���r���rQ���r��s���rQ���c�����������������C���s,���|d�u�r|�S�|�d�t�|��dd��dd��S�)Nrg����~z~0z~1)rm���r0���rw���r���r���r���r����7��s����r����)F)3r���Z __future__r���r���r3���� functools�inspect� itertoolsrV����sysZjsonpointerr���r���r����r�����collections.abcr���r����ImportErrorZunicoderm���� __author__�__version__Z__website__Z__license__�version_info�bytesr"���� Exceptionr ���r���r����AssertionErrorr���r ����partial�loadsrN���r)���r-����objectr#���ra���r4���r5���r6���r7���r8���r9���rQ���r����r���r���r���r����<module>!���sT��� %�&)2$S�F