ó
šßÈ[c           @` s1  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l 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 m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z m  Z  g  Z! y d d l" Z" e# Z$ Wn e% k
 r¥e& Z$ n Xe# a' d d „ Z( d „  Z) d d d d „ Z+ d „  Z, d d „ Z- d „  Z. d „  Z/ d) Z0 d e# d& „ Z1 d d d e# d' „ Z2 d( „  Z3 d S(*   sè   An extensible ASCII table reader and writer.

ui.py:
  Provides the main user functions for reading and writing tables.

:Copyright: Smithsonian Astrophysical Observatory (2010)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
i    (   t   absolute_importt   divisiont   print_functionNi   (   t   core(   t   basic(   t   cds(   t   daophot(   t   ecsv(   t
   sextractor(   t   ipac(   t   latex(   t   html(   t	   fastbasic(   t   cparser(   t
   fixedwidthi   (   t   Table(   t   get_readable_fileobj(   t   six(   t   AstropyWarningt   AstropyDeprecationWarningi † c         ` sU  t  ˆ  t j ƒ sš yq ˆ  d ˆ  d  d } x: t ˆ  ƒ D], \ } } | t | ƒ 7} | | k r8 Pq8 q8 Wt j j ˆ  | d  ƒ ‰  Wqš t k
 r– qš Xn  t  ˆ  t j ƒ rQˆ  |  ‰  t	 j
 d ˆ  t	 j t	 j Bƒ rÙ t St	 j d ˆ  d t	 j ƒ rt j j ˆ  ƒ rt St	 j
 d ˆ  t	 j t	 j Bƒ r.t St ‡  f d †  d Dƒ ƒ rQt Sn  t S(   so   
    Determine if ``table`` probably contains HTML content.  See PR #3693 and issue
    #3691 for context.
    i    i   s+   ( http[s]? | ftp | file ) :// .+ \.htm[l]?$s
   \.htm[l]?$iûÿÿÿs   \s* <! \s* DOCTYPE \s* HTMLc         3` s7   |  ]- } t  j d  j | ƒ ˆ  t  j t  j Bƒ Vq d S(   s   < \s* {0} [^>]* >N(   t   ret   searcht   formatt
   IGNORECASEt   VERBOSE(   t   .0t   element(   t   table(    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pys	   <genexpr>Y   s   R   t   trt   td(   R   R   R   (   t
   isinstanceR   t   string_typest	   enumeratet   lent   ost   linesept   joint	   ExceptionR   t   matchR   R   t   TrueR   t   patht   existst   allt   False(   R   t   maxcharst   sizet   it   line(    (   R   s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt   _probably_html2   s2    
.c         C` s
   |  a  d S(   s°   
    Set the default value of the ``guess`` parameter for read()

    Parameters
    ----------
    guess : bool
        New default ``guess`` value (e.g., True or False)

    N(   t   _GUESS(   t   guess(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt	   set_guess`   s    c         K` s:   |  d k r t j }  n  t j |  d | d | | } | S(   s
  
    Initialize a table reader allowing for common customizations.  Most of the
    default behavior for various parameters is determined by the Reader class.

    Parameters
    ----------
    Reader : `~astropy.io.ascii.BaseReader`
        Reader class (DEPRECATED). Default is :class:`Basic`.
    Inputter : `~astropy.io.ascii.BaseInputter`
        Inputter class
    Outputter : `~astropy.io.ascii.BaseOutputter`
        Outputter class
    delimiter : str
        Column delimiter string
    comment : str
        Regular expression defining a comment line in table
    quotechar : str
        One-character string to quote fields containing special characters
    header_start : int
        Line index for the header line not counting comment or blank lines.
        A line with only whitespace is considered blank.
    data_start : int
        Line index for the start of data not counting comment or blank lines.
        A line with only whitespace is considered blank.
    data_end : int
        Line index for the end of data not counting comment or blank lines.
        This value can be negative to count from the end.
    converters : dict
        Dictionary of converters.
    data_Splitter : `~astropy.io.ascii.BaseSplitter`
        Splitter class to split data columns.
    header_Splitter : `~astropy.io.ascii.BaseSplitter`
        Splitter class to split header columns.
    names : list
        List of names corresponding to each data column.
    include_names : list, optional
        List of names to include in output.
    exclude_names : list
        List of names to exclude from output (applied after ``include_names``).
    fill_values : dict
        Specification of fill values for bad or missing table values.
    fill_include_names : list
        List of names to include in fill_values.
    fill_exclude_names : list
        List of names to exclude from fill_values (applied after ``fill_include_names``).

    Returns
    -------
    reader : `~astropy.io.ascii.BaseReader` subclass
        ASCII format reader instance
    t   Inputtert	   OutputterN(   t   NoneR   t   BasicR   t   _get_reader(   t   ReaderR4   R5   t   kwargst   reader(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt
   get_readern   s    6c         C` sƒ   |  d  k	 r0 | d  k	 r0 t d j | ƒ ƒ ‚ n  |  d  k	 r |  t j k r[ t j |  } q t d j |  t t j ƒ ƒ ƒ ‚ n  | S(   Ns*   Cannot supply both format and {0} keywordss*   ASCII format {0!r} not in allowed list {1}(   R6   t
   ValueErrorR   R   t   FORMAT_CLASSESt   sorted(   R   t   ReaderWritert   label(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt   _get_format_classª   s    	c         K` s÷  t  2d | k r  d g | d <n  i  } | j d t ƒ } d | k rM t } n  | j d ƒ } | j | ƒ t | | j d ƒ d ƒ } | d k	 r¦ | | d <| j } n  d | k r¼ | d =n  | d k rÑ t } n  | rÃt	 |  ƒ | d <d	 | k r–| j d
 ƒ } y+ t
 |  d
 | ƒ } | j ƒ  }  Wd QXWn$ t k
 rC‚  q–t k
 rSq–Xt j d |  ƒ sv|  t j }  n  | d s–t	 |  ƒ | d <q–n  t |  | | | ƒ }	 |	 d k rÃt } qÃn  | sót |   }
 | rÆ| d k	 rÆd j | ƒ t j k rÆt j | ƒ } t j d j | ƒ | d <t |   } y. | j |  ƒ }	 t  j i | d 6d d 6ƒ Wqðt j t j f k
 rÂ} | d k r•| ‚ n  |
 j |  ƒ }	 t  j i | d 6d d 6ƒ qðXqó|
 j |  ƒ }	 t  j i | d 6d d 6ƒ n  |	 S(   s  
    Read the input ``table`` and return the table.  Most of
    the default behavior for various parameters is determined by the Reader
    class.

    Parameters
    ----------
    table : str, file-like, list, pathlib.Path object
        Input table as a file name, file-like object, list of strings,
        single newline-separated string or pathlib.Path object .
    guess : bool
        Try to guess the table format. Defaults to None.
    format : str, `~astropy.io.ascii.BaseReader`
        Input table format
    Inputter : `~astropy.io.ascii.BaseInputter`
        Inputter class
    Outputter : `~astropy.io.ascii.BaseOutputter`
        Outputter class
    delimiter : str
        Column delimiter string
    comment : str
        Regular expression defining a comment line in table
    quotechar : str
        One-character string to quote fields containing special characters
    header_start : int
        Line index for the header line not counting comment or blank lines.
        A line with only whitespace is considered blank.
    data_start : int
        Line index for the start of data not counting comment or blank lines.
        A line with only whitespace is considered blank.
    data_end : int
        Line index for the end of data not counting comment or blank lines.
        This value can be negative to count from the end.
    converters : dict
        Dictionary of converters
    data_Splitter : `~astropy.io.ascii.BaseSplitter`
        Splitter class to split data columns
    header_Splitter : `~astropy.io.ascii.BaseSplitter`
        Splitter class to split header columns
    names : list
        List of names corresponding to each data column
    include_names : list
        List of names to include in output.
    exclude_names : list
        List of names to exclude from output (applied after ``include_names``)
    fill_values : dict
        specification of fill values for bad or missing table values
    fill_include_names : list
        List of names to include in fill_values.
    fill_exclude_names : list
        List of names to exclude from fill_values (applied after ``fill_include_names``)
    fast_reader : bool or dict
        Whether to use the C engine, can also be a dict with options which
        defaults to `False`; parameters for options dict:

        use_fast_converter: bool
            enable faster but slightly imprecise floating point conversion method
        parallel: bool or int
            multiprocessing conversion using ``cpu_count()`` or ``'number'`` processes
        exponent_style: str
            One-character string defining the exponent or ``'Fortran'`` to auto-detect
            Fortran-style scientific notation like ``'3.14159D+00'`` (``'E'``, ``'D'``, ``'Q'``),
            all case-insensitive; default ``'E'``, all other imply ``use_fast_converter``
    Reader : `~astropy.io.ascii.BaseReader`
        Reader class (DEPRECATED)
    encoding: str
        Allow to specify encoding to read the file (default= ``None``).

    Returns
    -------
    dat : `~astropy.table.Table`
        Output table
    t   fill_valuest    t   0t   fast_readerR5   R   R9   t
   guess_htmlt   readmet   encodingNs   [\r\n]s   fast_{0}R:   s&   Success with fast reader (no guessing)t   statust   forces>   Success with slow reader after failing with fast (no guessing)s1   Success with specified Reader class (no guessing)(   RD   RE   (   t   _read_tracet   getR'   R+   t   updateRB   R6   t   _format_nameR1   R0   R   t   readR=   R%   R   R   R"   R#   t   _guessR<   R   R   t   FAST_CLASSESt   copyt   appendt   ParameterErrorR   t   CParserError(   R   R2   R:   t
   new_kwargst   fast_reader_paramR   R9   RI   t   fileobjt   datR;   t   fast_kwargsRF   t   e(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyRP   ·   sp    J	

	
	c      	   ` sŽ  g  } t  | ƒ } | rr | d k	 rr d j | ƒ t j k rr | j ƒ  } t j d j | ƒ | d <| g | } n d } g  } | j d ƒ } xî | D]æ } | t k rÅ | d t j j ƒ  k rÅ q” n  | d k rð | d t j j ƒ  k rð q” n  t	 }	 xY | j
 ƒ  D]K \ }
 } |
 | k r(| | |
 <q| | |
 k r| | k rt }	 PqqW|	 s^q” n  | | k r” | j | ƒ q” q” Wt | ƒ d k r”d St j t t t t j t j t j f } xÌ| D]} t j ƒ  } y~ d | k rðt	 | d <n  t |   } t	 | _ | j |  ƒ } t j i | d 6d d	 6d
 j t j ƒ  | d ƒ d 6ƒ | SWqÂ| k
 rÅ} t j i | d 6d j | j j t | ƒ ƒ d	 6d
 j t j ƒ  | d ƒ d 6ƒ | j | ƒ qÂXqÂWy> t |   } | j |  ƒ } t j i | d 6d d	 6ƒ | SWn| k
 r‰} t j i | d 6d j | j j t | ƒ ƒ d	 6ƒ | j | ƒ d g } xÚ | D]Ò ‰  t g  t ˆ  ƒ D] } | d  k r€| ^ q€ƒ } t ˆ  j d t j  ƒ ƒ } d t! j" d | ƒ j# d ƒ g } ‡  f d †  | Dƒ } | j$ g  | D] \ }
 } d j |
 | ƒ ^ qƒ | j d j% | ƒ ƒ qjWd d d d d d d d d g	 } | j$ | ƒ t j d j% | ƒ ƒ ‚ n Xd S(!   s”  
    Try to read the table using various sets of keyword args.  Start with the
    standard guess list and filter to make it unique and consistent with
    user-supplied read keyword args.  Finally, if none of those work then
    try the original user-supplied keyword args.

    Parameters
    ----------
    table : str, file-like, list
        Input table as a file name, file-like object, list of strings, or
        single newline-separated string.
    read_kwargs : dict
        Keyword arguments from user to be supplied to reader
    format : str
        Table format
    fast_reader : bool
    fast_reader : bool or dict
        Whether to use the C engine, can also be a dict with options which
        defaults to `False`; parameters for options dict:

        use_fast_converter: bool
            enable faster but slightly imprecise floating point conversion method
        parallel: bool or int
            multiprocessing conversion using ``cpu_count()`` or ``'number'`` processes
        exponent_style: str
            Character to use for exponent or ``'Fortran'`` to auto-detect any
            Fortran-style scientific notation like ``'3.14159D+00'`` (``'E'``, ``'D'``, ``'Q'``),
            all case-insensitive; default ``'E'``, all other imply ``use_fast_converter``

    Returns
    -------
    dat : `~astropy.table.Table` or None
        Output table or None if only one guess format was available
    s   fast_{0}R9   RF   RK   i   t   strict_namesR:   s   Success (guessing)RJ   s
   {0:.3f} msiè  t   dts   {0}: {1}s<   Success with original kwargs without strict_names (guessing)sC   
ERROR: Unable to guess table format with the guesses listed below:R5   s   Reader:s	   \.(\w+)'>c         3` s   |  ] } | ˆ  | f Vq d  S(   N(    (   R   t   key(   R:   (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pys	   <genexpr>ù  s    s   {}: {!r}t    RD   sH   ************************************************************************sH   ** ERROR: Unable to guess table format with the guesses listed above. **sH   **                                                                    **sH   ** To figure out why the table did not read, use guess=False and      **sH   ** fast_reader=False, along with any appropriate arguments to read(). **sH   ** In particular specify the format and any known attributes like the **sH   ** delimiter.                                                         **s   
N(   R9   R5   (&   t   _get_guess_kwargs_listR6   R   R   RR   RS   RM   R+   t   valuesR'   t   itemsRT   R!   t   InconsistentTableErrorR=   t	   TypeErrort   AttributeErrort   OptionalTableImportErrorRU   R   RV   t   timeR<   t   guessingRP   RL   t	   __class__t   __name__t   strR?   t   reprR   R7   R   R   t   groupt   extendR$   (   R   t   read_kwargsR   RF   t   failed_kwargst   full_list_guessR[   t   filtered_guess_kwargst   guess_kwargst   guess_kwargs_okR_   t   valt   guess_exception_classest   t0R;   RZ   t   errt   linest   xt   sorted_keyst   reader_reprt	   keys_valst   kwargs_sortedt   msg(    (   R:   s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyRQ   i  sž    %%%		""	"2	c         C` sû  g  } |  j  d d ƒ r4 | j t d t j ƒ ƒ n  t rV | j t d t j ƒ ƒ n  t	 |  ƒ d k r½ xR t
 j t j t j g D]5 } |  j ƒ  } | j t d | ƒ ƒ | j | ƒ q Wn  | j t d t
 j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j ƒ t d t j  ƒ g ƒ xv t j! t j" t j t j t j# t j$ f D]J } xA d D]9 } x0 d D]( } | j t d | d
 | d | ƒ ƒ qÃWq¶Wq©W| S(   sQ  
    Get the full list of reader keyword argument dicts that are the basis
    for the format guessing process.  The returned full list will then be:

    - Filtered to be consistent with user-supplied kwargs
    - Cleaned to have only unique entries
    - Used one by one to try reading the input table

    Note that the order of the guess list has been tuned over years of usage.
    Maintainers need to be very careful about any adjustments as the
    reasoning may not be immediately evident in all cases.

    This list can (and usually does) include duplicates.  This is a result
    of the order tuning, but these duplicates get removed later.

    Parameters
    ----------
    read_kwargs : dict
       User-supplied read keyword args

    Returns
    -------
    guess_kwargs_list : list
        List of read format keyword arg dicts
    RG   R9   i    t   |t   ,R`   s   \st   "t   't	   delimitert	   quotecharN(   R   R‚   R`   s   \s(   Rƒ   R„   (%   t   popR6   RT   t   dictR   t   HTMLt   HAS_YAMLR   t   EcsvR!   R   t   FixedWidthTwoLineR   t	   FastBasicR   R7   RS   RN   Ro   t   Rdbt   FastTabt   TabR   t   CdsR   t   DaophotR   t
   SExtractorR	   t   IpacR
   t   Latext   AASText   FastCommentedHeadert   CommentedHeadert   FastNoHeadert   NoHeader(   Rp   t   guess_kwargs_listR;   t   first_kwargsR9   R…   R†   (    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyRa   
  s@    	"R…   t   commentR†   t   formatst   namest   include_namest   exclude_namest   strip_whitespacec         K` s“   |  d k r t j }  n  d | k r1 t | d <n  t j |  | |  } t | t j t j	 f ƒ r t | j
 d d ƒ t j ƒ r t d ƒ ‚ n  | S(   sE  
    Initialize a table writer allowing for common customizations.  Most of the
    default behavior for various parameters is determined by the Writer class.

    Parameters
    ----------
    Writer : ``Writer``
        Writer class (DEPRECATED). Defaults to :class:`Basic`.
    delimiter : str
        Column delimiter string
    comment : str
        String defining a comment line in table
    quotechar : str
        One-character string to quote fields containing special characters
    formats : dict
        Dictionary of format specifiers or formatting functions
    strip_whitespace : bool
        Strip surrounding whitespace from column values.
    names : list
        List of names corresponding to each data column
    include_names : list
        List of names to include in output.
    exclude_names : list
        List of names to exclude from output (applied after ``include_names``)
    fast_writer : bool
        Whether to use the fast Cython writer.

    Returns
    -------
    writer : `~astropy.io.ascii.BaseReader` subclass
        ASCII format writer instance
    R¢   R   RD   s´   for the commented_header writer you must supply a string
value for the `comment` keyword.  In order to disable writing
table comments use `del t.meta['comments']` prior to writing.N(   R6   R   R7   R'   R   t   _get_writerR   R˜   R   R—   RM   R   R   R=   (   t   Writert   fast_writerR:   t   writer(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt
   get_writer[  s    !c         K` sG  | j  d d ƒ } t | t j ƒ r‚ t j j | ƒ r‚ | d k r^ t j	 d j
 | ƒ t ƒ q | s t d j
 | ƒ ƒ ‚ q q‚ n  | d k rš t j } n  t |  t ƒ r² |  j n t } | |  d | j d ƒ ƒ}  |  d  j ƒ  } t j | | j d ƒ | j d ƒ | j d ƒ ƒ t | j d g  ƒ ƒ t | j ƒ }	 |	 r[t j	 d	 j
 |	 ƒ t ƒ n  |  j rmt } n  t | | d
 ƒ } t d
 | d | |  }
 |
 j t j k r½|
 j |  | ƒ d S|
 j |  ƒ } t j j  | ƒ } t! | d ƒ s&t" | d ƒ } | j | ƒ | j t j ƒ | j# ƒ  n | j | ƒ | j t j ƒ d S(   s¡  Write the input ``table`` to ``filename``.  Most of the default behavior
    for various parameters is determined by the Writer class.

    Parameters
    ----------
    table : `~astropy.io.ascii.BaseReader`, array_like, str, file_like, list
        Input table as a Reader object, Numpy struct array, file name,
        file-like object, list of strings, or single newline-separated string.
    output : str, file_like
        Output [filename, file-like object]. Defaults to``sys.stdout``.
    format : str
        Output table format. Defaults to 'basic'.
    delimiter : str
        Column delimiter string
    comment : str
        String defining a comment line in table
    quotechar : str
        One-character string to quote fields containing special characters
    formats : dict
        Dictionary of format specifiers or formatting functions
    strip_whitespace : bool
        Strip surrounding whitespace from column values.
    names : list
        List of names corresponding to each data column
    include_names : list
        List of names to include in output.
    exclude_names : list
        List of names to exclude from output (applied after ``include_names``)
    fast_writer : bool
        Whether to use the fast Cython writer.
    overwrite : bool
        If ``overwrite=None`` (default) and the file exists, then a
        warning will be issued. In a future release this will instead
        generate an exception. If ``overwrite=False`` and the file
        exists, then an exception is raised.
        This parameter is ignored when the ``output`` arg is not a string
        (e.g., a file object).
    Writer : ``Writer``
        Writer class (DEPRECATED).

    t	   overwritesx   {} already exists. Automatically overwriting ASCII files is deprecated. Use the argument 'overwrite=True' in the future.s   {} already existsRŸ   i    R    R¡   Rž   sK   The keys {} specified in the formats argument does not match a column name.R¤   R¥   Nt   writet   w($   R‡   R6   R   R   R   R"   R(   t   lexistst   warningst   warnR   R   t   IOErrort   syst   stdoutR   Rj   RM   RS   R   t   _apply_include_exclude_namest   sett   colnamesR   t   has_mixin_columnsR+   RB   R§   RO   RR   R©   R#   R$   t   hasattrt   opent   close(   R   t   outputR   R¤   R¥   R:   R¨   t	   table_clst   table0t   diff_format_with_namesR¦   Rz   t   outstr(    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyR©   ‘  sJ    *%		c           C` s   t  j t ƒ S(   s¨  
    Return a traceback of the attempted read formats for the last call to
    `~astropy.io.ascii.read` where guessing was enabled.  This is primarily for
    debugging.

    The return value is a list of dicts, where each dict includes the keyword
    args ``kwargs`` used in the read call and the returned ``status``.

    Returns
    -------
    trace : list of dicts
       Ordered list of format guesses and status
    (   RS   t   deepcopyRL   (    (    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt   get_read_traceî  s    (   R…   R   R†   Rž   RŸ   R    R¡   R¢   (4   t   __doc__t
   __future__R    R   R   R   R"   R¯   RS   Rh   R¬   RD   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   t
   utils.dataR   t   externR   t   utils.exceptionsR   R   RL   t   yamlR'   RŠ   t   ImportErrorR+   R1   R0   R3   R6   R<   RB   RP   RQ   Ra   t   extra_writer_parsR§   R©   R¾   (    (    (    s2   lib/python2.7/site-packages/astropy/io/ascii/ui.pyt   <module>	   sT   

.	<	²	¡	M 6]