|
1 | 1 | from io import StringIO |
| 2 | +from functools import partial |
2 | 3 |
|
3 | 4 | import requests |
4 | 5 | import yaml |
@@ -60,8 +61,9 @@ def get_namespaced_collections(self, path): |
60 | 61 | nested = filter(lambda x: x[1:].startswith(path), self._specs['paths'].keys()) |
61 | 62 | collections = set() |
62 | 63 | for p in nested: |
63 | | - splitted = p[1:].split('/', 2) |
64 | | - collections.add(splitted[1]) |
| 64 | + splitted = p[len(path) + 1:].split('/', 2) |
| 65 | + if self._is_collection(p) and len(splitted) == 2: |
| 66 | + collections.add(splitted[1]) |
65 | 67 | return list(sorted(collections)) |
66 | 68 |
|
67 | 69 | def get_collection(self, path): |
@@ -94,6 +96,28 @@ def get_actions(self, path): |
94 | 96 | for name in sorted(actions) |
95 | 97 | ] |
96 | 98 |
|
| 99 | + def get_nested_namespaces(self, path): |
| 100 | + def _is_nested_namespace(base_path, path): |
| 101 | + if path[1:].startswith(base_path): |
| 102 | + comp = path[1:].split('/') |
| 103 | + return ( |
| 104 | + len(comp) > 1 |
| 105 | + and not comp[-1].startswith('{') |
| 106 | + ) |
| 107 | + return False |
| 108 | + |
| 109 | + nested = filter( |
| 110 | + partial(_is_nested_namespace, path), |
| 111 | + self._specs['paths'].keys(), |
| 112 | + ) |
| 113 | + current_level = len(path[1:].split('/')) |
| 114 | + nested_namespaces = [] |
| 115 | + for ns in nested: |
| 116 | + name = ns[1:].split('/')[current_level] |
| 117 | + if not self._is_collection(f'/{path}/{name}'): |
| 118 | + nested_namespaces.append(name) |
| 119 | + return nested_namespaces |
| 120 | + |
97 | 121 | def get_nested_collections(self, path): |
98 | 122 | p = self._get_path(path) |
99 | 123 | nested = filter( |
@@ -162,3 +186,15 @@ def _get_info(self, path): |
162 | 186 | def _is_action(self, operation_id): |
163 | 187 | op_id_cmps = operation_id.rsplit('_', 2) |
164 | 188 | return op_id_cmps[-2] not in ('list', 'retrieve') |
| 189 | + |
| 190 | + def _is_collection(self, path): |
| 191 | + path_length = len(path[1:].split('/')) |
| 192 | + for p in self._specs['paths'].keys(): |
| 193 | + comp = p[1:].split('/') |
| 194 | + if not p.startswith(path): |
| 195 | + continue |
| 196 | + if p == path: |
| 197 | + return True |
| 198 | + if len(comp) > path_length and comp[path_length].startswith('{'): |
| 199 | + return True |
| 200 | + return False |
0 commit comments