|
2 | 2 | import yaml
|
3 | 3 |
|
4 | 4 |
|
5 |
| -def get_links(node, keys=()): |
| 5 | +def get_resources(node, keys=()): |
6 | 6 | """
|
7 |
| - Return a dict of {keys: link} containing each link in the document. |
| 7 | + Return a dict of {url: resource}. |
8 | 8 | """
|
9 |
| - links = {} |
| 9 | + resources = {} |
10 | 10 | for key, link in getattr(node, 'links', {}).items():
|
| 11 | + # Get all the resources at this level |
11 | 12 | index = keys + (key,)
|
12 |
| - links[index] = link |
| 13 | + if link.url not in resources: |
| 14 | + resources[link.url] = {} |
| 15 | + resources[link.url][link.action] = get_resource(index, link) |
13 | 16 | for key, data in getattr(node, 'data', {}).items():
|
| 17 | + # Descend into any nested structures. |
14 | 18 | index = keys + (key,)
|
15 |
| - links.update(get_links(data, index)) |
16 |
| - return links |
| 19 | + resources.update(get_resources(data, index)) |
| 20 | + return resources |
17 | 21 |
|
18 | 22 |
|
19 |
| -def get_url_to_links_mapping(links): |
| 23 | +def get_resource(keys, link): |
| 24 | + display_name = '_'.join(keys) |
| 25 | + path_fields = [field for field in link.fields if field.location == 'path'] |
| 26 | + query_fields = [field for field in link.fields if field.location == 'query'] |
| 27 | + return { |
| 28 | + 'description': link.description |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | +def insert_into(target, keys, value): |
20 | 33 | """
|
21 |
| - Given a dict of {keys: link} containing each link in the document. |
22 |
| - Return a dict of {url: [(keys, link), ...]} |
| 34 | + Nested dictionary insertion. |
| 35 | +
|
| 36 | + >>> example = {} |
| 37 | + >>> insert_into(example, ['a', 'b', 'c'], 123) |
| 38 | + >>> example |
| 39 | + {'a': {'b': {'c': 123}}} |
23 | 40 | """
|
24 |
| - urls = {} |
25 |
| - for keys, link in links.items(): |
26 |
| - if link.url not in urls: |
27 |
| - urls[link.url] = [] |
28 |
| - urls[link.url].append((keys, link)) |
29 |
| - return urls |
| 41 | + for key in keys[:-1]: |
| 42 | + if key not in target: |
| 43 | + target[key] = {} |
| 44 | + target = target[key] |
| 45 | + target[keys[-1]] = value |
30 | 46 |
|
31 | 47 |
|
32 |
| -def get_path_components(urls): |
| 48 | +def layout_resources(resources): |
33 | 49 | """
|
34 |
| - Return a list of path component lists, giving a tree structure |
35 |
| - for the given list of urls. |
| 50 | + Transform a URL-dict of resources into a RAML layout of resources. |
36 | 51 |
|
37 |
| - For example, |
38 |
| - ['/users', '/users/{pk}', '/groups', '/groups/{pk}'] |
| 52 | + { |
| 53 | + '/users': {'get': ...}, |
| 54 | + '/users/{pk}': {'get': ..., 'delete': ...}, |
| 55 | + '/groups': {'get': ...}, |
| 56 | + ] |
39 | 57 |
|
40 | 58 | ==>
|
41 | 59 |
|
42 |
| - [ |
43 |
| - ['/users'], |
44 |
| - ['/users', '/{pk}'], |
45 |
| - ['/groups'], |
46 |
| - ['/groups', '/{pk}'], |
| 60 | + { |
| 61 | + '/users': { |
| 62 | + 'get': ... |
| 63 | + '/{pk}': { |
| 64 | + 'get': ... |
| 65 | + 'delete': ... |
| 66 | + '/groups': { |
| 67 | + 'get': ... |
| 68 | + } |
47 | 69 | ]
|
48 | 70 | """
|
49 |
| - ret = [] |
| 71 | + output = {} |
50 | 72 | parents = ['']
|
51 | 73 |
|
52 |
| - for url in sorted(set(urls)): |
| 74 | + items = sorted(resources.items(), key=lambda item: item[0]) |
| 75 | + for url, resource in items: |
53 | 76 | while parents:
|
54 | 77 | parent_url = ''.join(parents)
|
55 | 78 | if url.startswith(parent_url):
|
56 |
| - component = url[len(parent_url):] |
| 79 | + component = '/' + url[len(parent_url):].strip('/') |
57 | 80 | parents.append(component)
|
58 |
| - ret.append(parents[1:]) |
| 81 | + insert_into(output, parents[1:], resource) |
59 | 82 | break
|
60 | 83 | parents.pop()
|
61 | 84 |
|
62 |
| - return ret |
63 |
| - |
64 |
| - |
65 |
| -def get_resource(keys, link): |
66 |
| - display_name = '_'.join(keys) |
67 |
| - return { |
68 |
| - 'displayName': display_name, |
69 |
| - 'description': link.description |
70 |
| - } |
| 85 | + return output |
71 | 86 |
|
72 | 87 |
|
73 | 88 | def encode_raml(document):
|
74 |
| - links = get_links(document) |
75 |
| - urls = [link.url for link in links.values()] |
76 |
| - urls_to_links = get_url_to_links_mapping(links) |
77 |
| - |
78 |
| - for components in get_path_components(urls): |
79 |
| - url = ''.join(components) |
80 |
| - links = urls_to_links[url] |
81 |
| - for keys, link in links: |
82 |
| - index = ['/' + component.lstrip('/') for component in components] |
83 |
| - index.append(link.action) |
84 |
| - print index, get_resource(keys, link) |
85 |
| - |
86 |
| - ##print components, [link.action for keys, link in urls_to_links[url]] |
87 |
| - |
| 89 | + resources = get_resources(document) |
| 90 | + resources = layout_resources(resources) |
88 | 91 | structure = {
|
89 | 92 | 'title': document.title,
|
90 |
| - 'baseUri': document.url, |
| 93 | + 'baseUri': 'http://127.0.0.1:8000' #document.url, |
91 | 94 | }
|
92 |
| - return yaml.safe_dump(structure, default_flow_style=False) |
| 95 | + structure.update(resources) |
| 96 | + output = '#%RAML 0.8\n' |
| 97 | + output += yaml.safe_dump(structure, default_flow_style=False) |
| 98 | + return output |
0 commit comments