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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

1# This file is part of the Python aiocoap library project. 

2# 

3# Copyright (c) 2012-2014 Maciej Wasilak <http://sixpinetrees.blogspot.com/>, 

4# 2013-2014 Christian Amsüss <c.amsuess@energyharvesting.at> 

5# 

6# aiocoap is free software, this file is published under the MIT license as 

7# described in the accompanying LICENSE file. 

8 

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

10 

11import string 

12 

13#: "unreserved" characters from RFC3986 

14unreserved = string.ascii_letters + string.digits + '-._~' 

15 

16#: "sub-delims" characters from RFC3986 

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

18 

19def quote_factory(safe_characters): 

20 """Return a quote function that escapes all characters not in the 

21 safe_characters iterable.""" 

22 safe_set = set(ord(x) for x in safe_characters) 

23 if any(c >= 128 for c in safe_set): 

24 raise ValueError("quote_factory does not support non-ASCII safe characters") 

25 def quote(input_string): 

26 encoded = input_string.encode('utf8') 

27 return "".join(chr(x) if x in safe_set else "%%%02X"%x for x in encoded) 

28 return quote