U
    <c                     @   s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZ ddgZedG d	d de	eeef  ZG d
d de	eeef  ZdS )    )IOBase)IterableTupleOptional)functional_datapipe)IterDataPipe) get_file_binaries_from_pathnames_deprecation_warningFileOpenerIterDataPipeFileLoaderIterDataPipeZ
open_filesc                       sF   e Zd ZdZdee eee ed fddZdd	 Z	d
d Z
  ZS )r
   aL  
    Given pathnames, opens files and yield pathname and file stream
    in a tuple (functional name: ``open_files``).

    Args:
        datapipe: Iterable datapipe that provides pathnames
        mode: An optional string that specifies the mode in which
            the file is opened by ``open()``. It defaults to ``r``, other options are
            ``b`` for reading in binary mode and ``t`` for text mode.
        encoding: An optional string that specifies the encoding of the
            underlying file. It defaults to ``None`` to match the default encoding of ``open``.
        length: Nominal length of the datapipe

    Note:
        The opened file handles will be closed by Python's GC periodically. Users can choose
        to close them explicitly.

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.iter import FileLister, FileOpener, StreamReader
        >>> dp = FileLister(root=".").filter(lambda fname: fname.endswith('.txt'))
        >>> dp = FileOpener(dp)
        >>> dp = StreamReader(dp)
        >>> list(dp)
        [('./abc.txt', 'abc')]
    rN)datapipemodeencodinglengthc                    sV   t    || _|| _|| _| jdkr4td|d|krL|d k	rLtd|| _d S )N)btrbrtr   zInvalid mode {}r   z-binary mode doesn't take an encoding argument)super__init__r   r   r   
ValueErrorformatr   )selfr   r   r   r   	__class__ N/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/data/datapipes/iter/fileopener.pyr   +   s    

zFileOpenerIterDataPipe.__init__c                 c   s   t | j| j| jE d H  d S )N)r   r   r   r   r   r   r   r   __iter__C   s    zFileOpenerIterDataPipe.__iter__c                 C   s$   | j dkrtdt| j| j S )Nr   z%{} instance doesn't have valid length)r   	TypeErrorr   type__name__r   r   r   r   __len__F   s    
zFileOpenerIterDataPipe.__len__)r   Nr   )r#   
__module____qualname____doc__r   strr   intr   r    r$   __classcell__r   r   r   r   r
      s      c                   @   s$   e Zd Zdee eedddZdS )r   r   r   r   r   r   c                 C   s    t | jdddd t|||dS )Nz1.12z1.13Z
FileOpener)Zdeprecation_versionZremoval_versionZnew_class_namer+   )r	   r#   r
   )clsr   r   r   r   r   r   __new__N   s    zFileLoaderIterDataPipe.__new__N)r   r   )r#   r%   r&   r   r(   r)   r-   r   r   r   r   r   L   s     N)ior   typingr   r   r   Z%torch.utils.data.datapipes._decoratorr   Z#torch.utils.data.datapipes.datapiper   Z'torch.utils.data.datapipes.utils.commonr   r	   __all__r(   r
   r   r   r   r   r   <module>   s   =