Coverage for aiocoap/util/linkformat.py: 100%
18 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-16 16:09 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-16 16:09 +0000
1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
2#
3# SPDX-License-Identifier: MIT
5"""This module contains in-place modifications to the LinkHeader module to
6satisfy RFC6690 constraints.
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"""
12from .vendored import link_header
14class LinkFormat(link_header.LinkHeader):
15 def __str__(self):
16 return ','.join(str(link) for link in self.links)
18class Link(link_header.Link):
19 # This is copy-pasted from the link_header module's code, just replacing
20 # the '; ' with ';'.
21 #
22 # Original copyright Michael Burrows <mjb@asplake.co.uk>, distributed under
23 # the BSD license
24 def __str__(self):
25 def str_pair(key, value):
26 if value is None:
27 return key
28# workaround to accomodate copper
29# elif RE_ONLY_TOKEN.match(value) or key.endswith('*'):
30# return '%s=%s' % (key, value)
31 else:
32 return '%s="%s"' % (key, value.replace('"', r'\"'))
33 return ';'.join(['<%s>' % self.href] +
34 [str_pair(key, value)
35 for key, value in self.attr_pairs])
37def parse(linkformat):
38 data = link_header.parse(linkformat)
39 data.__class__ = LinkFormat
40 for l in data.links:
41 l.__class__ = Link
42 return data