Current File : //proc/self/root/proc/self/root/lib64/python2.7/site-packages/numpy/lib/_datasource.pyo |
�
E�`Qc @ s� d Z d Z d d l Z d d l m Z m Z m Z e Z d e f d � � YZ
e
� Z d e j d � Z d e f d
� � YZ
d e
f d � � YZ d S(
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()
s restructuredtext eni����N( t rmtreet copyfilet copyfileobjt _FileOpenersc B s2 e Z d Z d � Z d � Z d � Z d � Z RS( 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
c C s t | _ i t d 6| _ d S( N( t Falset _loadedt opent Nonet
_file_openers( t self( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt __init__F s c C s� | j r
d Sy d d l } | j | j d <Wn t k
r@ n Xy d d l } | j | j d <Wn t k
rt n Xt | _ d S( Ni����s .bz2s .gz( R t bz2t BZ2FileR t ImportErrort gzipR t True( R R R ( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt _loadI s
c C s | j � | j j � 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.
( R R t keys( R ( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR X s
c C s | j � | j | S( N( R R ( R t key( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyt __getitem__j s
( t __name__t
__module__t __doc__R
R R R ( ( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR / s
t rc C s t | � } | 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 ( t patht modet destpatht ds( ( s; /usr/lib64/python2.7/site-packages/numpy/lib/_datasource.pyR p s R c B s� e Z d Z e j d � Z d � Z d � Z d � Z d � Z d � Z
d � Z d � Z d � Z
d
� Z d � Z d � Z d
d � Z RS( 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'
c C sO | r'