Coverage for aiocoap/util/linkformat.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-15 22:10 +0000

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

2# 

3# SPDX-License-Identifier: MIT 

4 

5"""This module contains in-place modifications to the LinkHeader module to 

6satisfy RFC6690 constraints. 

7 

8It is a general nursery for what aiocoap needs of link-format management before 

9any of this is split out into its own package. 

10""" 

11 

12from .vendored import link_header 

13 

14 

15class LinkFormat(link_header.LinkHeader): 

16 def __str__(self): 

17 return ",".join(str(link) for link in self.links) 

18 

19 

20class Link(link_header.Link): 

21 # This is copy-pasted from the link_header module's code, just replacing 

22 # the '; ' with ';'. 

23 # 

24 # Original copyright Michael Burrows <mjb@asplake.co.uk>, distributed under 

25 # the BSD license 

26 def __str__(self): 

27 def str_pair(key, value): 

28 if value is None: 

29 return key 

30 # workaround to accomodate copper 

31 # elif RE_ONLY_TOKEN.match(value) or key.endswith('*'): 

32 # return '%s=%s' % (key, value) 

33 else: 

34 return '%s="%s"' % (key, value.replace('"', r"\"")) 

35 

36 return ";".join( 

37 ["<%s>" % self.href] 

38 + [str_pair(key, value) for key, value in self.attr_pairs] 

39 ) 

40 

41 

42def parse(linkformat): 

43 data = link_header.parse(linkformat) 

44 data.__class__ = LinkFormat 

45 for link in data.links: 

46 link.__class__ = Link 

47 return data