o
    mi                     @  s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ dgZ			
		
	ddddZd
S )a(  
This file provides a single function `serialize_in_chunks()` which can serialize a
Graph into a number of NT files with a maximum number of triples or maximum file size.

There is an option to preserve any prefixes declared for the original graph in the first
file, which will be a Turtle file.
    )annotations)	ExitStackcontextmanager)Path)TYPE_CHECKINGBinaryIO	GeneratorOptionalTuple)Graph)_nt_rowserialize_in_chunks'  NchunkFgr   max_triplesintmax_file_size_kbOptional[int]file_name_stemstr
output_dirOptional[Path]write_prefixesboolreturnNonec              	     s&  du rt   stdtd fdd}ddd}|rFtt   d ddd}|||  W d   n1 sAw   Y  d}	t }
|dur|d }|rXdnd}t| 	dD ]N\}}t
|d}t||krtd|d|d  d| |dkr|
||\}}d}	n|	t| |kr|d7 }|
||\}}d}	|	||7 }	qanOt| }||kr| jt   d dd n/|rdnd}t| 	dD ]!\}}|| dkr|
||\}}|d7 }|t
|d q	 W d   dS W d   dS 1 sw   Y  dS )a:  Serializes a given Graph into a series of n-triples with a given length.

    Args:
        g: The graph to serialize.
        max_file_size_kb: Maximum size per NT file in kB (1,000 bytes)
            Equivalent to ~6,000 triples, depending on Literal sizes.
        max_triples: Maximum size per NT file in triples
            Equivalent to lines in file.
            If both this parameter and max_file_size_kb are set, max_file_size_kb will be used.
        file_name_stem: Prefix of each file name.
            e.g. "chunk" = chunk_000001.nt, chunk_000002.nt...
        output_dir: The directory you want the files to be written to.
        write_prefixes: The first file created is a Turtle file containing original graph prefixes.

    See `../test/test_tools/test_chunk_serializer.py` for examples of this in use.
    Nz>If you specify an output_dir, it must actually be a directory!file_nor   r   ,Generator[Tuple[Path, BinaryIO], None, None]c                 3  sn    t r	d us	J t  dt| d d }t|d}||fV  W d    d S 1 s0w   Y  d S )N_   z.ntwb)r   r   r   zfillopen)r   fpfhr   r    W/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/tools/chunk_serializer.py_start_new_file;   s   ""z,serialize_in_chunks.<locals>._start_new_filer   r   r   c                 S  sB   g }| j  D ]\}}|d| d| d qdt|d S )NzPREFIX z: <>
)Znamespace_manager
namespacesappendjoinsorted)r   Zpreskvr'   r'   r(   _serialize_prefixesD   s   z0serialize_in_chunks.<locals>._serialize_prefixesz_000000.ttlwzutf-8)encodingr   i     )NNNzcannot write triple z as it's serialized size of z exceeds max_file_size_kb = z_all.ntnt)Zdestinationformat)r   r   r   r   )r   r   r   r   )r   cwdis_dir
ValueErrorr   r#   writer   	enumerateZtriplesr   encodelenenter_context	serialize)r   r   r   r   r   r   r)   r2   r%   Zbytes_writtenZxstackZmax_file_sizer   itZ	row_bytesr$   ZfhbZgraph_lengthr'   r&   r(   r      sh   
$)r   Nr   NF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )__doc__
__future__r   
contextlibr   r   pathlibr   typingr   r   r   r	   r
   Zrdflib.graphr   Zrdflib.plugins.serializers.ntr   __all__r   r'   r'   r'   r(   <module>   s    