From b5f373fad21a9ae97f02933ac216d65e98645928 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 20 Apr 2018 18:41:18 +0300 Subject: Change: icktool status tabular output --- icktool | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 8 deletions(-) (limited to 'icktool') diff --git a/icktool b/icktool index 81dd035..a6cfa1f 100755 --- a/icktool +++ b/icktool @@ -121,23 +121,37 @@ class Icktool(cliapp.Application): self._prettyson(version) def cmd_status(self, args): + table = Table() + table.set_columns('project', 'status', 'build_status', 'log_id') + api = self._new_api() token = self._new_token(api) api.set_token(token) + projects = api.show('/projects') builds = api.show('/builds') + for project in projects['projects']: project_name = project['project'] - status = api.show('/projects/{}/status'.format(project_name)) + project_status = api.show( + '/projects/{}/status'.format(project_name)) + + row = { + 'project': project_name, + 'status': project_status['status'], + 'build_status': 'n/a', + 'log_id': 'n/a' + } + + build = self._latest_build(project_name, builds) - self._prettyson(builds) if build: - actions = build['actions'] - current = build['current_action'] - log = build['log'] - print(project_name, status['status'], log, actions[current]) - else: - print(project_name, status['status'], 'no builds') + row['build_status'] = build['status'] + row['log_id'] = build['log'] + + table.append_row(**row) + + self.output.write(table.format()) def _latest_build(self, project_name, builds): builds = [b for b in builds['builds'] if b['project'] == project_name] @@ -217,4 +231,58 @@ class Icktool(cliapp.Application): self.output.write('\n') +class Table: + + def __init__(self): + self._column_names = None + self._rows = [] + + def set_columns(self, *column_names): + self._column_names = column_names + + def append_row(self, **kwargs): + self._rows.append(kwargs) + + def format(self): + assert self._column_names is not None + headings = { + key: key + for key in self._column_names + } + self._rows.insert(0, headings) + widths = self._column_widths() + underlines = { + key: '-' * widths[key] + for key in self._column_names + } + self._rows.insert(1, underlines) + lines = [self._format_row(widths, row) for row in self._rows] + return ''.join('{}\n'.format(line) for line in lines) + + def _format_headings(self, widths): + row = { + key: key + for key in self._column_names + } + return self._format_row(widths, row) + + def _format_row(self, widths, row): + return ' | '.join( + self._format_cell(widths[x], row[x]) + for x in self._column_names + ) + + def _format_cell(self, width, value): + return '%*s' % (width, value) + + def _column_widths(self): + return { + key: self._width(key) + for key in self._column_names + } + + def _width(self, column_name): + return max(len(str(row[column_name])) for row in self._rows) + + Icktool(version=ick2.__version__).run() -- cgit v1.2.1