forked from core-api/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
62 lines (52 loc) · 1.9 KB
/
python.py
File metadata and controls
62 lines (52 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import unicode_literals
from coreapi.codecs.base import BaseCodec
from coreapi.document import Document, Link, Array, Object, Error
def _to_repr(node):
if isinstance(node, Document):
content = ', '.join([
'%s: %s' % (repr(key), _to_repr(value))
for key, value in node.items()
])
return 'Document(url=%s, title=%s, content={%s})' % (
repr(node.url), repr(node.title), content
)
elif isinstance(node, Object):
return '{%s}' % ', '.join([
'%s: %s' % (repr(key), _to_repr(value))
for key, value in node.items()
])
elif isinstance(node, Array):
return '[%s]' % ', '.join([
_to_repr(value) for value in node
])
elif isinstance(node, Link):
args = "url=%s" % repr(node.url)
if node.action:
args += ", action=%s" % repr(node.action)
if node.inplace is not None:
args += ", inplace=%s" % repr(node.inplace)
if node.fields:
fields_repr = ', '.join([
'required(%s)' % repr(field.name)
if field.required else
repr(field.name)
for field in node.fields
])
args += ", fields=[%s]" % fields_repr
return "Link(%s)" % args
elif isinstance(node, Error):
return 'Error(%s)' % repr(list(node.messages))
return repr(node)
class PythonCodec(BaseCodec):
"""
A Python representation of a Document, for use with '__repr__'.
"""
media_type = 'text/python'
def dump(self, node, **kwargs):
# Object and Array only have the class name wrapper if they
# are the outermost element.
if isinstance(node, Object):
return 'Object(%s)' % _to_repr(node)
elif isinstance(node, Array):
return 'Array(%s)' % _to_repr(node)
return _to_repr(node)