
    i                        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y	)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_chunksNc           
        t        j                         j                         st        d      t        dfd       }dd}|r=t        t               dz  dd      5 }|j                   ||              ddd       d	}	t               5 }
||d
z  }|rdnd	}t        | j                  d            D ]  \  }}t        |      j                  d      }t        |      |kD  rt        d|d|d
z   d|       |d	k(  r|
j                   ||            \  }}d	}	n2|	t        |      z   |k\  r!|dz  }|
j                   ||            \  }}d	}	|	j                  |      z  }	 nt        |       }||k  r#| j                  t               dz  d       nx|rdnd	}t        | j                  d            D ]U  \  }}||z  d	k(  r|
j                   ||            \  }}|dz  }j                  t        |      j                  d             W 	 ddd       y	 ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)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!c              3     K   t         rJ t               dt        |       j                  d       dz  }t	        |d      5 }||f d d d        y # 1 sw Y   y xY ww)N_   z.ntwb)r   r   strzfillopen)file_nofpfhfile_name_stem
output_dirs      LC:\Projects\mas-dev\.venv\Lib\site-packages\rdflib/tools/chunk_serializer.py_start_new_filez,serialize_in_chunks.<locals>._start_new_fileH   s`     )))*>"2!CL4F4Fq4I3J# NN"d^rb&L ^^s   AA"A	A"AA"c                    g }| j                   j                         D ]  \  }}|j                  d| d| d        dj                  t	        |            dz   S )NzPREFIX z: <>
)namespace_manager
namespacesappendjoinsorted)gpreskvs       r   _serialize_prefixesz0serialize_in_chunks.<locals>._serialize_prefixesQ   sY    ''224DAqKK'!Cs!,- 5 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   intreturnz,Generator[Tuple[Path, BinaryIO], None, None])r&   r   r3   r   )r   cwdis_dir
ValueErrorr   r   writer   	enumeratetriplesr   encodelenenter_context	serialize)r&   max_triplesmax_file_size_kbr   r   write_prefixesr   r*   r   bytes_writtenxstackmax_file_sizer   it	row_bytesr   fhbgraph_lengths      ``              r   r   r      sx   L XXZ
L
 	
  . .!1==sW
HH(+,

 M	',t3M)aqG!!)),>"?@1#AJ--g6	y>M1$.qe3OPY\`P`Oaa}  O  ~P  Q  6$22?73KLGB$%M#c)n4FqLG$22?73KLGB$%M9!55 A& q6L{* $Z 0n5EW3M M    .!1%aii0B&CDDAq;!+"("6"6w7O"PC1IIgaj//89	 E
 Q 
 A	 

 
 
s   'H*FH7*H47I )i'  NchunkNF)r&   r   r>   r2   r?   zOptional[int]r   r   r   zOptional[Path]r@   boolr3   None)__doc__
__future__r   
contextlibr   r   pathlibr   typingr   r   r	   r
   r   rdflib.graphr   rdflib.plugins.serializers.ntr   __all__r    r+   r   <module>rU      s    # 0  F F  1 !
!
 &*!!% mmm $m 	m
 m m 
mr+   