From dcb4804af80238935a34ca9e3d37bc88335eac7f Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 31 May 2013 03:25:19 +0200 Subject: hostinfo: fix some pylint --- bin/hostinfo | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/bin/hostinfo b/bin/hostinfo index 4c87e98..c5bb9c3 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -2,16 +2,15 @@ # -*- coding: utf-8 -*- import sys -import socket import argparse import yaml import os -from dns import resolver,reversename +from dns import resolver, reversename -own_directory = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) -lib = os.path.join(own_directory, '..') -if os.path.exists(os.path.join(lib, 'hostinfo')): - sys.path = [lib] + sys.path +OWN_DIRECTORY = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) +LIB = os.path.join(OWN_DIRECTORY, '..') +if os.path.exists(os.path.join(LIB, 'hostinfo')): + sys.path = [LIB] + sys.path from hostinfo import printer @@ -65,7 +64,6 @@ def match(host, search): key_elem = search.split('.') data = host - prev_key = None result = match_key(data, key_elem) if negate: @@ -82,10 +80,12 @@ def print_search(basepath, flags, search): metadata = os.path.join(basepath, 'metadata', 'hosts') if not os.path.exists(metadata): - sys.stderr.write("Invalid hostinfo data. 'metadata/hosts' not found!\n\n") + sys.stderr.write("Invalid hostinfo data. " + "'metadata/hosts' not found!\n") sys.exit(1) hosts = _get_data(metadata) + length = [len(_get_label(host)) for host in hosts['hosts']] for host in hosts['hosts']: data = _get_data(os.path.join(basepath, host)) key, result = match(data, search) @@ -94,7 +94,7 @@ def print_search(basepath, flags, search): print(host) else: p = printer.Printer(data, flags) - p.info(key, label=_get_label(host), maxlength=max(map(len, map(_get_label, hosts['hosts'])))) + p.info(key, label=_get_label(host), maxlength=max(length)) def print_info(path, flags, key=None): data = _get_data(path) @@ -113,15 +113,16 @@ def print_keys(path): for k in set([a['interface'] for a in data[key]]): print "%s%s.%s" % (prefix, key, k) elif key == 'ports': - for k in set([p['process'] for p in data[key] if 'process' in p]): + names = [p['process'] for p in data[key] if 'process' in p] + for k in set(names): 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): - for v in data[key]: - _print_keys(v, "%s%s." % (prefix, key)) + for value in data[key]: + _print_keys(value, "%s%s." % (prefix, key)) data = _get_data(path) _print_keys(data) @@ -154,7 +155,7 @@ def find_host(basepath, host): try: # try to use reverse dns - addr=reversename.from_address(host) + addr = reversename.from_address(host) hostname = str(resolver.query(addr,"PTR")[0]) path = os.path.join(basepath, hostname[0:-1]) except: @@ -176,9 +177,11 @@ def main(): parser.add_argument("-o", "--oneline", action="store_true", help="each line is a complete record") parser.add_argument("-f", "--file", action="store_true", - help="print the path of the file the information is read from") + help="print the path of the file the " + "information is read from") parser.add_argument("-k", "--keys", action="store_true", - help="print only the available keys (used for bash completion)") + help="print only the available keys " + "(used for bash completion)") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") parser.add_argument("-n", "--nospaces", action="store_true", -- cgit v1.2.3-1-g7c22