forked from core-api/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
70 lines (48 loc) · 1.83 KB
/
Copy pathbase.py
File metadata and controls
70 lines (48 loc) · 1.83 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
63
64
65
66
67
68
69
70
from coreapi.compat import string_types
import itypes
# Helper functions to get an expected type from a dictionary.
def dereference(lookup_string, struct):
"""
Dereference a JSON pointer.
http://tools.ietf.org/html/rfc6901
"""
keys = lookup_string.strip('#/').split('/')
node = struct
for key in keys:
node = _get_dict(node, key)
return node
def is_json_pointer(value):
return isinstance(value, dict) and ('$ref' in value) and (len(value) == 1)
def _get_string(item, key, default=''):
value = item.get(key)
return value if isinstance(value, string_types) else default
def _get_dict(item, key, default={}, dereference_using=None):
value = item.get(key)
if isinstance(value, dict):
if dereference_using and is_json_pointer(value):
return dereference(value['$ref'], dereference_using)
return value
return default.copy()
def _get_list(item, key, default=[]):
value = item.get(key)
return value if isinstance(value, list) else list(default)
def _get_bool(item, key, default=False):
value = item.get(key)
return value if isinstance(value, bool) else default
# Helper functions to get an expected type from a list.
def get_dicts(item, dereference_using=None):
ret = [value for value in item if isinstance(value, dict)]
if dereference_using:
return [
dereference(value['$ref'], dereference_using) if is_json_pointer(value) else value
for value in ret
]
return ret
def get_strings(item):
return [value for value in item if isinstance(value, string_types)]
class BaseCodec(itypes.Object):
media_type = None
def load(self, bytes, base_url=None):
raise NotImplementedError() # pragma: nocover
def dump(self, document, **kwargs):
raise NotImplementedError() # pragma: nocover