B
    dIo                 @   s   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	m
Z
mZmZ d dlmZ g Zdd Zdd Zd	d
 ZdefddZdd ZG dd dZdS )    N)log)Getchcolor_printterminal_sizeconf)dtype_info_namec             C   s$   t |tr|jdddS t|S d S )Nzutf-8replace)errors)
isinstancebytesdecodestr)format_val r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/table/pprint.pydefault_format_func   s    
r   c                s    fddS )zWrap format function to trap masked values.

    String format functions and most user functions will not be able to deal
    with masked values, so we wrap them to ensure they are passed to str().
    c                s   |t jjkrt|S  | |S )N)npmamaskedr   )r   r   )format_funcr   r   <lambda>    s   z,_use_str_for_masked_values.<locals>.<lambda>r   )r   r   )r   r   _use_str_for_masked_values   s    r   c             c   s"   dd V  dd V  dd V  dS )zIterate through possible string-derived format functions.

    A string can either be a format specifier for the format built-in,
    a new-style format string, or an old-style format string.
    c             S   s
   t || S )N)format)r   r   r   r   r   r   *       z3_possible_string_format_functions.<locals>.<lambda>c             S   s
   |  |S )N)r   )r   r   r   r   r   r   +   r   c             S   s   | | S )Nr   )r   r   r   r   r   r   ,   r   Nr   )r   r   r   r   !_possible_string_format_functions$   s    

r   c                s    fdd}|S )aZ  
    Return a wrapped ``auto_format_func`` function which is used in
    formatting table columns.  This is primarily an internal function but
    gets used directly in other parts of astropy, e.g. `astropy.io.ascii`.

    Parameters
    ----------
    col_name : object, optional
        Hashable object to identify column like id or name. Default is None.

    possible_string_format_functions : func, optional
        Function that yields possible string formatting functions
        (defaults to internal function to do this).

    Returns
    -------
    Wrapped ``auto_format_func`` function
    c          
      sv  | dkrt | |S |  jjkr0 jj|  | |S t| rdd }y,|| |}t|tsjtd|t|W nJ t	k
r } z,|t
jjkrt|S td| d| W dd}~X Y nX y|| t
jj W n t	k
r   t|}Y nX n||t
jjkrt|S x^| D ]D}y || |}|| kr*tdW n t	k
rF   wY nX P qW td| t|}| jj| < |S )	a  Format ``val`` according to ``format_`` for a plain format specifier,
        old- or new-style format strings, or using a user supplied function.
        More importantly, determine and cache (in _format_funcs) a function
        that will do this subsequently.  In this way this complicated logic is
        only done for the first value.

        Returns the formatted value.
        Nc             S   s   | |S )Nr   )r   r   r   r   r   r   U   r   zAget_auto_format_func.<locals>._auto_format_func.<locals>.<lambda>z?Format function for value {} returned {} instead of string typezFormat function for value z	 failed: z!the format passed in did nothing.z0unable to parse format string {} for its column.)r   info_format_funcscallabler
   r   
ValueErrorr   type	Exceptionr   r   r   r   )r   r   r   outerr)col possible_string_format_functionsr   r   _auto_format_funcE   sD    	


&


z/get_auto_format_func.<locals>._auto_format_funcr   )r$   r%   r&   r   )r$   r%   r   get_auto_format_func/   s    Cr'   c                s2    fdd}|    j}|  g }|| S )zGet the set of names to show in pprint from the table pprint_include_names
    and pprint_exclude_names attributes.

    These may be fnmatch unix-style globs.
    c                sP   t  }| rBx@ jD ]*}x$| D ]}t||r|| P qW qW n
|| |S )N)setcolnamesfnmatchaddupdate)Z
name_globsdefaultZmatch_namesnameZ	name_glob)tabler   r   get_matches   s    


z._get_pprint_include_names.<locals>.get_matches)pprint_include_namesr)   Zpprint_exclude_names)r/   r0   Zinclude_namesZexclude_namesr   )r/   r   _get_pprint_include_names   s    r2   c            	   @   sB   e Zd ZedddZdddZddd	Zdd
dZdddZdS )TableFormatterNc             C   s   | dkrt j} |dkrt j}| dks,|dkr6t \}}| dkrD|} n| dk rRtj} | dk r^d} |dkrl|}n|dk rztj}|dk rd}| |fS )a  Get the output size (number of lines and character width) for Column and
        Table pformat/pprint methods.

        If no value of ``max_lines`` is supplied then the height of the
        screen terminal is used to set ``max_lines``.  If the terminal
        height cannot be determined then the default will be determined
        using the ``astropy.table.conf.max_lines`` configuration item. If a
        negative value of ``max_lines`` is supplied then there is no line
        limit applied.

        The same applies for max_width except the configuration item is
        ``astropy.table.conf.max_width``.

        Parameters
        ----------
        max_lines : int or None
            Maximum lines of output (header + data rows)

        max_width : int or None
            Maximum width (characters) output

        Returns
        -------
        max_lines, max_width : int

        Nr      
   )r   	max_lines	max_widthr   sysmaxsize)r6   r7   lineswidthr   r   r   _get_pprint_size   s&    
zTableFormatter._get_pprint_sizeTFc	          	   C   s  |dkr|j jdk	}i }	| j|||||||	d}
t|
}t|dkrVtdd |D }|rddlm} |	d }xzt|D ]n\}}||d krqz||k rd	nd
}d| d||	  d| d}d| d }||k rd| d }|||< qzW |dkr|
|d  |dd |d nD|r6tdd |D nd}x$|	d D ]}|| |||< qDW |	d dk	r~d| ||	d < td}d}|r||}|stdnt|j jtr||j j}|r|d}|d}|dkr|dkr tdd}nd}d}d d!d"d#d$}|| }|r2||fn|f}x(t|D ]\}}t||| ||< qBW |	d% r|d&t| d' ||	fS )(a  Return a list of formatted string representation of column values.

        Parameters
        ----------
        max_lines : int
            Maximum lines of output (header + data rows)

        show_name : bool
            Include column name. Default is True.

        show_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include column dtype. Default is False.

        show_length : bool
            Include column length at end.  Default is to show this only
            if the column is not shown completely.

        html : bool
            Output column as HTML

        align : str
            Left/right alignment of columns. Default is '>' (right) for all
            columns. Other allowed values are '<', '^', and '0=' for left,
            centered, and 0-padded, respectively.

        Returns
        -------
        lines : list
            List of lines with formatted column values

        outs : dict
            Dict which is used to pass back additional values
            defined within the iterator.

        N)	show_name	show_unit
show_dtypeshow_lengthoutsr   c             s   s   | ]}t |V  qd S )N)len).0xr   r   r   	<genexpr>  s    z.TableFormatter._pformat_col.<locals>.<genexpr>)
xml_escapen_header   thtd<>z</z<tr>z</tr>z<thead>z</thead>z<table>z</table>c             s   s   | ]}t |V  qd S )N)rB   )rC   rD   r   r   r   rE   '  s    	i_centersi_dashes-z(?P<fill>.?)(?P<align>[<^>=])z1column align must be one of '<', '^', '>', or '='fillalign=0z(fill character must be '0' for '=' align ljustcenterrjustzfill)rK   ^rL   rR   r@   z	Length = z rows)r   unit_pformat_col_iterlistrB   maxastropy.utils.xml.writerrF   	enumeratestrippopinsertappendrV   recompilematchr   r
   r   r   groupgetattr)selfr$   r6   r=   r>   r?   r@   htmlrQ   rA   Zcol_strs_iterZcol_strsZ	col_widthrF   rG   iZcol_strrJ   r   rowZre_fill_alignrf   Z	fill_charZ
align_charZjustify_methodsZjustify_methodZjustify_argsr   r   r   _pformat_col   sr    *
 








zTableFormatter._pformat_colc          
   #   s  |  |d\}}t ddgdd r\tdd D tdd D tdkd}	g }
d}|r|
| t jj}r|d	d
	dd D  d7 }|d7 }|V  |r|
| |d7 }t jj
pdV  |r(|
| |d7 }yt j}W n tk
r   d}Y nX t|V  |s:|s:|rL|}	|d7 }dV  ||8 }|d }t } jjpzt jddt jddpt}t |} jj|t |kr|dkrd}||rdnd }|| |d  }ttd|d t|d t g}nd}tt } fdd}x`|D ]X}||krZdV  n@y||V  W n0 tk
r   td |  jjY nX qDW ||d< ||d< |
|d< |	|d< dS )a?  Iterator which yields formatted string representation of column values.

        Parameters
        ----------
        max_lines : int
            Maximum lines of output (header + data rows)

        show_name : bool
            Include column name. Default is True.

        show_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        outs : dict
            Must be a dict which is used to pass back additional values
            defined within the iterator.

        show_dtype : bool
            Include column dtype. Default is False.

        show_length : bool
            Include column length at end.  Default is to show this only
            if the column is not shown completely.
        shaper   rH   Nc             s   s   | ]
}d V  qdS )r   Nr   )rC   nr   r   r   rE   t  s    z3TableFormatter._pformat_col_iter.<locals>.<genexpr>c             s   s   | ]}|d  V  qdS )rH   Nr   )rC   rp   r   r   r   rE   u  s    z [,c             s   s   | ]}t |V  qd S )N)r   )rC   rp   r   r   r   rE     s    ]rT   objectz---   default_formatr%   Tc                sf   rTr | f  S  | f  } | f  }| d| S n |  S d S )Nz .. r   )idxleftright)r$   
col_formatr   	multidim0	multidim1	multidimstrivial_multidimsr   r   format_col_str  s    z8TableFormatter._pformat_col_iter.<locals>.format_col_strz...z@Unable to parse format string "{}" for entry "{}" in column "{}"r@   rG   rM   rN   )r<   rh   tupler   prodrc   r   r   r.   joinrZ   r   dtypeAttributeErrorrB   r   r   r'   r   getZconcatenateZaranger   )ri   r$   r6   r=   r>   rA   r?   r@   _rN   rM   rG   Zcol_namer   Zn_print2n_rowsZpssfZauto_format_funci0i1indicesr~   rv   r   )r$   ry   r   rz   r{   r|   r}   r   r[   T  s    
 







z TableFormatter._pformat_col_iterc          	      s  |  ||\}}g }|dkr4tdd |j D }t|j}|
dksPt|
tr\|
g| }
nBt|
ttfrt|
|krt	d
t|
|ntd
t|
t|}xbt|
|j D ]N\}}|jj|krq| j||||||d\}}|d r|dd }|| qW |sd	gdd
ifS |d }t|d }dd }dg| }t|d }xh|||krt|dkrpP t|dkr||d< P || |kr|| t|d }|||< qRW g }|rddlm |dkrdt| }|	dk	r"t|	trd|	}	|d| d|	 d n|d| d x~t|D ]r  |d krTq> |k rbdnd fdd|D }dd| d } |k rd| d }|| q>W |d  n6x4t|D ]( d fd!d|D }|| qW ||fS )"a  Return a list of lines for the formatted string representation of
        the table.

        Parameters
        ----------
        max_lines : int or None
            Maximum number of rows to output

        max_width : int or None
            Maximum character width of output

        show_name : bool
            Include a header row for column names. Default is True.

        show_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include a header row for column dtypes. Default is False.

        html : bool
            Format the output as an HTML table. Default is False.

        tableid : str or None
            An ID tag for the table; only used if html is set.  Default is
            "table{id}", where id is the unique integer id of the table object,
            id(table)

        tableclass : str or list of str or None
            CSS classes for the table; only used if html is set.  Default is
            none

        align : str or list or tuple
            Left/right alignment of columns. Default is '>' (right) for all
            columns. Other allowed values are '<', '^', and '0=' for left,
            centered, and 0-padded, respectively. A list of strings can be
            provided for alignment of tables with multiple columns.

        Returns
        -------
        rows : list
            Formatted table as a list of strings

        outs : dict
            Dict which is used to pass back additional values
            defined within the iterator.

        Nc             s   s   | ]}|j jV  qd S )N)r   rZ   )rC   r$   r   r   r   rE   !  s    z0TableFormatter._pformat_table.<locals>.<genexpr>z=got {} alignment values instead of the number of columns ({})z3align keyword must be str or list or tuple (got {}))r=   r>   r?   rQ   r@   rn   z<No columns>FrG   r   c             S   s   t dd | D t|  d S )Nc             s   s   | ]}t |d  V  qdS )r   N)rB   )rC   cr   r   r   rE   I  s    zBTableFormatter._pformat_table.<locals>.outwidth.<locals>.<genexpr>rH   )sumrB   )colsr   r   r   outwidthH  s    z/TableFormatter._pformat_table.<locals>.outwidthz...rt   rH   )rF   r/    z<table id="z	" class="z">rI   rJ   c             3   s2   | ]*}d  d|     d dV  qdS )rK   rL   z</N)r`   )rC   r$   )rk   rJ   rF   r   r   rE   m  s   z<tr>rT   z</tr>z<thead>z</thead>z</table>c             3   s   | ]}|  V  qd S )Nr   )rC   r$   )rk   r   r   rE   v  s    )r<   anycolumnsvaluesrB   r
   r   r\   r   r   r   	TypeErrorr    r2   zipr   r.   rm   rc   ra   r^   rF   idr   range)ri   r/   r6   r7   r=   r>   r?   rj   ZtableidZ
tableclassrQ   r   Zn_colsr1   Zalign_r$   r:   rA   rG   r   r   Zdots_colmiddlerowsvalsrl   r   )rk   rJ   rF   r   _pformat_table  s    7







zTableFormatter._pformat_tablec                s  d}d |r d7  |r  d7  |r, d7  |s8|s8|r@ d7  t d|||d}t|drb||d< | ||\}	}|dkr|	d	7 }	|	  }
t }d}d
}x||
 }|r.yttjdkrdnd W n tk
r   Y nX ||| jf |} fddt	t
|D }x"t||D ]\}}t|| qW d
}t  tddd xJy|  }W n( tk
r|   td td dS X ||krFP qFW t| | dkrP n|dks|dkr||
7 }n|dkr||
 }nr|dkrnf|dkrd}nV|dkrt
|}nB|dkr |d8 }n.|dkr4|d7 }n|dkrNd}tddd |dk r\d}|t
||
 krzt
||
 }td qW dS ) ar  Interactive "more" of a table or column.

        Parameters
        ----------
        max_lines : int or None
            Maximum number of rows to output

        max_width : int or None
            Maximum character width of output

        show_name : bool
            Include a header row for column names. Default is True.

        show_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include a header row for column dtypes. Default is False.
        z
f br<>qhpnr   rH   rn   )r6   r=   r>   r?   r   r7   Nrt   Tntclsclearc             3   s   | ]}| k rd ndV  qdS )redr-   Nr   )rC   rk   )rG   r   r   rE     s   z.TableFormatter._more_tabcol.<locals>.<genexpr>z.-- f, <space>, b, r, p, n, <, >, q h (help) --r   )end
zZConsole does not support getting a character as required by more().  Use pprint() instead.qfbrrK   rL   prp   hFz
    Browsing keys:
       f, <space> : forward one page
       b : back one page
       r : refresh same page
       n : next row
       p : previous row
       < : go to beginning
       > : go to end
       q : quit browsing
       h : print this help)dicthasattrr<   r   ossystemr.   r!   pformatr   rB   r   r   printlowerr   error)ri   Ztabcolr6   r7   r=   r>   r?   Zallowed_keyskwargsZ
max_lines1Zdelta_linesZinkeyr   Z	showlinesr   r:   colorscolorlinekeyr   )rG   r   _more_tabcol{  s    


















zTableFormatter._more_tabcol)NN)NTNFNFN)FN)	NNTNFFNNN)NNTNF)	__name__
__module____qualname__staticmethodr<   rm   r[   r   r   r   r   r   r   r3      s   4 
{
   
  r3   )r   r8   rd   r*   numpyr   Zastropyr   Zastropy.utils.consoler   r   r   r   Zastropy.utils.data_infor   __all__r   r   r   r'   r2   r3   r   r   r   r   <module>   s   	
Z