
    i                        d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d	d
dZ G d de      Zy)    )annotationsN)IOAnyOptional)uuid4)Dataset)_nq_row)_nt_row)
SerializerAD)addremovec                  T     e Zd ZdZ	 	 d fdZ	 	 d	 	 	 	 	 	 	 	 	 ddZd Zd Z xZS )	PatchSerializerz
    Creates an RDF patch file to add and remove triples/quads.
    Can either:
    - Create an add or delete patch for a single Dataset.
    - Create a patch to represent the difference between two Datasets.
    c                2    || _         t        | 	  |       y N)storesuper__init__)selfr   	__class__s     OC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/plugins/serializers/patch.pyr   zPatchSerializer.__init__   s     $
    c                &    |j                  d      }|j                  d      }|j                  d      |j                  d      sdt                 j                  |t        j                  d       Cj                          j                  j                         k7  rt        j                  d        fd	}d fd
	}|r|t        v sJ d|        |sd} |        |r7t        j                  |      }	 | j                  j                         |	       nH|rF j                  |      \  }
} ||
j                         dd        ||j                         dd       j                  dj                  d             y)ar  
        Serialize the store to the given stream.
        :param stream: The stream to serialize to.
        :param base: The base URI to use for the serialization.
        :param encoding: The encoding to use for the serialization.
        :param kwargs: Additional keyword arguments.
        Supported keyword arguments:
        - operation: The operation to perform. Either 'add' or 'remove'.
        - target: The target Dataset to compare against.
        NB: Only one of 'operation' or 'target' should be provided.
        - header_id: The header ID to use.
        - header_prev: The previous header ID to use.
        	operationtarget	header_idheader_prevzuuid:Nz&PatchSerializer does not support base.zBPatchSerializer does not use custom encoding. Given encoding was: c                     j                  d dj                   d             r%j                  d dj                   d             j                  dj                   d             y )NzH id <z> .
replacezH prev <z>
zTX .
)writeencode)encodingr   r   streams   r   write_headerz/PatchSerializer.serialize.<locals>.write_headerA   sa    LL6)E299(INOx}C8??)TULL9=>r   c           	         | D ]l  }|s%j                   j                  |j                        }|D ]>  }j                  j	                  ||j                  |      j                  d             @ n y )Nr!   )r   get_context
identifierr"   
_patch_rowr#   )contextsop_codeuse_passed_contextscontexttripler$   r   r%   s        r   write_triplesz0PatchSerializer.serialize.<locals>.write_triplesG   sf    #*"jj44W5G5GHG%FLL0B0BGLSS$i & $r   zInvalid operation: r   r   T)r-   r   zTC .
r!   )F)getr   r$   warningswarnloweradd_remove_methodsr   r+   _diffr"   r#   )r   r%   baser$   kwargsr   r   r&   r0   operation_codeto_add	to_remover   r   s   `` `        @@r   	serializezPatchSerializer.serialize   s_   ( JJ{+	H%JJ{+	jj/y)I==MMBCHNN$48K8K8M$MMM''/j2
	?		  22U6I)4UU2 I/33I>N$**--/@ $

6 2FI&//+SdK),,.NX__Xy9:r   c                F    || j                   z
  }| j                   |z
  }||fS r   )r   )r   r   rows_to_addrows_to_removes       r   r6   zPatchSerializer._diffc   s)    tzz)f,N**r   c                    || j                   j                  j                  k(  r| dt        |       S | dt	        ||       S )N )r   default_contextr)   r
   r	   )r   r/   
context_idr   s       r   r*   zPatchSerializer._patch_rowh   sI    33>>>['&/!233['&*"=!>??r   )r   r   )NN)
r%   z	IO[bytes]r7   Optional[str]r$   rD   r8   r   returnNone)	__name__
__module____qualname____doc__r   r<   r6   r*   __classcell__)r   s   @r   r   r      sd       #"&	C;C; C;  	C;
 C; 
C;J+
@r   r   )
__future__r   r2   typingr   r   r   uuidr   rdflibr   !rdflib.plugins.serializers.nquadsr	   rdflib.plugins.serializers.ntr
   rdflib.serializerr   r5   r    r   r   <module>rT      s;    "  $ $   5 1 ( C0 ]@j ]@r   