U
    e»{jx6  ã                   @   sÄ   d dl Z d dlZd dlZd dlmZ d dl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 }|D ]}||kr*| ||| f¡ q*| D ]\}}||krN| ||f¡ qN| 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__© úQ/var/www/html/bwave/python/venv/lib/python3.8/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Ì   t ƒ }|V  t|tjƒs6tj d|jd|j› �|j¡‚|jD ]Š}t|tj	ƒsjtj d|jd|j› �|j¡‚t
|jƒdkrœtj d|jdt
|jƒ› d�|j¡‚|jd \}}|  |¡}|  |¡}|||< q<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')])
    z!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 itemsr   N)r   Ú
isinstanceÚyamlÚSequenceNodeÚconstructorZConstructorErrorZ
start_markÚidÚvalueÚMappingNodeÚlenZconstruct_object)ÚloadÚnodeZomapZsubnodeZkey_nodeZ
value_noder   r(   r   r   r   Ú_construct_odict5   s8    
ü

üü

r-   c           
      C   s�   g }t j|||d�}| jdk	r*|| j| j< d}|D ]6\}}|  ||i¡}	t|	t jƒrZ|	jr^d}| |	¡ q2|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
    ©Ú
flow_styleNTF)r$   r%   Ú	alias_keyÚrepresented_objectsÚrepresent_datar#   Ú
ScalarNodeÚstyler   Údefault_flow_styler/   )
ÚdumpÚtagÚsequencer/   r(   r,   Ú
best_styler   r   Úitemr   r   r   Ú_repr_pairsp   s    	


r;   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'
    útag:yaml.org,2002:omap)r;   r!   ©Z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_mappingr=   r   r   r   Ú_repr_column_dict›   s    rA   c                 C   sÔ   G dd„ dt ƒ}tjtjtjtjf}z†t| ƒdksDtdd„ | D ƒƒsH|‚| d j}| d j	dd… }| D ]6}t
|jj|ƒrŒ|j	dd… |kr�|‚t ||j¡}qh|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   rC   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éÿÿÿÿrD   r   )Ú
ValueErrorrE   ZfloatingÚintegerZbool_Zunicode_r*   ÚallÚdtypeÚshapeÚ
issubclassÚtypeZpromote_typesÚinfo)ÚcolrC   Z
np_classesrL   rM   r   r   r   r   Ú _get_variable_length_array_shape¦   s     
 
rR   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Ú_NrH   )r   Ú
startswithÚendswith)rL   r	   r   r   r   Ú_get_datatype_from_dtypeÛ   s    

rY   c           
      C   sJ  | 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< d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|	||< qÆ|�r"t|ƒ|d< |d dk�r"d|d< |�rF|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 rD   r   ©Úxr   r   r   Ú<lambda>  ó    z%_get_col_attributes.<locals>.<lambda>r
   c                 S   s   | d k	S rD   r   r[   r   r   r   r]     r^   r   c                 S   s   | d k	S rD   r   r[   r   r   r   r]     r^   r   c                 S   s   | S rD   r   r[   r   r   r   r]     r^   ÚsubtypeÚjson)ú,ú:)Ú
separators)rP   rL   rM   r   rE   rT   rR   rZ   rY   r    Úgetattrr`   ÚdumpsÚlist)
rQ   rL   r_   rM   r	   ÚattrsÚattrZ
nontrivialZxformZcol_attrr   r   r   Ú_get_col_attributeså   s>    



üri   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   )rf   ÚcolumnsÚvaluesr   r   )ÚtableÚheaderr   r   r   r   !  s    
c                 C   st   ddl m} G dd„ d|ƒ}| tt¡ | tt¡ t | ¡} dd„ | d D ƒ| d< | d= t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   )ÚAstropyDumperc                   @   s   e Zd ZdZddd„ZdS )z)get_yaml_from_header.<locals>.TableDumperzP
        Custom Dumper that represents OrderedDict as an !!omap object.
        Nc                 S   s   g }t j|||d�}| jdk	r*|| j| j< d}t|dƒr~| ¡ }t|dƒrT| ¡  n*t|ƒ}zt|ƒ}W n t	k
r|   Y nX |D ]V\}}|  
|¡}	|  
|¡}
t|	t jƒr°|	jr´d}t|
t jƒrÆ|
jrÊd}| |	|
f¡ q‚|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)   r0   r1   Úhasattrr!   r   rf   ÚsortedÚ	TypeErrorr2   r#   r3   r4   r   r5   r/   )r   r7   Úmappingr/   r(   r,   r9   Zitem_keyZ
item_valueZnode_keyZ
node_valuer   r   r   r@   T  s<    






ÿÿ

z;get_yaml_from_header.<locals>.TableDumper.represent_mapping)N)r   r   r   r   r@   r   r   r   r   ÚTableDumperO  s   rt   c                 S   s   g | ]}t |ƒ‘qS r   )ri   )rF   rQ   r   r   r   Ú
<listcomp>�  s     z(get_yaml_from_header.<locals>.<listcomp>rj   r	   Né‚   )r5   ZDumperÚwidth)Úastropy.io.misc.yamlro   Zadd_representerr   r?   r    rA   Úcopyr$   r6   Ú
splitlines)rn   ro   rt   Úlinesr   r   r   r   7  s    .
   ÿ
c                   @   s   e Zd ZdS )ÚYamlParseErrorN)r   r   r   r   r   r   r   r|   Š  s   r|   c              
   C   sz   ddl m} G dd„ d|ƒ}| dt¡ t d | ¡¡}ztj||d�}W n* t	k
rt } zt
ƒ |‚W 5 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   )Ú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.
        NrB   r   r   r   r   ÚTableLoader¥  s   r~   r<   Ú
)ÚLoaderN)rx   r}   Zadd_constructorr-   ÚtextwrapÚdedentÚjoinr$   r+   Ú	Exceptionr|   )r{   r}   r~   Zheader_yamlrn   Úerrr   r   r   r   Ž  s    )N)ry   r`   r�   Úcollectionsr   ÚnumpyrE   r$   Ú__all__rf   r   r   r    r-   r;   r?   rA   rR   rY   ri   r   r   r„   r|   r   r   r   r   r   Ú<module>   s&   
;
5
<S