U
    e»{jÂ  ã                   @   sž   d Z ddlZddlZddlZddlmZ dddgZejdd„ ƒZdd
d„Z	e	Z
zddlmZ ejZ	W n ek
rx   Y nX ejddd„ƒZdd„ Zdd„ ZdS )z8
This module includes a fast iterator-based XML parser.
é    N)ÚdataÚget_xml_iteratorÚget_xml_encodingÚxml_readlinesc              	   c   sb   t | ƒr| V  dS tj| dd��8}tj d¡r8|jV  nt|tj	ƒrL|V  n|jV  W 5 Q R X 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)ÚfdZnew_fd© r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/utils/xml/iterparser.pyÚ_convert_to_fd_or_read_function   s    )
r   é   c                 #   s¼   ddl m} t| ƒs| j}n| }g ‰g ‰‡ ‡‡fdd„}‡ ‡‡fdd„}| ¡ ‰ dˆ _|ˆ _|ˆ _ˆjˆ _	ˆ j
}||ƒ}|r¤||dƒ ˆE d H  ˆd d …= ||ƒ}qx|d	dƒ ˆE d H  d S )
Nr   )Úexpatc                    s(   ˆ  d| |ˆ jˆ jff¡ ˆd d …= d S )NT)ÚappendÚCurrentLineNumberÚCurrentColumnNumber)ÚnameÚattr©ÚparserÚqueueÚtextr   r   ÚstartS   s    ÿz_fast_iterparse.<locals>.startc                    s(   ˆ  d| d ˆ¡ ¡ ˆ jˆ jff¡ d S )NFÚ )r   ÚjoinÚstripr   r   )r   r   r   r   ÚendY   s    
üÿz_fast_iterparse.<locals>.endTFr#   )Úxml.parsersr   r
   r   ÚParserCreateÚspecified_attributesÚStartElementHandlerÚEndElementHandlerr   ÚCharacterDataHandlerÚParse)r   Z
buffersizer   r   r"   r&   r-   r   r   r   r   Ú_fast_iterparseH   s,    





r.   é   )Ú_iterparserFc              	   c   s8   t | ƒ�&}|rt|ƒ}nt|ƒ}t|ƒV  W 5 Q R X 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_iterparser.   Úiter)ÚsourceZ_debug_python_based_parserr   Úcontextr   r   r   r   €   s
    %

c              	   C   sF   t | ƒ�*}t|ƒ\}}}}|r&|dkr.tdƒ‚W 5 Q R X | d¡pDd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 filer   zutf-8)r   ÚnextÚOSErrorÚget)r3   Úiteratorr"   Útagr   Úposr   r   r   r   ­   s
    
c              	   C   s8   t | ƒ}tj| |d��}| d¡ | ¡ }W 5 Q R X |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   )r   r   r   ÚseekÚ	readlines)r3   r   ÚinputZ	xml_linesr   r   r   r   Å   s
    
)r   )F)Ú__doc__Ú
contextlibr   r   Zastropy.utilsr   Ú__all__Úcontextmanagerr   r.   r1   r#   r0   Z
IterParserÚImportErrorr   r   r   r   r   r   r   Ú<module>   s$   

6
/
,