
    IR-e                         d Z ddlZddlZddlZddlmZ g dZej        d             ZddZ	e	Z
	 dd	lmZ ej        Z	n# e$ r Y nw xY wej        dd            Zd Zd ZdS )z8
This module includes a fast iterator-based XML parser.
    N)data)get_xml_iteratorget_xml_encodingxml_readlinesc              #   4  K   t          |           r| V  dS t          j        | d          5 }t          j                            d          r
|j        V  n(t          |t          j	                  r|V  n	|j        V  ddd           dS # 1 swxY w Y   dS )a  
    Returns a function suitable for streaming input, or a file object.

    This function is only useful if passing off to C code where:

       - If it's a real file object, we want to use it as a real
         C file object to avoid the Python overhead.

       - If it's not a real file object, it's much handier to just
         have a Python function to call.

    This is somewhat quirky behavior, of course, which is why it is
    private.  For a more useful version of similar behavior, see
    `astropy.utils.misc.get_readable_fileobj`.

    Parameters
    ----------
    fd : object
        May be:

            - a file object.  If the file is uncompressed, this raw
              file object is returned verbatim.  Otherwise, the read
              method is returned.

            - a function that reads from a stream, in which case it is
              returned verbatim.

            - a file path, in which case it is opened.  Again, like a
              file object, if it's uncompressed, a raw file object is
              returned, otherwise its read method.

            - an object with a :meth:`read` method, in which case that
              method is returned.

    Returns
    -------
    fd : context-dependent
        See above.
    Nbinaryencodingwin)
callabler   get_readable_fileobjsysplatform
startswithread
isinstanceioFileIO)fdnew_fds     <lib/python3.11/site-packages/astropy/utils/xml/iterparser.py_convert_to_fd_or_read_functionr      s      R || 		"2	9	9	9 "V<""5)) 	"+&"),, "k!!!" " " " " " " " " " " " " " " " " "s   ABBB   c              #   x  	
K   ddl m} t          |           s| j        }n| }g 	g 
	
fd}	
fd}|                                d_        |_        |_        
j        _	        j
        } ||          }|r& ||d           	E d {V  	d d =  ||          }|& |dd           	E d {V  d S )Nr   )expatc                 \                         d| |j        j        ff           d d = d S )NT)appendCurrentLineNumberCurrentColumnNumber)nameattrparserqueuetexts     r   startz_fast_iterparse.<locals>.startS   sA    4 8&:TUV	
 	
 	
 GGG    c                                          d| d                                                              j        j        ff           d S )NF )r   joinstripr   r   )r    r"   r#   r$   s    r   endz_fast_iterparse.<locals>.endY   sU    ##%%)6+EF		
 	
 	
 	
 	
r&   TFr(   )xml.parsersr   r   r   ParserCreatespecified_attributesStartElementHandlerEndElementHandlerr   CharacterDataHandlerParse)r   
buffersizer   r   r%   r+   r2   r   r"   r#   r$   s           @@@r   _fast_iterparser4   H   sW     !!!!!!B<< wED      
 
 
 
 
 
 
 !!F"&F!&F"F"&+FLE4
D
  dE!!!HtJ	    
E"dOOOr&      )_iterparserFc              #      K   t          |           5 }|rt          |          }nt          |          }t          |          V  ddd           dS # 1 swxY w Y   dS )af  
    Returns an iterator over the elements of an XML file.

    The iterator doesn't ever build a tree, so it is much more memory
    and time efficient than the alternative in ``cElementTree``.

    Parameters
    ----------
    source : path-like, readable file-like, or callable
        Handle that contains the data or function that reads it.
        If a function or callable object, it must directly read from a stream.
        Non-callable objects must define a ``read`` method.

    Returns
    -------
    parts : iterator

        The iterator returns 4-tuples (*start*, *tag*, *data*, *pos*):

            - *start*: when `True` is a start element event, otherwise
              an end element event.

            - *tag*: The name of the element

            - *data*: Depends on the value of *event*:

                - if *start* == `True`, data is a dictionary of
                  attributes

                - if *start* == `False`, data is a string containing
                  the text content of the element

            - *pos*: Tuple (*line*, *col*) indicating the source of the
              event.
    N)r   _slow_iterparser4   iter)source_debug_python_based_parserr   contexts       r   r   r      s      J 
)	0	0 B% 	*%b))GG%b))G7mm                 s   3AAAc                     t          |           5 }t          |          \  }}}}|r|dk    rt          d          	 ddd           n# 1 swxY w Y    |j        d          pdS )a  
    Determine the encoding of an XML file by reading its header.

    Parameters
    ----------
    source : path-like, readable file-like, or callable
        Handle that contains the data or function that reads it.
        If a function or callable object, it must directly read from a stream.
        Non-callable objects must define a ``read`` method.

    Returns
    -------
    encoding : str
    xmlzInvalid XML fileNr
   zutf-8)r   nextOSErrorget)r:   iteratorr%   tagr   poss         r   r   r      s     
&	!	! .X $XsD# 	.u,--- %. . . . . . . . . . . . . . . 48J*7*s   ,A		AAc                     t          |           }t          j        | |          5 }|                    d           |                                }ddd           n# 1 swxY w Y   |S )a  
    Get the lines from a given XML file.  Correctly determines the
    encoding and always returns unicode.

    Parameters
    ----------
    source : path-like, readable file-like, or callable
        Handle that contains the data or function that reads it.
        If a function or callable object, it must directly read from a stream.
        Non-callable objects must define a ``read`` method.

    Returns
    -------
    lines : list of unicode
    r	   r   N)r   r   r   seek	readlines)r:   r
   input	xml_liness       r   r   r      s       ''H		"6H	=	=	= &

1OO%%	& & & & & & & & & & & & & & & s   *AA #A )r   )F)__doc__
contextlibr   r   astropy.utilsr   __all__contextmanagerr   r4   r8   r(   r6   
IterParserImportErrorr   r   r    r&   r   <module>rR      s   
     				 



      
C
C
C 3" 3" 3"l* * * *^ "	!,OO 	 	 	D	 ) ) ) )X+ + +0    s   > AA