o
    i                     @   s   d 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edede
e dede
e dedd	f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.
    )	ExitStackcontextmanager)Path)TYPE_CHECKINGBinaryIO	GeneratorOptionalTuple)Graph)_nt_rowserialize_in_chunks'  NchunkFgmax_triplesmax_file_size_kbfile_name_stem
output_dirwrite_prefixesreturnc              	      sH  du rt   stdtdtdttt tf ddf f fdd}dt	dt
fdd	}|rWtt   d
 ddd}|||  W d   n1 sRw   Y  d}	t }
|dur|d }|rid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 }	qrnOt| }||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 )ap  
    Serializes a given Graph into a series of n-triples with a given length.

    :param g:
        The graph to serialize.

    :param max_file_size_kb:
        Maximum size per NT file in kB (1,000 bytes)
        Equivalent to ~6,000 triples, depending on Literal sizes.

    :param 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.

    :param file_name_stem:
        Prefix of each file name.
        e.g. "chunk" = chunk_000001.nt, chunk_000002.nt...

    :param output_dir:
        The directory you want the files to be written to.

    :param 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   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   strzfillopen)r   fpfhr   r    X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/tools/chunk_serializer.py_start_new_fileF   s   ""z,serialize_in_chunks.<locals>._start_new_filer   c                 S   sB   g }| j  D ]\}}|d| d| d qdt|d S )NzPREFIX z: <>
)namespace_manager
namespacesappendjoinsorted)r   preskvr    r    r!   _serialize_prefixesO   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)destinationformat)r   cwdis_dir
ValueErrorr   intr   r	   r   r
   r   r   writer   	enumeratetriplesr   encodelenenter_context	serialize)r   r   r   r   r   r   r"   r-   r   bytes_writtenxstackmax_file_sizer   it	row_bytesr   fhbgraph_lengthr    r   r!   r      sh   &,$)r   Nr   NF)__doc__
contextlibr   r   pathlibr   typingr   r   r   r   r	   rdflib.graphr
   rdflib.plugins.serializers.ntr   __all__r7   r   boolr   r    r    r    r!   <module>   s6    