Coverage for aiocoap/util/contenttype.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

11 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"""Helpers around content types 

10 

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

12types in their usual string representation. 

13 

14Unless content types get used a lot more in aiocoap, this provides only 

15accessors to some of their relevant properties, without aiming to build 

16semantically accessible objects to encapsulate them. 

17 

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

19 

20def categorize(contenttype: str): 

21 """Return 'cbor', 'json' or 'link-format' if the content type indicates it 

22 is that format itself or derived from it.""" 

23 

24 media_type, *_ = contenttype.split(';') 

25 _, _, subtype = media_type.partition('/') 

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