o
    mi                     @  s   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 deZdS )    )annotationsN)IOAnyOptional)uuid4)Dataset)_nq_row)_nt_row)
SerializerAD)addremovec                      sB   e Zd ZdZd fddZ		ddddZdd Zdd Z  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.
    storer   c                   s   || _ t | d S N)r   super__init__)selfr   	__class__ Z/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/plugins/serializers/patch.pyr      s   zPatchSerializer.__init__Nstream	IO[bytes]baseOptional[str]encodingkwargsr   returnNonec                   s4  | d}| d}| d| dsdt  j |dur(td  dur=  j kr=td    fd	d
}d fdd	}|r]|tv s\J d| n|sad}|  |rtt |}	|j |	 n|r	|\}
}||
 ddd || ddd 
d d dS )an  Serialize the store to the given stream.

        Args:
            stream: The stream to serialize to.
            base: The base URI to use for the serialization.
            encoding: The encoding to use for the serialization.
            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                     sN    d d d r d d d  d d d S )NzH id <z> .
replacezH prev <z>
zTX .
)writeencoder   )r   r#   r$   r   r   r   write_headerD   s   z/PatchSerializer.serialize.<locals>.write_headerFc              	     sH   | D ]}|sj |j}|D ]}||j| d qqd S )Nr%   )r   Zget_context
identifierr&   
_patch_rowr'   )contextsZop_codeuse_passed_contextscontexttriple)r   r   r   r   r   write_triplesJ   s   z0PatchSerializer.serialize.<locals>.write_tripleszInvalid operation: r   r   T)r,   r   zTC .
r%   )F)getr   r   warningswarnloweradd_remove_methodsr   r+   _diffr&   r'   )r   r   r   r   r   r!   r"   r(   r/   Zoperation_codeZto_add	to_remover   )r   r#   r$   r   r   r   	serialize   s<   





zPatchSerializer.serializec                 C  s   || j  }| j | }||fS r   )r   )r   r"   Zrows_to_addZrows_to_remover   r   r   r5   f   s   

zPatchSerializer._diffc                 C  s4   || j jjkr| dt| S | dt|| S )N )r   Zdefault_contextr)   r	   r   )r   r.   Z
context_idr!   r   r   r   r*   k   s   zPatchSerializer._patch_row)r   r   )NN)
r   r   r   r   r   r   r   r   r   r    )	__name__
__module____qualname____doc__r   r7   r5   r*   __classcell__r   r   r   r   r      s    
Hr   )
__future__r   r1   typingr   r   r   uuidr   Zrdflibr   Z!rdflib.plugins.serializers.nquadsr   Zrdflib.plugins.serializers.ntr	   Zrdflib.serializerr
   r4   r   r   r   r   r   <module>   s    
