σ
ίΘ[c           @` sϋ   d  Z  d d l m Z m Z m Z m Z d d l m 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    Z d d  Z e Z y d d l m Z e j Z Wn e k
 rΟ n Xe j e d   Z d   Z d   Z d S(   u8   
This module includes a fast iterator-based XML parser.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixNi   (   t   datau   get_xml_iteratoru   get_xml_encodingu   xml_readlinesc         c` s€   t  j |   r |  Vd St j |  d d q } t j j d  rM | j VnM t  j rx t	 | t
  rm | Vq | j Vn" t	 | t j  r | Vn | j VWd QXd S(   uμ  
    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.
    Nt   encodingu   binaryu   win(   R   t   callableR   t   get_readable_fileobjt   syst   platformt
   startswitht   readt   PY2t
   isinstancet   filet   iot   FileIO(   t   fdt   new_fd(    (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   _convert_to_fd_or_read_function   s    )	i
   c   	      #` s+  d d l  m } t j |   s+ |  j } n |  } g   g       f d   }     f d   } | j     t j r t   _ n  t   _	 |   _
 |   _  j   _   j } | |  } x= | r| | t  x  D] } | Vqα W 2| |  } qΗ W| d t  x  D] } | VqWd  S(   Ni    (   t   expatc         ` s-    j  t |  |   j   j f f   2d  S(   N(   t   appendt   Truet   CurrentLineNumbert   CurrentColumnNumber(   t   namet   attr(   t   parsert   queuet   text(    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   start]   s    c         ` s8    j  t |  d j   j     j   j f f  d  S(   Nu    (   R   t   Falset   joint   stripR   R   (   R   (   R   R   R   (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   endb   s    u    (   t   xml.parsersR   R   R   R   t   ParserCreateR   R   t   returns_unicodet   specified_attributest   StartElementHandlert   EndElementHandlerR   t   CharacterDataHandlert   ParseR    (	   R   t
   buffersizeR   R   R   R#   R+   R   t   elem(    (   R   R   R   s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   _fast_iterparseR   s4    							i   (   t   _iterparserc         c` sE   t  |   3 } | r$ t |  } n t |  } t |  VWd QXd S(   u  
    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
    ----------
    fd : readable file-like object or read function

    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   t   _slow_iterparseR.   t   iter(   t   sourcet   _debug_python_based_parserR   t   context(    (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   get_xml_iterator   s
    "c         C` se   t  |   D } t j |  \ } } } } | s= | d k rL t d   n  Wd QX| j d  pd d S(   uΣ   
    Determine the encoding of an XML file by reading its header.

    Parameters
    ----------
    source : readable file-like object, read function or str path

    Returns
    -------
    encoding : str
    u   xmlu   Invalid XML fileNu   encodingu   utf-8(   R5   R   t   nextt   IOErrort   get(   R2   t   iteratorR   t   tagR   t   pos(    (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   get_xml_encoding°   s
    c         C` sG   t  |   } t j |  d |   } | j d  | j   } Wd QX| S(   u  
    Get the lines from a given XML file.  Correctly determines the
    encoding and always returns unicode.

    Parameters
    ----------
    source : readable file-like object, read function or str path

    Returns
    -------
    lines : list of unicode
    R   i    N(   R<   R   R   t   seekt	   readlines(   R2   R   t   inputt	   xml_lines(    (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   xml_readlinesΕ   s
    i   (   t   __doc__t
   __future__R    R   R   R   t   externR   t
   contextlibR   R	   t    R   t   __all__t   contextmanagerR   R.   R0   R/   t
   IterParsert   ImportErrorR    R5   R<   RA   (    (    (    s;   lib/python2.7/site-packages/astropy/utils/xml/iterparser.pyt   <module>   s&   "=,)	