ó
šßÈ[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	 y d d l
 Z
 e Z Wn e k
 r e Z n Xy d d l m Z Wn# e k
 r¹ d „  Z d	 „  Z n Xe j Z e j Z d
 d d „  ƒ  YZ d S(   uZ   
Contains a class that makes it simple to stream out well-formed and
nicely-indented XML.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsi   (   t   sixNi   (   t   _iterparserc         C` s:   |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  S(   u<   
        Escapes &, < and > in an XML CDATA string.
        u   &u   &amp;u   <u   &lt;u   >u   &gt;(   t   replace(   t   s(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   xml_escape_cdata   s    c         C` s^   |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d	 d
 ƒ }  |  S(   uE   
        Escapes &, ', ", < and > in an XML attribute value.
        u   &u   &amp;u   'u   &apos;u   "u   &quot;u   <u   &lt;u   >u   &gt;(   R   (   R   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt
   xml_escape    s    t	   XMLWriterc           B` sÈ   e  Z d  Z d „  Z e e d „ Z i  d „ Z e j	 d d „ ƒ Z
 e j	 i  d „ ƒ Z d „  Z d „  Z d e e d	 „ Z d
 „  Z d e i  d „ Z d „  Z d „  Z d d „ Z e d „  ƒ Z RS(   uN  
    A class to write well-formed and nicely indented XML.

    Use like this::

        w = XMLWriter(fh)
        with w.tag('html'):
            with w.tag('body'):
                w.data('This is the content')

    Which produces::

        <html>
         <body>
          This is the content
         </body>
        </html>
    c         C` sh   | j  |  _  t | d ƒ r* | j |  _ n  d |  _ g  |  _ g  |  _ d d |  _ t |  _ t |  _ d S(   uY   
        Parameters
        ----------
        file : writable file-like object.
        u   flushi    u    i@   N(	   t   writet   hasattrt   flusht   _opent   _tagst   _datat   _indentationR   R	   (   t   selft   file(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   __init__C   s    				c         C` sï   |  j  r8 | r |  j d ƒ n |  j d ƒ d |  _  n  |  j rë d j |  j ƒ } | rÉ |  j d ƒ } t j | d | d | ƒ} |  j d ƒ |  j |  j | ƒ ƒ |  j d ƒ |  j |  j ƒ  ƒ n |  j |  j | ƒ ƒ g  |  _ n  d	 S(
   u)   
        Flush internal buffers.
        u   >
u   >i    u    i   t   initial_indentt   subsequent_indentu   
N(   R   R   R   t   joint   get_indentation_spacest   textwrapt   fillR   (   R   t   indentt   wrapt   data(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   _flushT   s&    			c         K` sú   |  j  ƒ  g  |  _ |  j j | ƒ |  j |  j d ƒ ƒ |  j d j | ƒ ƒ | s[ | rä | j ƒ  } | j | ƒ t	 t
 j | ƒ ƒ } | j ƒ  xN | D]C \ } } | d k	 rš |  j | ƒ } |  j d j | | ƒ ƒ qš qš Wn  d |  _ t |  j ƒ S(   um  
        Opens a new element.  Attributes can be given as keyword
        arguments, or as a string/string dictionary.  The method
        returns an opaque identifier that can be passed to the
        :meth:`close` method, to close all open elements up to and
        including this one.

        Parameters
        ----------
        tag : str
            The element name

        attrib : dict of str -> str
            Attribute dictionary.  Alternatively, attributes can
            be given as keyword arguments.

        Returns
        -------
        id : int
            Returns an element identifier.
        iÿÿÿÿu   <{}u    {}="{}"i   N(   R   R   R   t   appendR   R   t   formatt   copyt   updatet   listR   t	   iteritemst   sortt   NoneR	   R   t   len(   R   t   tagt   attribt   extrat   kt   v(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   startn   s     
	
#	u
   escape_xmlc         +` sœ   |  j  } | d k rT t rE ˆ  d k r0 i  ‰  n  ‡  f d †  |  _  qŠ t d ƒ ‚ n6 | d k ro d „  |  _  n | d k rŠ t d ƒ ‚ n  d V| |  _  d S(	   uY  Context manager to control how XML data tags are cleaned (escaped) to
        remove potentially unsafe characters or constructs.

        The default (``method='escape_xml'``) applies brute-force escaping of
        certain key XML characters like ``<``, ``>``, and ``&`` to ensure that
        the output is not valid XML.

        In order to explicitly allow certain XML tags (e.g. link reference or
        emphasis tags), use ``method='bleach_clean'``.  This sanitizes the data
        string using the ``clean`` function of the
        `http://bleach.readthedocs.io/en/latest/clean.html <bleach>`_ package.
        Any additional keyword arguments will be passed directly to the
        ``clean`` function.

        Finally, use ``method='none'`` to disable any sanitization. This should
        be used sparingly.

        Example::

          w = writer.XMLWriter(ListWriter(lines))
          with w.xml_cleaning_method('bleach_clean'):
              w.start('td')
              w.data('<a href="http://google.com">google.com</a>')
              w.end()

        Parameters
        ----------
        method : str
            Cleaning method.  Allowed values are "escape_xml",
            "bleach_clean", and "none".

        **clean_kwargs : keyword args
            Additional keyword args that are passed to the
            bleach.clean() function.
        u   bleach_cleanc         ` s   t  j |  ˆ   S(   N(   t   bleacht   clean(   t   x(   t   clean_kwargs(    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   <lambda>Å   s    uT   bleach package is required when HTML escaping is disabled.
Use "pip install bleach".u   nonec         S` s   |  S(   N(    (   R0   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR2   Ê   s    u
   escape_xmluE   allowed values of method are "escape_xml", "bleach_clean", and "none"N(   R   t
   HAS_BLEACHR&   t
   ValueError(   R   t   methodR1   t   current_xml_escape_cdata(    (   R1   s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   xml_cleaning_methodš   s    %		c         k` s)   |  j  | | |  d V|  j | ƒ d S(   uU  
        A convenience method for creating wrapper elements using the
        ``with`` statement.

        Examples
        --------

        >>> with writer.tag('foo'):  # doctest: +SKIP
        ...     writer.element('bar')
        ... # </foo> is implicitly closed here
        ...

        Parameters are the same as to `start`.
        N(   R-   t   end(   R   R(   R)   R*   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR(   Ò   s    c         C` s@   |  j  ƒ  |  j |  j ƒ  ƒ |  j d j |  j | ƒ ƒ ƒ d S(   u¢   
        Adds a comment to the output stream.

        Parameters
        ----------
        comment : str
            Comment text, as a Unicode string.
        u   <!-- {} -->
N(   R   R   R   R    R   (   R   t   comment(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR9   æ   s    	
c         C` s   |  j  j | ƒ d S(   u¦   
        Adds character data to the output stream.

        Parameters
        ----------
        text : str
            Character data, as a Unicode string.
        N(   R   R   (   R   t   text(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR   ó   s    	c         C` sû   | r_ |  j  s' t d j | ƒ ƒ ‚ n  | |  j  d k rw t d j |  j  d | ƒ ƒ ‚ qw n |  j  sw t d ƒ ‚ n  |  j  j ƒ  } |  j r¢ |  j | | ƒ n# |  j rÅ d |  _ |  j d ƒ d S| rá |  j |  j ƒ  ƒ n  |  j d j | ƒ ƒ d S(	   u  
        Closes the current element (opened by the most recent call to
        `start`).

        Parameters
        ----------
        tag : str
            Element name.  If given, the tag must match the start tag.
            If omitted, the current element is closed.
        u   unbalanced end({})iÿÿÿÿu   expected end({}), got {}u   unbalanced end()i    u   />
Nu   </{}>
(	   R   R4   R    t   popR   R   R   R   R   (   R   R(   R   R   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR8   þ   s$    						c         C` s*   x# t  |  j ƒ | k r% |  j ƒ  q Wd S(   uð   
        Closes open elements, up to (and including) the element identified
        by the given identifier.

        Parameters
        ----------
        id : int
            Element identifier, as returned by the `start` method.
        N(   R'   R   R8   (   R   t   id(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   close  s    
c         K` sC   |  j  | | |  | r) |  j | ƒ n  |  j d t d | ƒ d S(   u¤   
        Adds an entire element.  This is the same as calling `start`,
        `data`, and `end` in sequence. The ``text`` argument
        can be omitted.
        R   R   N(   R-   R   R8   t   False(   R   R(   R:   R   R)   R*   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   element*  s    c         C` s   d  S(   N(    (   R   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR   5  s    c         C` s   t  |  j ƒ S(   u\   
        Returns the number of indentation levels the file is currently
        in.
        (   R'   R   (   R   (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   get_indentation8  s    i    c         C` s   |  j  t |  j ƒ |  S(   u`   
        Returns a string of spaces that matches the current
        indentation level.
        (   R   R'   R   (   R   t   offset(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR   ?  s    c         C` s[   i  } xN | D]F } t  |  | ƒ d k	 r t j t  |  | ƒ ƒ | | j d d ƒ <q q W| S(   u  
        Converts an object with a bunch of attributes on an object
        into a dictionary for use by the `XMLWriter`.

        Parameters
        ----------
        obj : object
            Any Python object

        attrs : sequence of str
            Attribute names to pull from the object

        Returns
        -------
        attrs : dict
            Maps attribute names to the values retrieved from
            ``obj.attr``.  If any of the attributes is `None`, it will
            not appear in the output dictionary.
        u   _u   -N(   t   getattrR&   R   t	   text_typeR   (   t   objt   attrst   dt   attr(    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   object_attrsF  s
    /N(   t   __name__t
   __module__t   __doc__R   t   TrueR>   R   R-   t
   contextlibt   contextmanagerR7   R(   R9   R   R&   R8   R=   R?   R   R@   R   t   staticmethodRH   (    (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyR
   /   s"   	,7					(    (   RK   t
   __future__R    R   R   R   t   externR   RM   R   R.   RL   R3   t   ImportErrorR>   t    R   R   R	   t   escape_xml_cdatat
   escape_xmlR
   (    (    (    s7   lib/python2.7/site-packages/astropy/utils/xml/writer.pyt   <module>   s"   "

				