
    01i                        S r SSKJr  SSKJrJr  SSKJr  SSKJ	r	J
r
JrJrJr  SSKJr  SSKJr  S/r     S             SS
 jjrg	)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c  [         R                  " 5       mTR                  5       (       d  [        S5      e[        SUU4S jj5       nSS jnU(       a:  [        [        T5      T S3-  SSS9 nUR                  U" U 5      5        SSS5        S	n	[        5        n
Ub  US
-  nU(       a  SOS	n[        U R                  S5      5       H  u  p[        U5      R                  S5      n[        U5      U:  a  [        SU< SUS
-   SU 35      eUS	:X  a  U
R                  U" U5      5      u  nnS	n	O3U	[        U5      -   U:  a!  US-  nU
R                  U" U5      5      u  nnS	n	U	WR                  U5      -  n	M     O[        U 5      nUU::  a   U R                  [        T5      T S3-  SS9  O~U(       a  SOS	n[        U R                  S5      5       HU  u  pX-  S	:X  a  U
R                  U" U5      5      u  nnUS-  nWR                  [        U5      R                  S5      5        MW      SSS5        g SSS5        g! , (       d  f       GN= f! , (       d  f       g= f)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!c              3     >#    [         (       a  Tc   e[        T5      T S[        U 5      R                  S5       S3-  n[	        US5       nX4v   S S S 5        g ! , (       d  f       g = f7f)N_   z.ntwb)r   r   strzfillopen)file_nofpfhfile_name_stem
output_dirs      W/home/james-whalen/.local/lib/python3.13/site-packages/rdflib/tools/chunk_serializer.py_start_new_file,serialize_in_chunks.<locals>._start_new_file;   s_     =)))*>"2!CL4F4Fq4I3J# NN"d^r&L ^^s   A	A,A	A,
A)%A,c                    / nU R                   R                  5        H  u  p#UR                  SU SU S35        M     SR                  [	        U5      5      S-   $ )NzPREFIX z: <>
)namespace_manager
namespacesappendjoinsorted)gpreskvs       r   _serialize_prefixes0serialize_in_chunks.<locals>._serialize_prefixesD   sW    ''224DA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   r5   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      st   2 XXZ
L
 	
  . .!1==sW
HH(+,

 M	',t3M)aqG!!)),>"?@#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DDA!+"("6"6w7O"PC1IIgaj//89	 E
 Q 
 A	 

 
 
s   3IFI
I
I%)i'  NchunkNF)r'   r   r@   r4   rA   zOptional[int]r   r   r   zOptional[Path]rB   boolr5   None)__doc__
__future__r   
contextlibr   r   pathlibr   typingr   r   r	   r
   r   rdflib.graphr   rdflib.plugins.serializers.ntr   __all__r    r-   r   <module>rW      s    # 0  F F  1 !
!
 &*!!% ``` $` 	`
 ` ` 
`r-   