-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathuri.py
More file actions
148 lines (117 loc) · 2.97 KB
/
Copy pathuri.py
File metadata and controls
148 lines (117 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""URI."""
# Read: https://stackoverflow.com/questions/176264
# https://www.rfc-editor.org/rfc/rfc3986#section-3
# standard
from urllib.parse import urlsplit
# local
from .email import email
from .hostname import hostname
from .url import url
from .utils import validator
def _file_url(value: str):
if not value.startswith("file:///"):
return False
return True
def _ipfs_url(value: str):
if not value.startswith("ipfs://"):
return False
return True
def _telnet_url(value: str):
"""Validate a telnet URL per RFC 4248.
Ref: https://www.rfc-editor.org/rfc/rfc4248"
Examples:
>>> _telnet_url('telnet://example.com')
True
>>> _telnet_url('telnet://example.com:23')
True
>>> _telnet_url('telnet://example.com:23/')
True
Args:
value:
Telnet URL to validate.
Returns:
(Literal[True]): If `value` is a Telnet URI.
(ValidationError): If `value` is an invalid URI.
"""
try:
scheme, netloc, path, query, fragment = urlsplit(value)
except ValueError:
return False
if scheme != "telnet":
return False
if not netloc:
return False
if query or fragment:
return False
if path and path != "/":
return False
# handling any additional checks user/pass
if "@" in netloc:
netloc = netloc.rsplit("@", 1)[1]
return hostname(netloc, may_have_port=True)
@validator
def uri(value: str, /):
"""Return whether or not given value is a valid URI.
Examples:
>>> uri('mailto:example@domain.com')
True
>>> uri('file:path.txt')
ValidationError(func=uri, args={'value': 'file:path.txt'})
Args:
value:
URI to validate.
Returns:
(Literal[True]): If `value` is a valid URI.
(ValidationError): If `value` is an invalid URI.
"""
if not value:
return False
# TODO: work on various validations
# url
if any(
# fmt: off
value.startswith(item)
for item in {
"ftp",
"ftps",
"git",
"http",
"https",
"irc",
"rtmp",
"rtmps",
"rtsp",
"sftp",
"ssh",
}
# fmt: on
):
return url(value)
# email
if value.startswith("mailto:"):
return email(value[len("mailto:") :])
# file
if value.startswith("file:"):
return _file_url(value)
# ipfs
if value.startswith("ipfs:"):
return _ipfs_url(value)
# magnet
if value.startswith("magnet:?"):
return True
# telephone
if value.startswith("tel:"):
return True
# data
if value.startswith("data:"):
return True
# urn
if value.startswith("urn:"):
return True
# urc
if value.startswith("urc:"):
return True
# telnet
if value.startswith("telnet:"):
return _telnet_url(value)
return False