B
    d7                 @   s   d dl Z d dlZd dlZd dlmZ d dlZdddgZG dd deZ	G dd	 d	e
Zd
d ZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd deZdd ZdS )    N)OrderedDictget_header_from_yamlget_yaml_from_headerget_yaml_from_tablec                   s    e Zd ZdZ fddZ  ZS )ColumnOrderListzq
    List of tuples that sorts in a specific order that makes sense for
    astropy table column attributes.
    c                s   t    ddddddg}t| }g }x&|D ]}||kr,|||| f q,W x&| D ]\}}||krT|||f qTW | d d = | | d S )Nnameunitdatatypeformatdescriptionmeta)supersortdictappendextend)selfargskwargsZcolumn_keysZin_dictZout_listkeyval)	__class__ _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/table/meta.pyr      s    


zColumnOrderList.sort)__name__
__module____qualname____doc__r   __classcell__r   r   )r   r   r      s   r   c                   s    e Zd ZdZ fddZ  ZS )
ColumnDictz
    Specialized dict subclass to represent attributes of a Column
    and return items() in a preferred order.  This is only for use
    in generating a YAML map representation that has a fixed order.
    c                s   t t  S )zt
        Return items as a ColumnOrderList, which sorts in the preferred
        way for column attributes.
        )r   r   items)r   )r   r   r   r    ,   s    zColumnDict.items)r   r   r   r   r    r   r   r   )r   r   r   %   s   r   c       	      c   s   ddl }t }|V  t||js>|jd|jd|j |jx|jD ]}t||j	st|jd|jd|j |jt
|jdkr|jd|jdt
|j d|j|jd \}}| |}| |}|||< qFW dS )	a  
    Construct OrderedDict from !!omap in yaml safe load.

    Source: https://gist.github.com/weaver/317164
    License: Unspecified

    This is the same as SafeConstructor.construct_yaml_omap(),
    except the data type is changed to OrderedDict() and setitem is
    used instead of append in the loop

    Examples
    --------
    ::

      >>> yaml.load('''  # doctest: +SKIP
      ... !!omap
      ... - foo: bar
      ... - mumble: quux
      ... - baz: gorp
      ... ''')
      OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])

      >>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''')  # doctest: +SKIP
      OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
    r   Nz!while constructing an ordered mapzexpected a sequence, but found z*expected a mapping of length 1, but found    z*expected a single mapping item, but found z items)yamlr   
isinstanceSequenceNodeconstructorZConstructorErrorZ
start_markidvalueMappingNodelenZconstruct_object)	loadnoder"   ZomapZsubnodeZkey_nodeZ
value_noder   r'   r   r   r   _construct_odict4   s,    


r,   c             C   s   ddl }g }|j|||d}| jdk	r2|| j| j< d}x>|D ]6\}}	| ||	i}
t|
|jrd|
jrhd}||
 q<W |dkr| j	dk	r| j	|_
n||_
|S )z
    This is the same code as BaseRepresenter.represent_sequence(),
    but the value passed to dump.represent_data() in the loop is a
    dictionary instead of a tuple.

    Source: https://gist.github.com/weaver/317164
    License: Unspecified
    r   N)
flow_styleTF)r"   r$   	alias_keyrepresented_objectsrepresent_datar#   
ScalarNodestyler   default_flow_styler-   )dumptagsequencer-   r"   r'   r+   
best_styler   r   itemr   r   r   _repr_pairsj   s     	


r9   c             C   s   t | d| S )a  
    Represent OrderedDict in yaml dump.

    Source: https://gist.github.com/weaver/317164
    License: Unspecified

    >>> data = OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')])
    >>> yaml.dump(data, default_flow_style=False)  # doctest: +SKIP
    '!!omap\n- foo: bar\n- mumble: quux\n- baz: gorp\n'
    >>> yaml.dump(data, default_flow_style=True)  # doctest: +SKIP
    '!!omap [foo: bar, mumble: quux, baz: gorp]\n'
    ztag:yaml.org,2002:omap)r9   r    )dumperdatar   r   r   _repr_odict   s    r<   c             C   s   |  d|S )z
    Represent ColumnDict in yaml dump.

    This is the same as an ordinary mapping except that the keys
    are written in a fixed order that makes sense for astropy table
    columns.
    ztag:yaml.org,2002:map)represent_mapping)r:   r;   r   r   r   _repr_column_dict   s    r>   c             C   s   G dd dt }tjtjtjtjf}yt| dksDtdd | D sH|| d j}| d j	dd }x>| D ]6}t
|jj|r|j	dd |kr|t||j}qjW |d }W n  |k
r   | jj}d	}Y nX ||fS )
aL  Check if object-type ``col`` is really a variable length list.

    That is true if the object consists purely of list of nested lists, where
    the shape of every item can be represented as (m, n, ..., *) where the (m,
    n, ...) are constant and only the lists in the last axis have variable
    shape. If so the returned value of shape will be a tuple in the form (m, n,
    ..., None).

    If ``col`` is a variable length array then the return ``dtype`` corresponds
    to the type found by numpy for all the individual values. Otherwise it will
    be ``np.dtype(object)``.

    Parameters
    ==========
    col : column-like
        Input table column, assumed to be object-type

    Returns
    =======
    shape : tuple
        Inferred variable length shape or None
    dtype : np.dtype
        Numpy dtype that applies to col
    c               @   s   e Zd ZdZdS )z6_get_variable_length_array_shape.<locals>.ConvertErrorz!Local conversion error used belowN)r   r   r   r   r   r   r   r   ConvertError   s   r?   r   c             s   s   | ]}t |tjV  qd S )N)r#   npZndarray).0r   r   r   r   	<genexpr>   s    z3_get_variable_length_array_shape.<locals>.<genexpr>N)Nr   )
ValueErrorr@   ZfloatingintegerZbool_Zunicode_r)   alldtypeshape
issubclasstypeZpromote_typesinfo)colr?   Z
np_classesrG   rH   r   r   r   r    _get_variable_length_array_shape   s     

 
rM   c             C   s.   | j }|drd}|dr*|dd }|S )zCReturn string version of ``dtype`` for writing to ECSV ``datatype``)bytesstrstring_NrC   )r   
startswithendswith)rG   r	   r   r   r   _get_datatype_from_dtype   s    

rT   c       
      C   sN  | j j}d}| jdd }|jdkrZ|dkrDtt}t| \}}qptt}tt}n|rptt}| j j}t|}t	 }| j j|d< ||d< 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 |}	||	r|r||	n|	||< qW |r&t||d< |d dkr&d|d< |rJ|d  tjt|dd7  < |S )a  
    Extract information from a column (apart from the values) that is required
    to fully serialize the column.

    Parameters
    ----------
    col : column-like
        Input Table column

    Returns
    -------
    attrs : dict
        Dict of ECSV attributes for ``col``
    Nr!   objectr   r   r	   r   c             S   s   | d k	S )Nr   )xr   r   r   <lambda>      z%_get_col_attributes.<locals>.<lambda>r
   c             S   s   | d k	S )Nr   )rV   r   r   r   rW     rX   r   c             S   s   | d k	S )Nr   )rV   r   r   r   rW   	  rX   r   c             S   s   | S )Nr   )rV   r   r   r   rW   
  rX   subtypejson),:)
separators)rK   rG   rH   r   r@   rO   rM   rU   rT   r   getattrrZ   dumpslist)
rL   rG   rY   rH   r	   attrsattrZ
nontrivialZxformZcol_attrr   r   r   _get_col_attributes   s<    



rc   c             C   s*   dt | j i}| jr"| j|d< t|S )a/  
    Return lines with a YAML representation of header content from the ``table``.

    Parameters
    ----------
    table : `~astropy.table.Table` object
        Table for which header content is output

    Returns
    -------
    lines : list
        List of text lines with YAML header content
    colsr   )r`   columnsvaluesr   r   )tableheaderr   r   r   r     s    
c                s   yddl  W n tk
r(   tdY nX ddlm} G  fddd|}|tt |tt t		| } dd | d	 D | d
< | d	=  j
| d|dd }|S )a  
    Return lines with a YAML representation of header content from a Table.

    The ``header`` dict must contain these keys:

    - 'cols' : list of table column objects (required)
    - 'meta' : table 'meta' attribute (optional)

    Other keys included in ``header`` will be serialized in the output YAML
    representation.

    Parameters
    ----------
    header : dict
        Table header content

    Returns
    -------
    lines : list
        List of text lines with YAML header content
    r   NzN`import yaml` failed, PyYAML package is required for serializing mixin columns)AstropyDumperc                   s   e Zd ZdZd fdd	ZdS )z)get_yaml_from_header.<locals>.TableDumperzP
        Custom Dumper that represents OrderedDict as an !!omap object.
        Nc                s  g } j |||d}| jdk	r*|| j| j< d}t|dr~| }t|drT|  n*t|}yt|}W n tk
r|   Y nX x^|D ]V\}}| 	|}	| 	|}
t
|	 jr|	jrd}t
|
 jr|
jrd}||	|
f qW |dkr| jdk	r| j|_n||_|S )a|  
            This is a combination of the Python 2 and 3 versions of this method
            in the PyYAML library to allow the required key ordering via the
            ColumnOrderList object.  The Python 3 version insists on turning the
            items() mapping into a list object and sorting, which results in
            alphabetical order for the column keys.
            )r-   NTr    r   F)r(   r.   r/   hasattrr    r   r`   sorted	TypeErrorr0   r#   r1   r2   r   r3   r-   )r   r5   mappingr-   r'   r+   r7   Zitem_keyZ
item_valueZnode_keyZ
node_value)r"   r   r   r=   S  s6    








z;get_yaml_from_header.<locals>.TableDumper.represent_mapping)N)r   r   r   r   r=   r   )r"   r   r   TableDumperN  s   rn   c             S   s   g | ]}t |qS r   )rc   )rA   rL   r   r   r   
<listcomp>~  s    z(get_yaml_from_header.<locals>.<listcomp>rd   r	      )r3   ZDumperwidth)r"   ImportErrorastropy.io.misc.yamlri   Zadd_representerr   r<   r   r>   copyr4   
splitlines)rh   ri   rn   linesr   )r"   r   r   0  s    ,
c               @   s   e Zd ZdS )YamlParseErrorN)r   r   r   r   r   r   r   rw     s   rw   c          
   C   s   yddl }W n tk
r(   tdY nX ddlm} G dd d|}|dt td| }y|j	||d	}W n. t
k
r } ztt|W dd}~X Y nX |S )
aS  
    Get a header dict from input ``lines`` which should be valid YAML.  This
    input will typically be created by get_yaml_from_header.  The output is a
    dictionary which describes all the table and column meta.

    The get_cols() method in the io/ascii/ecsv.py file should be used as a
    guide to using the information when constructing a table using this
    header dict information.

    Parameters
    ----------
    lines : list
        List of text lines with YAML header content

    Returns
    -------
    header : dict
        Dictionary describing table and column meta

    r   NzN`import yaml` failed, PyYAML package is required for serializing mixin columns)AstropyLoaderc               @   s   e Zd ZdZdS )z)get_header_from_yaml.<locals>.TableLoaderz
        Custom Loader that constructs OrderedDict from an !!omap object.
        This does nothing but provide a namespace for adding the
        custom odict constructor.
        N)r   r   r   r   r   r   r   r   TableLoader  s   ry   ztag:yaml.org,2002:omap
)Loader)r"   rr   rs   rx   Zadd_constructorr,   textwrapdedentjoinr*   	Exceptionrw   rO   )rv   r"   rx   ry   Zheader_yamlrh   errr   r   r   r     s    )N)rZ   r|   rt   collectionsr   numpyr@   __all__r`   r   r   r   r,   r9   r<   r>   rM   rT   rc   r   r   r   rw   r   r   r   r   r   <module>   s$   
6
4
:V