FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-client/coreapi/codecs/python.py at master · core-api/python-client · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
core-api
/
python-client
Public archive
Notifications
You must be signed in to change notification settings
Fork
57
Star
183
Code
Issues
19
Pull requests
10
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-client
/
coreapi
/
codecs
/
python.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (71 loc) · 2.7 KB
Breadcrumbs
python-client
/
coreapi
/
codecs
/
python.py
Copy path
File metadata and controls
82 lines (71 loc) · 2.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Note that `DisplayCodec` is deliberately omitted from the documentation,
# as it is considered an implementation detail.
# It may move into a utility function in the future.
from
__future__
import
unicode_literals
from
coreapi
.
codecs
.
base
import
BaseCodec
from
coreapi
.
document
import
Document
,
Link
,
Array
,
Object
,
Error
,
Field
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
,
Error
):
content
=
', '
.
join
([
'%s: %s'
%
(
repr
(
key
),
_to_repr
(
value
))
for
key
,
value
in
node
.
items
()
])
return
'Error(title=%s, content={%s})'
%
(
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
.
encoding
:
args
+=
", encoding=%s"
%
repr
(
node
.
encoding
)
if
node
.
transform
:
args
+=
", transform=%s"
%
repr
(
node
.
transform
)
if
node
.
description
:
args
+=
", description=%s"
%
repr
(
node
.
description
)
if
node
.
fields
:
fields_repr
=
', '
.
join
(
_to_repr
(
item
)
for
item
in
node
.
fields
)
args
+=
", fields=[%s]"
%
fields_repr
return
"Link(%s)"
%
args
elif
isinstance
(
node
,
Field
):
args
=
repr
(
node
.
name
)
if
not
node
.
required
and
not
node
.
location
:
return
args
if
node
.
required
:
args
+=
', required=True'
if
node
.
location
:
args
+=
', location=%s'
%
repr
(
node
.
location
)
if
node
.
schema
:
args
+=
', schema=%s'
%
repr
(
node
.
schema
)
return
'Field(%s)'
%
args
return
repr
(
node
)
class
PythonCodec
(
BaseCodec
):
"""
A Python representation of a Document, for use with '__repr__'.
"""
media_type
=
'text/python'
def
encode
(
self
,
document
,
**
options
):
# Object and Array only have the class name wrapper if they
# are the outermost element.
if
isinstance
(
document
,
Object
):
return
'Object(%s)'
%
_to_repr
(
document
)
elif
isinstance
(
document
,
Array
):
return
'Array(%s)'
%
_to_repr
(
document
)
return
_to_repr
(
document
)
Back
|
FazBrowse Home
|
New Git URL