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

48 statements  

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

1# Note that `DisplayCodec` is deliberately omitted from the documentation, 

2# as it is considered an implementation detail. 

3# It may move into a utility function in the future. 

4from __future__ import unicode_literals 

5from coreapi.codecs.base import BaseCodec 

6from coreapi.document import Document, Link, Array, Object, Error, Field 

7 

8 

9def _to_repr(node): 

10 if isinstance(node, Document): 

11 content = ', '.join([ 

12 '%s: %s' % (repr(key), _to_repr(value)) 

13 for key, value in node.items() 

14 ]) 

15 return 'Document(url=%s, title=%s, content={%s})' % ( 

16 repr(node.url), repr(node.title), content 

17 ) 

18 

19 elif isinstance(node, Error): 

20 content = ', '.join([ 

21 '%s: %s' % (repr(key), _to_repr(value)) 

22 for key, value in node.items() 

23 ]) 

24 return 'Error(title=%s, content={%s})' % ( 

25 repr(node.title), content 

26 ) 

27 

28 elif isinstance(node, Object): 

29 return '{%s}' % ', '.join([ 

30 '%s: %s' % (repr(key), _to_repr(value)) 

31 for key, value in node.items() 

32 ]) 

33 

34 elif isinstance(node, Array): 

35 return '[%s]' % ', '.join([ 

36 _to_repr(value) for value in node 

37 ]) 

38 

39 elif isinstance(node, Link): 

40 args = "url=%s" % repr(node.url) 

41 if node.action: 

42 args += ", action=%s" % repr(node.action) 

43 if node.encoding: 

44 args += ", encoding=%s" % repr(node.encoding) 

45 if node.transform: 

46 args += ", transform=%s" % repr(node.transform) 

47 if node.description: 

48 args += ", description=%s" % repr(node.description) 

49 if node.fields: 

50 fields_repr = ', '.join(_to_repr(item) for item in node.fields) 

51 args += ", fields=[%s]" % fields_repr 

52 return "Link(%s)" % args 

53 

54 elif isinstance(node, Field): 

55 args = repr(node.name) 

56 if not node.required and not node.location: 

57 return args 

58 if node.required: 

59 args += ', required=True' 

60 if node.location: 

61 args += ', location=%s' % repr(node.location) 

62 if node.schema: 

63 args += ', schema=%s' % repr(node.schema) 

64 return 'Field(%s)' % args 

65 

66 return repr(node) 

67 

68 

69class PythonCodec(BaseCodec): 

70 """ 

71 A Python representation of a Document, for use with '__repr__'. 

72 """ 

73 media_type = 'text/python' 

74 

75 def encode(self, document, **options): 

76 # Object and Array only have the class name wrapper if they 

77 # are the outermost element. 

78 if isinstance(document, Object): 

79 return 'Object(%s)' % _to_repr(document) 

80 elif isinstance(document, Array): 

81 return 'Array(%s)' % _to_repr(document) 

82 return _to_repr(document)