
     iD                         d Z ddlZddlZddlZddlZ G d dej                  Z G d dej                  Z G d dej	                  Z
 G d	 d
ej                  Z G d dej                  ZddZddZddZdS )a  
Picklable read-only I/O classes --- :mod:`MDAnalysis.lib.picklable_file_io`
===========================================================================

Provide with an interface for pickling read-only IO file object.
These classes are used for further pickling :class:`MDAnalysis.core.universe`
in a object composition approach.

.. autoclass:: FileIOPicklable
   :members:

.. autoclass:: BufferIOPicklable
   :members:

.. autoclass:: TextIOPicklable
   :members:

.. autoclass:: BZ2Picklable
   :members:

.. autoclass:: GzipPicklable
   :members:

.. autofunction:: pickle_open

.. autofunction:: bz2_pickle_open

.. autofunction:: gzip_pickle_open


.. versionadded:: 2.0.0
    Nc                   0     e Zd ZdZd fd	Zd Zd Z xZS )FileIOPicklablea  File object (read-only) that can be pickled.

    This class provides a file-like object (as returned by :func:`open`,
    namely :class:`io.FileIO`) that, unlike standard Python file objects,
    can be pickled. Only read mode is supported.

    When the file is pickled, filename and position of the open file handle in
    the file are saved. On unpickling, the file is opened by filename,
    and the file is seeked to the saved position.
    This means that for a successful unpickle, the original file still has to
    be accessible with its filename.

    Note
    ----
    This class only supports reading files in binary mode. If you need to open
    a file in text mode, use the :func:`pickle_open`.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode : str
        only reading ('r') mode works. It exists to be consistent
        with a wider API.

    Example
    -------
    ::

        >>> import pickle
        >>> from MDAnalysis.tests.datafiles import PDB
        >>> file = FileIOPicklable(PDB)
        >>> _ = file.readline()
        >>> file_pickled = pickle.loads(pickle.dumps(file))
        >>> print(file.tell(), file_pickled.tell())
        55 55

    See Also
    ---------
    TextIOPicklable
    BufferIOPicklable


    .. versionadded:: 2.0.0
    rc                 Z    || _         t                                          ||           d S N)_modesuper__init__selfnamemode	__class__s      j/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/lib/picklable_file_io.pyr
   zFileIOPicklable.__init__o   s*    
t$$$$$    c                     |d         }|                      |d           	 |                     |d                    d S # t          $ r Y d S w xY w)Nname_valr   )r   tell_valr
   seekKeyError)r   stater   s      r   __setstate__zFileIOPicklable.__setstate__s   sf    Z d%%%	IIeJ'((((( 	 	 	DD	s   > 
AAc                     | j         dk    r't          d                    | j                             | j        | j        | j         f| j        |                                 dfS Nr   z;Can only pickle files that were opened in read mode, not {})r   r   )r   RuntimeErrorformatr   r   tellr   prots     r   __reduce_ex__zFileIOPicklable.__reduce_ex__{   sg    :''-vdj'9'9  
 NY
#		<<
 	
r   )r   __name__
__module____qualname____doc__r
   r   r!   __classcell__r   s   @r   r   r   >   sf        . .`% % % % % %  

 

 

 

 

 

 

r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )BufferIOPicklablea  A picklable buffer object for read-only FileIO object.

    This class provides a buffered :class:`io.BufferedReader`
    that can be pickled.
    Note that this only works in read mode.

    Parameters
    ----------
    raw : FileIO object

    Example
    -------
    ::

        file = FileIOPicklable('filename')
        buffer_wrapped = BufferIOPicklable(file)

    See Also
    ---------
    FileIOPicklable
    TextIOPicklable


    .. versionadded:: 2.0.0
    c                 b    t                                          |           |j        | _        d S r   r	   r
   r   	raw_classr   rawr   s     r   r
   zBufferIOPicklable.__init__   (    r   c                     |d         }|d         } ||          }|                      |           |                     |d                    d S Nr-   r   r   r
   r   )r   r   r-   r   r/   s        r   r   zBufferIOPicklable.__setstate__   sR    +&	Z iooc		%
#$$$$$r   c                 b    | j         | j        f| j         | j        |                                 dfS N)r-   r   r   )r-   r   r   r   s     r   r!   zBufferIOPicklable.__reduce_ex__   s9     NYL!^ I IIKK 
 	
r   r"   r(   s   @r   r*   r*      s`         4' ' ' ' '% % %
 
 
 
 
 
 
r   r*   c                   .     e Zd ZdZ fdZd Zd Z xZS )TextIOPicklablea  Character and line based picklable file-like object.

    This class provides a file-like :class:`io.TextIOWrapper` object that can
    be pickled. Note that this only works in read mode.

    Parameters
    ----------
    raw : FileIO object

    Example
    -------
    ::

        file = FileIOPicklable('filename')
        text_wrapped = TextIOPicklable(file)

    See Also
    ---------
    FileIOPicklable
    BufferIOPicklable


    .. versionadded:: 2.0.0
    .. versionchanged:: 2.8.0
       The raw class instance instead of the class name
       that is wrapped inside will be serialized.
       After deserialization, the current position is no longer reset
       so `universe.trajectory[i]` is not needed to seek to the
       original position.
    c                 b    t                                          |           |j        | _        d S r   r,   r.   s     r   r
   zTextIOPicklable.__init__   r0   r   c                     |d         }|d         }|d         } ||          }|                      |           ||                     |           d S d S r2   r3   )r   argsr-   r   r   r/   s         r   r   zTextIOPicklable.__setstate__   sb    %	JJ ioocIIdOOOOO r   c                     	 |                                  }n# t          $ r d }Y nw xY w	 | j        }n!# t          $ r | j        j        j        }Y nw xY w| j        j        | j        f| j        ||dfS r5   )	r   OSErrorr   AttributeErrorbuffer_fpr   __new__r-   )r   r    curr_locr   s       r   r!   zTextIOPicklable.__reduce_ex__   s    	yy{{HH 	 	 	HHH		(9DD 	( 	( 	(;?'DDD	( N"^!^ $ 
 	
s    &&2 AAr"   r(   s   @r   r7   r7      s`         >' ' ' ' '	 	 	
 
 
 
 
 
 
r   r7   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )BZ2Picklablea  File object (read-only) for bzip2 (de)compression that can be pickled.

    This class provides a file-like object (as returned by :func:`bz2.open`,
    namely :class:`bz2.BZ2File`) that, unlike standard Python file objects,
    can be pickled. Only read mode is supported.

    When the file is pickled, filename and position of the open file handle in
    the file are saved. On unpickling, the file is opened by filename,
    and the file is seeked to the saved position.
    This means that for a successful unpickle, the original file still has to
    be accessible with its filename.

    Note
    ----
    This class only supports reading files in binary mode. If you need to open
    to open a compressed file in text mode, use :func:`bz2_pickle_open`.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode : str
        can only be 'r', 'rb' to make pickle work.

    Example
    -------
    ::

        >>> import pickle
        >>> from MDAnalysis.tests.datafiles import XYZ_bz2
        >>> file = BZ2Picklable(XYZ_bz2)
        >>> _ = file.readline()
        >>> file_pickled = pickle.loads(pickle.dumps(file))
        >>> print(file.tell(), file_pickled.tell())
        5 5

    See Also
    ---------
    FileIOPicklable
    BufferIOPicklable
    TextIOPicklable
    GzipPicklable


    .. versionadded:: 2.0.0
    rbc                 Z    || _         t                                          ||           d S r   )_bz_moder	   r
   r   s      r   r
   zBZ2Picklable.__init__3  *    t$$$$$r   c                     | j                             d          s't          d                    | j                             | j        j        |                                 dS r   )rF   
startswithr   r   r?   r   r   r   s    r   __getstate__zBZ2Picklable.__getstate__7  s\    }'',, 	''-vdm'<'<   !HMtyy{{CCCr   c                     |d         }|d         }|                      |           	 |                     |           d S # t          $ r Y d S w xY wNr   r   r   r   r:   r   r   s       r   r   zBZ2Picklable.__setstate__?  b    JJd	IIdOOOOO 	 	 	DD	   > 
AArD   r#   r$   r%   r&   r
   rK   r   r'   r(   s   @r   rC   rC     si        / /b% % % % % %D D D      r   rC   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )GzipPicklablea  Gzip file object (read-only) that can be pickled.

    This class provides a file-like object (as returned by :func:`gzip.open`,
    namely :class:`gzip.GzipFile`) that, unlike standard Python file objects,
    can be pickled. Only read mode is supported.

    When the file is pickled, filename and position of the open file handle in
    the file are saved. On unpickling, the file is opened by filename,
    and the file is seeked to the saved position.
    This means that for a successful unpickle, the original file still has to
    be accessible with its filename.

    Note
    ----
    This class only supports reading files in binary mode. If you need to open
    to open a compressed file in text mode, use the :func:`gzip_pickle_open`.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode : str
        can only be 'r', 'rb' to make pickle work.

    Example
    -------
    ::

        >>> import pickle
        >>> from MDAnalysis.tests.datafiles import MMTF_gz
        >>> file = GzipPicklable(MMTF_gz)
        >>> _ = file.readline()
        >>> file_pickled = pickle.loads(pickle.dumps(file))
        >>> print(file.tell(), file_pickled.tell())
        1218 1218

    See Also
    ---------
    FileIOPicklable
    BufferIOPicklable
    TextIOPicklable
    BZ2Picklable


    .. versionadded:: 2.0.0
    rD   c                 Z    || _         t                                          ||           d S r   )_gz_moder	   r
   r   s      r   r
   zGzipPicklable.__init__{  rG   r   c                     | j                             d          s't          d                    | j                             | j        |                                 dS r   )rV   rI   r   r   r   r   rJ   s    r   rK   zGzipPicklable.__getstate__  sZ    }'',, 	''-vdm'<'<   !I499;;???r   c                     |d         }|d         }|                      |           	 |                     |           d S # t          $ r Y d S w xY wrM   r   rN   s       r   r   zGzipPicklable.__setstate__  rO   rP   rQ   rR   r(   s   @r   rT   rT   I  si        / /b% % % % % %@ @ @      r   rT   rtc                     |dvrt          d          t          j        |           } t          |           }|dk    rt	          |          S |dv rt          |          S dS )a  Open file and return a stream with pickle function implemented.

    This function returns a FileIOPicklable object wrapped in a
    BufferIOPicklable class when given the "rb" reading mode,
    or a FileIOPicklable object wrapped in a TextIOPicklable class with the "r"
    or "rt" reading mode. It can be used as a context manager, and replace the
    built-in :func:`open` function in read mode that only returns an
    unpicklable file object.
    In order to serialize a :class:`MDAnalysis.core.Universe`, this function
    can used to open trajectory/topology files. This object composition is more
    flexible and easier than class inheritance to implement pickling
    for new readers.

    Note
    ----
    Can be only used with read mode.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode: {'r', 'rt', 'rb'} (optional)
        'r':  open for reading in text mode;
        'rt': read in text mode (default);
        'rb': read in binary mode;

    Returns
    -------
    stream-like object: BufferIOPicklable or TextIOPicklable
        when mode is 'r' or 'rt', returns TextIOPicklable;
        when mode is 'rb', returns BufferIOPicklable

    Raises
    ------
    ValueError
        if `mode` is not one of the allowed read modes

    Examples
    -------
    open as context manager::

        with pickle_open('filename') as f:
            line = f.readline()

    open as function::

        f = pickle_open('filename')
        line = f.readline()
        f.close()

    See Also
    --------
    :func:`MDAnalysis.lib.util.anyopen`
    :func:`io.open`


    .. versionadded:: 2.0.0
    >   r   rD   rY   6Only read mode ('r', 'rt', 'rb') files can be pickled.rD   >   r   rY   N)
ValueErrorosfspathr   r*   r7   )r   r   r/   s      r   pickle_openr_     sz    z $$$G
 
 	
 9T??D
$

Ct|| %%%			s### 
	r   rD   c                     |dvrt          d          |                    dd          }t          | |          }d|v rt          |          S |S )as  Open a bzip2-compressed file in binary or text mode
    with pickle function implemented.

    This function returns a BZ2Picklable object when given the "rb" or "r"
    reading mode, or a BZ2Picklable object wrapped in a TextIOPicklable class
    with the "rt" reading mode.
    It can be used as a context manager, and replace the built-in
    :func:`bz2.open` function in read mode that only returns an
    unpicklable file object.

    Note
    ----
    Can be only used with read mode.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode: {'r', 'rt', 'rb'} (optional)
        'r':  open for reading in binary mode;
        'rt': read in text mode;
        'rb': read in binary mode; (default)

    Returns
    -------
    stream-like object: BZ2Picklable or TextIOPicklable
        when mode is 'rt', returns TextIOPicklable;
        when mode is 'r' or 'rb', returns BZ2Picklable

    Raises
    ------
    ValueError
        if `mode` is not one of the allowed read modes

    Examples
    -------
    open as context manager::

        with bz2_pickle_open('filename') as f:
            line = f.readline()

    open as function::

        f = bz2_pickle_open('filename')
        line = f.readline()
        f.close()

    See Also
    --------
    :func:`io.open`
    :func:`bz2.open`
    :func:`MDAnalysis.lib.util.anyopen`
    :func:`MDAnalysis.lib.picklable_file_io.pickle_open`
    :func:`MDAnalysis.lib.picklable_file_io.gzip_pickle_open`


    .. versionadded:: 2.0.0
    >   r   rD   rY   r[   t )r\   replacerC   r7   )r   r   bz_modebinary_files       r   bz2_pickle_openrf     sg    z $$$G
 
 	
 ll3##GtW--K
d{{{+++r   c                     |dvrt          d          |                    dd          }t          | |          }d|v rt          |          S |S )ay  Open a gzip-compressed file in binary or text mode
    with pickle function implemented.

    This function returns a GzipPicklable object when given the "rb" or "r"
    reading mode, or a GzipPicklable object wrapped in a TextIOPicklable class
    with the "rt" reading mode.
    It can be used as a context manager, and replace the built-in
    :func:`gzip.open` function in read mode that only returns an
    unpicklable file object.

    Note
    ----
    Can be only used with read mode.

    Parameters
    ----------
    name : str
        either a text or byte string giving the name (and the path
        if the file isn't in the current working directory) of the file to
        be opened.
    mode: {'r', 'rt', 'rb'} (optional)
        'r':  open for reading in binary mode;
        'rt': read in text mode;
        'rb': read in binary mode; (default)

    Returns
    -------
    stream-like object: GzipPicklable or TextIOPicklable
        when mode is 'rt', returns TextIOPicklable;
        when mode is 'r' or 'rb', returns GzipPicklable

    Raises
    ------
    ValueError
        if `mode` is not one of the allowed read modes

    Examples
    -------
    open as context manager::

        with gzip_pickle_open('filename') as f:
            line = f.readline()

    open as function::

        f = gzip_pickle_open('filename')
        line = f.readline()
        f.close()

    See Also
    --------
    :func:`io.open`
    :func:`gzip.open`
    :func:`MDAnalysis.lib.util.anyopen`
    :func:`MDAnalysis.lib.picklable_file_io.pickle_open`
    :func:`MDAnalysis.lib.picklable_file_io.bz2_pickle_open`


    .. versionadded:: 2.0.0
    >   r   rD   rY   r[   ra   rb   )r\   rc   rT   r7   )r   r   gz_modere   s       r   gzip_pickle_openri   #  sg    z $$$G
 
 	
 ll3##Gg..K
d{{{+++r   )rY   rQ   )r&   ior]   bz2gzipFileIOr   BufferedReaderr*   TextIOWrapperr7   BZ2FilerC   GzipFilerT   r_   rf   ri    r   r   <module>rs      s  . @ 
			 				 



 G
 G
 G
 G
 G
bi G
 G
 G
T1
 1
 1
 1
 1
) 1
 1
 1
hB
 B
 B
 B
 B
b& B
 B
 B
JE E E E E3; E E EPE E E E EDM E E EPF$ F$ F$ F$RF F F FRF F F F F Fr   