3
‡Œði¾7  ã               @   s  d 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m	Z	m
Z
 ddd	d
dddddddddddgZddlZddlZejr†de_ddlZddlZddlZddlZddlmZ ddlmZm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"m#Z#m$Z$ ddl%m&Z' ddl(m)Z)m*Z* dd l+m,Z, d!Z-d"Z.d"Z/d#Z0d$Z1ej2d%ƒZ3ej2d&ƒZ4ej2d'ƒj5Z6d(d)„ Z7dAd+d„Z8d,d„ Z9ej2d-ej:ej;B ƒZ<d.d/„ Z=dBd1d„Z>dCd2d„Z?dDd3d„Z@d4d„ ZAd5d„ ZBd6d„ Zd7d	„ ZCdEd8d
„ZDej2d9ejƒZEd:d„ ZFdFd=d„ZGdHd?d@„ZHdS )IzMiscellaneous utilities.é    )Úunicode_literals)Údivision)Úabsolute_import)Úutils)ÚbytesÚintÚstrÚcollapse_rfc2231_valueÚdecode_paramsÚdecode_rfc2231Úencode_rfc2231Ú
formataddrÚ
formatdateÚformat_datetimeÚgetaddressesÚ
make_msgidÚ	mktime_tzÚ	parseaddrÚ	parsedateÚparsedate_tzÚparsedate_to_datetimeÚunquoteN)Údatetime)Úquoter   )ÚStringIO)r   )ÚAddressList)r   )r   r   Ú_parsedate_tz)Údecodestring)Ú_bencodeÚ_qencode)ÚCharsetz, Ú z
ú'z[][\\()<>@,:;".]z[\\"]u'   ([^í €-í¯¿]|\A)[í°€-í¿¿]([^í°€-í¿¿]|\Z)c             C   s   | j ddƒ}|jddƒS )NÚasciiÚsurrogateescapeÚreplace)ÚencodeÚdecode)ÚstringÚoriginal_bytes© r*   ú>/tmp/pip-build-6m2fasei/future/future/backports/email/utils.pyÚ	_sanitizeH   s    r,   úutf-8c             C   s�   | \}}|j dƒ |rŒy|j dƒ W n8 tk
r\   t|tƒrFt|ƒ}|j|ƒ}d||f S X d}tj|ƒrpd}tj	d|ƒ}d||||f S |S )aÿ  The inverse of parseaddr(), this takes a 2-tuple of the form
    (realname, email_address) and returns the string value suitable
    for an RFC 2822 From, To or Cc header.

    If the first element of pair is false, then the second element is
    returned unmodified.

    Optional charset if given is the character set that is used to encode
    realname in case realname is not ASCII safe.  Can be an instance of str or
    a Charset-like object which has a header_encode method.  Default is
    'utf-8'.
    r#   z%s <%s>r!   ú"z\\\g<0>z%s%s%s <%s>)
r&   ÚUnicodeEncodeErrorÚ
isinstancer   r    Úheader_encodeÚ
specialsreÚsearchÚ	escapesreÚsub)ÚpairÚcharsetÚnameÚaddressÚencoded_nameÚquotesr*   r*   r+   r   P   s     



c             C   s   t j| ƒ}t|ƒ}|jS )z7Return a list of (REALNAME, EMAIL) for each fieldvalue.)Ú
COMMASPACEÚjoinÚ_AddressListÚaddresslist)ÚfieldvaluesÚallÚar*   r*   r+   r   r   s    
a_  
  =\?                   # literal =?
  (?P<charset>[^?]*?)   # non-greedy up to the next ? is the charset
  \?                    # literal ?
  (?P<encoding>[qb])    # either a "q" or a "b", case insensitive
  \?                    # literal ?
  (?P<atom>.*?)         # non-greedy up to the next ?= is the atom
  \?=                   # literal ?=
  c             C   sf   ddddddddg| d	  | d
 ddddddddddddg| d d  | d | d | d | d |f S )Nz"%s, %02d %s %04d %02d:%02d:%02d %sÚMonÚTueÚWedÚThuÚFriÚSatÚSuné   é   ÚJanÚFebÚMarÚAprÚMayÚJunÚJulÚAugÚSepÚOctÚNovÚDecé   r   é   é   é   r*   )Ú	timetupleÚzoner*   r*   r+   Ú_format_timetuple_and_zone…   s    r^   Fc       	      C   s”   | dkrt j ƒ } |rrt j| ƒ}t jr4|d r4t j}nt j}tt|ƒdƒ\}}|dkrZd}nd}d|||d f }nt j| ƒ}|r†d	}nd
}t||ƒS )aƒ  Returns a date string as specified by RFC 2822, e.g.:

    Fri, 09 Nov 2001 01:08:47 -0000

    Optional timeval if given is a floating point time value as accepted by
    gmtime() and localtime(), otherwise the current time is used.

    Optional localtime is a flag that when True, interprets timeval, and
    returns a date relative to the local timezone instead of UTC, properly
    taking daylight savings time into account.

    Optional argument usegmt means that the timezone is written out as
    an ascii string, not numeric one (so "GMT" instead of "+0000"). This
    is needed for HTTP, and is only used when localtime==False.
    NrX   i  r   ú-ú+z
%s%02d%02dé<   ÚGMTz-0000éÿÿÿÿ)	ÚtimeÚ	localtimeÚdaylightÚaltzoneÚtimezoneÚdivmodÚabsÚgmtimer^   )	Útimevalre   ÚusegmtÚnowÚoffsetÚhoursÚminutesÚsignr]   r*   r*   r+   r   Ž   s"    

c             C   sV   | j ƒ }|r2| jdks$| jtjjkr,tdƒ‚d}n| jdkrBd}n
| jdƒ}t||ƒS )a$  Turn a datetime into a date string as specified in RFC 2822.

    If usegmt is True, dt must be an aware datetime with an offset of zero.  In
    this case 'GMT' will be rendered instead of the normal +0000 required by
    RFC2822.  This is to support HTTP headers involving date stamps.
    Nz%usegmt option requires a UTC datetimerb   z-0000z%z)r\   Útzinfor   rh   ÚutcÚ
ValueErrorÚstrftimer^   )Údtrm   rn   r]   r*   r*   r+   r   »   s    

c             C   sh   t j ƒ }t jdt j|ƒƒ}tjƒ }tjdƒ}| dkr:d} nd|  } |dkrRtjƒ }d|||| |f }|S )an  Returns a string suitable for RFC 2822 compliant Message-ID, e.g:

    <20020201195627.33539.96671@nightshade.la.mastaler.com>

    Optional idstring if given is a string used to strengthen the
    uniqueness of the message id.  Optional domain if given provides the
    portion of the message id after the '@'.  It defaults to the locally
    defined hostname.
    z%Y%m%d%H%M%Si † Nr!   Ú.z<%s.%s.%s%s@%s>)	rd   rv   rk   ÚosÚgetpidÚrandomÚ	randrangeÚsocketÚgetfqdn)ÚidstringÚdomainrl   ZutcdateÚpidÚrandintÚmsgidr*   r*   r+   r   Î   s    

c             C   sj   t t| ƒƒ}|d d… g|dd …  \}}|d krDtj|d d… Ž S tj|d d… dtjtj|d�ƒiŽS )NrX   rJ   rs   )Úsecondsrc   rc   )Úlistr   r   rh   Ú	timedelta)ÚdataZ	_3to2listÚdtupleÚtzr*   r*   r+   r   æ   s    c             C   s   t | ƒj}|sdS |d S )Nr!   r   )r!   r!   )r>   r?   )ÚaddrÚaddrsr*   r*   r+   r   ï   s    
c             C   s`   t | ƒdkr\| jdƒr<| jdƒr<| dd… jddƒjddƒS | jdƒr\| jdƒr\| dd	… S | S )
zRemove quotes from a string.rX   r.   z\\ú\z\"ú<ú>rc   rc   )ÚlenÚ
startswithÚendswithr%   )r   r*   r*   r+   r   ÷   s    c             C   s&   | j tdƒ}t|ƒdkr"dd| fS |S )z#Decode string according to RFC 2231rK   N)ÚsplitÚTICKr�   )ÚsÚpartsr*   r*   r+   r     s    
c             C   s@   t | d|pdd�} |dkr&|dkr&| S |dkr2d}d||| f S )zÜEncode string according to RFC 2231.

    If neither charset nor language is given, then s is returned as-is.  If
    charset is given but not language, the string is encoded using the empty
    string for language.
    r!   r#   )ÚsafeÚencodingNz%s'%s'%s)Ú	url_quote)r”   r7   Úlanguager*   r*   r+   r     s    z&^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$c             C   st  | dd… } g }i }| j dƒ\}}|j||fƒ x�| rÀ| j dƒ\}}|jdƒrTd}nd}t|ƒ}tj|ƒ}|r¨|jddƒ\}}|dk	rŽt|ƒ}|j|g ƒj|||fƒ q2|j|dt	|ƒ fƒ q2W |�rpx¦|j
ƒ D ]š\}}g }d}	|jƒ  x2|D ]*\}}
}|�rt|
d	d
�}
d}	|j|
ƒ qðW t	tj|ƒƒ}|	�rZt|ƒ\}}}|j|||d| ffƒ qÒ|j|d| fƒ qÒW |S )zDecode parameters list according to RFC 2231.

    params is a sequence of 2-tuples containing (param name, string value).
    Nr   Ú*TFr8   Únumz"%s"zlatin-1)r—   )ÚpopÚappendr‘   r   Úrfc2231_continuationÚmatchÚgroupr   Ú
setdefaultr   ÚitemsÚsortÚurl_unquoteÚEMPTYSTRINGr=   r   )ÚparamsÚ
new_paramsÚrfc2231_paramsr8   ÚvalueÚencodedÚmor›   ÚcontinuationsÚextendedr”   r7   r™   r*   r*   r+   r
     sD    

r%   úus-asciic             C   s^   t | tƒ st| ƒdkr t| ƒS | \}}}t|dƒ}yt|||ƒS  tk
rX   t|ƒS X d S )NrY   zraw-unicode-escape)r0   Útupler�   r   r   r   ÚLookupError)r©   ÚerrorsÚfallback_charsetr7   r™   ÚtextÚrawbytesr*   r*   r+   r	   U  s    

rX   c       	      C   s  | dkrt j jt jjƒjƒ S | jdk	r.| jƒ S | jƒ dd… |f }tj|ƒ}tj	|ƒ}y t j
|jd�}t j||jƒ}W n€ tk
rø   | t j tj|ƒdd… Ž  }tjo²|jdk}|r¾tjntj }|t j
|d�krêt j|tj| ƒ}n
t j|ƒ}Y nX | j|d�S )aÅ  Return local time as an aware datetime object.

    If called without arguments, return current time.  Otherwise *dt*
    argument should be a datetime instance, and it is converted to the
    local time zone according to the system time zone database.  If *dt* is
    naive (that is, dt.tzinfo is None), it is assumed to be in local time.
    In this case, a positive or zero value for *isdst* causes localtime to
    presume initially that summer time (for example, Daylight Saving Time)
    is or is not (respectively) in effect for the specified time.  A
    negative value for *isdst* causes the localtime() function to attempt
    to divine whether summer time is in effect for the specified time.

    NrX   )r„   rJ   r   )rs   rc   )r   rn   rh   rt   Ú
astimezoners   r\   rd   Úmktimere   r†   Ú	tm_gmtoffÚtm_zoneÚAttributeErrorrk   rf   Útm_isdstrg   Útznamer%   )	rw   ÚisdstÚtmr„   ÚlocaltmÚdeltar‰   ÚdstÚgmtoffr*   r*   r+   re   k  s$    


re   )r-   )NFF)F)NN)NN)r%   r®   rc   )Nrc   )IÚ__doc__Ú
__future__r   r   r   Úfuturer   Zfuture.builtinsr   r   r   Ú__all__ry   ÚreÚPY2ÚASCIIrd   Úbase64r{   r}   Zfuture.backportsr   Zfuture.backports.urllib.parser   r˜   r   r¤   ÚwarningsÚior   Z!future.backports.email._parseaddrr   r>   r   r   r   r   Úquoprir   Z_qdecodeZfuture.backports.email.encodersr   r   Zfuture.backports.email.charsetr    r<   r¥   ÚUEMPTYSTRINGÚCRLFr“   Úcompiler2   r4   r3   Ú_has_surrogatesr,   r   r   ÚVERBOSEÚ
IGNORECASEÚecrer^   r   r   r   r   r   r   r   rž   r
   r	   re   r*   r*   r*   r+   Ú<module>   sˆ   


"	
-

	
8 
