codekingpro/portable-devtools
114k
1"""2Regex for URIs3 4These regex are directly derived from the collected ABNF in RFC39865(except for DIGIT, ALPHA and HEXDIG, defined by RFC2234).6 7They should be processed with re.VERBOSE.8 9Thanks Mark Nottingham for this code - https://gist.github.com/13854910"""11import re12 13# basics14 15DIGIT = r"[\x30-\x39]"16 17ALPHA = r"[\x41-\x5A\x61-\x7A]"18 19HEXDIG = r"[\x30-\x39A-Fa-f]"20 21# pct-encoded = "%" HEXDIG HEXDIG22pct_encoded = r" %% %(HEXDIG)s %(HEXDIG)s" % locals()23 24# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"25unreserved = r"(?: %(ALPHA)s | %(DIGIT)s | \- | \. | _ | ~ )" % locals()26 27# gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"28gen_delims = r"(?: : | / | \? | \# | \[ | \] | @ )"29 30# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"31# / "*" / "+" / "," / ";" / "="32sub_delims = r"""(?: ! | \$ | & | ' | \( | \) |33 \* | \+ | , | ; | = )"""34 35# pchar = unreserved / pct-encoded / sub-delims / ":" / "@"36pchar = r"(?: %(unreserved)s | %(pct_encoded)s | %(sub_delims)s | : | @ )" % locals(37)38 39# reserved = gen-delims / sub-delims40reserved = r"(?: %(gen_delims)s | %(sub_delims)s )" % locals()41 42 43# scheme44 45# scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )46scheme = r"%(ALPHA)s (?: %(ALPHA)s | %(DIGIT)s | \+ | \- | \. )*" % locals()47 48 49# authority50 51# dec-octet = DIGIT ; 0-952# / %x31-39 DIGIT ; 10-9953# / "1" 2DIGIT ; 100-19954# / "2" %x30-34 DIGIT ; 200-24955# / "25" %x30-35 ; 250-25556dec_octet = r"""(?: %(DIGIT)s |57 [\x31-\x39] %(DIGIT)s |58 1 %(DIGIT)s{2} |59 2 [\x30-\x34] %(DIGIT)s |60 25 [\x30-\x35]61 )62""" % locals()63 64# IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet65IPv4address = r"%(dec_octet)s \. %(dec_octet)s \. %(dec_octet)s \. %(dec_octet)s" % locals(66)67 68# IPv6address69IPv6address = r"([A-Fa-f0-9:]+[:$])[A-Fa-f0-9]{1,4}"70 71# IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )72IPvFuture = r"v %(HEXDIG)s+ \. (?: %(unreserved)s | %(sub_delims)s | : )+" % locals()73 74# IP-literal = "[" ( IPv6address / IPvFuture ) "]"75IP_literal = r"\[ (?: %(IPv6address)s | %(IPvFuture)s ) \]" % locals()76 77# reg-name = *( unreserved / pct-encoded / sub-delims )78reg_name = r"(?: %(unreserved)s | %(pct_encoded)s | %(sub_delims)s )*" % locals()79 80# userinfo = *( unreserved / pct-encoded / sub-delims / ":" )81userinfo = r"(?: %(unreserved)s | %(pct_encoded)s | %(sub_delims)s | : )" % locals(82)83 84# host = IP-literal / IPv4address / reg-name85host = r"(?: %(IP_literal)s | %(IPv4address)s | %(reg_name)s )" % locals()86 87# port = *DIGIT88port = r"(?: %(DIGIT)s )*" % locals()89 90# authority = [ userinfo "@" ] host [ ":" port ]91authority = r"(?: %(userinfo)s @)? %(host)s (?: : %(port)s)?" % locals()92 93# Path94 95# segment = *pchar96segment = r"%(pchar)s*" % locals()97 98# segment-nz = 1*pchar99segment_nz = r"%(pchar)s+" % locals()100 101# segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )102# ; non-zero-length segment without any colon ":"103segment_nz_nc = r"(?: %(unreserved)s | %(pct_encoded)s | %(sub_delims)s | @ )+" % locals()104 105# path-abempty = *( "/" segment )106path_abempty = r"(?: / %(segment)s )*" % locals()107 108# path-absolute = "/" [ segment-nz *( "/" segment ) ]109path_absolute = r"/ (?: %(segment_nz)s (?: / %(segment)s )* )?" % locals()110 111# path-noscheme = segment-nz-nc *( "/" segment )112path_noscheme = r"%(segment_nz_nc)s (?: / %(segment)s )*" % locals()113 114# path-rootless = segment-nz *( "/" segment )115path_rootless = r"%(segment_nz)s (?: / %(segment)s )*" % locals()116 117# path-empty = 0<pchar>118path_empty = r"" # FIXME119 120# path = path-abempty ; begins with "/" or is empty121# / path-absolute ; begins with "/" but not "//"122# / path-noscheme ; begins with a non-colon segment123# / path-rootless ; begins with a segment124# / path-empty ; zero characters125path = r"""(?: %(path_abempty)s |126 %(path_absolute)s |127 %(path_noscheme)s |128 %(path_rootless)s |129 %(path_empty)s130 )131""" % locals()132 133### Query and Fragment134 135# query = *( pchar / "/" / "?" )136query = r"(?: %(pchar)s | / | \? )*" % locals()137 138# fragment = *( pchar / "/" / "?" )139fragment = r"(?: %(pchar)s | / | \? )*" % locals()140 141# URIs142 143# hier-part = "//" authority path-abempty144# / path-absolute145# / path-rootless146# / path-empty147hier_part = r"""(?: (?: // %(authority)s %(path_abempty)s ) |148 %(path_absolute)s |149 %(path_rootless)s |150 %(path_empty)s151 )152""" % locals()153 154# relative-part = "//" authority path-abempty155# / path-absolute156# / path-noscheme157# / path-empty158relative_part = r"""(?: (?: // %(authority)s %(path_abempty)s ) |159 %(path_absolute)s |160 %(path_noscheme)s |161 %(path_empty)s162 )163""" % locals()164 165# relative-ref = relative-part [ "?" query ] [ "#" fragment ]166relative_ref = r"%(relative_part)s (?: \? %(query)s)? (?: \# %(fragment)s)?" % locals(167)168 169# URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]170URI = r"^(?: %(scheme)s : %(hier_part)s (?: \? %(query)s )? (?: \# %(fragment)s )? )$" % locals(171)172 173# URI-reference = URI / relative-ref174URI_reference = r"^(?: %(URI)s | %(relative_ref)s )$" % locals()175 176# absolute-URI = scheme ":" hier-part [ "?" query ]177absolute_URI = r"^(?: %(scheme)s : %(hier_part)s (?: \? %(query)s )? )$" % locals(178)179 180 181def is_uri(uri):182 return re.match(URI, uri, re.VERBOSE)183 184 185def is_uri_reference(uri):186 return re.match(URI_reference, uri, re.VERBOSE)187 188 189def is_absolute_uri(uri):190 return re.match(absolute_URI, uri, re.VERBOSE)191 