ó ½z]c@sdZddlmZddlmZddlmZdefd„ƒYZd„Zd„Zd „Z d „Z de d „Z e d „Ze d „Ze d„Ze d„Ze d„Zd„Zied6ed6ed6ed6ZedddgƒZd„Zd„ZdS(s3Module containing the validation logic for rfc3986.i(t exceptions(tmisc(t normalizerst ValidatorcBs€eZdZedddddddgƒZd„Zd „Zd „Zd „Zd „Z d „Z d„Z d„Z d„Z RS(sObject used to configure validation of all objects in rfc3986. .. versionadded:: 1.0 Example usage:: >>> from rfc3986 import api, validators >>> uri = api.uri_reference('https://github.com/') >>> validator = validators.Validator().require_presence_of( ... 'scheme', 'host', 'path', ... ).allow_schemes( ... 'http', 'https', ... ).allow_hosts( ... '127.0.0.1', 'github.com', ... ) >>> validator.validate(uri) >>> invalid_uri = rfc3986.uri_reference('imap://mail.google.com') >>> validator.validate(invalid_uri) Traceback (most recent call last): ... rfc3986.exceptions.MissingComponentError: ('path was required but missing', URIReference(scheme=u'imap', authority=u'mail.google.com', path=None, query=None, fragment=None), ['path']) tschemetuserinfothosttporttpathtquerytfragmentcCs}tƒ|_tƒ|_tƒ|_t|_itd6td6td6td6td6td6td6|_|jjƒ|_ dS( s#Initialize our default validations.RRRRRR R N( tsettallowed_schemest allowed_hostst allowed_portstTruetallow_passwordtFalsetrequired_componentstcopytvalidated_components(tself((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyt__init__:s     cGs.x'|D]}|jjtj|ƒƒqW|S(s Require the scheme to be one of the provided schemes. .. versionadded:: 1.0 :param schemes: Schemes, without ``://`` that are allowed. :returns: The validator instance. :rtype: Validator (R taddRtnormalize_scheme(RtschemesR((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyt allow_schemesKs cGs.x'|D]}|jjtj|ƒƒqW|S(sðRequire the host to be one of the provided hosts. .. versionadded:: 1.0 :param hosts: Hosts that are allowed. :returns: The validator instance. :rtype: Validator (R RRtnormalize_host(RthostsR((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyt allow_hosts[s cGsVxO|D]G}t|ddƒ}d|ko6dknr|jj|ƒqqW|S(sðRequire the port to be one of the provided ports. .. versionadded:: 1.0 :param ports: Ports that are allowed. :returns: The validator instance. :rtype: Validator tbasei iiÿÿ(tintRR(RtportsRtport_int((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyt allow_portsks cCs t|_|S(s®Allow passwords to be present in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator (RR(R((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytallow_use_of_password}s cCs t|_|S(s¶Prevent passwords from being included in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator (RR(R((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytforbid_use_of_passwordŠs cGsug|D]}|jƒ^q}x5|D]-}||jkr&tdj|ƒƒ‚q&q&W|jjd„|Dƒƒ|S(sACheck the validity of the components provided. This can be specified repeatedly. .. versionadded:: 1.1 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator s"{}" is not a valid componentcSsi|]}t|“qS((R(t.0t component((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pys ¬s (tlowertCOMPONENT_NAMESt ValueErrortformatRtupdate(Rt componentstcR&((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytcheck_validity_of—s  cGsug|D]}|jƒ^q}x5|D]-}||jkr&tdj|ƒƒ‚q&q&W|jjd„|Dƒƒ|S(s3Require the components provided. This can be specified repeatedly. .. versionadded:: 1.0 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator s"{}" is not a valid componentcSsi|]}t|“qS((R(R%R&((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pys Ås (R'R(R)R*RR+(RR,R-R&((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytrequire_presence_of°s  cCsÛ|jst|ƒng|jjƒD]\}}|r&|^q&}g|jjƒD]\}}|rT|^qT}|rˆt||ƒn|ržt||ƒnt|j|dƒt|j |dƒt|j |dƒdS(sƒCheck a URI for conditions specified on this validator. .. versionadded:: 1.0 :param uri: Parsed URI to validate. :type uri: rfc3986.uri.URIReference :raises MissingComponentError: When a required component is missing. :raises UnpermittedComponentError: When a component is not one of those allowed. :raises PasswordForbidden: When a password is present in the userinfo component but is not permitted by configuration. :raises InvalidComponentsError: When a component was found to be invalid. RRRN( Rtcheck_passwordRtitemsRt ensure_required_components_existtensure_components_are_validt ensure_one_ofR R R(RturiR&trequiredRR((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytvalidateÉs  (t__name__t __module__t__doc__t frozensetR(RRRR"R#R$R.R/R7(((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyRs$       cCsN|j}|sdS|jddƒ}t|ƒdkr;dStj|ƒ‚dS(s4Assert that there is no password present in the uri.Nt:i(RtsplittlenRtPasswordForbidden(R5Rt credentials((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyR0ós cCsIt||ƒ}|dk rE|rE||krEtj|||ƒ‚ndS(s=Assert that the uri's attribute is one of the allowed values.N(tgetattrtNoneRtUnpermittedComponentError(tallowed_valuesR5t attributetvalue((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyR4þscCsStg|D]!}t||ƒdkr |^q ƒ}|rOtj||Œ‚ndS(s;Assert that all required components are present in the URI.N(tsortedRARBRtMissingComponentError(R5RR&tmissing_components((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyR2s  $cCs8|r|dk o|j|ƒS|dkp7|j|ƒS(s Determine if a value is valid based on the provided matcher. :param str value: Value to validate. :param matcher: Compiled regular expression to use to validate the value. :param require: Whether or not the value is required. N(RBtmatch(RFtmatchertrequire((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytis_valids   cCs8t|tj|ƒ}|r4|dk r4t||ƒS|S(snDetermine if the authority string is valid. :param str authority: The authority to validate. :param str host: (optional) The host portion of the authority to validate. :param bool require: (optional) Specify if authority must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N(RMRtSUBAUTHORITY_MATCHERRBt host_is_valid(t authorityRRLt validated((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytauthority_is_valid$s cCst|tj|ƒ}|rC|dk rCtjj|ƒrCt|ƒS|r}|dk r}tjj|ƒr}tjj|ƒdk S|S(sDetermine if the host string is valid. :param str host: The host to validate. :param bool require: (optional) Specify if host must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N( RMRt HOST_MATCHERRBt IPv4_MATCHERRJtvalid_ipv4_host_addresst IPv6_MATCHERtIPv6_NO_RFC4007_MATCHER(RRLRQ((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyRO8s $ $cCst|tj|ƒS(s+Determine if the scheme is valid. :param str scheme: The scheme string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a scheme. :returns: ``True`` if the scheme is valid. ``False`` otherwise. :rtype: bool (RMRtSCHEME_MATCHER(RRL((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytscheme_is_validLs cCst|tj|ƒS(s+Determine if the path component is valid. :param str path: The path string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a path. :returns: ``True`` if the path is valid. ``False`` otherwise. :rtype: bool (RMRt PATH_MATCHER(RRL((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyt path_is_valid[s cCst|tj|ƒS(s0Determine if the query component is valid. :param str query: The query string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a query. :returns: ``True`` if the query is valid. ``False`` otherwise. :rtype: bool (RMRt QUERY_MATCHER(R RL((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytquery_is_validjs cCst|tj|ƒS(s?Determine if the fragment component is valid. :param str fragment: The fragment string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a fragment. :returns: ``True`` if the fragment is valid. ``False`` otherwise. :rtype: bool (RMRtFRAGMENT_MATCHER(R RL((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytfragment_is_validys cCsHtg|jdƒD].}dt|ddƒko<dkn^qƒS(s4Determine if the given host is a valid IPv4 address.t.iRi iÿ(tallR=R(Rtbyte((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyRUˆsRRR R RRRcCs“y|jƒ}Wntjk r'tSX|dkrBt|dƒS|dkrRtSyt|dƒ}Wntk rztSXd|kodkSS(s4Determine if the userinfo, host, and port are valid.RRiiÿÿ(tauthority_infoRtInvalidAuthorityRRORRt TypeError(R5R&tsubauthority_dictR((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pytsubauthority_component_is_valid™s   cCsœtgƒ}xn|D]f}|tkrJt||ƒs|j|ƒqqnt|}|t||ƒƒs|j|ƒqqW|r˜tj||Œ‚ndS(s0Assert that all components are valid in the URI.N(R t_SUBAUTHORITY_VALIDATORSRgRt_COMPONENT_VALIDATORSRARtInvalidComponentsError(R5Rtinvalid_componentsR&t validator((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyR3±s    N(R:tRRRtobjectRR0R4R2RMRBRRRRORYR[R]R_RURiR RhRgR3(((sD/tmp/pip-build-kBFYxq/urllib3/urllib3/packages/rfc3986/validators.pyts.Þ