o
    i                     @   sV   d dl Z d dlZd dlmZ d dlmZmZ defddZdd Z	d	d
 Z
dd ZdS )    N)TemporaryDirectory)ZipFilePathPointeropen_datafilecontentsc                 C   sn   t j| d}t j||t jjd}t|d}||| W d   ||fS 1 s.w   Y  ||fS )z
    Create a zip file under root_dir with a single file at rel_dir/file_name
    containing 'contents'. Return the full path to the zip file and the
    relative path inside the zip.
    ztestdata.zip/wN)ospathjoinreplacesepzipfileZipFilewritestr)root_dirrel_dir	file_namer   zip_patharcnamezf r   U/home/ubuntu/.local/lib/python3.10/site-packages/nltk/test/unit/test_open_datafile.py_create_test_zip   s   
r   c               	   C   s   t  R} tjdd}d}d}|d}t| |||\}}|tjjdd }t||}t	||dd}	|	
 }
W d   n1 sBw   Y  |
|ksMJ W d   dS 1 sXw   Y  dS )	zIopen_datafile should open a file inside a zip when given dir + file_name.corporatestpkg
sample.txtzHello from zipped data
utf-8r   r   encodingNr   r   r	   r
   encoder   r   r   r   r   read)tmpdirr   r   textdatar   r   	dir_entryr	   fresultr   r   r   2test_open_datafile_directory_and_filename_from_zip   s   


"r(   c               	   C   s   t  V} tjdd}d}d}|d}t| |||\}}|tjjdd }t||}||}	t	|	dd}
|

 }W d   n1 sFw   Y  ||ksQJ W d   dS 1 s\w   Y  dS )	zJopen_datafile should open a file pointer directly when file_name is empty.r   r   r   z%Direct file pointer from zipped data
r   r   )r   Nr   )r"   r   r   r#   r$   r   r   r%   dir_pointerfile_pointerr&   r'   r   r   r   (test_open_datafile_file_pointer_from_zip-   s   



"r+   c            
   	   C   s   t  V} tjdd}d}d}t| |||\}}|tjjdd }t||}t||dd}|	 }	W d   n1 s=w   Y  t
|	ttfsKJ |	|ksQJ W d   dS 1 s\w   Y  dS )z?open_datafile should return a binary stream when encoding=None.r   r   z
binary.bins    r   Nr   )r   r   r	   r
   r   r   r   r   r   r!   
isinstancebytes	bytearray)
r"   r   r   binary_datar   r   r%   r)   r&   r'   r   r   r   'test_open_datafile_binary_mode_from_zipB   s   

"r0   )r   r   tempfiler   	nltk.datar   r   r-   r   r(   r+   r0   r   r   r   r   <module>   s    