3
 [ð]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   ú9/tmp/pip-build-w1as9q70/Werkzeug/werkzeug/contrib/atom.pyÚ_make_text_block*   s    r   c             C   s   | j ƒ }| 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.
    ÚWerkzeugNc             K   sŒ  || _ |jddƒ| _|jdƒ| _|jd| jƒ| _|jd| jƒ| _|jdƒ| _|jdf ƒ| _|jdƒ| _|jd	ƒ| _	|jd
ƒ| _
|jdƒ| _|jdƒ| _|jddƒ| _|jdƒ| _| jd krÂ| j| _|jdg ƒ| _|rÜt|ƒng | _t| jdƒ �st| jttf ƒ�r| jg| _x2t| jƒD ]$\}}t|tƒ�sd|i| j|< �qW | j �sTtdƒ‚| j�sdtdƒ‚x"| jD ]}d|k�rltdƒ‚�qlW d S )NÚ
title_typeÚtextÚurlÚfeed_urlÚidÚupdatedÚauthorÚiconÚlogoÚrightsÚrights_typeÚsubtitleÚsubtitle_typeÚ	generatorÚlinksÚ__iter__r
   ztitle is requiredz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   s>    


zAtomFeed.__init__c             O   sR   t |ƒdkr2| r2t|d tƒr2| jj|d ƒ n| j|d< | jjt||Žƒ dS )zÖ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.
        é   r   r   N)Úlenr-   Ú	FeedEntryr+   Úappendr   )r2   Úargsr3   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'   r7   r+   )r2   r   r   r   Ú__repr__£   s    zAtomFeed.__repr__c       	      #   sr  | j s&tdd„ | jD ƒƒr&ddif| _ | jsVtdd„ | jD ƒƒ}|rL|d$ ntjƒ | _dV  d	V  d
td| j| j	ƒ V  dt
| jƒ V  dt| jƒ V  | jr®dt
| jƒ V  | jrÄdt
| jƒ V  x,| jD ]"‰ ddj‡ fdd„ˆ D ƒƒ V  qÌW xf| j D ]\}dV  dt
|d ƒ V  d|k�r2dt
|d ƒ V  d|k�rNdt
|d ƒ V  dV  � qúW | j�rxd
td| j| jƒ V  | j�r�dt
| jƒ V  | j�r¨dt
| jƒ V  | j�rÆd
td| j| jƒ V  | j\}}}|�sä|�sä|�r8dg}|�r|jdt
|ƒ ƒ |�r|jd t
|ƒ ƒ |jd!t
|ƒ ƒ dj|ƒV  x.| jD ]$}x|jƒ D ]}d
| V  �qNW �q@W d"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   )r?   Úentryr   r   r   ú
<listcomp>²   s    z%AtomFeed.generate.<locals>.<listcomp>r6   z'<?xml version="1.0" encoding="utf-8"?>
z+<feed xmlns="http://www.w3.org/2005/Atom">
z  r'   z  <id>%s</id>
z  <updated>%s</updated>
z  <link href="%s" />
z   <link href="%s" rel="self" />
z  <link %s/>
Ú c             3   s"   | ]}d |t ˆ | ƒf V  qdS )z%s="%s" N)r   )r?   Úk)Úlinkr   r   rA   À   s    z  <author>
z    <name>%s</name>
Úuriz    <uri>%s</uri>
Úemailz    <email>%s</email>
z  </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$   r9   Úgenerate)	r2   Údatesr   Zgenerator_nameZgenerator_urlZgenerator_versionÚtmprB   Úliner   )rF   r   rN   ª   s^    

zAtomFeed.generatec             C   s   dj | jƒ ƒS )zConvert the feed into a string.rD   )rM   rN   )r2   r   r   r   Ú	to_stringà   s    zAtomFeed.to_stringc             C   s   t | jƒ dd�S )z&Return a response object for the feed.zapplication/atom+xml)Úmimetype)r   rR   )r2   r   r   r   Úget_responseä   s    zAtomFeed.get_responsec             C   s   | j ƒ ||ƒS )z&Use the class as WSGI response object.)rT   )r2   ÚenvironZstart_responser   r   r   Ú__call__è   s    zAtomFeed.__call__c             C   s   | j ƒ S )N)rR   )r2   r   r   r   Ú__str__ì   s    zAtomFeed.__str__)r   NN)NN)r=   Ú
__module__Ú__qualname__Ú__doc__r)   r5   r;   r>   rN   rR   rT   rV   rW   r   r   r   r   r   @   s   0
$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 )r8   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   sH  || _ |jddƒ| _|| _|jddƒ| _|jdƒ| _|jd| jƒ| _|jdƒ| _|jdƒ| _|jd	dƒ| _	|jd
f ƒ| _
|jdƒ| _|jdƒ| _|jdg ƒ| _|jdg ƒ| _|jd|ƒ| _t| j
dƒ sÚt| j
ttf ƒrä| j
g| _
x.t| j
ƒD ] \}}t|tƒsðd|i| j
|< qðW | j �s$tdƒ‚| j�s4tdƒ‚| j�sDtdƒ‚d S )Nr   r   r   Úhtmlr   r   r   ÚsummaryÚsummary_typer   Ú	publishedr    r%   Ú
categoriesÚxml_baser&   r
   ztitle is requiredzid is requiredzupdated is required)r'   r(   r   r   r   r   r   r   r\   r]   r   r^   r    r%   r_   r`   r,   r-   r   r.   r/   r0   )r2   r'   r   r   r3   r4   r   r   r   r   r5     s6    

zFeedEntry.__init__c             C   s   d| j j| jf S )Nz<%s %r>)r<   r=   r'   )r2   r   r   r   r>   ?  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	r„d	t| j	ƒ V  x`| j
D ]V}d
V  dt|d ƒ V  d|krÂdt|d ƒ V  d|krÜdt|d ƒ V  dV  qŒW x,| jD ]"‰ddj‡fdd„ˆD ƒƒ V  qîW x.| jD ]$‰ ddj‡ fdd„ˆ D ƒƒ V  �qW | j�rbdtd| j| jƒ V  | j�r€dtd| j| jƒ V  dV  dS )zYields pieces of ATOM XML.rD   z xml:base="%s"z
<entry%s>
z  r'   z  <id>%s</id>
z  <updated>%s</updated>
z  <published>%s</published>
z  <link href="%s" />
z  <author>
z    <name>%s</name>
r
   rG   z    <uri>%s</uri>
rH   z    <email>%s</email>
z  </author>
z  <link %s/>
c             3   s"   | ]}d |t ˆ | ƒf V  qdS )z%s="%s" N)r   )r?   rE   )rF   r   r   rA   Y  s    z%FeedEntry.generate.<locals>.<genexpr>z  <category %s/>
c             3   s"   | ]}d |t ˆ | ƒf V  qdS )z%s="%s" N)r   )r?   rE   )Úcategoryr   r   rA   ]  s    r\   r   z	</entry>
N)r`   r   r   r'   r   r   r   r   r^   r   r   r%   rM   r_   r\   r]   r   r   )r2   Úbaser   r   )ra   rF   r   rN   B  s<    

zFeedEntry.generatec             C   s   dj | jƒ ƒS )z,Convert the feed item into a unicode object.rD   )rM   rN   )r2   r   r   r   rR   e  s    zFeedEntry.to_stringc             C   s   | j ƒ S )N)rR   )r2   r   r   r   rW   i  s    zFeedEntry.__str__)NNN)	r=   rX   rY   rZ   r5   r>   rN   rR   rW   r   r   r   r   r8   ð   s   -
 #r8   )N)rZ   Úwarningsr   Ú_compatr   r   Úutilsr   Zwrappersr   ÚwarnÚDeprecationWarningr	   r   r   Úobjectr   r8   r   r   r   r   Ú<module>   s$   
 0