
 X}E                 @   sL  d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d d l m Z d d	 l m Z d
 d   Z d d   Z d d   Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z e Z d S)a  
Syndication feed generation library -- used for generating RSS, etc.

Sample usage:

>>> from django.utils import feedgenerator
>>> feed = feedgenerator.Rss201rev2Feed(
...     title="Poynter E-Media Tidbits",
...     link="http://www.poynter.org/column.asp?id=31",
...     description="A group Weblog by the sharpest minds in online media/journalism/publishing.",
...     language="en",
... )
>>> feed.add_item(
...     title="Hello",
...     link="http://www.holovaty.com/test/",
...     description="Testing."
... )
>>> with open('test.rss', 'w') as fp:
...     feed.write(fp, 'utf-8')

For definitions of the different versions of RSS, see:
http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/02/04/incompatible-rss
    )unicode_literalsN)datetime_safesix)RemovedInDjango20Warning)
force_text
iri_to_uri)StringIO)urlparse)SimplerXMLGeneratorc       
      C   s   d } d } t  j |   }  | |  j   } | |  j d } |  j d | | f  } t j rp | j d  } n  |  j   } | d  k r | d S| j	 d d | j
 d } t | d  \ } }	 | d | |	 f Sd  S)NJanFebMarAprMayJunJulAugSepOctNovDecMonTueWedThuFriSatSun   z%s, %%d %s %%Y %%H:%%M:%%S zutf-8z-0000   <   z	%+03d%02d)zJanzFebzMarzAprzMayzJunzJulzAugzSepzOctzNovzDec)zMonzTuezWedzThuzFrizSatzSun)r   new_datetimeweekdaymonthstrftimer   PY2decode	utcoffsetdayssecondsdivmod)
datemonthsr(   Zdowr#   time_stroffsettimezonehourminute r2   H/home/ubuntu/projects/ifolica/build/django/django/utils/feedgenerator.pyrfc2822_date%   s    	r4   c             C   s   t  j |   }  |  j d  } t j r9 | j d  } n  |  j   } | d  k rY | d S| j d d | j d } t	 | d  \ } } | d | | f Sd  S)Nz%Y-%m-%dT%H:%M:%Szutf-8Zr   r    z
%+03d:%02d)
r   r!   r$   r   r%   r&   r'   r(   r)   r*   )r+   r-   r.   r/   r0   r1   r2   r2   r3   rfc3339_date<   s    	r6   c             C   sZ   t  |   } d } | d k	 r= d t j |  j d  } n  d | j | | j | j f S)z
    Creates a TagURI.

    See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
     Nz,%sz%Y-%m-%dztag:%s%s:%s/%s)r	   r   r!   r$   hostnamepathfragment)urlr+   bitsdr2   r2   r3   get_tag_uriL   s
    r>   c               @   s   e  Z d  Z d Z d d d d d d d d d d d d 
 Z d d d d d d d d f  d d d 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 d   Z d S)SyndicationFeedzGBase class for all syndication feeds. Subclasses should provide write()Nc             K   s  d d   } |	 r( d d   |	 D }	 n  | d  k	 rC t  |  } n  i | |  d 6t |  d 6| |  d 6| |  d 6| |  d	 6| |  d
 6t |  d 6| |  d 6|	 p f  d 6t |
  d 6| |  d 6| p | d 6| d 6|  _ |  j j |  g  |  _ d  S)Nc             S   s   t  |  d d S)Nstrings_onlyT)r   )sr2   r2   r3   
to_unicode^   s    z,SyndicationFeed.__init__.<locals>.to_unicodec             S   s   g  |  ] } t  |   q Sr2   )r   ).0cr2   r2   r3   
<listcomp>a   s   	 z,SyndicationFeed.__init__.<locals>.<listcomp>titlelinkdescriptionlanguageauthor_emailauthor_nameauthor_linksubtitle
categoriesfeed_urlfeed_copyrightidttl)r   r   feedupdateitems)selfrF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   Z	feed_guidrR   kwargsrB   r2   r2   r3   __init__[   s*    zSyndicationFeed.__init__c                s[  d d     | r.   f d d   | D } n  | d k	 rI t  |  } n  | d k rp | d k rg g  n | } n t j d t d d | g } i   |  d	 6t |  d
 6  |  d 6  |  d 6  |  d 6t |  d 6| d 6| d 6  |  d 6  |	  d 6|
 d 6| d 6| pf  d 6  |  d 6| d 6} | j |  |  j j |  d S)a  
        Adds an item to the feed. All args are expected to be Python Unicode
        objects except pubdate and updateddate, which are datetime.datetime
        objects, and enclosures, which is an iterable of instances of the
        Enclosure class.
        c             S   s   t  |  d d S)Nr@   T)r   )rA   r2   r2   r3   rB      s    z,SyndicationFeed.add_item.<locals>.to_unicodec                s   g  |  ] }   |   q Sr2   r2   )rC   rD   )rB   r2   r3   rE      s   	 z,SyndicationFeed.add_item.<locals>.<listcomp>NzEThe enclosure keyword argument is deprecated, use enclosures instead.
stacklevel   rF   rG   rH   rJ   rK   rL   pubdateupdateddatecomments	unique_idunique_id_is_permalink
enclosuresrN   item_copyrightrR   )r   warningswarnr   r   rT   rU   append)rV   rF   rG   rH   rJ   rK   rL   r[   r]   r^   r_   	enclosurerN   ra   rR   r\   r`   rW   itemr2   )rB   r3   add_itemw   s<    	
zSyndicationFeed.add_itemc             C   s   t  |  j  S)N)lenrU   )rV   r2   r2   r3   	num_items   s    zSyndicationFeed.num_itemsc             C   s   i  S)zx
        Return extra attributes to place on the root (i.e. feed/channel) element.
        Called from write().
        r2   )rV   r2   r2   r3   root_attributes   s    zSyndicationFeed.root_attributesc             C   s   d S)zd
        Add elements in the root (i.e. feed/channel) element. Called
        from write().
        Nr2   )rV   handlerr2   r2   r3   add_root_elements   s    z!SyndicationFeed.add_root_elementsc             C   s   i  S)zZ
        Return extra attributes to place on each item (i.e. item/entry) element.
        r2   )rV   rf   r2   r2   r3   item_attributes   s    zSyndicationFeed.item_attributesc             C   s   d S)zF
        Add elements on each item (i.e. item/entry) element.
        Nr2   )rV   rk   rf   r2   r2   r3   add_item_elements   s    z!SyndicationFeed.add_item_elementsc             C   s   t  d   d S)z
        Outputs the feed in the given encoding to outfile, which is a file-like
        object. Subclasses should override this.
        z;subclasses of SyndicationFeed must provide a write() methodN)NotImplementedError)rV   outfileencodingr2   r2   r3   write   s    zSyndicationFeed.writec             C   s#   t    } |  j | |  | j   S)zE
        Returns the feed in the given encoding as a string.
        )r   rr   getvalue)rV   rq   rA   r2   r2   r3   writeString   s    	zSyndicationFeed.writeStringc             C   s}   d } d } x[ |  j  D]P } xG | D]? } | j |  } | r# | d k sV | | k rb | } qb q# q# Wq W| p| t j j   S)z
        Returns the latest item's pubdate or updateddate. If no items
        have either of these attributes this returns the current date/time.
        Nr\   r[   )zupdateddatezpubdate)rU   getdatetimenow)rV   Zlatest_dateZ	date_keysrf   Zdate_keyZ	item_dater2   r2   r3   latest_post_date   s    z SyndicationFeed.latest_post_date)__name__
__module____qualname____doc__rX   rg   ri   rj   rl   rm   rn   rr   rt   rx   r2   r2   r2   r3   r?   Y   s"   	,r?   c               @   s"   e  Z d  Z d Z d d   Z d S)	EnclosurezRepresents an RSS enclosurec             C   s&   | | |  _  |  _ t |  |  _ d S)z2All args are expected to be Python Unicode objectsN)length	mime_typer   r;   )rV   r;   r~   r   r2   r2   r3   rX      s    zEnclosure.__init__N)ry   rz   r{   r|   rX   r2   r2   r2   r3   r}      s   r}   c               @   sd   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z e	 d d    Z
 d S)RssFeedz"application/rss+xml; charset=utf-8c             C   s}   t  | |  } | j   | j d |  j    | j d |  j    |  j |  |  j |  |  j |  | j d  d  S)NZrsschannel)	r
   startDocumentstartElementrss_attributesrj   rl   write_itemsendChannelElement
endElement)rV   rp   rq   rk   r2   r2   r3   rr      s    
zRssFeed.writec             C   s   i |  j  d 6d d 6S)Nversionzhttp://www.w3.org/2005/Atomz
xmlns:atom)_version)rV   r2   r2   r3   r      s    zRssFeed.rss_attributesc             C   sN   xG |  j  D]< } | j d |  j |   |  j | |  | j d  q
 Wd  S)Nrf   )rU   r   rm   rn   r   )rV   rk   rf   r2   r2   r3   r      s    zRssFeed.write_itemsc             C   sR  | j  d |  j d  | j  d |  j d  | j  d |  j d  |  j d d  k	 r | j  d d  i d d 6|  j d d 6 n  |  j d	 d  k	 r | j  d	 |  j d	  n  x% |  j d
 D] } | j  d |  q W|  j d d  k	 r| j  d |  j d  n  | j  d t |  j     |  j d d  k	 rN| j  d |  j d  n  d  S)NrF   rG   rH   rO   z	atom:linkrV   relhrefrI   rN   categoryrP   	copyrightZlastBuildDaterR   )addQuickElementrS   r4   rx   )rV   rk   catr2   r2   r3   rl     s    +zRssFeed.add_root_elementsc             C   s   | j  d  d  S)Nr   )r   )rV   rk   r2   r2   r3   r     s    zRssFeed.endChannelElementc             C   s   t  j d t d d |  j S)NzKThe mime_type attribute of RssFeed is deprecated. Use content_type instead.rY   rZ   )rb   rc   r   content_type)rV   r2   r2   r3   r     s    zRssFeed.mime_typeN)ry   rz   r{   r   rr   r   r   rl   r   propertyr   r2   r2   r2   r3   r      s   
r   c               @   s"   e  Z d  Z d Z d d   Z d S)RssUserland091Feedz0.91c             C   sS   | j  d | d  | j  d | d  | d d  k	 rO | j  d | d  n  d  S)NrF   rG   rH   )r   )rV   rk   rf   r2   r2   r3   rn   $  s    z$RssUserland091Feed.add_item_elementsN)ry   rz   r{   r   rn   r2   r2   r2   r3   r   !  s   r   c               @   s"   e  Z d  Z d Z d d   Z d S)Rss201rev2Feedz2.0c             C   sV  | j  d | d  | j  d | d  | d d  k	 rO | j  d | d  n  | d r | d r | j  d d | d | d f  nL | d r | j  d | d  n+ | d r | j  d | d i d	 d
 6 n  | d d  k	 r| j  d t | d   n  | d d  k	 r(| j  d | d  n  | d d  k	 ri  } t | j d  t  rst | d  j   | d <n  | j  d | d |  n  | d d  k	 r| j  d | d  n  | d r-t | d  } t |  d k rt	 d   n  | d } | j  d d i | j
 d 6| j d 6| j d 6 n  x" | d D] } | j  d |  q8Wd  S)NrF   rG   rH   rK   rJ   authorz%s (%s)z
dc:creatorz http://purl.org/dc/elements/1.1/zxmlns:dcr[   ZpubDater]   r^   r_   ZisPermaLinkZguidrR   r`   r   zrRSS feed items may only have one enclosure, see http://www.rssboard.org/rss-profile#element-channel-item-enclosurer   re   r7   r;   r~   typerN   r   )r   r4   
isinstanceru   boolstrlowerlistrh   
ValueErrorr;   r~   r   )rV   rk   rf   Z
guid_attrsr`   re   r   r2   r2   r3   rn   /  sD    %





z Rss201rev2Feed.add_item_elementsN)ry   rz   r{   r   rn   r2   r2   r2   r3   r   +  s   r   c               @   sj   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z	 e
 d d    Z d S)	Atom1Feedz#application/atom+xml; charset=utf-8zhttp://www.w3.org/2005/Atomc             C   sZ   t  | |  } | j   | j d |  j    |  j |  |  j |  | j d  d  S)NrS   )r
   r   r   rj   rl   r   r   )rV   rp   rq   rk   r2   r2   r3   rr   d  s    
zAtom1Feed.writec             C   sA   |  j  d d  k	 r/ i |  j d 6|  j  d d 6Si |  j d 6Sd  S)NrI   xmlnszxml:lang)rS   ns)rV   r2   r2   r3   rj   l  s    zAtom1Feed.root_attributesc             C   s  | j  d |  j d  | j  d d i d d 6|  j d d 6 |  j d d  k	 r} | j  d d i d d 6|  j d d 6 n  | j  d	 |  j d	  | j  d
 t |  j     |  j d d  k	 rT| j d i   | j  d |  j d  |  j d d  k	 r| j  d |  j d  n  |  j d d  k	 rD| j  d |  j d  n  | j d  n  |  j d d  k	 r| j  d |  j d  n  x/ |  j d D]  } | j  d d i | d 6 qW|  j d d  k	 r| j  d |  j d  n  d  S)NrF   rG   r7   	alternater   r   rO   rV   rQ   updatedrK   r   namerJ   emailrL   urirM   rN   r   termrP   rights)r   rS   r6   rx   r   r   )rV   rk   r   r2   r2   r3   rl   r  s(    (+zAtom1Feed.add_root_elementsc             C   sN   xG |  j  D]< } | j d |  j |   |  j | |  | j d  q
 Wd  S)Nentry)rU   r   rm   rn   r   )rV   rk   rf   r2   r2   r3   r     s    zAtom1Feed.write_itemsc             C   sA  | j  d | d  | j  d d i | d d 6d d 6 | d d  k	 rf | j  d t | d   n  | d	 d  k	 r | j  d
 t | d	   n  | d d  k	 r%| j d i   | j  d | d  | d d  k	 r | j  d | d  n  | d d  k	 r| j  d | d  n  | j d  n  | d d  k	 rB| d } n t | d | d  } | j  d |  | d d  k	 r| j  d | d i d d 6 n  xJ | d D]> } | j  d d i d d 6| j d 6| j d 6| j d 6 qWx, | d D]  } | j  d d i | d 6 qW| d d  k	 r=| j  d | d  n  d  S) NrF   rG   r7   r   r   r   r[   Z	publishedr\   r   rK   r   r   rJ   r   rL   r   r^   rQ   rH   summaryhtmlr   r`   re   r~   rN   r   r   ra   r   )r   r6   r   r   r>   r;   r~   r   )rV   rk   rf   r^   re   r   r2   r2   r3   rn     s<    %!

zAtom1Feed.add_item_elementsc             C   s   t  j d t d d |  j S)NzMThe mime_type attribute of Atom1Feed is deprecated. Use content_type instead.rY   rZ   )rb   rc   r   r   )rV   r2   r2   r3   r     s    zAtom1Feed.mime_typeN)ry   rz   r{   r   r   rr   rj   rl   r   rn   r   r   r2   r2   r2   r3   r   _  s   0r   )r|   
__future__r   rv   rb   Zdjango.utilsr   r   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   Zdjango.utils.sixr   Z#django.utils.six.moves.urllib.parser	   Zdjango.utils.xmlutilsr
   r4   r6   r>   objectr?   r}   r   r   r   r   ZDefaultFeedr2   r2   r2   r3   <module>   s&   4
4j