xref: /illumos-gate/usr/src/lib/pyzfs/common/table.py (revision e8921a52)
19f923083SAlexander Pyhalov#!@PYTHON@
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#
226d52f363SLori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23*e8921a52SAndy Fiddaman# Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
24842727c2SChris Kirby#
25842727c2SChris Kirby
26842727c2SChris Kirbyimport zfs.util
27842727c2SChris Kirby
28842727c2SChris Kirbyclass Table:
29842727c2SChris Kirby	__slots__ = "fields", "rjustfields", "maxfieldlen", "lines"
30842727c2SChris Kirby	__repr__ = zfs.util.default_repr
31842727c2SChris Kirby
32842727c2SChris Kirby	def __init__(self, fields, rjustfields=()):
33842727c2SChris Kirby		# XXX maybe have a defaults, too?
34842727c2SChris Kirby		self.fields = fields
35842727c2SChris Kirby		self.rjustfields = rjustfields
36842727c2SChris Kirby		self.maxfieldlen = dict.fromkeys(fields, 0)
37842727c2SChris Kirby		self.lines = list()
38*e8921a52SAndy Fiddaman
39842727c2SChris Kirby	def __updatemax(self, k, v):
40842727c2SChris Kirby		self.maxfieldlen[k] = max(self.maxfieldlen.get(k, None), v)
41842727c2SChris Kirby
42842727c2SChris Kirby	def addline(self, sortkey, values):
43842727c2SChris Kirby		"""values is a dict from field name to value"""
44842727c2SChris Kirby
45842727c2SChris Kirby		va = list()
46842727c2SChris Kirby		for f in self.fields:
47842727c2SChris Kirby			v = str(values[f])
48842727c2SChris Kirby			va.append(v)
49842727c2SChris Kirby			self.__updatemax(f, len(v))
50*e8921a52SAndy Fiddaman		if sortkey == None:
51*e8921a52SAndy Fiddaman			sortkey = []
52842727c2SChris Kirby		self.lines.append((sortkey, va))
53842727c2SChris Kirby
54842727c2SChris Kirby	def printme(self, headers=True):
55842727c2SChris Kirby		if headers:
56842727c2SChris Kirby			d = dict([(f, f.upper()) for f in self.fields])
57842727c2SChris Kirby			self.addline(None, d)
58842727c2SChris Kirby
59842727c2SChris Kirby		self.lines.sort()
60842727c2SChris Kirby		for (k, va) in self.lines:
61842727c2SChris Kirby			line = str()
62842727c2SChris Kirby			for i in range(len(self.fields)):
63842727c2SChris Kirby				if not headers:
64842727c2SChris Kirby					line += va[i]
65842727c2SChris Kirby					line += "\t"
66842727c2SChris Kirby				else:
67842727c2SChris Kirby					if self.fields[i] in self.rjustfields:
68842727c2SChris Kirby						fmt = "%*s  "
69842727c2SChris Kirby					else:
70842727c2SChris Kirby						fmt = "%-*s  "
71842727c2SChris Kirby					mfl = self.maxfieldlen[self.fields[i]]
72842727c2SChris Kirby					line += fmt % (mfl, va[i])
73842727c2SChris Kirby			print(line)
74