
    Pi                        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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           
        t          j                                                    st          d          t          dfd            }dd}|r[t          t                     dz  dd          5 }|                     ||                      ddd           n# 1 swxY w Y   d}	t                      5 }
||dz  }|rdnd}t          | 	                    d                    D ]\  }}t          |                              d          }t          |          |k    rt          d|d|dz   d|           |dk    r$|
                     ||                    \  }}d}	n>|	t          |          z   |k    r(|dz  }|
                     ||                    \  }}d}	|	|                    |          z  }	nt          |           }||k    r+|                     t                     dz  d           n|rdnd}t          | 	                    d                    D ]i\  }}||z  dk    r&|
                     ||                    \  }}|dz  }|                    t          |                              d                     j	 ddd           dS 	 ddd           dS # 1 swxY 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     K   t           rJ t                     dt          |                               d           dz  }t	          |d          5 }||fV  d d d            d S # 1 swxY w Y   d S )N_   z.ntwb)r   r   r   zfillopen)r   fpfhr   r   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/rdflib/tools/chunk_serializer.py_start_new_filez,serialize_in_chunks.<locals>._start_new_file;   s       	*)))*> N NCLL4F4Fq4I4I N N NN"d^^ 	rb&LLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A++A/2A/r   r   r   c                    g }| j                                         D ]!\  }}|                    d| d| d           "d                    t	          |                    dz   S )NzPREFIX z: <>
)namespace_manager
namespacesappendjoinsorted)r   preskvs       r)   _serialize_prefixesz0serialize_in_chunks.<locals>._serialize_prefixesD   so    '2244 	. 	.DAqKK,!,,,,,----yy&&--    z_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   r   r   r    )r   r   r   r   )r   cwdis_dir
ValueErrorr   r&   writer   	enumeratetriplesr   encodelenenter_context	serialize)r   r   r   r   r   r   r*   r6   r(   bytes_writtenxstackmax_file_sizer   it	row_bytesr'   fhbgraph_lengths      ``              r)   r   r      s   2 XZZ
 
L
 
 	
       ^. . . .  -.====sW
 
 
 	-HH((++,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-
 M	 (',t3M)0aaqG!!)),>"?"?@@ 6 61#AJJ--g66	y>>M11$ Qq  Q  QPY\`P`  Q  Q  O  Q  Q   66$22??73K3KLLGB$%MM#c)nn4FFqLG$22??73K3KLLGB$%M9!5!556& q66L{** $Z 0 0n3M3M3M M       .4!!1%aii0B&C&CDD : :DAq;!++"("6"6w7O7O"P"PC1IIgajj//889999Q( ( ( ( ( ( ( (6	( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s%   :B%%B),B) GJ66J:=J:)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   rdflib.graphr   rdflib.plugins.serializers.ntr   __all__r    r7   r)   <module>rY      s     # " " " " " 0 0 0 0 0 0 0 0       F F F F F F F F F F F F F F       1 1 1 1 1 1 !
!
 &*!!% ` ` ` ` ` ` `r7   