U
    (Q^7<                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ej
d	edd
 dZdddZdd ZeG dd deZeG dd deZdS )a  
    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: 2007 Pallets
    :license: BSD-3-Clause
    N)datetime   )implements_to_string)string_typesescape)BaseResponsez\'werkzeug.contrib.atom' is deprecated as of version 0.15 and will be removed in version 1.0.)
stacklevelzhttp://www.w3.org/1999/xhtmlc                 C   sB   |dkrd| t || f S |s.d| t|| f S d| |t|| f S )z?Helper function for the builder that creates an XML text block.Zxhtmlz/<%s type="xhtml"><div xmlns="%s">%s</div></%s>
z<%s>%s</%s>
z<%s type="%s">%s</%s>
)XHTML_NAMESPACEr   )namecontentcontent_type r   ;/tmp/pip-install-bd4o36v9/Werkzeug/werkzeug/contrib/atom.py_make_text_block*   s    r   c                 C   s   |   }| jr|S |d S )z$Format a datetime object for iso8601Z)	isoformattzinfo)objZiso8601r   r   r   format_iso86018   s    r   c                   @   sV   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )AtomFeeda	  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.
    )ZWerkzeugNNNc                 K   s  || _ |dd| _|d| _|d| j| _|d| j| _|d| _|dd| _|d	| _|d
| _	|d| _
|d| _|d| _|dd| _|d| _| jd kr| j| _|dg | _|rt|ng | _t| jdrt| jttf r| jg| _t| jD ]$\}}t|tsd|i| j|< q| j sNtd| js^td| jD ]}d|krdtdqdd S )N
title_typetexturlfeed_urlidupdatedauthorr   iconlogorightsrights_typesubtitlesubtitle_type	generatorlinks__iter__r   title is requiredid is requiredz#author must contain at least a name)titlegetr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   default_generatorr%   listentrieshasattr
isinstancer   dict	enumerate
ValueError	TypeError)selfr)   r-   kwargsir   r   r   r   __init__t   sB    
 


zAtomFeed.__init__c                 O   sP   t |dkr0|s0t|d tr0| j|d  n| j|d< | jt|| dS )zAdd 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.
           r   r   N)lenr/   	FeedEntryr-   appendr   )r4   argsr5   r   r   r   add   s    
zAtomFeed.addc                 C   s   d| j j| jt| jf S )Nz<%s %r (%d entries)>)	__class____name__r)   r9   r-   r4   r   r   r   __repr__   s
    zAtomFeed.__repr__c           	      #   s`  | j s&tdd | jD r&ddif| _ | jsVtdd | jD }|rL|d nt | _dV  d	V  d
td| j| j	 V  dt
| j V  dt| j V  | jrdt
| j V  | jrdt
| j V  | jD ]" dd fdd D  V  q| j D ]Z}dV  dt
|d  V  d|kr,dt
|d  V  d|krHdt
|d  V  dV  q| jrnd
td| j| j V  | jrdt
| j V  | jrdt
| j V  | jrd
td| j| j V  | j\}}}|s|s|r.dg}|r|dt
|  |r|d t
|  |d!t
|  d|V  | jD ] }| D ]}d
| V  q@q4d"V  d#S )$z-Return a generator that yields pieces of XML.c                 s   s   | ]}|j  V  qd S N)r   ).0er   r   r   	<genexpr>   s     z$AtomFeed.generate.<locals>.<genexpr>r   zUnknown authorc                 S   s   g | ]
}|j qS r   )r   )rC   entryr   r   r   
<listcomp>   s     z%AtomFeed.generate.<locals>.<listcomp>z'<?xml version="1.0" encoding="utf-8"?>
z+<feed xmlns="http://www.w3.org/2005/Atom">
  r)     <id>%s</id>
  <updated>%s</updated>
  <link href="%s" />
z   <link href="%s" rel="self" />
  <link %s/>
 c                 3   s"   | ]}d |t  | f V  qdS z%s="%s" Nr   rC   klinkr   r   rE      s      <author>
    <name>%s</name>
uri    <uri>%s</uri>
email    <email>%s</email>
  </author>
r"   z  <icon>%s</icon>
z  <logo>%s</logo>
r    z  <generatorz	 uri="%s"z version="%s"z>%s</generator>
z</feed>
N)r   anyr-   r   sortedr   utcnowr   r)   r   r   r   r   r   r   r%   joinr"   r#   r   r   r    r!   r$   r;   generate)	r4   datesr   Zgenerator_nameZgenerator_urlZgenerator_versiontmprF   liner   rR   r   r_      s`    




zAtomFeed.generatec                 C   s   d |  S )zConvert the feed into a string.rN   r^   r_   r@   r   r   r   	to_string   s    zAtomFeed.to_stringc                 C   s   t |  ddS )z&Return a response object for the feed.zapplication/atom+xml)mimetype)r   rd   r@   r   r   r   get_response   s    zAtomFeed.get_responsec                 C   s   |   ||S )z&Use the class as WSGI response object.)rf   )r4   environZstart_responser   r   r   __call__   s    zAtomFeed.__call__c                 C   s   |   S rB   rd   r@   r   r   r   __str__   s    zAtomFeed.__str__)NN)r?   
__module____qualname____doc__r+   r7   r=   rA   r_   rd   rf   rh   rj   r   r   r   r   r   @   s   /
$6r   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )r:   a	  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.
    Nc                 K   sB  || _ |dd| _|| _|dd| _|d| _|d| j| _|d| _|d| _|d	d| _	|d
d| _
|d| _|d| _|dg | _|dg | _|d|| _t| j
drt| j
ttf r| j
g| _
t| j
D ] \}}t|tsd|i| j
|< q| j std| js.td| js>tdd S )Nr   r   r   htmlr   r   r   summarysummary_typer   r   	publishedr    r%   
categoriesxml_baser&   r   r'   r(   zupdated is required)r)   r*   r   r   r   r   r   r   ro   rp   r   rq   r    r%   rr   rs   r.   r/   r   r0   r1   r2   )r4   r)   r   r   r5   r6   r   r   r   r   r7     s:     

zFeedEntry.__init__c                 C   s   d| j j| jf S )Nz<%s %r>)r>   r?   r)   r@   r   r   r   rA   ?  s    zFeedEntry.__repr__c                 #   s~  d}| j rdt| j  }d| V  dtd| j| j V  dt| j V  dt| j V  | jrndt| j V  | j	rd	t| j	 V  | j
D ]V}d
V  dt|d  V  d|krdt|d  V  d|krdt|d  V  dV  q| jD ]"ddfddD  V  q| jD ]$ dd fdd D  V  q| jrVdtd| j| j V  | jrtdtd| j| j V  dV  dS )zYields pieces of ATOM XML.rN   z xml:base="%s"z
<entry%s>
rI   r)   rJ   rK   z  <published>%s</published>
rL   rT   rU   r   rV   rW   rX   rY   rZ   rM   c                 3   s"   | ]}d |t  | f V  qdS rO   r   rP   rR   r   r   rE   X  s    z%FeedEntry.generate.<locals>.<genexpr>z  <category %s/>
c                 3   s"   | ]}d |t  | f V  qdS rO   r   rP   )categoryr   r   rE   \  s    ro   r   z	</entry>
N)rs   r   r   r)   r   r   r   r   rq   r   r   r%   r^   rr   ro   rp   r   r   )r4   baser   r   )rt   rS   r   r_   B  s@    



zFeedEntry.generatec                 C   s   d |  S )z,Convert the feed item into a unicode object.rN   rc   r@   r   r   r   rd   e  s    zFeedEntry.to_stringc                 C   s   |   S rB   ri   r@   r   r   r   rj   i  s    zFeedEntry.__str__)NNN)	r?   rk   rl   rm   r7   rA   r_   rd   rj   r   r   r   r   r:      s   ,
 #r:   )N)rm   warningsr   _compatr   r   utilsr   wrappersr   warnDeprecationWarningr
   r   r   objectr   r:   r   r   r   r   <module>   s&   
 0