From bb1710a6add4991803e64dd0039ea51e310e566a Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Sat, 25 Nov 2017 09:20:22 +0100 Subject: [PATCH] tabulate: formatted datetime cells --- renga/models/_tabulate.py | 14 ++++++++++++-- tests/test_cli.py | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/renga/models/_tabulate.py b/renga/models/_tabulate.py index 626b40b8e4..8e6bd56da2 100644 --- a/renga/models/_tabulate.py +++ b/renga/models/_tabulate.py @@ -17,12 +17,22 @@ # limitations under the License. """Print a collection as a table.""" +from datetime import datetime from operator import attrgetter from tabulate import tabulate as tblte -def tabulate(collection, headers, **kwargs): +def format_cell(cell, datetime_fmt=None): + """Format a cell.""" + if datetime_fmt and isinstance(cell, datetime): + return cell.strftime(datetime_fmt) + return cell + + +def tabulate(collection, headers, datetime_fmt='%Y-%m-%d %H:%M:%S', + **kwargs): """Pretty-print a collection.""" - table = [attrgetter(*headers)(c) for c in collection] + table = [(format_cell(cell, datetime_fmt=datetime_fmt) + for cell in attrgetter(*headers)(c)) for c in collection] return tblte(table, headers=[h.upper() for h in headers], **kwargs) diff --git a/tests/test_cli.py b/tests/test_cli.py index 038f9b134f..16755ac81e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -214,7 +214,7 @@ def test_deployer(runner, deployer_responses): result = runner.invoke(cli.cli, ['contexts', 'list']) assert result.exit_code == 0 assert context_id in result.output - assert '1984-01-01 00:00:00+00:00' in result.output + assert '1984-01-01 00:00:00' in result.output result = runner.invoke(cli.cli, ['contexts', 'run', context_id, 'docker']) assert result.exit_code == 0 @@ -226,7 +226,7 @@ def test_deployer(runner, deployer_responses): assert result.exit_code == 0 assert execution_id in result.output assert 'running' in result.output - assert '1984-01-01 00:00:00+00:00' in result.output + assert '1984-01-01 00:00:00' in result.output result = runner.invoke(cli.cli, ['executions', 'logs', context_id, execution_id])