
    HR-eC                        d Z ddlmZmZ ddlmZmZ  G d dej                  Z G d de          Z G d d	ej	                  Z
 G d
 dej                  Z G d dej                  Z G d de
          Z G d de          Z G d de          Z G d de
          Z G d de          Z G d de          Z G d de          ZdS )zAn extensible ASCII table reader and writer.

fixedwidth.py:
  Read or write a table with fixed width columns.

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
   )basiccore)DefaultSplitterInconsistentTableErrorc                   *    e Zd ZdZdZdZdZd Zd ZdS )FixedWidthSplittera  
    Split line based on fixed start and end positions for each ``col`` in
    ``self.cols``.

    This class requires that the Header class will have defined ``col.start``
    and ``col.end`` for each column.  The reference to the ``header.cols`` gets
    put in the splitter object by the base Reader.read() function just in time
    for splitting data lines by a ``data`` object.

    Note that the ``start`` and ``end`` positions are defined in the pythonic
    style so line[start:end] is the desired substring for a column.  This splitter
    class does not have a hook for ``process_lines`` since that is generally not
    useful for fixed-width input.

     F|c              #   v    K   |D ]1fd j         D             } j        r fd|D             V  -|V  2d S )Nc                 8    g | ]}|j         |j                 S  )startend).0xlines     ;lib/python3.11/site-packages/astropy/io/ascii/fixedwidth.py
<listcomp>z/FixedWidthSplitter.__call__.<locals>.<listcomp>'   s&    ===aD15)===    c                 :    g | ]}                     |          S r   )process_val)r   r   selfs     r   r   z/FixedWidthSplitter.__call__.<locals>.<listcomp>)   s'    999qt''**999r   )colsr   )r   linesvalsr   s   `  @r   __call__zFixedWidthSplitter.__call__%   su       	 	D====49===D 9999D9999999



	 	r   c                     | j         pd}| j        pd}||z   |z   }| j        r||z   }||z   }nd}d}d t          ||          D             }||                    |          z   |z   S )Nr	   c                 D    g | ]\  }}d |t          |          z
  z  |z   S ) len)r   valwidths      r   r   z+FixedWidthSplitter.join.<locals>.<listcomp>7   s1    SSS:3us3xx'(3.SSSr   )delimiter_pad	delimiterbookendzipjoin)r   r   widthspadr%   padded_delimbookend_leftbookend_rights           r   r(   zFixedWidthSplitter.join-   s     &BN(b	Y,< 	$s?L)OMMLMSST6ARARSSSl//555EEr   N)	__name__
__module____qualname____doc__r$   r&   r%   r   r(   r   r   r   r   r      sR           MGI  F F F F Fr   r   c                       e Zd ZdZdZdS )FixedWidthHeaderSplitterz$Splitter class that splits on ``|``.r
   Nr.   r/   r0   r1   r%   r   r   r   r3   r3   ;   s        ..IIIr   r3   c                   L    e Zd ZdZeZ	 dZ	  ed          Zd Z	d Z
d Zd ZdS )FixedWidthHeaderz*
    Fixed width table header reader.
    Nz`~!#$%^&*-_+=\|":'c                     t          |                     |                    D ]\  }}||k    r nt          d          |S )NzNo header line found in table)	enumerateprocess_linesr   )r   r   indexir   s        r   get_linezFixedWidthHeader.get_lineL   sV     !3!3E!:!:;; 	J 	JGAtEzz  ))HIIIr   c                     t           ddg          }t          j         j                             |                    }t          j         j                             |                    }||t          d           j        j        }|st          d           
                    |d                   \  }}} fdt          dt          |          dz             D              _        n)|݉                     ||          }	t          t          |	           j        j        d	hz
            dk    rt          d
           j                             j        j        d	h          }
t          |	                              |
          st          d|
            
                    |	          \  } _        }d |D              _                             |||                    d          z             }	 
                    |	          \   _        }}                                  t3          |          D ]m\  }}|dk    rb                     |||z             }	 
                    |	          d         }t5           j        |          D ]\  }}|rt9          |||           nt3           j                  D ]\  }}||         |_        ||         |_         dS )a:  
        Initialize the header Column objects from the table ``lines``.

        Based on the previously set Header attributes find or create the column names.
        Sets ``self.cols`` with the list of Columns.

        Parameters
        ----------
        lines : list
            List of table lines

        header_rowsnameNz:Cannot set position_line without also setting header_startz7No data lines found so cannot autogenerate column names    c                 D    g | ]}j                             |          S r   )auto_formatformat)r   r;   r   s     r   r   z-FixedWidthHeader.get_cols.<locals>.<listcomp>|   s*    VVV$*11!44VVVr   r   r   z]Position line should only contain delimiters and one other character, e.g. "--- ------- ---".z,Characters in position line must be part of c                      g | ]}||dz
  nd S Nr   r   r   r   s     r   r   z-FixedWidthHeader.get_cols.<locals>.<listcomp>   s$     T T Ta!-QT T T Tr   )getattrr   _get_line_index
start_liner9   position_line
ValueErrordata
data_linesr   get_fixedwidth_paramsranger!   namesr<   setsplitterr%   set_of_position_line_charactersunionissubset
col_startscol_endsr:   _set_cols_from_namesr8   r'   r   setattrr   r   )r   r   r>   rI   rJ   rM   r   startsendsr   charsetrW   iiattrcolr"   r;   s   `                r   get_colszFixedWidthHeader.get_colsT   s    dMF8<< )$/4;M;Me;T;TUU
, 2 25 9 9
 
 ( P   -J ,M   "&!;!;JqM!J!JD&$VVVVeAs4yyST}>U>UVVVDJJ ( }}UM::s4yyDM$;S#AABBaGG0G   >DD],c2  4yy))'22 0PwPP   372L2LT2R2R/dox T T8 T T T ==
[5F5Fv5N5N(NOOD'+'A'A$'G'G$DJ!!###!+.. 	0 	0HBv~~}}UJO<<11$77: #DIt 4 4 0 0HC 0T3///  	** 	 	FAsq	CI1gCGG	 	r   c                    | j         y| j        rt          | j                   }d | j        D             }t          |          t          |          k    rt	          d          fdt          ||          D             }n^| j         | j                            | j        j                  }dg}g }|D ]^}|rJ|	                    |d         t          |          z              |	                    |d         dz              N|dxx         dz  cc<   _|dd         }d |D             }t          |          t          |          k    s t          |          t          |          k    rt          d	          ne| j         #t          | j                   }|dd         dgz   }nd
 | j        D             }dg|dd         z   }fdt          ||          D             }|||fS )a  
        Split ``line`` on the delimiter and determine column values and
        column start and end positions.  This might include null columns with
        zero length (e.g. for ``header row = "| col1 || col2 | col3 |"`` or
        ``header2_row = "----- ------- -----"``).  The null columns are
        stripped out.  Returns the values between delimiters and the
        corresponding start and end positions.

        Parameters
        ----------
        line : str
            Input line

        Returns
        -------
        vals : list
            List of values.
        starts : list
            List of starting indices.
        ends : list
            List of ending indices.

        Nc                      g | ]}||dz   nd S rE   r   rF   s     r   r   z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>   s$    LLLQ]AEELLLr   z=Fixed width col_starts and col_ends must have the same lengthc                 N    g | ]!\  }}||                                          "S r   stripr   r   r   r   s      r   r   z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>   1    PPP
sDsO))++PPPr   r@   r   c                 :    g | ]}||                                 S r   rd   rF   s     r   r   z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>   s%    111!q1AGGII111r   z Error parsing fixed width headerc                     g | ]}|d z   S )r   r   rF   s     r   r   z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>   s    555!A555r   c                 N    g | ]!\  }}||                                          "S r   rd   rf   s      r   r   z:FixedWidthHeader.get_fixedwidth_params.<locals>.<listcomp>   rg   r   )rV   rW   listr!   rK   r'   splitrR   r%   appendr   )r   r   rZ   r[   r   r"   s    `    r   rN   z&FixedWidthHeader.get_fixedwidth_params   s   8 ?&4=+D$/**FLLdmLLLD6{{c$ii'' S   QPPPc&$>O>OPPPDD_$)>::dm566DSFD $ $ $KKr
SXX 5666MM$r(Q,////2JJJ!OJJJJCRC[F11t111D4yyCKK''3t99D		+A+A,-OPPP ,B *do..abbzTF*55t}555tCRCyPPPPc&$>O>OPPPDVT!!r   c                     d S Nr   )r   r   s     r   writezFixedWidthHeader.write   s	     	r   )r.   r/   r0   r1   r3   splitter_classrJ   rQ   rS   r<   r`   rN   rq   r   r   r   r6   r6   A   s          .N@MC&)c*D&E&E#  _ _ _B>" >" >"@    r   r6   c                   "    e Zd ZdZeZ	 dZd ZdS )FixedWidthDataz!
    Base table data reader.
    Nc                    	
  j         j        g ndg}t           d|          }g                                  }t	          | D ]}                    |           g }|D ]+
fd j        D             }|                    |           ,fdt          t           j                            D             }|rVt          t           j                            D ]4	t          |	         t          	fd|D                                 |	<   5|D ]0}|                     j
                            ||                     1 j         j        < fd|D             }|                     j
                            ||                     D ]0}|                     j
                            ||                     1|S )Nr?   r>   c                 `    g | ]*}t          |j                  xdnt                    +S )Nr	   )rG   infostr)r   r_   col_attrr"   s     r   r   z(FixedWidthData.write.<locals>.<listcomp>  sI        &ch999sBC  r   c                 H    g | ]t          fd D                       S )c              3   B   K   | ]}t          |                   V  d S rp   r    r   r   i_cols     r   	<genexpr>z2FixedWidthData.write.<locals>.<listcomp>.<genexpr>  s/      77TDK  777777r   )max)r   r}   	vals_lists    @r   r   z(FixedWidthData.write.<locals>.<listcomp>  sG     
 
 
 7777Y77777
 
 
r   c              3   B   K   | ]}t          |                   V  d S rp   r    r|   s     r   r~   z'FixedWidthData.write.<locals>.<genexpr>   s/      &N&NDs4;'7'7&N&N&N&N&N&Nr   c                 .    g | ]}j         j        |z  S r   )headerposition_char)r   r#   r   s     r   r   z(FixedWidthData.write.<locals>.<listcomp>(  s#    JJJ%DK-5JJJr   )r   rI   rG   str_valsr'   rn   r   rO   r!   r   rR   r(   rJ   )r   r   default_header_rowsr>   col_str_itersr   	hdrs_listr)   ry   r}   r"   r   s   `       @@@@r   rq   zFixedWidthData.write  s#   $(K$:$BbbdM3FGG 	' 	# 	#DT"""" 	# 	# 	#H    9  D T""""
 
 
 
s49~~..
 
 

  	s49~~..   #5M3&N&N&N&NI&N&N&N#N#N! !u
  	; 	;DLL++D&99::::;$0JJJJ6JJJDLL++D&99::: 	; 	;DLL++D&99::::r   )r.   r/   r0   r1   r   rr   rI   rq   r   r   r   rt   rt      s<          (N@J* * * * *r   rt   c                   >     e Zd ZdZdZdZeZeZ		 	 	 	 	 d fd	Z
 xZS )	
FixedWidtha_  Fixed width table with single header line defining column names and positions.

    Examples::

      # Bar delimiter in header and data

      |  Col1 |   Col2      |  Col3 |
      |  1.2  | hello there |     3 |
      |  2.4  | many words  |     7 |

      # Bar delimiter in header only

      Col1 |   Col2      | Col3
      1.2    hello there    3
      2.4    many words     7

      # No delimiter with column positions specified as input

      Col1       Col2Col3
       1.2hello there   3
       2.4many words    7

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    fixed_widthzFixed widthNr   Tc                 D   |dg}t                                                       || j        j        _        || j        j        _        || j        _        || j        _        || j        _	        || j        _	        | j        j
        t          |          | j        _
        d S d S )Nr?   )super__init__rL   rR   r$   r&   r   rV   rW   r>   rI   r!   )r   rV   rW   r$   r&   r>   	__class__s         r   r   zFixedWidth.__init__R  s     !(K+8	(%,	"!+'"- +	9'#&{#3#3DI    ('r   )NNr   TN)r.   r/   r0   r1   _format_name_descriptionr6   header_classrt   
data_classr   __classcell__r   s   @r   r   r   1  sn         4 !L L#LJ 4 4 4 4 4 4 4 4 4 4r   r   c                       e Zd ZdZdZdS )FixedWidthNoHeaderHeaderz8Header reader for fixed with tables with no header line.Nr.   r/   r0   r1   rI   r   r   r   r   r   g  s        BBJJJr   r   c                       e Zd ZdZdZdS )FixedWidthNoHeaderDataz7Data reader for fixed width tables with no header line.r@   Nr   r   r   r   r   r   m  s        AAJJJr   r   c                   4     e Zd ZdZdZdZeZeZ	d fd	Z
 xZS )	FixedWidthNoHeadera?  Fixed width table which has no header line.

    When reading, column names are either input (``names`` keyword) or
    auto-generated.  Column positions are determined either by input
    (``col_starts`` and ``col_stops`` keywords) or by splitting the first data
    line.  In the latter case a ``delimiter`` is required to split the data
    line.

    Examples::

      # Bar delimiter in header and data

      |  1.2  | hello there |     3 |
      |  2.4  | many words  |     7 |

      # Compact table having no delimiter and column positions specified as input

      1.2hello there3
      2.4many words 7

    This class is just a convenience wrapper around the ``FixedWidth`` reader
    but with ``header_start=None`` and ``data_start=0``.

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    fixed_width_no_headerzFixed width with no headerNr   Tc                 T    t                                          ||||g            d S )Nr$   r&   r>   )r   r   )r   rV   rW   r$   r&   r   s        r   r   zFixedWidthNoHeader.__init__  s=    ' 	 	
 	
 	
 	
 	
r   )NNr   T)r.   r/   r0   r1   r   r   r   r   r   r   r   r   r   s   @r   r   r   s  s]         6 +L/L+L'J
 
 
 
 
 
 
 
 
 
r   r   c                       e Zd ZdZeZdS )FixedWidthTwoLineHeaderzHeader reader for fixed width tables splitting on whitespace.

    For fixed width tables with several header lines, there is typically
    a white-space delimited format line, so splitting on white space is
    needed.
    N)r.   r/   r0   r1   r   rr   r   r   r   r   r     s          %NNNr   r   c                       e Zd ZdZdZdS )FixedWidthTwoLineDataSplitterz5Splitter for fixed width tables splitting on ``' '``.r   Nr4   r   r   r   r   r     s        ??IIIr   r   c                       e Zd ZdZeZdS )FixedWidthTwoLineDataz8Data reader for fixed with tables with two header lines.N)r.   r/   r0   r1   r   rr   r   r   r   r   r     s        BB2NNNr   r   c                   >     e Zd ZdZdZdZeZeZ		 	 	 	 	 d fd	Z
 xZS )	FixedWidthTwoLinea  Fixed width table which has two header lines.

    The first header line defines the column names and the second implicitly
    defines the column positions.

    Examples::

      # Typical case with column extent defined by ---- under column names.

       col1    col2         <== header_start = 0
      -----  ------------   <== position_line = 1, position_char = "-"
        1     bee flies     <== data_start = 2
        2     fish swims

      # Pretty-printed table

      +------+------------+
      | Col1 |   Col2     |
      +------+------------+
      |  1.2 | "hello"    |
      |  2.4 | there world|
      +------+------------+

    See the :ref:`astropy:fixed_width_gallery` for specific usage examples.

    fixed_width_two_linez#Fixed width with second header lineN-Fc                     t          |          dk    rt          d| d          t                                          |||           |t          | j        j                  }|| j        _        || j        _        |dz   | j        _	        d S )Nr   zPosition_char="z" must be a single characterr   )
r!   rK   r   r   r   r>   rJ   r   rL   rI   )r   rJ   r   r$   r&   r>   r   s         r   r   zFixedWidthTwoLine.__init__  s     }""M-MMM   	'k 	 	
 	
 	
   788M$1!$1!,q0	r   )Nr   NFN)r.   r/   r0   r1   r   r   r   r   r   r   r   r   r   s   @r   r   r     sn         6 *L8L&J*L 1 1 1 1 1 1 1 1 1 1r   r   N)r1   r	   r   r   r   r   BaseSplitterr   r3   BasicHeaderr6   	BasicDatart   Basicr   r   r   r   r   r   r   r   r   r   r   <module>r      se            9 9 9 9 9 9 9 9(F (F (F (F (F* (F (F (FV       w w w w wu( w w wt3 3 3 3 3U_ 3 3 3l34 34 34 34 34 34 34 34l    /       ^   (
 (
 (
 (
 (
 (
 (
 (
V% % % % %. % % %    $6   3 3 3 3 3N 3 3 341 41 41 41 41
 41 41 41 41 41r   