Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/coreapi/codecs/base.py: 32%

21 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1import itypes 

2 

3 

4class BaseCodec(itypes.Object): 

5 media_type = None 

6 

7 # We don't implement stubs, to ensure that we can check which of these 

8 # two operations a codec supports. For example: 

9 # `if hasattr(codec, 'decode'): ...` 

10 

11 # def decode(self, bytestring, **options): 

12 # pass 

13 

14 # def encode(self, document, **options): 

15 # pass 

16 

17 # The following will be removed at some point, most likely in a 2.1 release: 

18 def dump(self, *args, **kwargs): 

19 # Fallback for v1.x interface 

20 return self.encode(*args, **kwargs) 

21 

22 def load(self, *args, **kwargs): 

23 # Fallback for v1.x interface 

24 return self.decode(*args, **kwargs) 

25 

26 @property 

27 def supports(self): 

28 # Fallback for v1.x interface. 

29 if '+' not in self.media_type: 

30 return ['data'] 

31 

32 ret = [] 

33 if hasattr(self, 'encode'): 

34 ret.append('encoding') 

35 if hasattr(self, 'decode'): 

36 ret.append('decoding') 

37 return ret 

38 

39 def get_media_types(self): 

40 # Fallback, while transitioning from `application/vnd.coreapi+json` 

41 # to simply `application/coreapi+json`. 

42 if hasattr(self, 'media_types'): 

43 return list(self.media_types) 

44 return [self.media_type]