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). --- hostinfo/printer.py | 26 +++----------------------- hostinfo/utils.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 hostinfo/utils.py (limited to 'hostinfo') diff --git a/hostinfo/printer.py b/hostinfo/printer.py index 5edc76f..4302e04 100644 --- a/hostinfo/printer.py +++ b/hostinfo/printer.py @@ -2,6 +2,7 @@ from __future__ import absolute_import from hostinfo import prefix +from hostinfo import utils def _get_full_key(prev_key, key): if prev_key == '': @@ -24,15 +25,6 @@ def _space(filter_key, full_key, printer, force=False): if filter_key is None and full_key == '': printer.space(force) -def _group_by(value, group, display_check, print_value): - groups = {} - for elem in value: - key = group(elem) - if key is not None and display_check(key): - groups.setdefault(key, []).append(print_value(elem)) - - return groups - class Printer: labels = { @@ -55,25 +47,13 @@ class Printer: prefix.Printer.flags = flags def cb_print_addresses(self, value, full_key, filter_key): - def _group_ip(address): - if 'vserver' not in address: - return address['interface'] - else: - return None - def _print_ip(address): return '%s/%s' % (address['address'], address['netmask']) display_check = self._is_group_displayed(full_key, filter_key) - return _group_by(value, _group_ip, display_check, _print_ip) + return utils.group_by(value, 'interface', None, display_check, _print_ip) def cb_print_ports(self, value, full_key, filter_key): - def _group_port(port): - if 'process' in port: - return port['process'] - else: - return 'UNKNOWN' - def _print_port(port): if port['proto'] in ['tcp6', 'udp6']: return '(%s) [%s]:%s' % (port['proto'].replace('6', ''), @@ -81,7 +61,7 @@ class Printer: return '(%s) %s:%s' % (port['proto'], port['ip'], port['port']) display_check = self._is_group_displayed(full_key, filter_key) - return (_group_by(value, _group_port, display_check, _print_port), + return (utils.group_by(value, 'process', 'UNKNOWN', display_check, _print_port), ['sshd', 'nrpe', 'munin-node']) def cb_print_vserver(self, value, full_key, filter_key): diff --git a/hostinfo/utils.py b/hostinfo/utils.py new file mode 100644 index 0000000..cfd8de3 --- /dev/null +++ b/hostinfo/utils.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +def group_by(value, group, default=None, display_check=None, print_value=None): + groups = {} + for elem in value: + key = None + if group in elem: + key = elem[group] + else: + key = default + + if display_check is None or display_check(key): + output = elem + if print_value is not None: + output = print_value(elem) + + groups.setdefault(key, []).append(output) + + return groups + -- cgit v1.2.3-1-g7c22