Skip to content
This repository was archived by the owner on Aug 26, 2024. It is now read-only.

Commit d89cce1

Browse files
committed
First working pass
1 parent 0ce30e9 commit d89cce1

File tree

1 file changed

+60
-54
lines changed

1 file changed

+60
-54
lines changed

raml_codec/encode.py

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,97 @@
22
import yaml
33

44

5-
def get_links(node, keys=()):
5+
def get_resources(node, keys=()):
66
"""
7-
Return a dict of {keys: link} containing each link in the document.
7+
Return a dict of {url: resource}.
88
"""
9-
links = {}
9+
resources = {}
1010
for key, link in getattr(node, 'links', {}).items():
11+
# Get all the resources at this level
1112
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)
1316
for key, data in getattr(node, 'data', {}).items():
17+
# Descend into any nested structures.
1418
index = keys + (key,)
15-
links.update(get_links(data, index))
16-
return links
19+
resources.update(get_resources(data, index))
20+
return resources
1721

1822

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):
2033
"""
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}}}
2340
"""
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
3046

3147

32-
def get_path_components(urls):
48+
def layout_resources(resources):
3349
"""
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.
3651
37-
For example,
38-
['/users', '/users/{pk}', '/groups', '/groups/{pk}']
52+
{
53+
'/users': {'get': ...},
54+
'/users/{pk}': {'get': ..., 'delete': ...},
55+
'/groups': {'get': ...},
56+
]
3957
4058
==>
4159
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+
}
4769
]
4870
"""
49-
ret = []
71+
output = {}
5072
parents = ['']
5173

52-
for url in sorted(set(urls)):
74+
items = sorted(resources.items(), key=lambda item: item[0])
75+
for url, resource in items:
5376
while parents:
5477
parent_url = ''.join(parents)
5578
if url.startswith(parent_url):
56-
component = url[len(parent_url):]
79+
component = '/' + url[len(parent_url):].strip('/')
5780
parents.append(component)
58-
ret.append(parents[1:])
81+
insert_into(output, parents[1:], resource)
5982
break
6083
parents.pop()
6184

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
7186

7287

7388
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)
8891
structure = {
8992
'title': document.title,
90-
'baseUri': document.url,
93+
'baseUri': 'http://127.0.0.1:8000' #document.url,
9194
}
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy