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

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

18 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"""This module contains in-place modifications to the LinkHeader module to 

10satisfy RFC6690 constraints. 

11 

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

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

14""" 

15 

16import link_header 

17 

18class LinkFormat(link_header.LinkHeader): 

19 def __str__(self): 

20 return ','.join(str(link) for link in self.links) 

21 

22class Link(link_header.Link): 

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

24 # the '; ' with ';'. 

25 # 

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

27 # the BSD license 

28 def __str__(self): 

29 def str_pair(key, value): 

30 if value is None: 

31 return key 

32# workaround to accomodate copper 

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

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

35 else: 

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

37 return ';'.join(['<%s>' % self.href] + 

38 [str_pair(key, value) 

39 for key, value in self.attr_pairs]) 

40 

41def parse(linkformat): 

42 data = link_header.parse(linkformat) 

43 data.__class__ = LinkFormat 

44 for l in data.links: 

45 l.__class__ = Link 

46 return data