B
    -,c                 @   s8   d Z ddlmZmZmZmZ ddlZG dd deZdS )z
future_mrcfile
--------------

Module which exports the :class:`FutureMrcFile` class.

Classes:
    :class:`FutureMrcFile`: An object which represents an MRC file being
        opened asynchronously.

    )absolute_importdivisionprint_functionunicode_literalsNc               @   sj   e Zd ZdZdi fddZdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dddZdd Zdd ZdS )FutureMrcFilea%  Object representing an MRC file being opened asynchronously.

    This API deliberately mimics a :class:`~concurrent.futures.Future` object
    from the :mod:`concurrent.futures` module in Python 3.2+ (which we do
    not use directly because this code still needs to run in Python 2.7).

     c             C   s0   dg| _ || _tj| j||d| _| j  dS )a  Initialise a new :class:`FutureMrcFile` object.

        This constructor starts a new thread which will invoke the callable
        given in ``open_function`` with the given arguments.

        Args:
            open_function: The callable to use to open the MRC file. (This will
                normally be :func:`mrcfile.open`, but could also be
                :class:`~mrcfile.mrcfile.MrcFile` or any of its subclasses.)
            args: A tuple of positional arguments to use when ``open_function``
                is called. (Normally a 1-tuple containing the name of the file
                to open.)
            kwargs: A dictionary of keyword arguments to use when
                ``open_function`` is called.
        N)targetargskwargs)_result_holder_open_function	threadingZThread_run_threadstart)selfZopen_functionr	   r
   r   r   5lib/python3.7/site-packages/mrcfile/future_mrcfile.py__init__    s    
zFutureMrcFile.__init__c          
   O   sL   y| j ||}|| jd< W n, tk
rF } z|| jd< W dd}~X Y nX dS )zjCall the open function and store the result in the holder list.

        (For internal use only.)
        r   N)r   r   	Exception)r   r	   r
   Zmrcexr   r   r   r   7   s
    zFutureMrcFile._runc             C   s   dS )zReturn :data:`False`.

        (See :meth:`concurrent.futures.Future.cancel` for more details. This
        implementation does not allow jobs to be cancelled.)
        Fr   )r   r   r   r   cancelB   s    zFutureMrcFile.cancelc             C   s   dS )zReturn :data:`False`.

        (See :meth:`concurrent.futures.Future.cancelled` for more details.
        This implementation does not allow jobs to be cancelled.)
        Fr   )r   r   r   r   	cancelledJ   s    zFutureMrcFile.cancelledc             C   s
   | j  S )zReturn :data:`True` if the :class:`~mrcfile.mrcfile.MrcFile` is
        currently being opened.

        (See :meth:`concurrent.futures.Future.running` for more details.)
        )r   is_alive)r   r   r   r   runningR   s    zFutureMrcFile.runningc             C   s
   |    S )zReturn :data:`True` if the file opening has finished.

        (See :meth:`concurrent.futures.Future.done` for more details.)
        )r   )r   r   r   r   doneZ   s    zFutureMrcFile.doneNc             C   s"   |  |}t|tr|n|S dS )a  Return the :class:`~mrcfile.mrcfile.MrcFile` that has been opened.

        (See :meth:`concurrent.futures.Future.result` for more details.)

        Args:
            timeout: Time to wait (in seconds) for the file opening to finish.
                If ``timeout`` is not specified or is :data:`None`, there is no
                limit to the wait time.

        Returns:
            An :class:`~mrcfile.mrcfile.MrcFile` object (or one of its
            subclasses).

        Raises:
            :exc:`RuntimeError`: If the operation has not finished within the
                 time limit set by ``timeout``. (Note that the type of this
                 exception will change in future if this class is replaced by
                 :class:`concurrent.futures.Future`.)
            :exc:`Exception`: Any exception raised by the
                :class:`~mrcfile.mrcfile.MrcFile` opening operation will be
                re-raised here.
        N)_get_result
isinstancer   )r   timeoutresultr   r   r   r   a   s    

zFutureMrcFile.resultc             C   s    |  |}t|tr|S dS dS )a+  Return the exception raised by the file opening operation.

        (See :meth:`concurrent.futures.Future.exception` for more details.)

        Args:
            timeout: Time to wait (in seconds) for the operation to finish. If
                ``timeout`` is not specified or is :data:`None`, there is no
                limit to the wait time.

        Returns:
            An :exc:`Exception`, if one was raised by the file opening
            operation, or :data:`None` if no exception was raised.

        Raises:
            :exc:`RuntimeError`: If the operation has not finished within the
                time limit set by ``timeout``. (Note that the type of this
                exception will change in future if this class is replaced by
                :class:`concurrent.futures.Future`.)
        N)r   r   r   )r   r   r   r   r   r   	exception~   s    

zFutureMrcFile.exceptionc             C   s*   | j j|d | j  r td| jd S )zjReturn the result or exception from the file opening operation.

        (For internal use only.)
        )r   zTimed out waiting for resultr   )r   joinr   RuntimeErrorr   )r   r   r   r   r   r      s    
zFutureMrcFile._get_resultc             C   s   t dS )znNot implemented.

        (See :meth:`concurrent.futures.Future.add_done_callback` for more details.)
        N)NotImplementedError)r   fnr   r   r   add_done_callback   s    zFutureMrcFile.add_done_callback)N)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r$   r   r   r   r   r      s   


r   )	r(   Z
__future__r   r   r   r   r   objectr   r   r   r   r   <module>   s   