Current File : //proc/self/root/proc/self/root/lib64/python2.7/site-packages/numpy/lib/_datasource.pyo
�
E�`Qc@s�dZdZddlZddlmZmZmZeZde	fd��YZ
e
�Zdejd�Zd	e	fd
��YZ
de
fd��YZdS(
s�A file interface for handling local and remote data files.
The goal of datasource is to abstract some of the file system operations when
dealing with data files so the researcher doesn't have to know all the
low-level details.  Through datasource, a researcher can obtain and use a
file with one function call, regardless of location of the file.

DataSource is meant to augment standard python libraries, not replace them.
It should work seemlessly with standard file IO operations and the os module.

DataSource files can originate locally or remotely:

- local files : '/home/guido/src/local/data.txt'
- URLs (http, ftp, ...) : 'http://www.scipy.org/not/real/data.txt'

DataSource files can also be compressed or uncompressed.  Currently only gzip
and bz2 are supported.

Example::

    >>> # Create a DataSource, use os.curdir (default) for local storage.
    >>> ds = datasource.DataSource()
    >>>
    >>> # Open a remote file.
    >>> # DataSource downloads the file, stores it locally in:
    >>> #     './www.google.com/index.html'
    >>> # opens the file and returns a file object.
    >>> fp = ds.open('http://www.google.com/index.html')
    >>>
    >>> # Use the file as you normally would
    >>> fp.read()
    >>> fp.close()

srestructuredtext eni����N(trmtreetcopyfiletcopyfileobjt_FileOpenerscBs2eZdZd�Zd�Zd�Zd�ZRS(s�
    Container for different methods to open (un-)compressed files.

    `_FileOpeners` contains a dictionary that holds one method for each
    supported file format. Attribute lookup is implemented in such a way that
    an instance of `_FileOpeners` itself can be indexed with the keys of that
    dictionary. Currently uncompressed files as well as files
    compressed with ``gzip`` or ``bz2`` compression are supported.

    Notes
    -----
    `_file_openers`, an instance of `_FileOpeners`, is made available for
    use in the `_datasource` module.

    Examples
    --------
    >>> np.lib._datasource._file_openers.keys()
    [None, '.bz2', '.gz']
    >>> np.lib._datasource._file_openers['.gz'] is gzip.open
    True

    cCst|_itd6|_dS(N(tFalset_loadedtopentNonet
_file_openers(tself((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt__init__Fs	cCs�|jr
dSy ddl}|j|jd<Wntk
r@nXy ddl}|j|jd<Wntk
rtnXt|_dS(Ni����s.bz2s.gz(Rtbz2tBZ2FileRtImportErrortgzipRtTrue(R	RR((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_loadIs	

cCs|j�|jj�S(s\
        Return the keys of currently supported file openers.

        Parameters
        ----------
        None

        Returns
        -------
        keys : list
            The keys are None for uncompressed files and the file extension
            strings (i.e. ``'.gz'``, ``'.bz2'``) for supported compression
            methods.

        (RRtkeys(R	((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRXs
cCs|j�|j|S(N(RR(R	tkey((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt__getitem__js
(t__name__t
__module__t__doc__R
RRR(((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR/s
			trcCst|�}|j||�S(s�
    Open `path` with `mode` and return the file object.

    If ``path`` is an URL, it will be downloaded, stored in the `DataSource`
    `destpath` directory and opened from there.

    Parameters
    ----------
    path : str
        Local file path or URL to open.
    mode : str, optional
        Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to
        append. Available modes depend on the type of object specified by path.
        Default is 'r'.
    destpath : str, optional
        Path to the directory where the source file gets downloaded to for use.
        If `destpath` is None, a temporary directory will be created. The
        default path is the current directory.

    Returns
    -------
    out : file object
        The opened file.

    Notes
    -----
    This is a convenience function that instantiates a `DataSource` and
    returns the file object from ``DataSource.open(path)``.

    (t
DataSourceR(tpathtmodetdestpathtds((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRps RcBs�eZdZejd�Zd�Zd�Zd�Zd�Z	d�Z
d�Zd�Zd	�Z
d
�Zd�Zd�Zd
d�ZRS(s
    DataSource(destpath='.')

    A generic data source file (file, http, ftp, ...).

    DataSources can be local files or remote files/URLs.  The files may
    also be compressed or uncompressed. DataSource hides some of the low-level
    details of downloading the file, allowing you to simply pass in a valid
    file path (or URL) and obtain a file object.

    Parameters
    ----------
    destpath : str or None, optional
        Path to the directory where the source file gets downloaded to for use.
        If `destpath` is None, a temporary directory will be created.
        The default path is the current directory.

    Notes
    -----
    URLs require a scheme string (``http://``) to be used, without it they
    will fail::

        >>> repos = DataSource()
        >>> repos.exists('www.google.com/index.html')
        False
        >>> repos.exists('http://www.google.com/index.html')
        True

    Temporary directories are deleted when the DataSource is deleted.

    Examples
    --------
    ::

        >>> ds = DataSource('/home/guido')
        >>> urlname = 'http://www.google.com/index.html'
        >>> gfile = ds.open('http://www.google.com/index.html')  # remote file
        >>> ds.abspath(urlname)
        '/home/guido/www.google.com/site/index.html'

        >>> ds = DataSource(None)  # use with temporary file
        >>> ds.open('/home/guido/foobar.txt')
        <open file '/home/guido.foobar.txt', mode 'r' at 0x91d4430>
        >>> ds.abspath('/home/guido/foobar.txt')
        '/tmp/tmpy4pgsP/home/guido/foobar.txt'

    cCsO|r'tjj|�|_t|_n$ddl}|j�|_t|_dS(s2Create a DataSource with a local path at destpath.i����N(	tosRtabspatht	_destpathRt
_istmpdestttempfiletmkdtempR(R	RR!((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR
�scCs|jrt|j�ndS(N(R RR(R	((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt__del__�s	cCs(tjj|�\}}|tj�kS(sMTest if the filename is a zip file by looking at the file extension.
        (RRtsplitextRR(R	tfilenametfnametext((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_iszip�scCs+d}x|D]}||kr
tSq
WtS(s4Test if the given mode will open a file for writing.twt+(R)R*(RR(R	Rt_writemodestc((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_iswritemode�s

cCs-|j|�rtjj|�S|dfSdS(sxSplit zip extension from filename and return filename.

        *Returns*:
            base, zip_ext : {tuple}

        N(R(RRR$R(R	R%((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_splitzipext�scCsP|g}|j|�sLx1tj�D] }|r%|j||�q%q%Wn|S(s9Return a tuple containing compressed filename variations.(R(RRtappend(R	R%tnamestzipext((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_possible_names�s	c	Cs>ddlm}||�\}}}}}}t|o:|�S(s=Test if path is a net location.  Tests the scheme and netloc.i����(turlparse(R3tbool(	R	RR3tschemetnetloctupathtuparamstuquerytufrag((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_isurl�scCs�ddlm}ddlm}|j|�}tjjtjj|��sitjtjj|��n|j	|�r�y>||�}t
|d�}zt||�Wd|j�XWq�|k
r�|d|��q�Xnt
j||�|S(shCache the file specified by path.

        Creates a copy of the file in the datasource cache.

        i����(turlopen(tURLErrortwbNsURL not found: %s(turllib2R<R=RRRtexiststdirnametmakedirsR;t_openRtclosetshutilR(R	RR<R=R7t	openedurltf((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt_cache	s 
cCs�|j|�s=|j|�}||j|j|��7}n+|j|j|��}||j|�}xB|D]:}|j|�ro|j|�r�|j|�}n|SqoWdS(sSearches for ``path`` and returns full path if found.

        If path is an URL, _findfile will cache a local copy and return
        the path to the cached file.
        If path is a local file, _findfile will return a path to that local
        file.

        The search will include possible compressed versions of the file and
        return the first occurence found.

        N(R;R2RR@RHR(R	Rtfilelisttname((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt	_findfile)s
c
Cs�ddlm}|j|jd�}t|�dkrD|d}n||�\}}}}}}	|j|�}|j|�}tjj|j||�S(sF
        Return absolute path of file in the DataSource directory.

        If `path` is an URL, then `abspath` will return either the location
        the file exists locally or the location it would exist when opened
        using the `open` method.

        Parameters
        ----------
        path : str
            Can be a local file or a remote URL.

        Returns
        -------
        out : str
            Complete path, including the `DataSource` destination directory.

        Notes
        -----
        The functionality is based on `os.path.abspath`.

        i����(R3ii(R3tsplitRtlent_sanitize_relative_pathRRtjoin(
R	RR3t	splitpathR5R6R7R8R9R:((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRIs
cCs�d}tjj|�}xd||kr~|}|jtj�jd�}|jtj�jd�}tjj|�\}}qW|S(svReturn a sanitised relative path for which
        os.path.abspath(os.path.join(base, path)).startswith(base)
        t/s..N(RRRtnormpathtlstriptseptpardirt
splitdrive(R	Rtlasttdrive((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRNtscCs�ddlm}ddlm}tjj|�r6tS|j|�}tjj|�r[tS|j|�r�y||�}~tSWq�|k
r�t	SXnt	S(s(
        Test if path exists.

        Test if `path` exists as (and in this order):

        - a local file.
        - a remote URL that has been downloaded and stored locally in the
          `DataSource` directory.
        - a remote URL that has not been downloaded, but is valid and accessible.

        Parameters
        ----------
        path : str
            Can be a local file or a remote URL.

        Returns
        -------
        out : bool
            True if `path` exists.

        Notes
        -----
        When `path` is an URL, `exists` will return True if it's either stored
        locally in the `DataSource` directory, or is a valid remote URL.
        `DataSource` does not discriminate between the two, the file is accessible
        if it exists in either location.

        i����(R<(R=(
R?R<R=RRR@RRR;R(R	RR<R=R7tnetfile((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR@�s
RcCs�|j|�r-|j|�r-td��n|j|�}|r�|j|�\}}|dkrv|jdd�nt||d|�Std|��dS(sR
        Open and return file-like object.

        If `path` is an URL, it will be downloaded, stored in the `DataSource`
        directory and opened from there.

        Parameters
        ----------
        path : str
            Local file path or URL to open.
        mode : {'r', 'w', 'a'}, optional
            Mode to open `path`.  Mode 'r' for reading, 'w' for writing, 'a' to
            append. Available modes depend on the type of object specified by
            `path`. Default is 'r'.

        Returns
        -------
        out : file object
            File object.

        sURLs are not writeableRR*tRs
%s not found.N(R;R-t
ValueErrorRKR.treplaceRtIOError(R	RRtfoundt_fnameR'((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR�s(RRRRtcurdirR
R#R(R-R.R2R;RHRKRRNR@R(((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR�s/
			
	
				 	 	+		5t
RepositorycBs_eZdZejd�Zd�Zd�Zd�Zd�Z	d�Z
dd�Zd	�ZRS(
s�
    Repository(baseurl, destpath='.')

    A data repository where multiple DataSource's share a base URL/directory.

    `Repository` extends `DataSource` by prepending a base URL (or directory)
    to all the files it handles. Use `Repository` when you will be working
    with multiple files from one base URL.  Initialize `Repository` with the
    base URL, then refer to each file by its filename only.

    Parameters
    ----------
    baseurl : str
        Path to the local directory or remote location that contains the
        data files.
    destpath : str or None, optional
        Path to the directory where the source file gets downloaded to for use.
        If `destpath` is None, a temporary directory will be created.
        The default path is the current directory.

    Examples
    --------
    To analyze all files in the repository, do something like this
    (note: this is not self-contained code)::

        >>> repos = np.lib._datasource.Repository('/home/user/data/dir/')
        >>> for filename in filelist:
        ...     fp = repos.open(filename)
        ...     fp.analyze()
        ...     fp.close()

    Similarly you could use a URL for a repository::

        >>> repos = np.lib._datasource.Repository('http://www.xyz.edu/data')

    cCs tj|d|�||_dS(s>Create a Repository with a shared url or directory of baseurl.RN(RR
t_baseurl(R	tbaseurlR((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR
scCstj|�dS(N(RR#(R	((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR#
scCsL|j|jd�}t|�dkrBtjj|j|�}n|}|S(s>Return complete path for path.  Prepends baseurl if necessary.ii(RLRbRMRRRO(R	RRPtresult((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt	_fullpaths
cCstj||j|��S(s8Extend DataSource method to prepend baseurl to ``path``.(RRKRe(R	R((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRKscCstj||j|��S(s_
        Return absolute path of file in the Repository directory.

        If `path` is an URL, then `abspath` will return either the location
        the file exists locally or the location it would exist when opened
        using the `open` method.

        Parameters
        ----------
        path : str
            Can be a local file or a remote URL. This may, but does not have
            to, include the `baseurl` with which the `Repository` was initialized.

        Returns
        -------
        out : str
            Complete path, including the `DataSource` destination directory.

        (RRRe(R	R((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRscCstj||j|��S(s�
        Test if path exists prepending Repository base URL to path.

        Test if `path` exists as (and in this order):

        - a local file.
        - a remote URL that has been downloaded and stored locally in the
          `DataSource` directory.
        - a remote URL that has not been downloaded, but is valid and
          accessible.

        Parameters
        ----------
        path : str
            Can be a local file or a remote URL. This may, but does not have
            to, include the `baseurl` with which the `Repository` was initialized.

        Returns
        -------
        out : bool
            True if `path` exists.

        Notes
        -----
        When `path` is an URL, `exists` will return True if it's either stored
        locally in the `DataSource` directory, or is a valid remote URL.
        `DataSource` does not discriminate between the two, the file is accessible
        if it exists in either location.

        (RR@Re(R	R((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR@3sRcCstj||j|�|�S(s�
        Open and return file-like object prepending Repository base URL.

        If `path` is an URL, it will be downloaded, stored in the DataSource
        directory and opened from there.

        Parameters
        ----------
        path : str
            Local file path or URL to open. This may, but does not have to,
            include the `baseurl` with which the `Repository` was initialized.
        mode : {'r', 'w', 'a'}, optional
            Mode to open `path`.  Mode 'r' for reading, 'w' for writing, 'a' to
            append. Available modes depend on the type of object specified by
            `path`. Default is 'r'.

        Returns
        -------
        out : file object
            File object.

        (RRRe(R	RR((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRTscCs5|j|j�r!td��ntj|j�SdS(s
        List files in the source Repository.

        Returns
        -------
        files : list of str
            List of file names (not containing a directory part).

        Notes
        -----
        Does not currently work for remote repositories.

        s-Directory listing of URLs, not supported yet.N(R;RbtNotImplementedErrorRtlistdir(R	((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRgms(
RRRRR`R
R#ReRKRR@RRg(((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyRa�s$						!(Rt
__docformat__RRERRRRRCtobjectRRR`RRa(((s;/usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt<module>!s?	$�O