|
| 1 | +import inflect |
| 2 | + |
| 3 | +from cmr import render |
| 4 | +from cnct.client.models import Action, Collection, NS, Resource, ResourceSet |
| 5 | + |
| 6 | + |
| 7 | +_COL_HTTP_METHOD_TO_METHOD = { |
| 8 | + 'get': '.all(), .filter(), .first(), .last()', |
| 9 | + 'post': '.create()', |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +class DefaultFormatter: |
| 14 | + |
| 15 | + def __init__(self, specs): |
| 16 | + self._specs = specs |
| 17 | + self._p = inflect.engine() |
| 18 | + |
| 19 | + def format_client(self): |
| 20 | + lines = [ |
| 21 | + f'# Welcome to {self._specs.title} {self._specs.version}', |
| 22 | + '## Introduction' |
| 23 | + ] + self._specs.description.splitlines() |
| 24 | + |
| 25 | + lines += [ |
| 26 | + '', |
| 27 | + '## Namespaces' |
| 28 | + ] |
| 29 | + for ns in self._specs.get_namespaces(): |
| 30 | + lines.append(f'* {ns}') |
| 31 | + |
| 32 | + lines += [ |
| 33 | + '', |
| 34 | + '## Collections' |
| 35 | + ] |
| 36 | + for col in self._specs.get_collections(): |
| 37 | + lines.append(f'* {col}') |
| 38 | + |
| 39 | + return render('\n'.join(lines)) |
| 40 | + |
| 41 | + def format_ns(self, ns): |
| 42 | + collections = self._specs.get_namespaced_collections(ns.path) |
| 43 | + if not collections: |
| 44 | + return render(f'~~{ns.path}~~ **does not exists.**') |
| 45 | + |
| 46 | + lines = [ |
| 47 | + f'# {ns.path.title()} namespace', |
| 48 | + f'**path: /{ns.path}**', |
| 49 | + '## Available collections', |
| 50 | + ] |
| 51 | + |
| 52 | + for collection in collections: |
| 53 | + lines.append(f'* {collection}') |
| 54 | + |
| 55 | + return render('\n'.join(lines)) |
| 56 | + |
| 57 | + def format_collection(self, collection): |
| 58 | + col_info = self._specs.get_collection(collection.path) |
| 59 | + if not col_info: |
| 60 | + return render(f'~~{collection.path}~~ **does not exists.**') |
| 61 | + |
| 62 | + if '/' in collection.path: |
| 63 | + _, collection_name = collection.path.rsplit('/', 1) |
| 64 | + else: |
| 65 | + collection_name = collection.path |
| 66 | + |
| 67 | + lines = [ |
| 68 | + f'# {collection_name.title()} collection', |
| 69 | + f'**path: /{collection.path}**', |
| 70 | + ] |
| 71 | + |
| 72 | + lines.extend(self._format_heading(col_info)) |
| 73 | + lines.append('### Available operations') |
| 74 | + if 'get' in col_info: |
| 75 | + lines.append(f'* GET: {_COL_HTTP_METHOD_TO_METHOD["get"]}') |
| 76 | + |
| 77 | + if 'post' in col_info: |
| 78 | + lines.append(f'* POST: {_COL_HTTP_METHOD_TO_METHOD["post"]}') |
| 79 | + |
| 80 | + return render('\n'.join(lines)) |
| 81 | + |
| 82 | + def format_resource(self, resource): |
| 83 | + res_info = self._specs.get_resource(resource.path) |
| 84 | + if not res_info: |
| 85 | + return render(f'~~{resource.path}~~ **does not exists.**') |
| 86 | + |
| 87 | + resource_name = resource.path.split('/')[-2] |
| 88 | + resource_name = self._p.singular_noun(resource_name) |
| 89 | + lines = [ |
| 90 | + f'# {resource_name.title()} resource', |
| 91 | + f'**path: /{resource.path}**', |
| 92 | + ] |
| 93 | + |
| 94 | + lines.extend(self._format_heading(res_info)) |
| 95 | + |
| 96 | + actions = self._specs.get_actions(resource.path) |
| 97 | + if actions: |
| 98 | + lines.append('### Available actions') |
| 99 | + for name, summary in actions: |
| 100 | + if summary: |
| 101 | + lines.append(f'* {name} - {summary}') |
| 102 | + else: |
| 103 | + lines.append(f'* {name}') |
| 104 | + lines.append('') |
| 105 | + |
| 106 | + nested = self._specs.get_nested_collections(resource.path) |
| 107 | + if nested: |
| 108 | + lines.append('### Available nested collections') |
| 109 | + for name, summary in nested: |
| 110 | + if summary: |
| 111 | + lines.append(f'* {name} - {summary}') |
| 112 | + else: |
| 113 | + lines.append(f'* {name}') |
| 114 | + |
| 115 | + return render('\n'.join(lines)) |
| 116 | + |
| 117 | + def format_action(self, action): |
| 118 | + action_info = self._specs.get_action(action.path) |
| 119 | + if not action_info: |
| 120 | + return render(f'~~{action.path}~~ **does not exists.**') |
| 121 | + |
| 122 | + _, action_name = action.path.rsplit('/', 1) |
| 123 | + |
| 124 | + lines = [ |
| 125 | + f'# {action_name.title()} action', |
| 126 | + f'**path: /{action.path}**', |
| 127 | + ] |
| 128 | + lines.extend(self._format_heading(action_info)) |
| 129 | + lines.append('## Available methods') |
| 130 | + for method in ('get', 'post', 'put', 'delete'): |
| 131 | + if method in action_info: |
| 132 | + lines.append(f'* {method.upper()}: .{method}()') |
| 133 | + |
| 134 | + return render('\n'.join(lines)) |
| 135 | + |
| 136 | + def format_resource_set(self, rs): |
| 137 | + col_info = self._specs.get_collection(rs.path) |
| 138 | + if not col_info: |
| 139 | + return render(f'~~{rs.path}~~ **does not exists.**') |
| 140 | + |
| 141 | + if '/' in rs.path: |
| 142 | + _, collection_name = rs.path.rsplit('/', 1) |
| 143 | + else: |
| 144 | + collection_name = rs.path |
| 145 | + |
| 146 | + lines = [ |
| 147 | + f'# Search the {collection_name.title()} collection', |
| 148 | + f'**path: /{rs.path}**', |
| 149 | + ] |
| 150 | + lines.extend(self._format_heading(col_info)) |
| 151 | + get_info = col_info['get'] |
| 152 | + params = get_info.get('parameters') |
| 153 | + if not params: |
| 154 | + return render('\n'.join(lines)) |
| 155 | + filters = list(sorted(filter(lambda x: '$ref' not in x, params), key=lambda x: x['name'])) |
| 156 | + lines.append(f'Support pagination: *{len(params) > len(filters)}*') |
| 157 | + if not filters: |
| 158 | + return render('\n'.join(lines)) |
| 159 | + |
| 160 | + lines.append('## Available filters') |
| 161 | + for filter_ in filters: |
| 162 | + lines.append(f'*{filter_["name"]}*') |
| 163 | + description = filter_['description'].splitlines() |
| 164 | + for line in description: |
| 165 | + line = line.strip() |
| 166 | + if line: |
| 167 | + lines.append(f' {line}') |
| 168 | + lines.append('') |
| 169 | + |
| 170 | + return render('\n'.join(lines)) |
| 171 | + |
| 172 | + def format(self, obj): |
| 173 | + if not self._specs: |
| 174 | + return render('**No OpenAPI specs available.**') |
| 175 | + |
| 176 | + if isinstance(obj, NS): |
| 177 | + return self.format_ns(obj) |
| 178 | + |
| 179 | + if isinstance(obj, Collection): |
| 180 | + return self.format_collection(obj) |
| 181 | + |
| 182 | + if isinstance(obj, Resource): |
| 183 | + return self.format_resource(obj) |
| 184 | + |
| 185 | + if isinstance(obj, Action): |
| 186 | + return self.format_action(obj) |
| 187 | + |
| 188 | + if isinstance(obj, ResourceSet): |
| 189 | + return self.format_resource_set(obj) |
| 190 | + |
| 191 | + return self.format_client() |
| 192 | + |
| 193 | + def _format_heading(self, info): |
| 194 | + lines = [] |
| 195 | + for section in ('summary', 'description'): |
| 196 | + if section in info: |
| 197 | + lines.append(f'### {section.title()}') |
| 198 | + lines.append(info[section]) |
| 199 | + return lines |
0 commit comments