1*842727c2SChris Kirby#! /usr/bin/python2.4 2*842727c2SChris Kirby# 3*842727c2SChris Kirby# CDDL HEADER START 4*842727c2SChris Kirby# 5*842727c2SChris Kirby# The contents of this file are subject to the terms of the 6*842727c2SChris Kirby# Common Development and Distribution License (the "License"). 7*842727c2SChris Kirby# You may not use this file except in compliance with the License. 8*842727c2SChris Kirby# 9*842727c2SChris Kirby# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*842727c2SChris Kirby# or http://www.opensolaris.org/os/licensing. 11*842727c2SChris Kirby# See the License for the specific language governing permissions 12*842727c2SChris Kirby# and limitations under the License. 13*842727c2SChris Kirby# 14*842727c2SChris Kirby# When distributing Covered Code, include this CDDL HEADER in each 15*842727c2SChris Kirby# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*842727c2SChris Kirby# If applicable, add the following below this CDDL HEADER, with the 17*842727c2SChris Kirby# fields enclosed by brackets "[]" replaced with your own identifying 18*842727c2SChris Kirby# information: Portions Copyright [yyyy] [name of copyright owner] 19*842727c2SChris Kirby# 20*842727c2SChris Kirby# CDDL HEADER END 21*842727c2SChris Kirby# 22*842727c2SChris Kirby# Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23*842727c2SChris Kirby# Use is subject to license terms. 24*842727c2SChris Kirby# 25*842727c2SChris Kirby 26*842727c2SChris Kirbyimport zfs.util 27*842727c2SChris Kirby 28*842727c2SChris Kirbyclass Table: 29*842727c2SChris Kirby __slots__ = "fields", "rjustfields", "maxfieldlen", "lines" 30*842727c2SChris Kirby __repr__ = zfs.util.default_repr 31*842727c2SChris Kirby 32*842727c2SChris Kirby def __init__(self, fields, rjustfields=()): 33*842727c2SChris Kirby # XXX maybe have a defaults, too? 34*842727c2SChris Kirby self.fields = fields 35*842727c2SChris Kirby self.rjustfields = rjustfields 36*842727c2SChris Kirby self.maxfieldlen = dict.fromkeys(fields, 0) 37*842727c2SChris Kirby self.lines = list() 38*842727c2SChris Kirby 39*842727c2SChris Kirby def __updatemax(self, k, v): 40*842727c2SChris Kirby self.maxfieldlen[k] = max(self.maxfieldlen.get(k, None), v) 41*842727c2SChris Kirby 42*842727c2SChris Kirby def addline(self, sortkey, values): 43*842727c2SChris Kirby """values is a dict from field name to value""" 44*842727c2SChris Kirby 45*842727c2SChris Kirby va = list() 46*842727c2SChris Kirby for f in self.fields: 47*842727c2SChris Kirby v = str(values[f]) 48*842727c2SChris Kirby va.append(v) 49*842727c2SChris Kirby self.__updatemax(f, len(v)) 50*842727c2SChris Kirby self.lines.append((sortkey, va)) 51*842727c2SChris Kirby 52*842727c2SChris Kirby def printme(self, headers=True): 53*842727c2SChris Kirby if headers: 54*842727c2SChris Kirby d = dict([(f, f.upper()) for f in self.fields]) 55*842727c2SChris Kirby self.addline(None, d) 56*842727c2SChris Kirby 57*842727c2SChris Kirby self.lines.sort() 58*842727c2SChris Kirby for (k, va) in self.lines: 59*842727c2SChris Kirby line = str() 60*842727c2SChris Kirby for i in range(len(self.fields)): 61*842727c2SChris Kirby if not headers: 62*842727c2SChris Kirby line += va[i] 63*842727c2SChris Kirby line += "\t" 64*842727c2SChris Kirby else: 65*842727c2SChris Kirby if self.fields[i] in self.rjustfields: 66*842727c2SChris Kirby fmt = "%*s " 67*842727c2SChris Kirby else: 68*842727c2SChris Kirby fmt = "%-*s " 69*842727c2SChris Kirby mfl = self.maxfieldlen[self.fields[i]] 70*842727c2SChris Kirby line += fmt % (mfl, va[i]) 71*842727c2SChris Kirby print(line) 72