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