ó
šßÈ[c           @` sl  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m Z d d l	 m
 Z
 m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d e f d „  ƒ  YZ d d d „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j  f d „  ƒ  YZ! d e j" f d „  ƒ  YZ# d S(   sÇ   An extensible HTML table reader and writer.

html.py:
  Classes to read and write HTML tables

`BeautifulSoup <http://www.crummy.com/software/BeautifulSoup/>`_
must be installed to read HTML tables.
i    (   t   absolute_importt   divisiont   print_functionNi   (   t   six(   t   zipt   rangei   (   t   core(   t   Column(   t   writer(   t   deepcopyt
   SoupStringc           B` s    e  Z d  Z d „  Z d „  Z RS(   s8   
    Allows for strings to hold BeautifulSoup data.
    c         O` s   t  j |  | | Ž S(   N(   t   strt   __new__(   t   clst   argst   kwargs(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR      s    c         C` s   | |  _  d  S(   N(   t   soup(   t   selft   val(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   __init__"   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR
      s   	t
   ListWriterc           B` s    e  Z d  Z d „  Z d „  Z RS(   sD   
    Allows for XMLWriter to write to a list instead of a file.
    c         C` s   | |  _  d  S(   N(   t   out(   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR   +   s    c         C` s   |  j  j | ƒ d  S(   N(   R   t   append(   R   t   data(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   write.   s    (   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR   &   s   	c         C` s‹   |  d k s |  j d k r t Sd | k r5 | d k S| d } t | t j ƒ rn d |  j k om |  d | k St | t ƒ r‡ | | k St S(   sb   
    Checks whether the given BeautifulSoup tag is the table
    the user intends to process.
    t   tablet   table_idi   t   idN(   t   Nonet   namet   Falset
   isinstanceR   t   string_typest   attrst   int(   R   t   htmldictt   numtableR   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   identify_table2   s    


t   HTMLInputterc           B` s   e  Z d  Z d „  Z RS(   s›   
    Input lines of HTML in a valid form.

    This requires `BeautifulSoup
        <http://www.crummy.com/software/BeautifulSoup/>`_ to be installed.
    c         C` sx  y d d l  m } Wn  t k
 r6 t j d ƒ ‚ n Xd |  j k r t j ƒ  * t j d d ƒ | d j	 | ƒ ƒ } Wd QXn | d j	 | ƒ |  j d ƒ } | j
 d	 ƒ } xš t | ƒ D]/ \ } } t | |  j | d
 ƒ r¼ | } Pq¼ q¼ Wt |  j d t ƒ rd j |  j d ƒ } n d j |  j d ƒ } t j d j | ƒ ƒ ‚ g  | j
 d ƒ D] }	 t |	 ƒ ^ q\}
 |
 S(   sh   
        Convert the given input into a list of SoupString rows
        for further processing.
        i    (   t   BeautifulSoups3   BeautifulSoup must be installed to read HTML tablest   parsert   ignores&   .*no parser was explicitly specified.*s   
NR   i   R   s
   number {0}s   id '{0}'s   ERROR: HTML table {0} not foundt   tr(   t   bs4R*   t   ImportErrorR   t   OptionalTableImportErrort   htmlt   warningst   catch_warningst   filterwarningst   joint   find_allt	   enumerateR(   R"   R%   t   formatt   InconsistentTableErrorR
   (   R   t   linesR*   R   t   tablest   it   possible_tableR   t	   err_descrt   xt	   soup_list(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   process_linesP   s*    ((   R   R   R   RA   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR)   H   s   t   HTMLSplitterc           B` s   e  Z d  Z d „  Z RS(   s    
    Split HTML table data.
    c         c` sú   xÏ | D]Ç } t  | t ƒ s+ t d ƒ ‚ n  | j } | j d ƒ } | r• g  | D]= } | j d ƒ r~ | j j ƒ  | d f n | j j ƒ  ^ qP Vn  | j d ƒ } | r g  | D] } | j j ƒ  ^ q± Vq q Wt | ƒ d k rö t	 j
 d ƒ ‚ n  d S(   s=   
        Return HTML data from lines as a generator.
        s'   HTML lines should be of type SoupStringt   tht   colspant   tdi    s.   HTML tables must contain data in a <table> tagN(   R"   R
   t	   TypeErrorR   R6   t   has_attrt   textt   stript   lenR   R9   (   R   R:   t   lineR   t   header_elementst   elt   data_elements(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   __call__{   s    	I((   R   R   R   RO   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRB   v   s   t   HTMLOutputterc           B` sY   e  Z d  Z e j e j ƒ e j e j ƒ e j e j ƒ e j e j	 ƒ g Z
 d „  Z RS(   sÍ   
    Output the HTML data as an ``astropy.table.Table`` object.

    This subclass allows for the final table to contain
    multidimensional columns (defined using the colspan attribute
    of <th>).
    c   	      C` sÜ   g  } d } x´ | t  | ƒ k  rÂ | | } t | d ƒ r¨ | | | | j !} t j | j ƒ } t t g  | D] } | j ^ qm Œ  ƒ | _ | j	 | ƒ | | j 7} q | j	 | ƒ | d 7} q Wt
 t |  ƒ j | | ƒ S(   s?   
        Process the data in multidimensional columns.
        i    RD   i   (   RJ   t   hasattrRD   R   R   R    t   listR   t   str_valsR   t   superRP   RO   (	   R   t   colst   metat   new_colst   col_numt   colt	   span_colst   new_colR?   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRO   ž   s    
+(   R   R   R   R   t   convert_numpyt   numpyR%   t   floatR   t   unicodet   default_convertersRO   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRP      s   t
   HTMLHeaderc           B` s    e  Z e Z d  „  Z d „  Z RS(   c         C` s[   xT t  | ƒ D]F \ } } t | t ƒ s7 t d ƒ ‚ n  | j } | j d k	 r | Sq Wd S(   sE   
        Return the line number at which header data begins.
        s'   HTML lines should be of type SoupStringN(   R7   R"   R
   RF   R   RC   R   (   R   R:   R<   RK   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt
   start_line¸   s    	c         C` sÿ   g  |  _  g  } xà |  j D]Õ } t | t ƒ rÅ t j d | d ƒ } t | d ƒ | _ |  j  j | ƒ | j | d ƒ xs t	 d t | d ƒ ƒ D], } |  j  j t j d ƒ ƒ | j d ƒ q’ Wq |  j  j t j d | ƒ ƒ | j | ƒ q W| |  _ d S(   sU   
        Set columns from header names, handling multicolumns appropriately.
        R    i    i   t    N(
   RU   t   namesR"   t   tupleR   R   R%   RD   R   R   (   R   t	   new_namesR    RY   R<   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   _set_cols_from_namesÆ   s    	 (   R   R   RB   t   splitter_classRb   Rg   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRa   µ   s   	t   HTMLDatac           B` s    e  Z e Z d  „  Z d „  Z RS(   c         C` s‹   xu t  | ƒ D]g \ } } t | t ƒ s7 t d ƒ ‚ n  | j } | j d k	 r | j d k	 rp t j	 d ƒ ‚ n  | Sq Wt j	 d ƒ ‚ d S(   sD   
        Return the line number at which table data begins.
        s'   HTML lines should be of type SoupStrings9   HTML tables cannot have headings and data in the same rows!   No start line found for HTML dataN(
   R7   R"   R
   RF   R   RE   R   RC   R   R9   (   R   R:   R<   RK   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRb   á   s    	c         C` sz   d } xY t  | ƒ D]K \ } } t | t ƒ s= t d ƒ ‚ n  | j } | j d k	 r | } q q W| d k rr d S| d S(   sB   
        Return the line number at which table data ends.
        iÿÿÿÿs'   HTML lines should be of type SoupStringi   N(   R7   R"   R
   RF   R   RE   R   (   R   R:   t
   last_indexR<   RK   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   end_lineó   s    	(   R   R   RB   Rh   Rb   Rk   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRi   Þ   s   	t   HTMLc           B` sb   e  Z d  Z d Z d g Z d Z d Z e Z e	 Z
 e Z i  d „ Z d „  Z d „  Z d „  Z RS(   sy
  Read and write HTML tables.

    In order to customize input and output, a dict of parameters may
    be passed to this class holding specific customizations.

    **htmldict** : Dictionary of parameters for HTML input/output.

        * css : Customized styling
            If present, this parameter will be included in a <style>
            tag and will define stylistic attributes of the output.

        * table_id : ID for the input table
            If a string, this defines the HTML id of the table to be processed.
            If an integer, this specifies the index of the input table in the
            available tables. Unless this parameter is given, the reader will
            use the first table found in the input file.

        * multicol : Use multi-dimensional columns for output
            The writer will output tuples as elements of multi-dimensional
            columns if this parameter is true, and if not then it will
            use the syntax 1.36583e-13 .. 1.36583e-13 for output. If not
            present, this parameter will be true by default.

        * raw_html_cols : column name or list of names with raw HTML content
            This allows one to include raw HTML content in the column output,
            for instance to include link references in a table.  This option
            requires that the bleach package be installed.  Only whitelisted
            tags are allowed through for security reasons (see the
            raw_html_clean_kwargs arg).

        * raw_html_clean_kwargs : dict of keyword args controlling HTML cleaning
            Raw HTML will be cleaned to prevent unsafe HTML from ending up in
            the table output.  This is done by calling ``bleach.clean(data,
            **raw_html_clean_kwargs)``.  For details on the available options
            (e.g. tag whitelist) see:
            http://bleach.readthedocs.io/en/latest/clean.html

        * parser : Specific HTML parsing library to use
            If specified, this specifies which HTML parsing library
            BeautifulSoup should use as a backend. The options to choose
            from are 'html.parser' (the standard library parser), 'lxml'
            (the recommended parser), 'xml' (lxml's XML parser), and
            'html5lib'. html5lib is a highly lenient parser and therefore
            might work correctly for unusual input if a different parser
            fails.

        * jsfiles : list of js files to include when writing table.

        * cssfiles : list of css files to include when writing table.

        * js : js script to include in the body when writing table.

        * table_class : css class for the table

    R1   s   .htmls
   HTML tablec         C` sm   t  t |  ƒ j ƒ  t | ƒ |  _ d | k r> t |  j d <n  d | k rZ d |  j d <n  |  j |  j _ d S(   sB   
        Initialize classes for HTML reading and writing.
        t   multicolR   i   N(   RT   Rl   R   R	   R1   t   Truet   inputter(   R   R&   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR   G  s    c         C` s"   t  ƒ  |  _ t t |  ƒ j | ƒ S(   sU   
        Read the ``table`` in HTML format and return a resulting ``Table``.
        (   RP   t	   outputterRT   Rl   t   read(   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRq   S  s    c      !   C` s  t  t j | j ƒ ƒ } | |  j j _ t |  j j t	 ƒ rT |  j j g |  j _ n  |  j j
 | ƒ g  } |  j j d g  ƒ } t | t j ƒ r | g } n  g  | D] } | j j | k ^ q¤ } |  j j d i  ƒ } t j t | ƒ ƒ } | j d ƒ | j d ƒ 3| j d d i d d 6ƒ Wd	 QX| j d d i d
 d 6d d 6ƒ Wd	 QXd |  j k r•| j d ƒ  | j |  j d ƒ Wd	 QXn  d |  j k rçx@ |  j d D]. }	 | j d d d d |	 d d ƒ Wd	 QXq²Wn  d |  j k r:xA |  j d D]/ }	 | j d d |	 ƒ | j d ƒ Wd	 QXqWn  Wd	 QX| j d ƒ 6d |  j k r¢| j d ƒ / | j d ƒ  | j |  j d ƒ Wd	 QXWd	 QXn  t |  j d t j ƒ rË|  j d }
 n d	 }
 d |  j k rý|  j d } i | d  6} n i  } | j d! d" |
 d | ƒa| j d# ƒ £| j d$ ƒ ‹ xƒ | D]{ } t | j ƒ d% k r‹|  j d& r‹| j d' d( | j d% ƒn | j d' ƒ | j | j j j ƒ  ƒ | j d) t ƒ qFWWd	 QXg  } g  } xó t | | ƒ D]â \ } } t | j ƒ d% k r”|  j d& r”| j d% } x¤ t | ƒ D]^ } t g  | D] } | | ^ q?ƒ } |  j | | j j ƒ  ƒ } | j  | ƒ | j  | ƒ q/Wqç|  j | | j j ƒ  ƒ } | j  | ƒ | j  | ƒ qçWWd	 QXx¥ t | Œ  D]— } | j d$ ƒ € xx t | | ƒ D]g \ } } | rd* n d+ } | j | |  5 | j d, ƒ | j | j ƒ  ƒ | j d) t ƒ Wd	 QXqWWd	 QXqàWWd	 QXWd	 QXWd	 QXd j! | ƒ g S(-   sR   
        Return data in ``table`` converted to HTML as a list of strings.
        t   raw_html_colst   raw_html_clean_kwargsR1   t   headRV   t   attribs   utf-8t   charsetNs   Content-types
   http-equivs   text/html;charset=UTF-8t   contentt   csst   stylet   cssfilest   linkt   relt
   stylesheett   hreft   types   text/csst   jsfilest   scriptt   srcRc   t   bodyt   jst   noneR   t   table_classt   classR   R   t   theadR-   i   Rm   RC   RD   t   indentt
   escape_xmlt   bleach_cleanRE   ("   RR   R   t
   itervaluest   columnsR   t   headerRU   R"   t   fill_valuesRe   t   _set_fill_valuesR1   t   getR#   t   infoR    R   t	   XMLWriterR   t   tagt   xml_cleaning_methodR   RJ   t   shapet   startRI   t   endR!   R   R   R   t   iter_str_valsR   R5   (   R   R   RU   R:   Rr   RY   t   cols_escapedRs   t   wt   filenamet   html_table_idt   html_table_classRu   t   col_str_iterst   new_cols_escapedt   col_escapedt   spanR<   RM   R[   t   new_col_iter_str_valst   col_iter_str_valst   rowt   method(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR   [  s’    %" #""#6c         c` sŸ   t  | d ƒ } t  | d ƒ } xz t | ƒ D]l \ } } | rh | rh | j | rh | j t j Vq+ qh n  | r’ | | j k r’ | j | Vq+ q’ n  | Vq+ Wd S(   sY   
        Return an iterator of the values with replacements based on fill_values
        t   maskR   N(   RQ   R7   R§   R   R   t   masked(   R   RY   RŸ   t   is_masked_columnt   has_fill_valuest   idxt   col_str(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyR   Â  s    		(   R   R   R   t   _format_namet   _io_registry_format_aliasest   _io_registry_suffixt   _descriptionRa   t   header_classRi   t
   data_classR)   t   inputter_classR   Rq   R   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyRl     s   7			g(    ($   R   t
   __future__R    R   R   R2   R]   t   externR   t   extern.six.movesR   R   Rc   R   R   R   t	   utils.xmlR   t   copyR	   R   R
   R   R(   t   BaseInputterR)   t   BaseSplitterRB   t   TableOutputterRP   t
   BaseHeaderRa   t   BaseDataRi   t
   BaseReaderRl   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/html.pyt   <module>	   s$   	.%)'