def format_resource(self, resource): res_info = self._specs.get_resource(resource.path) if not res_info: return render(f'~~{resource.path}~~ **does not exists.**') resource_name = resource.path.split('/')[-2] resource_name = self._p.singular_noun(resource_name) lines = [ f'# {resource_name.title()} resource', f'**path: /{resource.path}**', ] lines.extend(self._format_heading(res_info)) actions = self._specs.get_actions(resource.path) if actions: lines.append('### Available actions') for name, summary in actions: if summary: lines.append(f'* {name} - {summary}') else: lines.append(f'* {name}') lines.append('') nested = self._specs.get_nested_collections(resource.path) if nested: lines.append('### Available nested collections') for name, summary in nested: if summary: lines.append(f'* {name} - {summary}') else: lines.append(f'* {name}') return render('\n'.join(lines))
def print_operation(self, specs): lines = [ f'# {specs.name.title()} {specs.collection_name}', '', ] params = specs.info.get('parameters') if params: lines += [ '', '## Parameters', '', ] if specs.name == 'search': for param in specs.info.get('parameters'): if '$ref' in param: continue name = param['name'] description = param['description'].splitlines() req = '(*)' if param.get('required') else '' lines.append(f'**{name}** {req}') lines += [ f' {line}' for line in description ] return render('\n'.join(lines))
def format_action(self, action): action_info = self._specs.get_action(action.path) if not action_info: return render(f'~~{action.path}~~ **does not exists.**') _, action_name = action.path.rsplit('/', 1) lines = [ f'# {action_name.title()} action', f'**path: /{action.path}**', ] lines.extend(self._format_heading(action_info)) lines.append('## Available methods') for method in ('get', 'post', 'put', 'delete'): if method in action_info: lines.append(f'* {method.upper()}: .{method}()') return render('\n'.join(lines))
def print_namespace(self, specs): lines = [ f'# {specs.name.title()} namespace', '', '## Available collections', '', '', ] lines += [f'- {res}' for res in specs.collections.keys()] return '\n' + render('\n'.join(lines))
def cmd_list_account(config): if not config.accounts: raise ClickException('No account configured.') accounts = [AVAILABLE_ACCOUNTS] for acc in config.accounts.values(): active = ' ' if acc.id == config.active.id: active = '\u2713' accounts.append(f'| {acc.id} | {acc.name} | {active} |\n', ) click.echo(render(''.join(accounts)))
def format_ns(self, ns): namespaces = self._specs.get_nested_namespaces(ns.path) collections = self._specs.get_namespaced_collections(ns.path) if not (collections or namespaces): return render(f'~~{ns.path}~~ **does not exists.**') lines = [ f'# {ns.path.title()} namespace', f'**path: /{ns.path}**', ] if namespaces: lines.append('## Available namespaces') for namespace in namespaces: lines.append(f'* {namespace}') if collections: lines.append('## Available collections') for collection in collections: lines.append(f'* {collection}') return render('\n'.join(lines))
def list_reports(repo_dir): repo = load_repo(repo_dir) repo_info = [ f'# {repo.name} version {repo.version}\n', '---\n\n', repo.description, '\n\n---\n\n', ] if repo.reports: repo_info.append(AVAILABLE_REPORTS) for report in repo.reports: repo_info.append(f'| {report.local_id} | {report.name} |\n') click.echo(render(''.join(repo_info)))
def format_resource_set(self, rs): col_info = self._specs.get_collection(rs.path) if not col_info: return render(f'~~{rs.path}~~ **does not exists.**') if '/' in rs.path: _, collection_name = rs.path.rsplit('/', 1) else: collection_name = rs.path lines = [ f'# Search the {collection_name.title()} collection', f'**path: /{rs.path}**', ] lines.extend(self._format_heading(col_info)) get_info = col_info['get'] params = get_info.get('parameters') if not params: return render('\n'.join(lines)) filters = list( sorted(filter(lambda x: '$ref' not in x, params), key=lambda x: x['name'])) lines.append(f'Support pagination: *{len(params) > len(filters)}*') if not filters: return render('\n'.join(lines)) lines.append('## Available filters') for filter_ in filters: lines.append(f'*{filter_["name"]}*') description = filter_['description'].splitlines() for line in description: line = line.strip() if line: lines.append(f' {line}') lines.append('') return render('\n'.join(lines))
def print_api(self, specs): lines = [ *_SPACE, f'# Welcome to {specs.title} {specs.version}', '', '', '## Introduction' '', ] + specs.description.splitlines() + ['<br><br>'] tagged = OrderedDict() for t in specs.tags.keys(): collections = [ name for name in specs.collections.keys() if specs.collections[name].tag == t ] namespaces = [ name for name in specs.namespaces.keys() if specs.namespaces[name].tag == t ] if collections or namespaces: tagged[t] = { 'namespaces': namespaces, 'collections': collections, } lines += [ '\n', '\n', ] for tag, children in tagged.items(): lines.append(f'## {tag}') lines.append('\n\n') if specs.tags[tag]: lines += specs.tags[tag].splitlines() lines.append('\n\n') if children['namespaces']: lines.append('#### Namespaces') for nsname in children['namespaces']: lines.append(f'* {nsname}') lines.append('\n\n') if children['collections']: lines.append('#### Collections') for colname in children['collections']: lines.append(f'* {colname}') lines.append('\n\n') return render('\n'.join(lines))
def format_collection(self, collection): col_info = self._specs.get_collection(collection.path) if not col_info: return render(f'~~{collection.path}~~ **does not exists.**') if '/' in collection.path: _, collection_name = collection.path.rsplit('/', 1) else: collection_name = collection.path lines = [ f'# {collection_name.title()} collection', f'**path: /{collection.path}**', ] lines.extend(self._format_heading(col_info)) lines.append('### Available operations') if 'get' in col_info: lines.append(f'* GET: {_COL_HTTP_METHOD_TO_METHOD["get"]}') if 'post' in col_info: lines.append(f'* POST: {_COL_HTTP_METHOD_TO_METHOD["post"]}') return render('\n'.join(lines))
def print_action(self, specs): lines = [ *_SPACE, f'# {specs.name.title()} action', '', ] if specs.summary: lines += ['## Summary'] + _SPACE lines += specs.summary.splitlines() + _SPACE if specs.description: lines += ['## Description'] + _SPACE lines += specs.description.splitlines() + _SPACE lines += ['## Methods'] lines += [f'- {res}' for res in specs.methods.keys()] + _SPACE return render('\n'.join(lines))
def show_report_info(repo_dir, local_id): repo = load_repo(repo_dir) report = get_report_by_id(repo, local_id) report_info = [ f'# {report.name} (ID: {report.local_id})\n', '---\n\n', report.description, '\n\n---\n\n', AVAILABLE_RENDERERS, ] for renderer in report.renderers: default = ' ' if renderer.id == report.default_renderer: default = '\u2713' report_info.append( f'| {renderer.id} | {renderer.description} | {default} |\n') click.echo(render(''.join(report_info)))
def print_resource(self, specs): lines = [] if specs.summary: lines += ['## Summary'] + _SPACE lines += specs.summary.splitlines() + _SPACE if specs.description: lines += ['## Description'] + _SPACE lines += specs.description.splitlines() + _SPACE if specs.actions: lines += ['## Actions'] + _SPACE lines += [f'- {res}' for res in specs.actions.keys()] + _SPACE if specs.collections: lines += ['## Nested collections'] + _SPACE lines += [f'- {res}' for res in specs.collections.keys()] + _SPACE if lines: return '\n' + render('\n'.join(lines)) return ''
def print_collection(self, specs): lines = _SPACE + [ f'# {specs.name.title()} collection', '', ] if specs.summary: lines += ['## Summary'] + _SPACE lines += specs.summary.splitlines() + _SPACE if specs.description: lines += ['## Description'] + _SPACE lines += specs.description.splitlines() + _SPACE if specs.operations: lines += ['## Operations'] lines += [ f'- {res}' for res in specs.operations.keys() if res not in ('get', 'update', 'delete') ] + _SPACE return render('\n'.join(lines))
def format(self, obj): if not self._specs: return render('**No OpenAPI specs available.**') if isinstance(obj, NS): return self.format_ns(obj) if isinstance(obj, Collection): return self.format_collection(obj) if isinstance(obj, Resource): return self.format_resource(obj) if isinstance(obj, Action): return self.format_action(obj) if isinstance(obj, ResourceSet): return self.format_resource_set(obj) return self.format_client()
def format_client(self): lines = [ f'# Welcome to {self._specs.title} {self._specs.version}', '## Introduction', ] + self._specs.description.splitlines() lines += [ '', '## Namespaces', ] for ns in self._specs.get_namespaces(): lines.append(f'* {ns}') lines += [ '', '## Collections', ] for col in self._specs.get_collections(): lines.append(f'* {col}') return render('\n'.join(lines))