Coverage for aiocoap/util/uri.py: 91%

11 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-10 11:47 +0000

1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors 

2# 

3# SPDX-License-Identifier: MIT 

4 

5"""Tools that I'd like to have in urllib.parse""" 

6 

7import string 

8 

9#: "unreserved" characters from RFC3986 

10unreserved = string.ascii_letters + string.digits + "-._~" 

11 

12#: "sub-delims" characters from RFC3986 

13sub_delims = "!$&'()*+,;=" 

14 

15 

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") 

22 

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) 

26 

27 return quote