U Zlx@sddlZddlmZmZddlmZddlmZmZm Z m Z m Z ddl m Z mZddlZddlmZmZdZd Zd d d d d ZeZGd ddZGdddZdS)N)MappingSequence) ip_address) SplitResult parse_qslurljoinurlsplit urlunsplit) MultiDictMultiDictProxy)_Quoter _Unquoterz1.1.1)URLPi)ZhttpZhttpsZwsZwssc@s,eZdZdZddZefddZddZdS) cached_propertyaRUse as a class method decorator. It operates almost exactly like the Python `@property` decorator, but it puts the result of the method it decorates into the instance dict after the first call, effectively replacing the function it decorates with an instance variable. It is, in Python parlance, a data descriptor. cCs:||_z |j|_Wntk r,d|_YnX|j|_dS)N)wrapped__doc__AttributeError__name__name)selfrr /__init__.py__init__(s   zcached_property.__init__cCsB|dkr |S|j|j|}||k r(|S||}||j|j<|SN)_cachegetrr)rinstownerZ _sentinelvalrrr__get__0s  zcached_property.__get__cCs tddS)Nzcached property is read-only)r)rrvaluerrr__set__:szcached_property.__set__N)r __module__ __qualname__rrsentinelr"r$rrrrrs rc @seZdZdZeZedddZeddddZeddd Zedd Z e Z e d d Z e dd Z ddddddZeddddddddddd ddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Ze d5d6Z!e d7d8Z"e#d9d:Z$e d;d<Z%e#d=d>Z&e d?d@Z'e#dAdBZ(e dCdDZ)e dEdFZ*e#dGdHZ+e#dIdJZ,e dKdLZ-e#dMdNZ.e#dOdPZ/e#dQdRZ0e dSdTZ1e#dUdVZ2e#dWdXZ3e#dYdZZ4e#d[d\Z5e#d]d^Z6e#d_d`Z7edadbZ8edcddZ9ededfZ:dgdhZ;didjZdodpZ?ddqdrdsZ@dtduZAdvdwZBdxdyZCdzd{ZDd|d}ZEd~dZFddZGdS)r)r_valz@:z/+)safe protectedz?/:@z=+&;T)r)r*qs)r)r+)r)+)Zunsafe)r+rFN)encodedstrictc Cs|dk rtdt|tr0|j|_|j|_dSt|trDt|}n t|tr\|sdt dnt d|s|dsvd}n0|j }|dkrt d|j |j |j||jdd}||d }|r||}||d }||d }t|d ||||}||_i|_dS) Nzstrict parameter is ignoredz$Cannot apply decoding to SplitResultz#Constructor parameter should be strr rz/Invalid URL: host is required for abolute urls.Tencoder)warningswarn isinstancerr(rstrrr ValueError TypeErrorhostname _make_netlocusernamepasswordport _PATH_QUOTER_normalize_path _QUERY_QUOTER_FRAGMENT_QUOTER) rr!r-r.netlochostpathqueryfragmentrrrrsD       z URL.__init__) schemeuserr=rDr>rErF query_stringrGr-c Cs|r|std|s |r td|r0|r0td|sF|sF|sF|sFd} n|j||||| d} | s||}| rx||}||}|| } |t|| ||| dd} |r| |S| SdS) zCreates and returns a new URLz1Can't build URL with "host" but without "scheme".z1Can't build URL with "scheme" but without "host".z6Only one of "query" or "query_string" should be passedrr/Tr-N)r8r;r?r@rArBr with_query) clsrHrIr=rDr>rErFrJrGr-rCurlrrrbuildsH       z URL.buildcCs4|j}|js,|r,|js |jr,|jdd}t|SN/rE)r(rE is_absoluterFrG_replacer rr!rrr__str__s z URL.__str__cCsd|jjt|S)Nz{}('{}'))format __class__rr7rrrr__repr__sz URL.__repr__cCsVt|tstS|j}|js.|r.|jdd}|j}|jsN|rN|jdd}||kSrP)r6rNotImplementedr(rErSrT)rotherZval1Zval2rrr__eq__s   z URL.__eq__cCsJ|jd}|dkrF|j}|js4|r4|jdd}t|}|jd<|S)NhashrQrR)rrr(rErSrTr^)rretr!rrr__hash__s  z URL.__hash__cCst|tstS|j|jkSrr6rr[r(rr\rrr__le__s z URL.__le__cCst|tstS|j|jkSrrarbrrr__lt__ s z URL.__lt__cCst|tstS|j|jkSrrarbrrr__ge__s z URL.__ge__cCst|tstS|j|jkSrrarbrrr__gt__s z URL.__gt__cCs||}|drtd|jj}|dkr6d|}n6|sH|sH|}n$|dd}||d |}|r~| |}t |jj |dddddS)NrQz/Appending path starting from slash is forbiddenrrErFrGTrK) r? startswithr8r(rErSrstripsplitappendjoinr@rrT)rrrEnew_pathpartsrrr __truediv__s        zURL.__truediv__cCs|jfSr)r(rYrrr __getstate__+szURL.__getstate__cCs>|ddkr*t|dtr*|dd|_n |^|_}i|_dS)Nrr r()r6dictr(r)rstateZunusedrrr __setstate__.s zURL.__setstate__cCs |jdk S)zA check for absolute URLs. Return True for absolute ones (having scheme or starting with //), False otherwise. N)raw_hostrYrrrrS6szURL.is_absolutecCs0|jdkrdSt|j}|dkr&dS|j|kS)zA check for default port. Return True if port is default for specified scheme, e.g. 'http://python.org' or 'http://python.org:80', False otherwise. NF)r> DEFAULT_PORTSrrH)rdefaultrrris_default_port?s   zURL.is_default_portcCs\|std|jjs td|j}|jdd|j|jdd}|j|dddd}t|dd S) z}Return an URL with scheme, host and port parts only. user, password, path, query and fragment are removed. URL should be absolutezURL should have schemeNFr/r)rCrErFrGTrK) rSr8r(rHr;r:r>rTr)rvrCr!rrroriginNsz URL.origincCs,|std|jjddd}t|ddS)zhReturn a relative part of the URL. scheme, user, password, host and port are removed. rxr)rHrCTrK)rSr8r(rTrrUrrrrelative_sz URL.relativecCs|jjS)zdScheme for absolute URLs. Empty string for relative URLs or URLs starting with // )r(rHrYrrrrHjsz URL.schemecCs|jj}|sdS|S)zEEncoded user part of URL. None if user is missing. N)r(r<rr_rrrraw_userssz URL.raw_usercCs ||jS)zEDecoded user part of URL. None if user is missing. ) _UNQUOTERr}rYrrrrIszURL.usercCs|jjS)zMEncoded password part of URL. None if password is missing. )r(r=rYrrr raw_passwordszURL.raw_passwordcCs ||jS)zMDecoded password part of URL. None if password is missing. )r~rrYrrrr=sz URL.passwordcCs|jjS)zDEncoded host part of URL. None for relative URLs. )r(r:rYrrrrts z URL.raw_hostcCsZ|j}|dkrdSd|kr|Szt|dWStk rT|ddYSXdS)zDDecoded host part of URL. None for relative URLs. N%asciiidna)rtrdecoder0 UnicodeError)rrawrrrrDszURL.hostcCs|jjpt|jjS)zPort part of URL. None for relative URLs or URLs without explicit port and scheme without default port substitution. )r(r>rurrHrYrrrr>szURL.portcCs|jj}|s|rd}|S)zNEncoded path of URL. / for absolute URLs without path part. rQ)r(rErSr|rrrraw_paths z URL.raw_pathcCs ||jS)zNDecoded path of URL. / for absolute URLs without path part. )_PATH_UNQUOTERrrYrrrrEszURL.pathcCstt|jdd}t|S)zA MultiDictProxy representing parsed query parameters in decoded representation. Empty value if URL has no query part. TZkeep_blank_values)r rraw_query_stringr r|rrrrFsz URL.querycCs|jjS)zOEncoded query part of URL. Empty string if query is missing. )r(rFrYrrrrszURL.raw_query_stringcCs ||jS)zODecoded query part of URL. Empty string if query is missing. ) _QS_UNQUOTERrrYrrrrJszURL.query_stringcCs|js |jSd|j|jS)zDecoded path of URL with query.{}?{})rJrErWrYrrrpath_qssz URL.path_qscCs|js |jSd|j|jS)zEncoded path of URL with query.r)rrrWrYrrr raw_path_qsszURL.raw_path_qscCs|jjS)zUEncoded fragment part of URL. Empty string if fragment is missing. )r(rGrYrrr raw_fragmentszURL.raw_fragmentcCs ||jS)zUDecoded fragment part of URL. Empty string if fragment is missing. )r~rrYrrrrGsz URL.fragmentcCsl|jj}|r6|sdg}qddg|ddd}n.|drZdg|ddd}n |d}t|S)zjA tuple containing encoded *path* parts. ('/',) for absolute URLs if *path* is missing. rQr N)r(rErSrjrhtuple)rrErnrrr raw_partss  z URL.raw_partscstfddjDS)zjA tuple containing decoded *path* parts. ('/',) for absolute URLs if *path* is missing. c3s|]}|VqdSr)r~).0partrYrr 3szURL.parts..)rrrYrrYrrn,sz URL.partscCsp|j}|r|dkr:|js|jr6t|jjdddddS|S|d}|jjd|ddddd}t|ddS) z]A new URL with last part of path removed and cleaned up query and fragment. rQr)rFrGTrKNrg)rrrrr(rTrjrl)rrErnr!rrrparent5s   z URL.parentcCs8|j}|r,|dd}|s"dS|dSn|dSdS)zThe last part of raw_parts.r Nrr)rrS)rrnrrrraw_nameFs  z URL.raw_namecCs ||jS)zThe last part of parts.)r~rrYrrrrSszURL.namec Csz|d}g}|D]F}|dkrBz |WqXtk r>YqXXq|dkrNqq||q|ddkrp|dd|S)NrQ...rrrr)rjpop IndexErrorrkrl)rMrEZsegmentsZ resolved_pathZsegrrrr@Xs     zURL._normalize_pathc Csz|d\}}}t|}WnRtk rnztj|ddd}Wn$tk rh|dd}YnXYn.X|j}|r|d|7}|jdkrd|d}|S) NrT)Zuts46rr[]) partitionrr8rr0rrZ compressedversion)rMrDZipsepZzonerrr _encode_hostus    zURL._encode_hostcCs|r||}n|}|r(|dt|}|r`|s6d}n|rD||}|rR||}|d|}n|rr|rr||}|r|d|}|S)N:r@)rr7_QUOTER)rMrIr=rDr>r0r_rrrr;s$     zURL._make_netloccCs<t|tstd|s"tdt|jj|dddS)z&Return a new URL with scheme replaced.zInvalid scheme typez3scheme replacement is not allowed for relative URLs)rHTrK) r6r7r9rSr8rr(rTlower)rrHrrr with_schemes  zURL.with_schemec Csr|j}|dkrd}n$t|tr0||}|j}ntd|sHtdt|jj |j |||j |j dddddS) zReturn a new URL with user replaced. Autoencode user if needed. Clear user/password if user is None. NzInvalid user typez1user replacement is not allowed for relative URLsFr/rCTrK) r(r6r7rr=r9rSr8rrTr;r:r>)rrIr!r=rrr with_users"   z URL.with_userc Csj|dkr nt|tr ||}ntd|s8td|j}t|jj|j |j ||j |j dddddS) zReturn a new URL with password replaced. Autoencode password if needed. Clear password if argument is None. NzInvalid password typez5password replacement is not allowed for relative URLsFr/rTrK) r6r7rr9rSr8r(rrTr;r<r:r>)rr=r!rrr with_passwords&   zURL.with_passwordc Csjt|tstd|s"td|s.td||}|j}t|jj|j |j |j ||j dddddS) zReturn a new URL with host replaced. Autoencode host if needed. Changing host for relative URLs is not allowed, use .join() instead. zInvalid host typez1host replacement is not allowed for relative URLszhost removing is not allowedFr/rTrK) r6r7r9rSr8rr(rrTr;r<r=r>)rrDr!rrr with_hosts"  z URL.with_hostc Csf|dk r$t|ts$tdt||s4td|j}t|jj |j |j |j |j |dddddS) z`Return a new URL with port replaced. Clear port to default if None is passed. Nz"port should be int or None, got {}z1port replacement is not allowed for relative URLsFr/rTrK)r6intr9rWtyperSr8r(rrTr;r<r=r:)rr>r!rrr with_ports  z URL.with_portrKcCsZ|s ||}|r ||}t|dkr@|ddkr@d|}t|jj|dddddS)z$Return a new URL with path replaced.rrQrrgTrK)r?rSr@lenrr(rT)rrEr-rrr with_paths  z URL.with_pathcs.|rt|dkrtd|}nt|dkr4|d}ntd|dkrJd}nt|tr|jg}|D]R\}}t|trzn$t|tkrt|}nt d || |d|qfd |}ndt|tr| |}nNt|tttfrt dn4t|tr"|jd fd d |D}nt d |S) Nrz7Either kwargs or single query parameter must be presentr rzCInvalid variable type: mapping value should be str or int, got {!r}=&zAInvalid query type: bytes, bytearray and memoryview are forbiddenc3s&|]\}}|d|VqdS)rNr)rkryZquoterrrr>sz%URL._get_str_query..zPInvalid query type: only str, mapping or sequence of (str, str) pairs is allowed)rr8r6r_QUERY_PART_QUOTERitemsr7rrr9rWrkrlrAbytes bytearray memoryviewr)rargskwargsrFZlstrryrrr_get_str_querysF             zURL._get_str_querycOs(|j||}t|jj|jj|dddS)aReturn a new URL with query part replaced. Accepts any Mapping (e.g. dict, multidict.MultiDict instances) or str, autoencode the argument if needed. It also can take an arbitrary number of keyword arguments. Clear query if None is passed. )rErFTrK)rrr(rTrE)rrr new_queryrrrrLFs zURL.with_querycOsL|j||}tt|dd}t|j}||t|jj||dddS)z)Return a new URL with query part updated.Tr)rFrK)rr rrFupdaterr(rT)rrrsrrFrrr update_queryXs   zURL.update_querycCs<|dkrd}nt|ts tdt|jj||dddS)zReturn a new URL with fragment replaced. Autoencode fragment if needed. Clear fragment to default if None is passed. NrzInvalid fragment type)rGTrK)r6r7r9rr(rTrB)rrGrrr with_fragmentbs  zURL.with_fragmentcCst|tstdd|kr"td||}|dkrrErJrGrYrrr human_reprszURL.human_repr)r)Hrr%r& __slots__r rr?rArrBrr~rrr classmethodrOrVrZr]r`rcrdrerfrorprsrSrwrzr{propertyrHr}rrIrr=rtrDr>rrErFrrJrrrrGrrnrrrr@rr;rrrrrrrrLrrrrlrrrrrr>sF     ' ,                              + r)r4Zcollections.abcrrZ ipaddressrZ urllib.parserrrrr Z multidictr r rZquotingr r __version____all__ruobjectr'rrrrrrs