From 53aa9466e6c10f896425a178548867b4dd9781e2 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 5 Jun 2013 03:11:25 +0200 Subject: utils: new group_by interface for generic grouping The old function _group_by has now an own module (hostinfo.utils) and got a slightly different interface. It is now used in all places, where a grouping of the different values is required (printing, searching, printing keys). --- bin/hostinfo | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/hostinfo b/bin/hostinfo index 2270208..ced856a 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -13,6 +13,7 @@ if os.path.exists(os.path.join(LIB, 'hostinfo')): sys.path = [LIB] + sys.path from hostinfo import printer +from hostinfo import utils def _get_data(path): stream = file(path, 'r') @@ -29,10 +30,10 @@ def match_key(data, keys): rest = keys[1:] if key == 'addresses' and 'addresses' in data: - return match_key({a['interface']: a for a in data}, rest) + return match_key(utils.group_by(data['addresses'], 'interface'), rest) if key == 'ports' and 'ports' in data: - return match_key({p['process']: p for p in data}, rest) + return match_key(utils.group_by(data['ports'], 'process', 'UNKNOWN'), rest) if isinstance(data, dict): if key in data: @@ -117,14 +118,11 @@ def print_keys(path): print "%s%s" % (prefix, key) if key == 'addresses': - for k in set([a['interface'] for a in data[key]]): + for k in utils.group_by(data[key], 'interface').keys(): print "%s%s.%s" % (prefix, key, k) elif key == 'ports': - names = [p['process'] for p in data[key] if 'process' in p] - for k in set(names): + for k in utils.group_by(data[key], 'process', 'UNKNOWN').keys(): print "%s%s.%s" % (prefix, key, k) - if len([p for p in data[key] if 'process' not in p]) > 0: - print "%s%s.%s" % (prefix, key, 'UNKNOWN') elif isinstance(data[key], dict): _print_keys(data[key], "%s%s." % (prefix, key)) elif isinstance(data[key], list): -- cgit v1.2.3-1-g7c22