B
    q\7*                 @   s   d dl m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mZmZ d d	lmZ d
ZG dd deZdddZdddZdd ZG dd deZdd Zdd ZdS )    )import_moduleN)deepcopy)OrderedDict)	MixinInfo   )Column)TableQTablehas_info_class)QuantityInfo)zastropy.time.core.Timezastropy.time.core.TimeDeltazastropy.units.quantity.Quantityz#astropy.coordinates.angles.Latitudez$astropy.coordinates.angles.Longitudez astropy.coordinates.angles.Anglez&astropy.coordinates.distances.Distancez'astropy.coordinates.earth.EarthLocationz+astropy.coordinates.sky_coordinate.SkyCoordz astropy.table.table.NdarrayMixinz!astropy.table.column.MaskedColumnc               @   s   e Zd ZdZdS )SerializedColumnaF  
    Subclass of dict that is a used in the representation to contain the name
    (and possible other info) for a mixin attribute (either primary data or an
    array-like attribute) that is serialized as a column in the table.

    Normally contains the single key ``name`` with the name of the column in the
    table.
    N)__name__
__module____qualname____doc__ r   r   6lib/python3.7/site-packages/astropy/table/serialize.pyr      s   r   r   c                s   j   j j}r  j|kr.|  dS i }xjddd tfddd dfddd dfd	d
d dffD ]2\}}}	t j |}
||
rh|	r|	|
n|
||< qhW  fdd|D }x|D ]}| }| j jkr|}n|d | }t|t	s|t
|fd|i| td|i|< n t||| t||< x" j jD ]}||krD||= qDW |r|d< qW  jd  jj d< ||< dS )a  Carry out processing needed to serialize ``col`` in an output table
    consisting purely of plain ``Column`` or ``MaskedColumn`` columns.  This
    relies on the object determine if any transformation is required and may
    depend on the ``serialize_method`` and ``serialize_context`` context
    variables.  For instance a ``MaskedColumn`` may be stored directly to
    FITS, but can also be serialized as separate data and mask columns.

    This function builds up a list of plain columns in the ``new_cols`` arg (which
    is passed as a persistent list).  This includes both plain columns from the
    original table and plain columns that represent data from serialized columns
    (e.g. ``jd1`` and ``jd2`` arrays from a ``Time`` column).

    For serialized columns the ``mixin_cols`` dict is updated with required
    attributes and information to subsequently reconstruct the table.

    Table mixin columns are always serialized and get represented by one
    or more data columns.  In earlier versions of the code *only* mixin
    columns were serialized, hence the use within this code of "mixin"
    to imply serialization.  Starting with version 3.1, the non-mixin
    ``MaskedColumn`` can also be serialized.
    Nunitc             S   s   | d k	o| dkS )N r   )xr   r   r   <lambda>N   s    z,_represent_mixin_as_column.<locals>.<lambda>formatc             S   s   | d k	S )Nr   )r   r   r   r   r   O   s    descriptionc             S   s   | d k	S )Nr   )r   r   r   r   r   P   s    metac             S   s   | S )Nr   )r   r   r   r   r   Q   s    c                s>   g | ]6}|krt | d ddd  jdd kr|qS )shaper   Nr   )getattrr   ).0key)col	obj_attrsr   r   
<listcomp>V   s    
z._represent_mixin_as_column.<locals>.<listcomp>.name__info__	__class__)infoZ_represent_as_dictZ_represent_as_dict_attrsr$   appendstrr   Z_represent_as_dict_primary_datar
   r   r   r   _represent_mixin_as_columnpopattrs_from_parentr   r   )r   r"   new_cols
mixin_colsexclude_classesZordered_keysr%   attr
nontrivialZxformcol_attrZ
data_attrs	data_attrdatanew_namer   )r   r   r   r(   %   s>    




r(   c             C   s\   i }g }x&|   D ]}t||jj|||d qW |s8| S t| j}||d< t||dd}|S )z
    Convert any mixin columns to plain Column or MaskedColumn and
    return a new table.  Exclude any mixin columns in ``exclude_classes``,
    which must be a tuple of classes.
    )r-   __serialized_columns__F)r   copy)itercolsr(   r%   r"   r   r   r   )tblr-   r,   r+   r   r   outr   r   r   _represent_mixins_as_columns{   s    
r9   c       
      C   s   |  d}|tkr td|td| \}}t|}t||}x(|	 D ]\}}||j
jkrP|| |< qPW |j
| }	x*|	 D ]\}}|| krt|	j
|| qW |	S )Nr$   z"unsupported class for construct {}z(.+)\.(\w+))r)   __construct_mixin_classes
ValueErrorr   rematchgroupsr   r   itemsr%   r*   Z_construct_from_dictsetattr)
r   r%   Zcls_full_namemod_nameZcls_namemoduleclsr.   valueZmixinr   r   r   (_construct_mixin_from_obj_attrs_and_info   s    

rE   c               @   s.   e Zd ZdZd
ddZedd Zdd Zd	S )
_TableLitea  
    Minimal table-like object for _construct_mixin_from_columns.  This allows
    manipulating the object like a Table but without the actual overhead
    for a full Table.

    More pressing, there is an issue with constructing MaskedColumn, where the
    encoded Column components (data, mask) are turned into a MaskedColumn.
    When this happens in a real table then all other columns are immediately
    Masked and a warning is issued. This is not desirable.
    r   c             C   s>   | j }|| |jj< x&t|D ]\}}||kr| | qW d S )N)colnamesr%   r"   	enumeratemove_to_end)selfr   indexrG   Ziir"   r   r   r   
add_column   s
    z_TableLite.add_columnc             C   s   t |  S )N)listkeys)rJ   r   r   r   rG      s    z_TableLite.colnamesc             C   s   |   S )N)values)rJ   r   r   r   r6      s    z_TableLite.itercolsN)r   )r   r   r   r   rL   propertyrG   r6   r   r   r   r   rF      s   

rF   c                s<  i }xH|  D ]<\}}t|trd|kr6|||d < qt||  |||< qW x| D ]
}||= qXW t fdd|D }x*|  D ]\}} | }|||<  |= qW |di }	t|dkrxTddd fd	d
d fddd fddd ffD ]$\}
}t|j	|
}||r||	|
< qW | |	d< t
||	} j||d d S )Nr"   c             3   s   | ]} j |V  qd S )N)rG   rK   )r   r"   )r8   r   r   	<genexpr>   s    z0_construct_mixin_from_columns.<locals>.<genexpr>r#   r   r   c             S   s   | dkS )N)Nr   r   )r   r   r   r   r      s    z/_construct_mixin_from_columns.<locals>.<lambda>r   c             S   s   | d k	S )Nr   )r   r   r   r   r      s    r   c             S   s   | d k	S )Nr   )r   r   r   r   r      s    r   c             S   s   | S )Nr   )r   r   r   r   r      s    )rK   )r?   
isinstancer   _construct_mixin_from_columnsrO   minr)   lenr   r%   rE   rL   )r3   r   r8   Zdata_attrs_mapr"   validxr1   r   r%   r.   r/   r0   r   )r8   r   rS      s4    





rS   c             C   s   d| j kr| S | j  }|d}t| j}x | D ]\}}t||| q6W tdd | D }|rlt	nt
}|t| |jd|dS )Nr4   c             s   s   | ]}t |jtV  qd S )N)rR   r%   r   )r   r   r   r   r   rQ     s   z1_construct_mixins_from_columns.<locals>.<genexpr>F)namesr5   r   )r   r5   r)   rF   columnsr?   rS   anyr6   r	   r   rM   rO   rG   )r7   r   r,   r8   r3   r   Zhas_quantitiesZout_clsr   r   r   _construct_mixins_from_columns   s    



r[   )r   )r   )	importlibr   r<   r5   r   collectionsr   Zastropy.utils.data_infor   columnr   tabler   r	   r
   Zastropy.units.quantityr   r:   dictr   r(   r9   rE   rF   rS   r[   r   r   r   r   <module>   s   
U
*