-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.py
93 lines (71 loc) · 2.6 KB
/
encode.py
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
from collections import OrderedDict
from coreapi.document import Link
from . import helpers
def get_spore_method_name(keys: list, action: str) -> str:
"""
TODO: better annotations in python3.6
TODO: enhance name of func. not optimal yet !
"""
if not keys:
return action
object_name = '_'.join(keys)
if action in ('list', ):
if not object_name.endswith('s'):
object_name = object_name + 's'
elif object_name.endswith('s'):
object_name = object_name.rstrip('s')
return '{}_{}'.format(action, object_name)
def build_method_from_link(link: Link, formats: list) -> OrderedDict:
method_attributes = [
('path', link.url),
('method', link.action),
('required_params', [
field.name for field in link.fields
if field.required and field.location in ('path', 'query')
]),
('optional_params', [
field.name for field in link.fields
if not field.required and field.location == 'query'
]),
('payload_required', link.action in ('post', 'put', 'patch'))
]
if getattr(link, 'encoding', ''):
method_attributes.append(('payload_format', link.encoding))
# TODO: enhance this part
if set(formats) != set(link.formats):
method_attributes.append(('formats', link.formats))
method_attributes.extend([
('authentication', link.authentication),
('documentation', link.description)
])
return OrderedDict(method_attributes)
def get_spore_methods_from_document(node, keys=(), formats=()):
links = []
for key, child in getattr(node, 'data', {}).items():
index = keys + (key, )
links.extend(get_spore_methods_from_document(child, index, formats))
for action, link in getattr(node, 'links', {}).items():
links.append((
get_spore_method_name(keys, action),
build_method_from_link(link, formats)
))
return links
def generate_spore_object(document, global_formats, spore_settings):
"""
TODO: spore settings definition and integration
"""
methods = get_spore_methods_from_document(document, (), global_formats)
for _, link in methods:
link['path'] = link['path'].replace(document.url, '/')
spore = OrderedDict()
spore["name"] = document.title
spore["base_url"] = document.url
spore["version"] = ''
spore["formats"] = global_formats
spore["methods"] = OrderedDict(methods)
spore["authority"] = ''
spore["meta"] = {
"authors": [],
"documentation": document.description
}
return spore