Path: blob/main/cddl/contrib/opensolaris/lib/pyzfs/common/table.py
39563 views
#! /usr/bin/python2.61#2# CDDL HEADER START3#4# The contents of this file are subject to the terms of the5# Common Development and Distribution License (the "License").6# You may not use this file except in compliance with the License.7#8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9# or http://www.opensolaris.org/os/licensing.10# See the License for the specific language governing permissions11# and limitations under the License.12#13# When distributing Covered Code, include this CDDL HEADER in each14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.15# If applicable, add the following below this CDDL HEADER, with the16# fields enclosed by brackets "[]" replaced with your own identifying17# information: Portions Copyright [yyyy] [name of copyright owner]18#19# CDDL HEADER END20#21# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.22#2324import zfs.util2526class Table:27__slots__ = "fields", "rjustfields", "maxfieldlen", "lines"28__repr__ = zfs.util.default_repr2930def __init__(self, fields, rjustfields=()):31# XXX maybe have a defaults, too?32self.fields = fields33self.rjustfields = rjustfields34self.maxfieldlen = dict.fromkeys(fields, 0)35self.lines = list()3637def __updatemax(self, k, v):38self.maxfieldlen[k] = max(self.maxfieldlen.get(k, None), v)3940def addline(self, sortkey, values):41"""values is a dict from field name to value"""4243va = list()44for f in self.fields:45v = str(values[f])46va.append(v)47self.__updatemax(f, len(v))48self.lines.append((sortkey, va))4950def printme(self, headers=True):51if headers:52d = dict([(f, f.upper()) for f in self.fields])53self.addline(None, d)5455self.lines.sort()56for (k, va) in self.lines:57line = str()58for i in range(len(self.fields)):59if not headers:60line += va[i]61line += "\t"62else:63if self.fields[i] in self.rjustfields:64fmt = "%*s "65else:66fmt = "%-*s "67mfl = self.maxfieldlen[self.fields[i]]68line += fmt % (mfl, va[i])69print(line)707172