o ,]P@sdZddlmZddlmZddlmZddlmZddlmZddlm Z ddl m Z dd l m Z dd l m ZGd d d eZGd ddeZGdddeZddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGd!d"d"eZGd#d$d$eZGd%d&d&eZGd'd(d(eZGd)d*d*eZGd+d,d,eZ id-ed.ed/ed0ed1ed2ed3ed4ed5ed6ed7ed8ed9ed:ed;edZ!Gd?d@d@eZ"dAS)Ba;Representing and manipulating email headers via custom objects. This module provides an implementation of the HeaderRegistry API. The implementation is designed to flexibly follow RFC5322 rules. Eventually HeaderRegistry will be a public API, but it isn't yet, and will probably change some before that happens. )unicode_literals)division)absolute_import)superstr)text_to_native_str)utils)errors)_header_value_parserc@s^eZdZdddZeddZeddZed d Zed d Zd dZ ddZ ddZ dS)AddressNcCsl|dur+|s|r tdt|\}}|rtd|||jr%|jd|j}|j}||_||_ ||_ dS)aCreate an object represeting a full email address. An address can have a 'display_name', a 'username', and a 'domain'. In addition to specifying the username and domain separately, they may be specified together by using the addr_spec keyword *instead of* the username and domain keywords. If an addr_spec string is specified it must be properly quoted according to RFC 5322 rules; an error will be raised if it is not. An Address object has display_name, username, domain, and addr_spec attributes, all of which are read-only. The addr_spec and the string value of the object are both quoted according to RFC5322 rules, but without any Content Transfer Encoding. Nz=addrspec specified when username and/or domain also specifiedz6Invalid addr_spec; only '{}' could be parsed from '{}'r) TypeErrorparser get_addr_spec ValueErrorformat all_defects local_partdomain _display_name _username_domain)self display_nameusernamer addr_speca_srestrG/usr/lib/python3/dist-packages/future/backports/email/headerregistry.py__init__s"  zAddress.__init__cC|jSNrrrrr r<zAddress.display_namecCr"r#)rr%rrr r@r&zAddress.usernamecCr"r#)rr%rrr rDr&zAddress.domaincCsTt|j}t|t|tjkrt|j}n|j}|jr$|d|jS|s(dS|S)zThe addr_spec (username@domain) portion of the address, quoted according to RFC 5322 rules, but with no Content Transfer Encoding. @<>)setrlenr DOT_ATOM_ENDS quote_stringr)rnamesetlprrr rHs zAddress.addr_speccCsd|j|j|jS)Nz6Address(display_name={!r}, username={!r}, domain={!r}))rrrrr%rrr __repr__Xs zAddress.__repr__cCs^t|j}t|t|tjkrt|j}n|j}|r,|jdkr#dn|j}d||S|jS)Nr(r z{} <{}>)r)rr*rSPECIALSr,rr)rr-disprrrr __str__\s  zAddress.__str__cCs8t|t|kr dS|j|jko|j|jko|j|jkSNF)typerrrrotherrrr __eq__gs   zAddress.__eq__)r r r N) __name__ __module__ __qualname__r!propertyrrrrr/r2r7rrrr r s %     r c@sFeZdZdddZeddZeddZdd Zd d Zd d Z dS)GroupNcCs$||_|r t||_dSt|_dS)aCreate an object representing an address group. An address group consists of a display_name followed by colon and an list of addresses (see Address) terminated by a semi-colon. The Group is created by specifying a display_name and a possibly empty list of Address objects. A Group can also be used to represent a single address that is not in a group, which is convenient when manipulating lists that are a combination of Groups and individual Addresses. In this case the display_name should be set to None. In particular, the string representation of a Group whose display_name is None is the same as the Address object, if there is one and only one Address object in the addresses list. N)rtuple _addresses)rr addressesrrr r!qszGroup.__init__cCr"r#r$r%rrr rr&zGroup.display_namecCr"r#)r>r%rrr r?r&zGroup.addressescCsd|j|jS)Nz'Group(display_name={!r}, addresses={!r})rrr?r%rrr r/szGroup.__repr__cCs|jdurt|jdkrt|jdS|j}|dur.t|}t|t|tjkr.t|}ddd|jD}|r?d|n|}d ||S)Nr, css|]}t|VqdSr#r).0xrrr sz Group.__str__.. z{}:{};) rr*r?rr)rr0r,joinr)rr1r-adrstrrrr r2s  z Group.__str__cCs,t|t|kr dS|j|jko|j|jkSr3)r4rr?r5rrr r7s   z Group.__eq__)NN) r8r9r:r!r;rr?r/r2r7rrrr r<os    r<c@sTeZdZdZddZddZeddZedd Zd d Z e d d Z ddZ dS) BaseHeadera|Base class for message headers. Implements generic behavior and provides tools for subclasses. A subclass must define a classmethod named 'parse' that takes an unfolded value string and a dictionary as its arguments. The dictionary will contain one key, 'defects', initialized to an empty list. After the call the dictionary must contain two additional keys: parse_tree, set to the parse tree obtained from parsing the header, and 'decoded', set to the string value of the idealized representation of the data from the value. (That is, encoded words are decoded, and values that have canonical representations are so represented.) The defects key is intended to collect parsing defects, which the message parser will subsequently dispose of as appropriate. The parser should not, insofar as practical, raise any errors. Defects should be added to the list instead. The standard header parsers register defects for RFC compliance issues, for obsolete RFC syntax, and for unrecoverable parsing errors. The parse method may add additional keys to the dictionary. In this case the subclass must define an 'init' method, which will be passed the dictionary as its keyword arguments. The method should use (usually by setting them as the value of similarly named attributes) and remove all the extra keys added by its parse method, and then use super to call its parent class with the remaining arguments and keywords. The subclass should also make sure that a 'max_count' attribute is defined that is either None or 1. XXX: need to better define this API. cCsZdgi}|||t|drt|d|d<t||d}|j|fi||S)Ndefectsdecoded)parser _has_surrogates _sanitizer__new__init)clsnamevaluekwdsrrrr rNs zBaseHeader.__new__cKs2|d}|d=|d}|d=||_||_||_dS)NrI parse_tree)_name _parse_tree_defects)rrQ _3to2kwargsrIrTrrr rOs  zBaseHeader.initcCr"r#)rUr%rrr rQr&zBaseHeader.namecCs t|jSr#)r=rWr%rrr rI zBaseHeader.defectscCst|jj|jjt|f|jfSr#)_reconstruct_header __class__r8 __bases__r__dict__r%rrr __reduce__szBaseHeader.__reduce__cCs t||Sr#)rrN)rPrRrrr _reconstructs zBaseHeader._reconstructc KsZ|d}|d= ttt|jdtddgttddg|jg}|j|dS)Npolicyz header-name:z header-seprEfws)r`) rHeader HeaderLabel ValueTerminalrQCFWSListWhiteSpaceTerminalrVfold)rrXr`headerrrr rhs   zBaseHeader.foldN) r8r9r:__doc__rNrOr;rQrIr^ classmethodr_rhrrrr rHs      rHcCstt||i|Sr#)r4rr_)cls_namebasesrRrrr rZsrZc@s&eZdZdZeejZeddZ dS)UnstructuredHeaderNcCs"|||d<t|d|d<dS)NrTrJ) value_parserrrPrRrSrrr rKszUnstructuredHeader.parse) r8r9r: max_count staticmethodrget_unstructuredrorkrKrrrr rn s  rnc@eZdZdZdS)UniqueUnstructuredHeaderr@Nr8r9r:rqrrrr rurucsFeZdZdZdZeejZe ddZ fddZ e ddZ ZS) DateHeaderaHeader whose value consists of a single timestamp. Provides an additional attribute, datetime, which is either an aware datetime using a timezone, or a naive datetime if the timezone in the input string is -0000. Also accepts a datetime as input. The 'value' attribute is the normalized form of the timestamp, which means it is the output of format_datetime on the datetime. NcCsz|s|dtd|d<d|d<t|d<dSt|tr%t|}||d<t |d|d<| |d|d<dS)NrIdatetimer rJrT) appendr HeaderMissingRequiredValuer TokenList isinstancerr parsedate_to_datetimeformat_datetimerorprrr rK,s   zDateHeader.parsec"|d|_tj|i|dS)Nry)pop _datetimerrOrargskwr[rr rO: zDateHeader.initcCr"r#)rr%rrr ry>r&zDateHeader.datetime)r8r9r:rjrqrrrrsrorkrKrOr;ry __classcell__rrrr rxs   rxc@rt)UniqueDateHeaderr@Nrvrrrr rCrwrcsPeZdZdZeddZeddZfddZe dd Z e d d Z Z S) AddressHeaderNcCst|\}}|r Jd|S)Nzthis should not happen)rget_address_list)rR address_listrrr roLs zAddressHeader.value_parsercCst|tr+|||d<}g}|jD]}|t|jdd|jDqt|j }nt |ds3|g}dd|D}g}||d<||d<d d d|D|d <d|vr_||d |d<dSdS) NrTcSs*g|]}t|jp d|jp d|jpdqS)r )r rrr)rBmbrrr [s  z'AddressHeader.parse..__iter__cSs&g|]}t|dstd|gn|qS)r?N)hasattrr<rBitemrrr rds groupsrIrAcSsg|]}t|qSrrrrrr rjsrJ) r}rror?rzr<r all_mailboxeslistrrrF)rPrRrSrraddrrIrrr rKRs,     zAddressHeader.parsecs,t|d|_d|_tj|i|dS)Nr)r=r_groupsr>rrOrrrr rOnszAddressHeader.initcCr"r#)rr%rrr rsr&zAddressHeader.groupscCs&|jdurtdd|jD|_|jS)NcSsg|] }|jD]}|qqSr)r?)rBgroupaddressrrr rzs  z+AddressHeader.addresses..)r>r=rr%rrr r?ws zAddressHeader.addresses) r8r9r:rqrrrorkrKrOr;rr?rrrrr rHs    rc@rt)UniqueAddressHeaderr@Nrvrrrr rrwrc@seZdZeddZdS)SingleAddressHeadercCs(t|jdkrtd|j|jdS)Nr@z9value of single address header {} is not a single addressr)r*r?rrrQr%rrr rs  zSingleAddressHeader.addressN)r8r9r:r;rrrrr rsrc@rt)UniqueSingleAddressHeaderr@Nrvrrrr rrwrcsZeZdZdZeejZeddZ fddZ e ddZ e dd Z e d d ZZS) MIMEVersionHeaderr@cCs|||d<}t||d<|d|j|jdurdn|j|d<|j|d<|jdur;d|d|d|d<dSd|d<dS)NrTrJrImajorminorz{}.{}version)rorextendrrrrrPrRrSrTrrr rKs    zMIMEVersionHeader.parsecs:|d|_|d|_|d|_tj|i|dS)Nrrr)r_version_major_minorrrOrrrr rOs   zMIMEVersionHeader.initcCr"r#)rr%rrr rr&zMIMEVersionHeader.majorcCr"r#)rr%rrr rr&zMIMEVersionHeader.minorcCr"r#)rr%rrr rr&zMIMEVersionHeader.version)r8r9r:rqrrrparse_mime_versionrorkrKrOr;rrrrrrrr rs     rcs8eZdZdZeddZfddZeddZZ S)ParameterizedMIMEHeaderr@cCs`|||d<}t||d<|d|j|jdur"i|d<dStdd|jD|d<dS)NrTrJrIparamscss,|]\}}t|t|fVqdSr#)r rMlower)rBrQrRrrr rDs   z0ParameterizedMIMEHeader.parse..)rorrrrdictrrrr rKs   zParameterizedMIMEHeader.parsecr)Nr)r_paramsrrOrrrr rOrzParameterizedMIMEHeader.initcCs |jSr#)rcopyr%rrr rrYzParameterizedMIMEHeader.params) r8r9r:rqrkrKrOr;rrrrrr rs  rcsJeZdZeejZfddZeddZ eddZ eddZ Z S) ContentTypeHeadercs6tj|i|t|jj|_t|jj|_dSr#) rrOr rMrVmaintype _maintypesubtype_subtyperrrr rOszContentTypeHeader.initcCr"r#)rr%rrr rr&zContentTypeHeader.maintypecCr"r#)rr%rrr rr&zContentTypeHeader.subtypecCs|jd|jS)N/)rrr%rrr content_typeszContentTypeHeader.content_type) r8r9r:rrrparse_content_type_headerrorOr;rrrrrrrr rs    rcs2eZdZeejZfddZeddZ Z S)ContentDispositionHeadercs<tj|i||jj}|dur||_dSt||_dSr#)rrOrVcontent_dispositionr rM_content_disposition)rrrcdrrr rOs"zContentDispositionHeader.initcCr"r#)rr%rrr rr&z,ContentDispositionHeader.content_disposition) r8r9r:rrr parse_content_disposition_headerrorOr;rrrrrr rs   rcsBeZdZdZeejZeddZ fddZ e ddZ Z S)ContentTransferEncodingHeaderr@cCs2|||d<}t||d<|d|jdS)NrTrJrI)rorrrrrrr rKs z#ContentTransferEncodingHeader.parsecs&tj|i|t|jj|_dSr#)rrOr rMrVcte_cterrrr rOsz"ContentTransferEncodingHeader.initcCr"r#)rr%rrr r r&z!ContentTransferEncodingHeader.cte)r8r9r:rqrrr&parse_content_transfer_encoding_headerrorkrKrOr;rrrrrr rs   rsubjectdatez resent-datez orig-datesenderz resent-sendertoz resent-toccz resent-ccbccz resent-bccfromz resent-fromzreply-toz mime-versionz content-type)zcontent-dispositionzcontent-transfer-encodingc@s8eZdZdZeedfddZddZddZd d Z d S) HeaderRegistryz%A header_factory and header registry.TcCs*i|_||_||_|r|jtdSdS)aCreate a header_factory that works with the Policy API. base_class is the class that will be the last class in the created header class's __bases__ list. default_class is the class that will be used if "name" (see __call__) does not appear in the registry. use_default_map controls whether or not the default mapping of names to specialized classes is copied in to the registry when the factory is created. The default is True. N)registry base_class default_classupdate_default_header_map)rrruse_default_maprrr r!*s  zHeaderRegistry.__init__cCs||j|<dS)zLRegister cls as the specialized class for handling "name" headers. N)rrrrQrPrrr map_to_type<szHeaderRegistry.map_to_typecCs0|j||j}ttd|j||jfiS)N_)rgetrrr4rr8rrrrr __getitem__BszHeaderRegistry.__getitem__cCs||||S)aCreate a header instance for header 'name' from 'value'. Creates a header instance by creating a specialized class for parsing and representing the specified header by combining the factory base_class with a specialized class from the registry or the default_class, and passing the name and value to the constructed class's constructor. r)rrQrRrrr __call__Fs zHeaderRegistry.__call__N) r8r9r:rjrHrnr!rrrrrrr r&s  rN)#rj __future__rrrfuture.builtinsrr future.utilsrfuture.backports.emailr r r robjectr r<rHrZrnrurxrrrrrrrrrrrrrrrr s        Z5d '7 %