summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2013-06-05 03:11:25 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2013-06-05 19:26:32 +0200
commit53aa9466e6c10f896425a178548867b4dd9781e2 (patch)
tree2bfc68d0ac428bb6bb632ca4f8abb6b286032deb
parent67177a327c7e3cb542561c6b3d5352faab190d7b (diff)
downloadtools-53aa9466e6c10f896425a178548867b4dd9781e2.tar.gz
tools-53aa9466e6c10f896425a178548867b4dd9781e2.tar.bz2
tools-53aa9466e6c10f896425a178548867b4dd9781e2.zip
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).
-rwxr-xr-xbin/hostinfo12
-rw-r--r--hostinfo/printer.py26
-rw-r--r--hostinfo/utils.py20
3 files changed, 28 insertions, 30 deletions
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):
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
+