ó
šßÈ[c           @` sú  d  d l  m Z m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l m	 Z	 d  d l
 m Z d  d l Z d d l m Z d d l m Z d d	 d
 d d d d d d d d g Z d
 g Z e	 ƒ  Z e	 ƒ  Z e	 ƒ  Z e j Z y d  d l Z Wn e k
 re Z n Xe Z e e j f 7Z d e f d „  ƒ  YZ  e! ƒ  Z" e j# d „  ƒ Z$ d d d „ Z& d „  Z' e d „ Z( d „  Z) e d „ Z* d „  Z+ e d „ Z, d „  Z- d „  Z. d „  Z/ d  „  Z0 d! „  Z1 d" „  Z2 d# „  Z3 d$ „  Z4 d% „  Z5 d S(&   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   OrderedDict(   t
   itemgetteri   (   t   six(   t   zipu   register_readeru   register_writeru   register_identifieru   identify_formatu
   get_readeru
   get_writeru   readu   writeu   get_formatsu   IORegistryErroru   delay_doc_updatest   IORegistryErrorc           B` s   e  Z d  Z RS(   u'   Custom error for registry clashes.
    (   t   __name__t
   __module__t   __doc__(    (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyR   (   s   c         c` s=   t  j |  ƒ d Vt  j |  ƒ t |  d ƒ t |  d ƒ d S(   uÔ  Contextmanager to disable documentation updates when registering
    reader and writer. The documentation is only built once when the
    contextmanager exits.

    .. versionadded:: 1.3

    Parameters
    ----------
    cls : class
        Class for which the documentation updates should be delayed.

    Notes
    -----
    Registering mutliple readers and writers can cause significant overhead
    because the documentation of the corresponding ``read`` and ``write``
    methods are build every time.

    .. warning::
        This contextmanager is experimental and may be replaced by a more
        general approach.

    Examples
    --------
    see for example the source code of ``astropy.table.__init__``.
    Nu   readu   write(   t   _delayed_docs_classest   addt   discardt   _update__doc__(   t   cls(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   delay_doc_updates4   s
    c      	   C` s  d d l  m } t t t ƒ t t ƒ Bd t d ƒ ƒ} g  } xÙ | D]Ñ } |  d k	 rt t |  | d | ƒ rt qE n  | t k r† d n d } | t k rž d n d } | t	 k r¶ d n d } d | d | d f }	 |	 | k ræ d n d	 }
 | j
 | d j | d | | | |
 f ƒ qE W| d k	 r¥| d
 k r^g  | D] } | d d k r9| ^ q9} q¥| d k r–g  | D] } | d d k rq| ^ qq} q¥t d ƒ ‚ n  | rØt t t | d t d d d ƒ ƒŒ  ƒ } n d } | | d d ƒ} t j | d d k ƒ s| j d ƒ n  | S(   uä  
    Get the list of registered I/O formats as a Table.

    Parameters
    ----------
    data_class : classobj, optional
        Filter readers/writer to match data class (default = all classes).

    readwrite : str or None, optional
        Search only for readers (``"Read"``) or writers (``"Write"``). If None
        search for both.  Default is None.

        .. versionadded:: 1.3

    Returns
    -------
    format_table : Table
        Table of available I/O formats.
    i   (   t   Tablet   keyi    i   u   Yesu   Nou   ascii.u    u   Readu   Writei   uQ   unrecognized value for "readwrite": {0}.
Allowed are "Read" and "Write" and None.i   t   namesu
   Data classu   Formatu   Auto-identifyu
   DeprecatedN(   u
   Data classu   Formatu   Readu   Writeu   Auto-identifyu
   Deprecated(   t   tableR   t   sortedt   sett   _readerst   _writersR   t   Nonet   _is_best_matcht   _identifierst   appendR	   t
   ValueErrort   listR   t   npt   anyt   remove_column(   t
   data_classt	   readwriteR   t   format_classest   rowst   format_classt   has_readt	   has_writet   has_identifyt   ascii_format_classt
   deprecatedt   rowt   datat   format_table(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   get_formatsX   s8    (,,-		c         C` sç  d } t  |  | ƒ } t | j t j ƒ s. d S| j j ƒ  } g  t | ƒ D] \ } } | | k rJ | ^ qJ } | r‹ | d } | |  } n  g  | d D] } t j d | ƒ ^ q– }	 d t	 d „  |	 Dƒ ƒ }
 t
 |  | j ƒ  ƒ } | j d ƒ | j d	 d
 d d ƒ } t j d d | d ƒ } | | d <| j d | ƒ | j | ƒ d | j k rp| j d d d g ƒ n  | d g | } | j g  | D] } |
 | ^ qƒ y d j | ƒ | _ Wn& t k
 râd j | ƒ | j _ n Xd S(   uŸ   
    Update the docstring to include all the available readers / writers for the
    ``data_class.read`` or ``data_class.write`` functions (respectively).
    u#   The available built-in formats are:Ni    i   u   (\S)u    c         s` s!   |  ] } | r | j  ƒ  Vq d  S(   N(   t   start(   t   .0t   match(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pys	   <genexpr>¶   s    u
   Data classt	   max_linesiÿÿÿÿt	   max_widthiP   u   -u   =u
   Deprecatedu    uZ   Deprecated format names like ``aastex`` will be removed in a future version. Use the full u%   name (e.g. ``ascii.aastex``) instead.u   
(   t   getattrt
   isinstanceR   R   t   string_typest
   splitlinest	   enumeratet   ret   searcht   minR0   t
   capitalizeR"   t   pformatt   subt   insertR   t   colnamest   extendt   joint   AttributeErrort   __func__(   R#   R$   t   FORMATS_TEXTt   class_readwrite_funct   linest   iit   linet   sep_indicest
   chop_indext   matchest   left_indentR/   t	   new_linest   table_rst_sep(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyR   œ   s8    1
)
	$c         C` sf   |  | f t  k s | r+ | t  |  | f <n t d j |  | j ƒ ƒ ‚ | t k rb t | d ƒ n  d S(   uì  
    Register a reader function.

    Parameters
    ----------
    data_format : str
        The data format identifier. This is the string that will be used to
        specify the data type when reading.
    data_class : classobj
        The class of the object that the reader produces.
    function : function
        The function to read in a data object.
    force : bool, optional
        Whether to override any existing function if already present.
        Default is ``False``.
    u:   Reader for format '{0}' and class '{1}' is already definedu   readN(   R   R   t   formatR	   R   R   (   t   data_formatR#   t   functiont   force(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   register_reader×   s    	c         C` sc   |  | f t  k r( t  j |  | f ƒ n t d j |  | j ƒ ƒ ‚ | t k r_ t | d ƒ n  d S(   uÓ   
    Unregister a reader function

    Parameters
    ----------
    data_format : str
        The data format identifier.
    data_class : classobj
        The class of the object that the reader produces.
    u2   No reader defined for format '{0}' and class '{1}'u   readN(   R   t   popR   RR   R	   R   R   (   RS   R#   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   unregister_readerô   s    	c         C` sf   |  | f t  k s | r+ | t  |  | f <n t d j |  | j ƒ ƒ ‚ | t k rb t | d ƒ n  d S(   uï  
    Register a table writer function.

    Parameters
    ----------
    data_format : str
        The data format identifier. This is the string that will be used to
        specify the data type when writing.
    data_class : classobj
        The class of the object that can be written.
    function : function
        The function to write out a data object.
    force : bool, optional
        Whether to override any existing function if already present.
        Default is ``False``.
    u:   Writer for format '{0}' and class '{1}' is already definedu   writeN(   R   R   RR   R	   R   R   (   RS   R#   RT   RU   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   register_writer
  s    	c         C` sc   |  | f t  k r( t  j |  | f ƒ n t d j |  | j ƒ ƒ ‚ | t k r_ t | d ƒ n  d S(   uÎ   
    Unregister a writer function

    Parameters
    ----------
    data_format : str
        The data format identifier.
    data_class : classobj
        The class of the object that can be written.
    u2   No writer defined for format '{0}' and class '{1}'u   writeN(   R   RW   R   RR   R	   R   R   (   RS   R#   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   unregister_writer'  s    	c         C` sJ   |  | f t  k s | r+ | t  |  | f <n t d j |  | j ƒ ƒ ‚ d S(   uS  
    Associate an identifier function with a specific data type.

    Parameters
    ----------
    data_format : str
        The data format identifier. This is the string that is used to
        specify the data type when reading/writing.
    data_class : classobj
        The class of the object that can be written.
    identifier : function
        A function that checks the argument specified to `read` or `write` to
        determine whether the input can be interpreted as a table of type
        ``data_format``. This function should take the following arguments:

           - ``origin``: A string ``"read"`` or ``"write"`` identifying whether
             the file is to be opened for reading or writing.
           - ``path``: The path to the file.
           - ``fileobj``: An open file object to read the file's contents, or
             `None` if the file could not be opened.
           - ``*args``: Positional arguments for the `read` or `write`
             function.
           - ``**kwargs``: Keyword arguments for the `read` or `write`
             function.

        One or both of ``path`` or ``fileobj`` may be `None`.  If they are
        both `None`, the identifier will need to work from ``args[0]``.

        The function should return True if the input can be identified
        as being of format ``data_format``, and False otherwise.
    force : bool, optional
        Whether to override any existing function if already present.
        Default is ``False``.

    Examples
    --------
    To set the identifier based on extensions, for formats that take a
    filename as a first argument, you can do for example::

        >>> def my_identifier(*args, **kwargs):
        ...     return (isinstance(args[0], basestring) and
        ...             args[0].endswith('.tbl'))
        >>> register_identifier('ipac', Table, my_identifier)
    u>   Identifier for format '{0}' and class '{1}' is already definedN(   R   R   RR   R	   (   RS   R#   t
   identifierRU   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   register_identifier=  s
    .	c         C` sG   |  | f t  k r( t  j |  | f ƒ n t d j |  | j ƒ ƒ ‚ d S(   uØ   
    Unregister an identifier function

    Parameters
    ----------
    data_format : str
        The data format identifier.
    data_class : classobj
        The class of the object that can be read/written.
    u6   No identifier defined for format '{0}' and class '{1}'N(   R   RW   R   RR   R	   (   RS   R#   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   unregister_identifiers  s    	c   	      C` sh   g  } x[ t  D]S \ } } t | | t  ƒ r t  | | f |  | | | | Ž r` | j | ƒ q` q q W| S(   u­  Loop through identifiers to see which formats match.

    Parameters
    ----------
    origin : str
        A string ``"read`` or ``"write"`` identifying whether the file is to be
        opened for reading or writing.
    data_class_required : object
        The specified class for the result of `read` or the class that is to be
        written.
    path : str, other path object or None
        The path to the file or None.
    fileobj : File object or None.
        An open file object to read the file's contents, or ``None`` if the
        file could not be opened.
    args : sequence
        Positional arguments for the `read` or `write` function. Note that
        these must be provided as sequence.
    kwargs : dict-like
        Keyword arguments for the `read` or `write` function. Note that this
        parameter must be `dict`-like.

    Returns
    -------
    valid_formats : list
        List of matching formats.
    (   R   R   R   (	   t   origint   data_class_requiredt   patht   fileobjt   argst   kwargst   valid_formatsRS   R#   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   identify_format†  s    c         C` s>   t  |  d | ƒ} | j d ƒ d j | j d d ƒ ƒ } | S(   NR$   u
   Data classu   
R4   iÿÿÿÿ(   R0   R"   RD   R?   (   R#   R$   R/   t   format_table_str(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   _get_format_table_str¬  s    c         C` s™   g  t  D]$ \ } } | |  k r | | f ^ q } xa | D], \ } } t | | | ƒ r8 t  | | f Sq8 Wt | d ƒ } t d j |  | j | ƒ ƒ ‚ d S(   u”  Get reader for ``data_format``.

    Parameters
    ----------
    data_format : str
        The data format identifier. This is the string that is used to
        specify the data type when reading/writing.
    data_class : classobj
        The class of the object that can be written.

    Returns
    -------
    reader : callable
        The registered reader function for this format and class.
    u   ReaduR   No reader defined for format '{0}' and class '{1}'.
The available formats are:
{2}N(   R   R   Rg   R   RR   R	   (   RS   R#   t   fmtR   t   readerst   reader_formatt   reader_classRf   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt
   get_reader³  s    1c         C` s™   g  t  D]$ \ } } | |  k r | | f ^ q } xa | D], \ } } t | | | ƒ r8 t  | | f Sq8 Wt | d ƒ } t d j |  | j | ƒ ƒ ‚ d S(   u”  Get writer for ``data_format``.

    Parameters
    ----------
    data_format : str
        The data format identifier. This is the string that is used to
        specify the data type when reading/writing.
    data_class : classobj
        The class of the object that can be written.

    Returns
    -------
    writer : callable
        The registered writer function for this format and class.
    u   WriteuR   No writer defined for format '{0}' and class '{1}'.
The available formats are:
{2}N(   R   R   Rg   R   RR   R	   (   RS   R#   Rh   R   t   writerst   writer_formatt   writer_classRf   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt
   get_writerÏ  s    1c   
      O` s  | j  d d ƒ } d } zÜ| d k rZd } d } t | ƒ r<t | d t ƒ rd d l m } t rœ t | d t j	 ƒ rœ t
 | d ƒ f | d } n  | d } y& | | d d d ƒ} | j ƒ  } Wn* t k
 râ ‚  q9t k
 rø d } q9X| g t | d ƒ } q<t | d d ƒ r<d } | d } q<n  t d |  | | | | ƒ } n  t | |  ƒ } | | | Ž  }	 t |	 |  ƒ sót |  |	 j ƒ rØy |  |	 ƒ }	 Wqðt k
 rÔt d	 j |  j ƒ ƒ ‚ qðXqót d
 j |  j ƒ ƒ ‚ n  Wd | d k	 r| j t j ƒ  Œ  n  X|	 S(   uV   
    Read in data.

    The arguments passed to this method depend on the format.
    u   formati    i   (   t   get_readable_fileobji   t   encodingu   binaryu   readu-   could not convert reader output to {0} class.u#   reader should return a {0} instanceN(   RW   R   t   lenR7   t
   PATH_TYPESt
   utils.dataRq   t   HAS_PATHLIBt   pathlibt   Patht   strt	   __enter__t   IOErrort	   ExceptionR   t   hasattrt   _get_valid_formatRl   t
   issubclasst	   __class__t	   TypeErrorRR   R	   t   __exit__t   syst   exc_info(
   R   Rb   Rc   RR   t   ctxR`   Ra   Rq   t   readerR.   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   readë  sN    

		c         O` s  | j  d d ƒ } | d k rà d } d } t | ƒ r¿ t | d t ƒ r– t rƒ t | d t j ƒ rƒ t | d ƒ f | d } n  | d } d } q¿ t	 | d d ƒ r¿ d } | d } q¿ n  t
 d |  j | | | | ƒ } n  t | |  j ƒ } | |  | | Ž d S(   uX   
    Write out data.

    The arguments passed to this method depend on the format.
    u   formati    i   u   readu   writeN(   RW   R   Rs   R7   Rt   Rv   Rw   Rx   Ry   R}   R~   R€   Rp   (   R.   Rb   Rc   RR   R`   Ra   t   writer(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   write)  s"    
	c         C` sZ   t  |  | ƒ rV d „  | Dƒ } x4 |  j D]& } | | k r? t S| | k r) t Sq) Wn  t S(   ud  
    Determine if class2 is the "best" match for class1 in the list
    of classes.  It is assumed that (class2 in classes) is True.
    class2 is the the best match if:

    - ``class1`` is a subclass of ``class2`` AND
    - ``class2`` is the nearest ancestor of ``class1`` that is in classes
      (which includes the case that ``class1 is class2``)
    c         S` s   h  |  ] \ } } | ’ q S(    (    (   R2   Rh   R   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pys	   <setcomp>S  s   	 (   R   t   __mro__t   Truet   False(   t   class1t   class2R%   t   classest   parent(    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyR   H  s    
c         C` s§   t  |  | | | | | ƒ } t | ƒ d k rZ t | |  j ƒ  ƒ } t d j | ƒ ƒ ‚ nE t | ƒ d k rŸ t d j d j t | d t d ƒ ƒƒ ƒ ƒ ‚ n  | d S(   uˆ   
    Returns the first valid format that can be used to read/write the data in
    question.  Mode can be either 'read' or 'write'.
    i    u>   Format could not be identified.
The available formats are:
{0}i   u&   Format is ambiguous - options are: {0}u   , R   (	   Re   Rs   Rg   R>   R   RR   RD   R   R   (   t   modeR   R`   Ra   Rb   Rc   Rd   Rf   (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyR~   \  s    	*(6   t
   __future__R    R   R   R   t
   contextlibR;   Rƒ   t   collectionsR   t   operatorR   t   numpyR    t   externR   t   extern.six.movesR   t   __all__t   __doctest_skip__R   R   R   R8   Rt   Rw   t   ImportErrorRŒ   Rv   R‹   Rx   R|   R   R   R   t   contextmanagerR   R   R0   R   RV   RX   RY   RZ   R\   R]   Re   Rg   Rl   Rp   R‡   R‰   R   R~   (    (    (    s2   lib/python2.7/site-packages/astropy/io/registry.pyt   <module>   sR   "						
		$D	;		6		&				>		