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

12 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-10 11:47 +0000

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

2# 

3# SPDX-License-Identifier: MIT 

4 

5"""Helpers around content types 

6 

7This uses the terminology clarified in 1_, and primarily deals with content 

8types in their usual string representation. 

9 

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. 

13 

14.. _1: https://tools.ietf.org/html/draft-bormann-core-media-content-type-format-01""" 

15 

16 

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.""" 

20 

21 media_type, *_ = contenttype.split(";") 

22 _, _, subtype = media_type.partition("/") 

23 

24 if subtype == "cbor-seq" or subtype.endswith("+cbor-seq"): 

25 return "cbor-seq" 

26 

27 if subtype == "cbor" or subtype.endswith("+cbor"): 

28 return "cbor" 

29 

30 if subtype == "json" or subtype.endswith("+json"): 

31 return "json" 

32 

33 if media_type == "application/link-format": 

34 return "link-format" 

35 

36 return None