B
    âêõbC  ã               @   sx   d Z ddlmZmZmZ dgZddlmZmZ ddl	Z	ddl
mZ ddlmZ ddlmZmZ dd	lmZ ddd„ZdS )z8
Tools to aid in input/output within the parmed package
é    )Úprint_functionÚdivisionÚabsolute_importÚgenopen)ÚTextIOWrapperÚBytesION)ÚPY2)Úurlopen)Ú	HTTPErrorÚURLError)ÚDEFAULT_ENCODINGÚrc       
   
   C   sr  |dkrt dƒ‚d}|  d¡r,| dd… } nv|  d¡sJ|  d¡sJ|  d	¡r¢d
}|dkr^t dƒ‚yt| ƒ}W n6 ttfk
r  } ztd| |f ƒ‚W dd}~X Y nX |  d¡r¤ddl}trò|ròt	ƒ }| 
| | ¡ ¡¡ | ¡  | d¡ t|ƒS |dkrnt	ƒ }tj | ¡r@| | d¡}| 
| ¡ ¡ W dQ R X | d¡ | | d¡}| 
| ¡ ¡ ~trf|S t|ƒS tr„| | |d ¡S |rŽ|} t| | |d ¡ƒS n’|  d¡r6ddl}	tr
|røt	ƒ }| 
| ¡ ¡ | d¡ | ¡  |	j|ddS |	 | |d ¡S n,|r"t|	j|ddƒS t|	 | |d ¡ƒS |rPtrF|S t|ƒS ntr`t| |ƒS t| |tdS dS )aì  
    Opens a file, automatically detecting compression schemes by filename
    extension. Note, these files are opened in a way that *always* returns a
    string. This is an important distinction in Python 3 where many file-like
    objects return bytes instead of strings. This is detected and handled
    properly so that the object returned from this function is always
    string-based (so you cannot write or read bytes directly from a file opened
    via ``genopen``).

    This routine also recognizes URLs and will read remote files when given a
    URL starting with either http:// or https://. Like with standard local file
    names, compression is automatically detected by filename extension, and both
    gzip and bzip2 files are supported.

    Parameters
    ----------
    name : str
        Name of the file to open or URL to a remote file to access
    mode : str, optional
        Whether to open the file to 'r'ead, 'w'rite, or 'a'ppend. Default is 'r'

    Returns
    -------
    file : file-like
        A file-like object in the requested mode

    Notes
    -----
    Python's BZ2File does not support writing to ``append`` mode (mode='a'), so
    it is faked here. The entire file contents are read into memory and then
    written into a 'new' file with the same name as the original. As such, it is
    noticeably slower and more resource-intensive (particularly for large files)
    than using gzipped files.

    In Python 2, opened URLs are not file-like *enough* for GzipFile or BZ2File
    to read directly from them, so they must first be loaded entirely into
    memory. With Python 3, this limitation is not present, so reading remote
    Gzipped or Bzipped files is significantly cheaper with respect to memory
    requirements.
    )Úwr   Úaz"open mode must be "w", "r", or "a"Fzfile:///é   Nzhttp://zhttps://zftp://T)r   r   z Cannot write or append a webpagezCould not open %s: %sz.bz2r   r   ÚrbÚwbÚbz.gzr   )ÚfileobjÚmode)Úencoding)Ú
ValueErrorÚ
startswithr	   r
   r   ÚIOErrorÚendswithÚbz2r   r   ÚwriteZ
decompressÚreadÚcloseÚseekr   ÚosÚpathÚexistsZBZ2FileÚgzipZGzipFileÚopenr   )
Únamer   Zis_urlZopen_urlÚer   r   ZtmpÚfr#   © r(   ú.lib/python3.7/site-packages/parmed/utils/io.pyr      sv    )

"





)r   )Ú__doc__Z
__future__r   r   r   Ú__all__Úior   r   r    Zparmed.utils.sixr   Z%parmed.utils.six.moves.urllib.requestr	   Z#parmed.utils.six.moves.urllib.errorr
   r   Zparmed.constantsr   r   r(   r(   r(   r)   Ú<module>   s   