From 897c10d729bb5e32e2286e0897fbc08d89b42789 Mon Sep 17 00:00:00 2001 From: tengqm Date: Tue, 20 Jan 2015 23:31:06 +0800 Subject: [PATCH] Added capabiity to print nested table --- senlinclient/common/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/senlinclient/common/utils.py b/senlinclient/common/utils.py index 913a832f..13bb83f4 100644 --- a/senlinclient/common/utils.py +++ b/senlinclient/common/utils.py @@ -46,6 +46,25 @@ def import_versioned_module(version, submodule=None): return importutils.import_module(module) +def format_nested_dict(d, fields, column_names): + pt = prettytable.PrettyTable(caching=False, print_empty=False, + header=True, field_names=column_names) + for n in column_names: + pt.align[n] = 'l' + + for field in d.keys(): + value = d[field] + if value is six.string_types: + pt.add_row([field, value]) + else: + pt.add_row([field, jsonutils.dumps(value, indent=2, + ensure_ascii=False)]) + + return pt.get_string() + +def nested_dict_formatter(d, column_names): + return lambda o: format_nested_dict(o, d, column_names) + def json_formatter(js): return jsonutils.dumps(js, indent=2, ensure_ascii=False)