FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-client/coreapi/document.py at master · pitcons/python-client · GitHub
pitcons
/
python-client
Public
forked from
core-api/python-client
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-client
/
coreapi
/
document.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
310 lines (248 loc) · 9.54 KB
Breadcrumbs
python-client
/
coreapi
/
document.py
Copy path
File metadata and controls
310 lines (248 loc) · 9.54 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# coding: utf-8
from
__future__
import
unicode_literals
from
collections
import
OrderedDict
,
namedtuple
from
coreapi
.
compat
import
string_types
import
itypes
def
_to_immutable
(
value
):
if
isinstance
(
value
,
dict
):
return
Object
(
value
)
elif
isinstance
(
value
,
list
):
return
Array
(
value
)
return
value
def
_repr
(
node
):
from
coreapi
.
codecs
.
python
import
PythonCodec
return
PythonCodec
().
encode
(
node
)
def
_str
(
node
):
from
coreapi
.
codecs
.
display
import
DisplayCodec
return
DisplayCodec
().
encode
(
node
)
def
_key_sorting
(
item
):
"""
Document and Object sorting.
Regular attributes sorted alphabetically.
Links are sorted based on their URL and action.
"""
key
,
value
=
item
if
isinstance
(
value
,
Link
):
action_priority
=
{
'get'
:
0
,
'post'
:
1
,
'put'
:
2
,
'patch'
:
3
,
'delete'
:
4
}.
get
(
value
.
action
,
5
)
return
(
1
, (
value
.
url
,
action_priority
))
return
(
0
,
key
)
# The field class, as used by Link objects:
# NOTE: 'type', 'description' and 'examaple' are now deprecated,
# in favor of 'schema'.
Field
=
namedtuple
(
'Field'
, [
'name'
,
'required'
,
'location'
,
'schema'
,
'description'
,
'type'
,
'example'
])
Field
.
__new__
.
__defaults__
=
(
False
,
''
,
None
,
None
,
None
,
None
)
# The Core API primatives:
class
Document
(
itypes
.
Dict
):
"""
The Core API document type.
Expresses the data that the client may access,
and the actions that the client may perform.
"""
def
__init__
(
self
,
url
=
None
,
title
=
None
,
description
=
None
,
media_type
=
None
,
content
=
None
):
content
=
{}
if
(
content
is
None
)
else
content
if
url
is
not
None
and
not
isinstance
(
url
,
string_types
):
raise
TypeError
(
"'url' must be a string."
)
if
title
is
not
None
and
not
isinstance
(
title
,
string_types
):
raise
TypeError
(
"'title' must be a string."
)
if
description
is
not
None
and
not
isinstance
(
description
,
string_types
):
raise
TypeError
(
"'description' must be a string."
)
if
media_type
is
not
None
and
not
isinstance
(
media_type
,
string_types
):
raise
TypeError
(
"'media_type' must be a string."
)
if
not
isinstance
(
content
,
dict
):
raise
TypeError
(
"'content' must be a dict."
)
if
any
([
not
isinstance
(
key
,
string_types
)
for
key
in
content
.
keys
()]):
raise
TypeError
(
'content keys must be strings.'
)
self
.
_url
=
''
if
(
url
is
None
)
else
url
self
.
_title
=
''
if
(
title
is
None
)
else
title
self
.
_description
=
''
if
(
description
is
None
)
else
description
self
.
_media_type
=
''
if
(
media_type
is
None
)
else
media_type
self
.
_data
=
{
key
:
_to_immutable
(
value
)
for
key
,
value
in
content
.
items
()}
def
clone
(
self
,
data
):
return
self
.
__class__
(
self
.
url
,
self
.
title
,
self
.
description
,
self
.
media_type
,
data
)
def
__iter__
(
self
):
items
=
sorted
(
self
.
_data
.
items
(),
key
=
_key_sorting
)
return
iter
([
key
for
key
,
value
in
items
])
def
__repr__
(
self
):
return
_repr
(
self
)
def
__str__
(
self
):
return
_str
(
self
)
def
__eq__
(
self
,
other
):
if
self
.
__class__
==
other
.
__class__
:
return
(
self
.
url
==
other
.
url
and
self
.
title
==
other
.
title
and
self
.
_data
==
other
.
_data
)
return
super
(
Document
,
self
).
__eq__
(
other
)
@
property
def
url
(
self
):
return
self
.
_url
@
property
def
title
(
self
):
return
self
.
_title
@
property
def
description
(
self
):
return
self
.
_description
@
property
def
media_type
(
self
):
return
self
.
_media_type
@
property
def
data
(
self
):
return
OrderedDict
([
(
key
,
value
)
for
key
,
value
in
self
.
items
()
if
not
isinstance
(
value
,
Link
)
])
@
property
def
links
(
self
):
return
OrderedDict
([
(
key
,
value
)
for
key
,
value
in
self
.
items
()
if
isinstance
(
value
,
Link
)
])
class
Object
(
itypes
.
Dict
):
"""
An immutable mapping of strings to values.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
data
=
dict
(
*
args
,
**
kwargs
)
if
any
([
not
isinstance
(
key
,
string_types
)
for
key
in
data
.
keys
()]):
raise
TypeError
(
'Object keys must be strings.'
)
self
.
_data
=
{
key
:
_to_immutable
(
value
)
for
key
,
value
in
data
.
items
()}
def
__iter__
(
self
):
items
=
sorted
(
self
.
_data
.
items
(),
key
=
_key_sorting
)
return
iter
([
key
for
key
,
value
in
items
])
def
__repr__
(
self
):
return
_repr
(
self
)
def
__str__
(
self
):
return
_str
(
self
)
@
property
def
data
(
self
):
return
OrderedDict
([
(
key
,
value
)
for
key
,
value
in
self
.
items
()
if
not
isinstance
(
value
,
Link
)
])
@
property
def
links
(
self
):
return
OrderedDict
([
(
key
,
value
)
for
key
,
value
in
self
.
items
()
if
isinstance
(
value
,
Link
)
])
class
Array
(
itypes
.
List
):
"""
An immutable list type container.
"""
def
__init__
(
self
,
*
args
):
self
.
_data
=
[
_to_immutable
(
value
)
for
value
in
list
(
*
args
)]
def
__repr__
(
self
):
return
_repr
(
self
)
def
__str__
(
self
):
return
_str
(
self
)
class
Link
(
itypes
.
Object
):
"""
Links represent the actions that a client may perform.
"""
def
__init__
(
self
,
url
=
None
,
action
=
None
,
encoding
=
None
,
transform
=
None
,
title
=
None
,
description
=
None
,
fields
=
None
):
if
(
url
is
not
None
)
and
(
not
isinstance
(
url
,
string_types
)):
raise
TypeError
(
"Argument 'url' must be a string."
)
if
(
action
is
not
None
)
and
(
not
isinstance
(
action
,
string_types
)):
raise
TypeError
(
"Argument 'action' must be a string."
)
if
(
encoding
is
not
None
)
and
(
not
isinstance
(
encoding
,
string_types
)):
raise
TypeError
(
"Argument 'encoding' must be a string."
)
if
(
transform
is
not
None
)
and
(
not
isinstance
(
transform
,
string_types
)):
raise
TypeError
(
"Argument 'transform' must be a string."
)
if
(
title
is
not
None
)
and
(
not
isinstance
(
title
,
string_types
)):
raise
TypeError
(
"Argument 'title' must be a string."
)
if
(
description
is
not
None
)
and
(
not
isinstance
(
description
,
string_types
)):
raise
TypeError
(
"Argument 'description' must be a string."
)
if
(
fields
is
not
None
)
and
(
not
isinstance
(
fields
, (
list
,
tuple
))):
raise
TypeError
(
"Argument 'fields' must be a list."
)
if
(
fields
is
not
None
)
and
any
([
not
(
isinstance
(
item
,
string_types
)
or
isinstance
(
item
,
Field
))
for
item
in
fields
]):
raise
TypeError
(
"Argument 'fields' must be a list of strings or fields."
)
self
.
_url
=
''
if
(
url
is
None
)
else
url
self
.
_action
=
''
if
(
action
is
None
)
else
action
self
.
_encoding
=
''
if
(
encoding
is
None
)
else
encoding
self
.
_transform
=
''
if
(
transform
is
None
)
else
transform
self
.
_title
=
''
if
(
title
is
None
)
else
title
self
.
_description
=
''
if
(
description
is
None
)
else
description
self
.
_fields
=
()
if
(
fields
is
None
)
else
tuple
([
item
if
isinstance
(
item
,
Field
)
else
Field
(
item
,
required
=
False
,
location
=
''
)
for
item
in
fields
])
@
property
def
url
(
self
):
return
self
.
_url
@
property
def
action
(
self
):
return
self
.
_action
@
property
def
encoding
(
self
):
return
self
.
_encoding
@
property
def
transform
(
self
):
return
self
.
_transform
@
property
def
title
(
self
):
return
self
.
_title
@
property
def
description
(
self
):
return
self
.
_description
@
property
def
fields
(
self
):
return
self
.
_fields
def
__eq__
(
self
,
other
):
return
(
isinstance
(
other
,
Link
)
and
self
.
url
==
other
.
url
and
self
.
action
==
other
.
action
and
self
.
encoding
==
other
.
encoding
and
self
.
transform
==
other
.
transform
and
self
.
description
==
other
.
description
and
sorted
(
self
.
fields
,
key
=
lambda
f
:
f
.
name
)
==
sorted
(
other
.
fields
,
key
=
lambda
f
:
f
.
name
)
)
def
__repr__
(
self
):
return
_repr
(
self
)
def
__str__
(
self
):
return
_str
(
self
)
class
Error
(
itypes
.
Dict
):
def
__init__
(
self
,
title
=
None
,
content
=
None
):
data
=
{}
if
(
content
is
None
)
else
content
if
title
is
not
None
and
not
isinstance
(
title
,
string_types
):
raise
TypeError
(
"'title' must be a string."
)
if
content
is
not
None
and
not
isinstance
(
content
,
dict
):
raise
TypeError
(
"'content' must be a dict."
)
if
any
([
not
isinstance
(
key
,
string_types
)
for
key
in
data
.
keys
()]):
raise
TypeError
(
'content keys must be strings.'
)
self
.
_title
=
''
if
(
title
is
None
)
else
title
self
.
_data
=
{
key
:
_to_immutable
(
value
)
for
key
,
value
in
data
.
items
()}
def
__iter__
(
self
):
items
=
sorted
(
self
.
_data
.
items
(),
key
=
_key_sorting
)
return
iter
([
key
for
key
,
value
in
items
])
def
__repr__
(
self
):
return
_repr
(
self
)
def
__str__
(
self
):
return
_str
(
self
)
def
__eq__
(
self
,
other
):
return
(
isinstance
(
other
,
Error
)
and
self
.
title
==
other
.
title
and
self
.
_data
==
other
.
_data
)
@
property
def
title
(
self
):
return
self
.
_title
def
get_messages
(
self
):
messages
=
[]
for
value
in
self
.
values
():
if
isinstance
(
value
,
Array
):
messages
+=
[
item
for
item
in
value
if
isinstance
(
item
,
string_types
)
]
return
messages
Back
|
FazBrowse Home
|
New Git URL