| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,91 @@ | |||
| 1 | + """URI.""" | ||
| 2 | + | ||
| 3 | + # Read: https://stackoverflow.com/questions/176264 | ||
| 4 | + # https://www.rfc-editor.org/rfc/rfc3986#section-3 | ||
| 5 | + | ||
| 6 | + # local | ||
| 7 | + from .email import email | ||
| 8 | + from .url import url | ||
| 9 | + from .utils import validator | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + def _file_url(value: str): | ||
| 13 | + if not value.startswith("file:///"): | ||
| 14 | + return False | ||
| 15 | + return True | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + def _ipfs_url(value: str): | ||
| 19 | + if not value.startswith("ipfs://"): | ||
| 20 | + return False | ||
| 21 | + return True | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + @validator | ||
| 25 | + def uri(value: str, /): | ||
| 26 | + """Return whether or not given value is a valid URI. | ||
| 27 | + | ||
| 28 | + Examples: | ||
| 29 | + >>> uri('mailto:example@domain.com') | ||
| 30 | + # Output: True | ||
| 31 | + >>> uri('file:path.txt') | ||
| 32 | + # Output: ValidationError(func=uri, ...) | ||
| 33 | + | ||
| 34 | + Args: | ||
| 35 | + value: | ||
| 36 | + URI to validate. | ||
| 37 | + | ||
| 38 | + Returns: | ||
| 39 | + (Literal[True]): If `value` is a valid URI. | ||
| 40 | + (ValidationError): If `value` is an invalid URI. | ||
| 41 | + """ | ||
| 42 | + if not value: | ||
| 43 | + return False | ||
| 44 | + | ||
| 45 | + # TODO: work on various validations | ||
| 46 | + | ||
| 47 | + # url | ||
| 48 | + if any( | ||
| 49 | + # fmt: off | ||
| 50 | + value.startswith(item) for item in { | ||
| 51 | + "ftp", "ftps", "git", "http", "https", | ||
| 52 | + "irc", "rtmp", "rtmps", "rtsp", "sftp", | ||
| 53 | + "ssh", "telnet", | ||
| 54 | + } | ||
| 55 | + # fmt: on | ||
| 56 | + ): | ||
| 57 | + return url(value) | ||
| 58 | + | ||
| 59 | + # email | ||
| 60 | + if value.startswith("mailto:"): | ||
| 61 | + return email(value.lstrip("mailto:")) | ||
| 62 | + | ||
| 63 | + # file | ||
| 64 | + if value.startswith("file:"): | ||
| 65 | + return _file_url(value) | ||
| 66 | + | ||
| 67 | + # ipfs | ||
| 68 | + if value.startswith("ipfs:"): | ||
| 69 | + return _ipfs_url(value) | ||
| 70 | + | ||
| 71 | + # magnet | ||
| 72 | + if value.startswith("magnet:?"): | ||
| 73 | + return True | ||
| 74 | + | ||
| 75 | + # telephone | ||
| 76 | + if value.startswith("tel:"): | ||
| 77 | + return True | ||
| 78 | + | ||
| 79 | + # data | ||
| 80 | + if value.startswith("data:"): | ||
| 81 | + return True | ||
| 82 | + | ||
| 83 | + # urn | ||
| 84 | + if value.startswith("urn:"): | ||
| 85 | + return True | ||
| 86 | + | ||
| 87 | + # urc | ||
| 88 | + if value.startswith("urc:"): | ||
| 89 | + return True | ||
| 90 | + | ||
| 91 | + return False | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments