a
    W%b7                     @   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
 g dZddlZddlZejrld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2dZ3e2dZ4e2dj5Z6dd Z7d?ddZ8dd  Z9e2d!ej:ej;B Z<d"d# Z=d@d%d&Z>dAd'd(Z?dBd)d*Z@d+d, ZAd-d. ZBd/d0 Zd1d2 ZCdCd3d4ZDe2d5ejZEd6d7 ZFdDd:d;ZGdEd=d>ZHdS )Fz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   |  dd}|ddS )Nasciisurrogateescapereplace)encodedecode)stringoriginal_bytes r*   l/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/future/backports/email/utils.py	_sanitizeH   s    r,   utf-8c                 C   s   | \}}| d |rz| d W n: ty^   t|trDt|}||}d||f  Y S 0 d}t|rrd}t	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     



r   c                 C   s   t | }t|}|jS )z7Return a list of (REALNAME, EMAIL) for each fieldvalue.)
COMMASPACEjoin_AddressListaddresslist)fieldvaluesallar*   r*   r+   r   r   s    
r   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   sH   dg d| d  | d g d| 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u rt   } |rrt | }t jr4|d r4t j}nt j}tt|d\}}|dkrZd}nd}d|||d f }nt | }|rd	}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.
    Ni  r   -+z
%s%02d%02d<   GMT-0000)	time	localtimedaylightaltzonetimezonedivmodabsgmtimer^   )	timevalrf   usegmtnowoffsethoursminutessignr]   r*   r*   r+   r      s"    

r   c                 C   sV   |   }|r2| jdu s$| jtjjkr,tdd}n| jdu rBd}n
| 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 datetimerc   rd   z%z)r\   tzinfor   ri   utc
ValueErrorstrftimer^   )dtrn   ro   r]   r*   r*   r+   r      s    

r   c                 C   sh   t   }t dt |}t }td}| du r:d} nd|  } |du rRt }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>)	re   rw   rl   osgetpidrandom	randrangesocketgetfqdn)idstringdomainrm   Zutcdatepidrandintmsgidr*   r*   r+   r      s    

r   c                 C   sj   t t| }|d d g|dd   \}}|d u rDtj|d d  S tj|d d dttj|diS )Nr_   rJ   rt   seconds)listr   r   ri   	timedelta)dataZ	_3to2listdtupletzr*   r*   r+   r      s    r   c                 C   s   t | j}|sdS |d S )N)r!   r!   r   )r>   r?   )addraddrsr*   r*   r+   r      s    
r   c                 C   s`   t | dkr\| dr<| dr<| dd ddddS | dr\| dr\| dd S | S )	zRemove quotes from a string.rX   r.   r_   z\\\z\"<>)len
startswithendswithr%   )r   r*   r*   r+   r      s    r   c                 C   s&   |  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    
r   c                 C   s@   t | d|pdd} |du r&|du r&| S |du r2d}d||| f S )zEncode 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    r   z&^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$c                 C   sh  | dd } g }i }|  d\}}|||f | r|  d\}}|drRd}nd}t|}t|}|r|dd\}}|durt|}||g |||f q0||dt	| f q0|rd|
 D ]\}}g }d}	|  |D ]*\}}
}|rt|
d	d
}
d}	||
 qt	t|}|	rPt|\}}}||||d| ff q||d| f q|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
   r%   us-asciic                 C   s`   t | trt| dkrt| S | \}}}t|d}zt|||W S  tyZ   t| Y S 0 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    

r	   r_   c           	      C   s  | du rt j t jj S | jdur.|  S |  dd |f }t|}t	|}z t j
|jd}t ||j}W n~ ty   | t j t|dd   }tjo|jdk}|rtjntj }|t j
|dkrt |tj| }n
t |}Y n0 | 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.

    Nr_   r   rJ   r   )rt   )r   ro   ri   ru   
astimezonert   r\   re   mktimerf   r   	tm_gmtofftm_zoneAttributeErrorrl   rg   tm_isdstrh   tznamer%   )	rx   isdsttmr   localtmdeltar   dstgmtoffr*   r*   r+   rf   k  s$    


rf   )r-   )NFF)F)NN)NN)r%   r   )Nr_   )I__doc__
__future__r   r   r   futurer   Zfuture.builtinsr   r   r   __all__rz   rePY2ASCIIre   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	   rf   r*   r*   r*   r+   <module>   st   


"
	
-

	
8  
