
    IR-ey6                         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g dZ G d de	          Z
 G d de          Zd Zdd	Zd
 Zd Zd Zd Zd Zd Zd Z G d de          Zd ZdS )    N)OrderedDict)get_header_from_yamlget_yaml_from_headerget_yaml_from_tablec                   "     e Zd ZdZ fdZ xZS )ColumnOrderListzq
    List of tuples that sorts in a specific order that makes sense for
    astropy table column attributes.
    c                 8   t                                                       g d}t          |           }g }|D ]#}||v r|                    |||         f           $| D ] \  }}||vr|                    ||f           !| d d = |                     |           d S )N)nameunitdatatypeformatdescriptionmeta)supersortdictappendextend)	selfargskwargscolumn_keysin_dictout_listkeyval	__class__s	           2lib/python3.11/site-packages/astropy/table/meta.pyr   zColumnOrderList.sort   s    SSSt** 	5 	5Cg~~gcl 3444 	, 	,HC+%%c
+++ GH    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r      sB         
        r   r   c                   "     e Zd ZdZ fdZ x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                 ^    t          t                                                                S )zt
        Return items as a ColumnOrderList, which sorts in the preferred
        way for column attributes.
        )r   r   items)r   r   s    r   r)   zColumnDict.items-   s    
 uww}}///r   )r    r!   r"   r#   r)   r$   r%   s   @r   r'   r'   &   sB         0 0 0 0 0 0 0 0 0r   r'   c           	   #     K   t                      }|V  t          |t          j                  s4t          j                            d|j        d|j         |j                  |j        D ]}t          |t          j	                  s4t          j                            d|j        d|j         |j                  t          |j                  dk    rBt          j                            d|j        dt          |j                   d|j                  |j        d         \  }}|                     |          }|                     |          }|||<   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constructorConstructorError
start_markidvalueMappingNodelenconstruct_object)loadnodeomapsubnodekey_node
value_noder   r3   s           r   _construct_odictr=   5   sh     4 ==D
JJJdD-.. 
///O7dg77O	
 
 	
 :  '4#344 	"333IWZII"	   w}"""333WS=O=OWWW"	    '}Q/*##H--%%j11S		) r   c                 P   g }t          j        |||          }| j        || j        | j        <   d}|D ]T\  }}|                     ||i          }	t          |	t           j                  r|	j        rd}|                    |	           U|| j	        | 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@   r3   r8   
best_styler   r   items
             r   _repr_pairsrL   p   s     ES%J???D~!37 0J  S""C:..411 	$* 	JT"."5DOO(DOKr   c                 H    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)rL   r)   dumperdatas     r   _repr_odictrR      s     v7FFFr   c                 .    |                      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_mappingrO   s     r   _repr_column_dictrU      s     ##$;TBBBr   c                     G d dt                     }t          j        t          j        t          j        t          j        f}	 t          |           dk    st          d | D                       s|| d         j        }| d         j	        dd         }| D ]K}t          |j        j        |          r|j	        dd         |k    r|t          j        ||j                  }L|dz   }n# |$ r | j        j        }d}Y nw xY w||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                       e Zd ZdZdS )6_get_variable_length_array_shape.<locals>.ConvertErrorz"Local conversion error used below.Nr    r!   r"   r#    r   r   ConvertErrorrX      s        0000r   r[   r   c              3   J   K   | ]}t          |t          j                  V  d S N)r,   npndarray).0r   s     r   	<genexpr>z3_get_variable_length_array_shape.<locals>.<genexpr>   s.      #O#OCJsBJ$?$?#O#O#O#O#O#Or   Nr]   rZ   )
ValueErrorr^   floatingintegerbool_unicode_r5   alldtypeshape
issubclasstypepromote_typesinfo)colr[   
np_classesri   rj   r   s         r    _get_variable_length_array_shaperq      s3   41 1 1 1 1z 1 1 1 +rz28R[AJs88q==#O#O3#O#O#O O O=AASbS! 	7 	7Ccinj99 #SYss^u=T=T""$UCI66EE     %<s   B#C' 'C=<C=c                     | j         }|                    d          rd}|                    d          r
|dd         }|S )zDReturn string version of ``dtype`` for writing to ECSV ``datatype``.)bytesstrstring_Nrb   )r
   
startswithendswith)ri   r   s     r   _get_datatype_from_dtypery      sM    zH+,,  !CRC=Or   c                 (   | j         j        }d}| j        dd         }|j        dk    re|dk    r,t	          j        t
                    }t          |           \  }}nZt	          j        t
                    }t	          j        t                    }n'|r%t	          j        t
                    }| j         j        }t          |          }t                      }| j         j        |d<   ||d<   dd t
          fd	d
 dfdd dfdd dffD ]8\  }}}t          | j         |          }	 ||	          r|r ||	          n|	||<   9|r#t          |          |d<   |d         dk    rd|d<   |r1|dxx         t          j        t          |          d          z  cc<   |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+   objectrZ   r
   r   r   c                 
    | d uS r]   rZ   xs    r   <lambda>z%_get_col_attributes.<locals>.<lambda>  s
    1D= r   r   c                 
    | d uS r]   rZ   r}   s    r   r   z%_get_col_attributes.<locals>.<lambda>  s
    Qd] r   r   c                 
    | d uS r]   rZ   r}   s    r   r   z%_get_col_attributes.<locals>.<lambda>  s
    !4- r   r   c                     | S r]   rZ   r}   s    r   r   z%_get_col_attributes.<locals>.<lambda>  s    1 r   subtypejson),:)
separators)rn   ri   rj   r
   r^   rt   rq   r{   ry   r'   getattrr   dumpslist)
ro   ri   r   rj   r   attrsattr
nontrivialxformcol_attrs
             r   _get_col_attributesr      s    HNEGIabbMEzXB;;HSMME=cBBNE77 HSMMEhv&&GG	 !(.'..H LLEHME&M E*	((#.	**D1	//6	d#	$ A Aj% 38T**:h 	A-2@%%///E$K &3G<<ix''%E) KiDJtE{{zJJJJLr   c                     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headers     r   r   r   !  sG     d5=//11223Fz $v'''r   c                 Z   ddl m}  G d d|          }|                    t          t                     |                    t
          t                     t          j        |           } d | d         D             | d<   | d= t          j	        | 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                       e Zd ZdZddZdS ))get_yaml_from_header.<locals>.TableDumperzP
        Custom Dumper that represents OrderedDict as an !!omap object.
        Nc                    g }t          j        |||          }| j        || j        | j        <   d}t	          |d          ri|                                }t	          |d          r|                                 n0t          |          }	 t          |          }n# t          $ r Y nw xY w|D ]\  }}| 
                    |          }	| 
                    |          }
t          |	t           j                  r|	j        rd}t          |
t           j                  r|
j        rd}|                    |	|
f           || j        | 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-   r4   rA   rB   hasattrr)   r   r   sorted	TypeErrorrC   r,   rD   rE   r   rF   r@   )r   rH   mappingr@   r3   r8   rJ   item_key
item_valuenode_key
node_values              r   rT   z;get_yaml_from_header.<locals>.TableDumper.represent_mappingS  s~    E#C:FFFD~);?(8Jw(( 	!--//7F++ LLNNNN"7mmG"(//$    )0 	5 	5$*..x88!00<<
"8T_== 'hn '!&Jz4?;;'DNDT' "'Jh
34444!*6&*&=DOO&0DOKs   B 
B('B(r]   )r    r!   r"   r#   rT   rZ   r   r   TableDumperr   N  s2        	 	'	 '	 '	 '	 '	 '	r   r   c                 ,    g | ]}t          |          S rZ   )r   )r`   ro   s     r   
<listcomp>z(get_yaml_from_header.<locals>.<listcomp>  s!    MMMs-c22MMMr   r   r   N   )rF   Dumperwidth)astropy.io.misc.yamlr   add_representerr   rR   r'   rU   copyr-   rG   
splitlines)r   r   r   liness       r   r   r   6  s    , 322222, , , , ,m , , ,\ [999
,=>>>YvFMMfVnMMMF:vI43  jll 
 Lr   c                       e Zd ZdS )YamlParseErrorN)r    r!   r"   rZ   r   r   r   r     s        Dr   r   c                 $   ddl m}  G d d|          }|                    dt                     t	          j        d                    |                     }	 t          j        ||          }n!# t          $ r}t                      |d}~ww xY w|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                       e Zd ZdZdS ))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.
        NrY   rZ   r   r   TableLoaderr     s        	 	 	 	r   r   rN   
)LoaderN)r   r   add_constructorr=   textwrapdedentjoinr-   r7   	Exceptionr   )r   r   r   header_yamlr   errs         r   r   r     s    * 322222    m     8:JKKK/$))E"2"233K(;{;;; ( ( (C'( Ms   A/ /
B9BBr]   )r   r   r   collectionsr   numpyr^   r-   __all__r   r   r   r'   r=   rL   rR   rU   rq   ry   r   r   r   r   r   r   rZ   r   r   <module>r      s      # # # # # #     
Q
Q
Q    d   40 0 0 0 0 0 0 08 8 8v   6G G G C C C2 2 2j  9 9 9x( ( (*P P Pf	 	 	 	 	Y 	 	 	& & & & &r   