1*6d52f363SLori Alt#! /usr/bin/python2.6 2842727c2SChris Kirby# 3842727c2SChris Kirby# CDDL HEADER START 4842727c2SChris Kirby# 5842727c2SChris Kirby# The contents of this file are subject to the terms of the 6842727c2SChris Kirby# Common Development and Distribution License (the "License"). 7842727c2SChris Kirby# You may not use this file except in compliance with the License. 8842727c2SChris Kirby# 9842727c2SChris Kirby# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10842727c2SChris Kirby# or http://www.opensolaris.org/os/licensing. 11842727c2SChris Kirby# See the License for the specific language governing permissions 12842727c2SChris Kirby# and limitations under the License. 13842727c2SChris Kirby# 14842727c2SChris Kirby# When distributing Covered Code, include this CDDL HEADER in each 15842727c2SChris Kirby# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16842727c2SChris Kirby# If applicable, add the following below this CDDL HEADER, with the 17842727c2SChris Kirby# fields enclosed by brackets "[]" replaced with your own identifying 18842727c2SChris Kirby# information: Portions Copyright [yyyy] [name of copyright owner] 19842727c2SChris Kirby# 20842727c2SChris Kirby# CDDL HEADER END 21842727c2SChris Kirby# 22*6d52f363SLori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 23842727c2SChris Kirby# 24842727c2SChris Kirby 25842727c2SChris Kirbyimport zfs.util 26842727c2SChris Kirby 27842727c2SChris Kirbyclass Table: 28842727c2SChris Kirby __slots__ = "fields", "rjustfields", "maxfieldlen", "lines" 29842727c2SChris Kirby __repr__ = zfs.util.default_repr 30842727c2SChris Kirby 31842727c2SChris Kirby def __init__(self, fields, rjustfields=()): 32842727c2SChris Kirby # XXX maybe have a defaults, too? 33842727c2SChris Kirby self.fields = fields 34842727c2SChris Kirby self.rjustfields = rjustfields 35842727c2SChris Kirby self.maxfieldlen = dict.fromkeys(fields, 0) 36842727c2SChris Kirby self.lines = list() 37842727c2SChris Kirby 38842727c2SChris Kirby def __updatemax(self, k, v): 39842727c2SChris Kirby self.maxfieldlen[k] = max(self.maxfieldlen.get(k, None), v) 40842727c2SChris Kirby 41842727c2SChris Kirby def addline(self, sortkey, values): 42842727c2SChris Kirby """values is a dict from field name to value""" 43842727c2SChris Kirby 44842727c2SChris Kirby va = list() 45842727c2SChris Kirby for f in self.fields: 46842727c2SChris Kirby v = str(values[f]) 47842727c2SChris Kirby va.append(v) 48842727c2SChris Kirby self.__updatemax(f, len(v)) 49842727c2SChris Kirby self.lines.append((sortkey, va)) 50842727c2SChris Kirby 51842727c2SChris Kirby def printme(self, headers=True): 52842727c2SChris Kirby if headers: 53842727c2SChris Kirby d = dict([(f, f.upper()) for f in self.fields]) 54842727c2SChris Kirby self.addline(None, d) 55842727c2SChris Kirby 56842727c2SChris Kirby self.lines.sort() 57842727c2SChris Kirby for (k, va) in self.lines: 58842727c2SChris Kirby line = str() 59842727c2SChris Kirby for i in range(len(self.fields)): 60842727c2SChris Kirby if not headers: 61842727c2SChris Kirby line += va[i] 62842727c2SChris Kirby line += "\t" 63842727c2SChris Kirby else: 64842727c2SChris Kirby if self.fields[i] in self.rjustfields: 65842727c2SChris Kirby fmt = "%*s " 66842727c2SChris Kirby else: 67842727c2SChris Kirby fmt = "%-*s " 68842727c2SChris Kirby mfl = self.maxfieldlen[self.fields[i]] 69842727c2SChris Kirby line += fmt % (mfl, va[i]) 70842727c2SChris Kirby print(line) 71