\dn4dZgdZddlZddlZddlZddlZddlZddlZddlZ ddl m Z ddl m Z ddl mZddl mZmZmZddlmZd Zd Zd Zd Zd Zejd ZejdZdZdZd%dZdZdZ d&dZ!d'dZ"d(dZ#dZ$dZ%dZ&dZ'd(dZ(ejdej)Z*dZ+ d)d"Z,d*d$Z-dS)+zMiscellaneous utilities.)collapse_rfc2231_value decode_paramsdecode_rfc2231encode_rfc2231 formataddr formatdateformat_datetime getaddresses make_msgid mktime_tz parseaddr parsedate parsedate_tzparsedate_to_datetimeunquoteN)quote) AddressList)r )r r _parsedate_tz)Charsetz, z 'z[][\\()<>@,:;".]z[\\"]cR |dS#t$rYdSwxYw)z8Return True if s contains surrogate-escaped binary data.FT)encodeUnicodeEncodeError)ss ..\python\lib\email\utils.py_has_surrogatesr3s;   u tts  &&cZ|dd}|ddS)Nutf-8surrogateescapereplace)rdecode)stringoriginal_bytess r _sanitizer%@s. ]]7,=>>N  ) 4 44rc|\}}|d|r |dd}t|rd}td|}|||d|dS#t $rDt |trt|}| |}|d|dcYSwxYw|S)aThe 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. The optional charset 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'. asciir"z\\\g<0>z <>) r specialsresearch escapesresubr isinstancestrr header_encode)paircharsetnameaddressquotes encoded_names rrrLsMD' NN7 C C KK F  && ==T22D$*FDD&&&'''B B" 7 7 7'3'' +!'**"0066L , ggg6 6 6 6  7 NsA88A CCcvtd|D}t|}|jS)z7Return a list of (REALNAME, EMAIL) for each fieldvalue.c34K|]}t|VdS)Nr0).0vs r zgetaddresses..ps(66Q#a&&666666r&) COMMASPACEjoin _AddressList addresslist) fieldvaluesallas rr r ns7 //66+666 6 6CSA =r&c dgd|d|dgd|ddz |d|d|d |d |fzS) Nz"%s, %02d %s %04d %02d:%02d:%02d %s)MonTueWedThuFriSatSun) JanFebMarAprMayJunJulAugSepOctNovDecr) timetuplezones r_format_timetuple_and_zonerbusj /999)A,G!  3 3 33-B B BDEE E  {{4   %c4 0 00r&cttjdz}tj}t jd}|d}nd|z}|t j}d|||||fz}|S)a{Returns a string suitable for RFC 2822 compliant Message-ID, e.g: <142480216486.20800.16526388040877946887@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. d@Nr.z<%d.%d.%d%s@%s>)intrdosgetpidrandom getrandbitssocketgetfqdn)idstringdomainrkpidrandintmsgids rr r sx$)++c/""G )++C $$G> ~!! #w& I IE Lr&c t|}|tdt|z|^}}|tj|ddStj|dddtjtj|iS)Nz!Invalid date value or format "%s"rMrqseconds)rrrr0rerg timedelta)dataparsed_date_tzdtupletzs rrrs"4((N>#   G3<<#4#4 Gqt9$$VT22::5#FF F >>#   3<<#4#4 qt9  Jr&cl|td}t|dkrdd|fS|S)z#Decode string according to RFC 2231rNN)splitTICKr)rpartss rrrs5 GGD!  E 5zzQT1} Lr&crtj|d|pd}|||S|d}|d|d|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. rr()safeencodingNr)urllibparser)rr3languages rrrsV  120B7CCA8+ (((AA ..r&z&^(?P\w+)\*((?P[0-9]+)\*?)?$c|dg}i}|ddD]\}}|d}t|}t|}|rW|dd\}}|t |}||g|||f||dt|zf|r| D]\}}g}d} | |D]@\}} }|r#tj | d } d } || Att|}| r1t|\} } }||| | d|zff||d|zf|S) zDecode parameters list according to RFC 2231. params is a sequence of 2-tuples containing (param name, string value). rr[N*r4numz"%s"Fzlatin-1)rT)rrrfc2231_continuationmatchgroupry setdefaultappendritemssortrr EMPTYSTRINGr?r) params new_paramsrfc2231_paramsr4valueencodedmor continuationsextendedrr3rs rrrs )JNabbz = = e--$$ ! ' ' - -  =//ID##hh  % %dB / / 6 6UG7L M M M M   tVeEll%:; < < < <:#1#7#7#9#9 : : D-EH    $1  Q$ ,,Q,CCA#H Q+**51122E :+9%+@+@(5!!4'8Ve^)L"MNNNN!!4%"89999 r&r!us-asciict|trt|dkrt|S|\}}}||}t |d} t |||S#t $rt|cYSwxYw)Nr\zraw-unicode-escape)r/tuplerrbytesr0 LookupError)rerrorsfallback_charsetr3rtextrawbytess rrr4s eU # #s5zzQu~~$GXt#T/00H8Wf--- t}}sA$$B?Brcd|@tjtjjS|j|S|dd|fz}tj|}tj |} tj |j }tj||j }n#t$r|tjtj|ddz }tjo |jdk}|r tjn tj }|tj |kr&tj|tj|}ntj|}YnwxYw||S)aReturn 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. NrrrMr)rq)rertrgrhrjrqr`rdmktimerlr tm_gmtofftm_zoneAttributeErrorgmtimedaylighttm_isdstaltzonetznamer!) rnisdsttmrlocaltmdeltardstgmtoffs rrlrlNs{ z $$X%6%:;;FFHHH y}}  x 'Bk"ooGnW%%G *"7+<===  ugo 6 6  * * *X& G(<(r UEMPTYSTRINGCRLFrcompiler+r-rr%rr rbrrr rr rrrASCIIrrrrlr_r&rrsN    $ """"""888888&&&&&&CCCCCCCCCC"!!!!!       RZ+ , , BJx    555D'''':1111&.FFF    / / / /"rz"KH...`*3,64%!%!%!%!%!%!r&