Coverage for aiocoap/util/uri.py: 91%
11 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
2#
3# SPDX-License-Identifier: MIT
5"""Tools that I'd like to have in urllib.parse"""
7import string
9#: "unreserved" characters from RFC3986
10unreserved = string.ascii_letters + string.digits + "-._~"
12#: "sub-delims" characters from RFC3986
13sub_delims = "!$&'()*+,;="
16def quote_factory(safe_characters):
17 """Return a quote function that escapes all characters not in the
18 safe_characters iterable."""
19 safe_set = set(ord(x) for x in safe_characters)
20 if any(c >= 128 for c in safe_set):
21 raise ValueError("quote_factory does not support non-ASCII safe characters")
23 def quote(input_string):
24 encoded = input_string.encode("utf8")
25 return "".join(chr(x) if x in safe_set else "%%%02X" % x for x in encoded)
27 return quote