2
0
mirror of https://github.com/xcat2/confluent.git synced 2024-11-25 19:10:10 +00:00

Add abbreviate to collate

Also, perform natural sort on 'nodenames' when not abbreviating
This commit is contained in:
Jarrod Johnson 2017-08-14 15:06:57 -04:00
parent 0393e55eb1
commit 28259511ed
2 changed files with 38 additions and 3 deletions

View File

@ -29,8 +29,11 @@ if path.startswith('/opt'):
sys.path.append(path)
import confluent.textgroup as tg
import confluent.client
argparser = optparse.OptionParser(usage="Usage: <other command> | %prog [options]")
argparser.add_option('-a', '--abbreviate', action='store_true',
help='Attempt to use confluent server to shorten noderanges')
argparser.add_option('-d', '--diff', action='store_true',
help='Show what differs between most common '
'output group and others')
@ -51,6 +54,11 @@ if sys.stdin.isatty():
grouped = tg.GroupedData()
if options.abbreviate:
grouped = tg.GroupedData(confluent.client.Command())
else:
grouped = tg.GroupedData()
def print_current():
if options.diff:
grouped.print_deviants(skipmodal=options.skipcommon, count=options.count,

View File

@ -13,6 +13,7 @@
# limitations under the License.
import difflib
import re
import sys
try:
@ -21,6 +22,18 @@ except NameError:
pass
numregex = re.compile('([0-9]+)')
def humanify_nodename(nodename):
"""Analyzes nodename in a human way to enable natural sort
:param nodename: The node name to analyze
:returns: A structure that can be consumed by 'sorted'
"""
return [int(text) if text.isdigit() else text.lower()
for text in re.split(numregex, nodename)]
def _colorize_line(orig, mask):
highlighted = False
newline = orig[0]
@ -53,10 +66,15 @@ def colordiff(first, second):
class GroupedData(object):
'''A post processor to sort and compare per-node data
def __init__(self):
:param confluentconnection: If given, will attempt to use the connection to abbreviate noderanges
'''
def __init__(self, confluentconnection=None):
self.bynode = {}
self.byoutput = {}
self.client = confluentconnection
def generate_byoutput(self):
self.byoutput = {}
@ -73,6 +91,15 @@ class GroupedData(object):
else:
self.bynode[node].append(line)
def get_group_text(self, nodes):
if self.client:
noderange = ''
for reply in self.client.create('/noderange//abbreviate', {'nodes': sorted(nodes)}):
noderange = reply['noderange']
return noderange
else:
return ','.join(sorted(nodes, key=humanify_nodename))
def print_all(self, output=sys.stdout, skipmodal=False, reverse=False,
count=False):
self.generate_byoutput()
@ -91,7 +118,7 @@ class GroupedData(object):
skipmodal = False
continue
currout = '====================================\n'
currout += ','.join(sorted(self.byoutput[outdata]))
currout += self.get_group_text(self.byoutput[outdata])
currout += '\n====================================\n'
if count:
currout += 'Count: {0}'.format(len(list(
@ -117,7 +144,7 @@ class GroupedData(object):
ismodal = False
continue
currout = '====================================\n'
currout += ','.join(sorted(self.byoutput[outdata]))
currout += self.get_group_text(self.byoutput[outdata])
currout += '\n====================================\n'
if count:
currout += 'Count: {0}'.format(len(list(