ó
~9Vc           @   s£   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z m Z d Z	 d d „ Z d „  Z e d	 e f d
 „  ƒ  Yƒ Z e d e f d „  ƒ  Yƒ Z d S(   se  
    werkzeug.contrib.atom
    ~~~~~~~~~~~~~~~~~~~~~

    This module provides a class called :class:`AtomFeed` which can be
    used to generate feeds in the Atom syndication format (see :rfc:`4287`).

    Example::

        def atom_feed(request):
            feed = AtomFeed("My Blog", feed_url=request.url,
                            url=request.host_url,
                            subtitle="My example blog for a feed test.")
            for post in Post.query.limit(10).all():
                feed.add(post.title, post.body, content_type='html',
                         author=post.author, url=post.url, id=post.uid,
                         updated=post.last_update, published=post.pub_date)
            return feed.get_response()

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿ(   t   datetime(   t   escape(   t   BaseResponse(   t   implements_to_stringt   string_typess   http://www.w3.org/1999/xhtmlc         C   sW   | d k r  d |  t  | |  f S| s= d |  t | ƒ |  f Sd |  | t | ƒ |  f S(   s?   Helper function for the builder that creates an XML text block.t   xhtmlu/   <%s type="xhtml"><div xmlns="%s">%s</div></%s>
u   <%s>%s</%s>
u   <%s type="%s">%s</%s>
(   t   XHTML_NAMESPACER   (   t   namet   contentt   content_type(    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   _make_text_block"   s    	c         C   s!   |  j  ƒ  } |  j r | S| d S(   s$   Format a datetime object for iso8601t   Z(   t	   isoformatt   tzinfo(   t   objt   iso8601(    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   format_iso8601-   s    	t   AtomFeedc           B   sb   e  Z d  Z d Z d
 d
 d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   sÒ	  A helper class that creates Atom feeds.

    :param title: the title of the feed. Required.
    :param title_type: the type attribute for the title element.  One of
                       ``'html'``, ``'text'`` or ``'xhtml'``.
    :param url: the url for the feed (not the url *of* the feed)
    :param id: a globally unique id for the feed.  Must be an URI.  If
               not present the `feed_url` is used, but one of both is
               required.
    :param updated: the time the feed was modified the last time.  Must
                    be a :class:`datetime.datetime` object.  If not
                    present the latest entry's `updated` is used.
                    Treated as UTC if naive datetime.
    :param feed_url: the URL to the feed.  Should be the URL that was
                     requested.
    :param author: the author of the feed.  Must be either a string (the
                   name) or a dict with name (required) and uri or
                   email (both optional).  Can be a list of (may be
                   mixed, too) strings and dicts, too, if there are
                   multiple authors. Required if not every entry has an
                   author element.
    :param icon: an icon for the feed.
    :param logo: a logo for the feed.
    :param rights: copyright information for the feed.
    :param rights_type: the type attribute for the rights element.  One of
                        ``'html'``, ``'text'`` or ``'xhtml'``.  Default is
                        ``'text'``.
    :param subtitle: a short description of the feed.
    :param subtitle_type: the type attribute for the subtitle element.
                          One of ``'text'``, ``'html'``, ``'text'``
                          or ``'xhtml'``.  Default is ``'text'``.
    :param links: additional links.  Must be a list of dictionaries with
                  href (required) and rel, type, hreflang, title, length
                  (all optional)
    :param generator: the software that generated this feed.  This must be
                      a tuple in the form ``(name, url, version)``.  If
                      you don't want to specify one of them, set the item
                      to `None`.
    :param entries: a list with the entries for the feed. Entries can also
                    be added later with :meth:`add`.

    For more information on the elements see
    http://www.atomenabled.org/developers/syndication/

    Everywhere where a list is demanded, any iterable can be used.
    t   Werkzeugc         K   s=  | |  _  | j d d ƒ |  _ | j d ƒ |  _ | j d |  j ƒ |  _ | j d |  j ƒ |  _ | j d ƒ |  _ | j d d ƒ |  _ | j d ƒ |  _ | j d	 ƒ |  _	 | j d
 ƒ |  _
 | j d ƒ |  _ | j d ƒ |  _ | j d d ƒ |  _ | j d ƒ |  _ |  j d  k r&|  j |  _ n  | j d g  ƒ |  _ | rMt | ƒ pPg  |  _ t |  j d ƒ s‚t |  j t t f ƒ r”|  j g |  _ n  xC t |  j ƒ D]2 \ } } t | t ƒ s¤i | d 6|  j | <q¤q¤W|  j  sòt d ƒ ‚ n  |  j s
t d ƒ ‚ n  x, |  j D]! } d | k rt d ƒ ‚ qqWd  S(   Nt
   title_typet   textt   urlt   feed_urlt   idt   updatedt   authort   icont   logot   rightst   rights_typet   subtitlet   subtitle_typet	   generatort   linkst   __iter__R   s   title is requireds   id is requireds#   author must contain at least a name(    (   t   titlet   getR   R   R   R   R   R   R   R   R   R   R   R   R    t   Nonet   default_generatorR!   t   listt   entriest   hasattrt
   isinstanceR   t   dictt	   enumeratet
   ValueErrort	   TypeError(   t   selfR#   R(   t   kwargst   iR   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   __init__h   s>    			c         O   sm   t  | ƒ d k rC | rC t | d t ƒ rC |  j j | d ƒ n& |  j | d <|  j j t | | Ž  ƒ d S(   sÖ   Add a new entry to the feed.  This function can either be called
        with a :class:`FeedEntry` or some keyword and positional arguments
        that are forwarded to the :class:`FeedEntry` constructor.
        i   i    R   N(   t   lenR*   t	   FeedEntryR(   t   appendR   (   R/   t   argsR0   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   add‹   s    ,c         C   s#   d |  j  j |  j t |  j ƒ f S(   Ns   <%s %r (%d entries)>(   t	   __class__t   __name__R#   R3   R(   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   __repr__–   s    	c   	      #   s  |  j  s= t t d „  |  j ƒ k r= i d d 6f |  _  q= n  |  j s t g  |  j D] } | j ^ qS ƒ } | r{ | d p„ t j ƒ  |  _ n  d Vd Vd t d |  j	 |  j
 ƒ Vd	 t |  j ƒ Vd
 t |  j ƒ V|  j rô d t |  j ƒ Vn  |  j rd t |  j ƒ Vn  x3 |  j D]( ‰  d d j ‡  f d †  ˆ  Dƒ ƒ VqWxr |  j  D]g } d Vd t | d ƒ Vd | k r’d t | d ƒ Vn  d | k r´d t | d ƒ Vn  d VqRW|  j räd t d |  j |  j ƒ Vn  |  j rd t |  j ƒ Vn  |  j r d t |  j ƒ Vn  |  j rGd t d |  j |  j ƒ Vn  |  j \ } } } | sk| sk| rÜd g } | r”| j d t | ƒ ƒ n  | r´| j d t | ƒ ƒ n  | j d t | ƒ ƒ d j | ƒ Vn  x1 |  j D]& } x | j ƒ  D] } d  | VqùWqæWd! Vd" S(#   s-   Return a generator that yields pieces of XML.c         S   s   t  |  j ƒ S(   N(   t   boolR   (   t   e(    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   <lambda>¡   s    s   Unknown authorR   iÿÿÿÿu'   <?xml version="1.0" encoding="utf-8"?>
u+   <feed xmlns="http://www.w3.org/2005/Atom">
s     R#   u     <id>%s</id>
u     <updated>%s</updated>
u     <link href="%s" />
u      <link href="%s" rel="self" />
u     <link %s/>
t    c         3   s)   |  ] } d  | t  ˆ  | ƒ f Vq d S(   s   %s="%s" N(   R   (   t   .0t   k(   t   link(    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pys	   <genexpr>³   s   u     <author>
u       <name>%s</name>
t   uriu       <uri>%s</uri>
t   emails       <email>%s</email>
s     </author>
R   u     <icon>%s</icon>
u     <logo>%s</logo>
R   u     <generatoru	    uri="%s"u    version="%s"u   >%s</generator>
u    u     u   </feed>
N(   R   t   Falset   mapR(   R   t   sortedR    t   utcnowR
   R#   R   R   R   R   R   R   R!   t   joinR   R   R   R   R   R   R    R5   t   generate(	   R/   t   entryt   datesR   t   generator_namet   generator_urlt   generator_versiont   tmpt   line(    (   RA   s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyRI      sd    		%"								c         C   s   d j  |  j ƒ  ƒ S(   s   Convert the feed into a string.u    (   RH   RI   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt	   to_stringÕ   s    c         C   s   t  |  j ƒ  d d ƒS(   s&   Return a response object for the feed.t   mimetypes   application/atom+xml(   R   RQ   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   get_responseÙ   s    c         C   s   |  j  ƒ  | | ƒ S(   s&   Use the class as WSGI response object.(   RS   (   R/   t   environt   start_response(    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   __call__Ý   s    c         C   s
   |  j  ƒ  S(   N(   RQ   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   __str__á   s    N(   R   NN(   R9   t
   __module__t   __doc__R%   R&   R2   R7   R:   RI   RQ   RS   RV   RW   (    (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyR   5   s   /#			8			R4   c           B   sD   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s‘	  Represents a single entry in a feed.

    :param title: the title of the entry. Required.
    :param title_type: the type attribute for the title element.  One of
                       ``'html'``, ``'text'`` or ``'xhtml'``.
    :param content: the content of the entry.
    :param content_type: the type attribute for the content element.  One
                         of ``'html'``, ``'text'`` or ``'xhtml'``.
    :param summary: a summary of the entry's content.
    :param summary_type: the type attribute for the summary element.  One
                         of ``'html'``, ``'text'`` or ``'xhtml'``.
    :param url: the url for the entry.
    :param id: a globally unique id for the entry.  Must be an URI.  If
               not present the URL is used, but one of both is required.
    :param updated: the time the entry was modified the last time.  Must
                    be a :class:`datetime.datetime` object.  Treated as
                    UTC if naive datetime. Required.
    :param author: the author of the entry.  Must be either a string (the
                   name) or a dict with name (required) and uri or
                   email (both optional).  Can be a list of (may be
                   mixed, too) strings and dicts, too, if there are
                   multiple authors. Required if the feed does not have an
                   author element.
    :param published: the time the entry was initially published.  Must
                      be a :class:`datetime.datetime` object.  Treated as
                      UTC if naive datetime.
    :param rights: copyright information for the entry.
    :param rights_type: the type attribute for the rights element.  One of
                        ``'html'``, ``'text'`` or ``'xhtml'``.  Default is
                        ``'text'``.
    :param links: additional links.  Must be a list of dictionaries with
                  href (required) and rel, type, hreflang, title, length
                  (all optional)
    :param categories: categories for the entry. Must be a list of dictionaries
                       with term (required), scheme and label (all optional)
    :param xml_base: The xml base (url) for this feed item.  If not provided
                     it will default to the item url.

    For more information on the elements see
    http://www.atomenabled.org/developers/syndication/

    Everywhere where a list is demanded, any iterable can be used.
    c         K   sç  | |  _  | j d d ƒ |  _ | |  _ | j d d ƒ |  _ | j d ƒ |  _ | j d |  j ƒ |  _ | j d ƒ |  _ | j d ƒ |  _ | j d	 d ƒ |  _	 | j d
 d ƒ |  _
 | j d ƒ |  _ | j d ƒ |  _ | j d g  ƒ |  _ | j d g  ƒ |  _ | j d | ƒ |  _ t |  j
 d ƒ sCt |  j
 t t f ƒ rU|  j
 g |  _
 n  xC t |  j
 ƒ D]2 \ } } t | t ƒ sei | d 6|  j
 | <qeqeW|  j  s³t d ƒ ‚ n  |  j sËt d ƒ ‚ n  |  j sãt d ƒ ‚ n  d  S(   NR   R   R	   t   htmlR   R   R   t   summaryt   summary_typeR   t	   publishedR   R!   t
   categoriest   xml_baseR"   R   s   title is requireds   id is requireds   updated is required(    (   R#   R$   R   R   R	   R   R   R   R[   R\   R   R]   R   R!   R^   R_   R)   R*   R   R+   R,   R-   (   R/   R#   R   R   R0   R1   R   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyR2     s6    					c         C   s   d |  j  j |  j f S(   Ns   <%s %r>(   R8   R9   R#   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyR:   2  s    	c         #   sá  d } |  j  r% d t |  j  ƒ } n  d | Vd t d |  j |  j ƒ Vd t |  j ƒ Vd t |  j ƒ V|  j r‹ d t |  j ƒ Vn  |  j	 r© d	 t |  j	 ƒ Vn  xr |  j
 D]g } d
 Vd t | d ƒ Vd | k ró d t | d ƒ Vn  d | k rd t | d ƒ Vn  d Vq³ Wx3 |  j D]( ‰ d d j ‡ f d †  ˆ Dƒ ƒ Vq(Wx3 |  j D]( ‰  d d j ‡  f d †  ˆ  Dƒ ƒ Vq^W|  j r±d t d |  j |  j ƒ Vn  |  j rØd t d |  j |  j ƒ Vn  d Vd S(   s   Yields pieces of ATOM XML.R>   s    xml:base="%s"u
   <entry%s>
u     R#   u     <id>%s</id>
u     <updated>%s</updated>
u     <published>%s</published>
u     <link href="%s" />
u     <author>
u       <name>%s</name>
R   RB   u       <uri>%s</uri>
RC   u       <email>%s</email>
u     </author>
u     <link %s/>
c         3   s)   |  ] } d  | t  ˆ  | ƒ f Vq d S(   s   %s="%s" N(   R   (   R?   R@   (   RA   (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pys	   <genexpr>O  s   u     <category %s/>
c         3   s)   |  ] } d  | t  ˆ  | ƒ f Vq d S(   s   %s="%s" N(   R   (   R?   R@   (   t   category(    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pys	   <genexpr>R  s   R[   R   u	   </entry>
N(   R_   R   R
   R#   R   R   R   R   R]   R   R   R!   RH   R^   R[   R\   R   R	   (   R/   t   baseR   (    (   R`   RA   s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyRI   8  sB    							c         C   s   d j  |  j ƒ  ƒ S(   s,   Convert the feed item into a unicode object.u    (   RH   RI   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyRQ   \  s    c         C   s
   |  j  ƒ  S(   N(   RQ   (   R/   (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyRW   `  s    N(	   R9   RX   RY   R%   R2   R:   RI   RQ   RW   (    (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyR4   å   s   ,		$	N(   RY   R    t   werkzeug.utilsR   t   werkzeug.wrappersR   t   werkzeug._compatR   R   R   R%   R
   R   t   objectR   R4   (    (    (    s7   /tmp/pip-build-zHWFj_/Werkzeug/werkzeug/contrib/atom.pyt   <module>   s   	¯