Coverage for aiocoap/util/contenttype.py: 92%
12 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
2#
3# SPDX-License-Identifier: MIT
5"""Helpers around content types
7This uses the terminology clarified in 1_, and primarily deals with content
8types in their usual string representation.
10Unless content types get used a lot more in aiocoap, this provides only
11accessors to some of their relevant properties, without aiming to build
12semantically accessible objects to encapsulate them.
14.. _1: https://tools.ietf.org/html/draft-bormann-core-media-content-type-format-01"""
17def categorize(contenttype: str):
18 """Return 'cbor', 'json' or 'link-format' if the content type indicates it
19 is that format itself or derived from it."""
21 media_type, *_ = contenttype.split(";")
22 _, _, subtype = media_type.partition("/")
24 if subtype == "cbor-seq" or subtype.endswith("+cbor-seq"):
25 return "cbor-seq"
27 if subtype == "cbor" or subtype.endswith("+cbor"):
28 return "cbor"
30 if subtype == "json" or subtype.endswith("+json"):
31 return "json"
33 if media_type == "application/link-format":
34 return "link-format"
36 return None