2N/A#! /usr/bin/python2.6
2N/A#
2N/A# CDDL HEADER START
2N/A#
2N/A# The contents of this file are subject to the terms of the
2N/A# Common Development and Distribution License (the "License").
2N/A# You may not use this file except in compliance with the License.
2N/A#
2N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A# or http://www.opensolaris.org/os/licensing.
2N/A# See the License for the specific language governing permissions
2N/A# and limitations under the License.
2N/A#
2N/A# When distributing Covered Code, include this CDDL HEADER in each
2N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A# If applicable, add the following below this CDDL HEADER, with the
2N/A# fields enclosed by brackets "[]" replaced with your own identifying
2N/A# information: Portions Copyright [yyyy] [name of copyright owner]
2N/A#
2N/A# CDDL HEADER END
2N/A#
2N/A# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A
2N/Aimport zfs.util
2N/A
2N/Aclass Table:
2N/A __slots__ = "fields", "rjustfields", "maxfieldlen", "lines"
2N/A __repr__ = zfs.util.default_repr
2N/A
2N/A def __init__(self, fields, rjustfields=()):
2N/A # XXX maybe have a defaults, too?
2N/A self.fields = fields
2N/A self.rjustfields = rjustfields
2N/A self.maxfieldlen = dict.fromkeys(fields, 0)
2N/A self.lines = list()
2N/A
2N/A def __updatemax(self, k, v):
2N/A self.maxfieldlen[k] = max(self.maxfieldlen.get(k, None), v)
2N/A
2N/A def addline(self, sortkey, values):
2N/A """values is a dict from field name to value"""
2N/A
2N/A va = list()
2N/A for f in self.fields:
2N/A v = str(values[f])
2N/A va.append(v)
2N/A self.__updatemax(f, len(v))
2N/A self.lines.append((sortkey, va))
2N/A
2N/A def printme(self, headers=True):
2N/A if headers:
2N/A d = dict([(f, f.upper()) for f in self.fields])
2N/A self.addline(None, d)
2N/A
2N/A self.lines.sort()
2N/A for (k, va) in self.lines:
2N/A line = str()
2N/A for i in range(len(self.fields)):
2N/A if not headers:
2N/A line += va[i]
2N/A line += "\t"
2N/A else:
2N/A if self.fields[i] in self.rjustfields:
2N/A fmt = "%*s "
2N/A else:
2N/A fmt = "%-*s "
2N/A mfl = self.maxfieldlen[self.fields[i]]
2N/A line += fmt % (mfl, va[i])
2N/A print(line)