1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
"""
SoftLayer.CLI.columns
~~~~~~~~~~~~~~~~~~~~~~
Utilties for user-defined columns
:license: MIT, see LICENSE for more details.
"""
import click
from SoftLayer import utils
# pylint: disable=unused-argument
class Column(object):
"""Column desctribes an attribute and how to fetch/display it."""
def __init__(self, name, path, mask=None):
self.name = name
self.path = path
self.mask = mask
# If the mask is not set explicitly, infer it from the path
if self.mask is None and isinstance(path, (tuple, list)):
self.mask = '.'.join(path)
class ColumnFormatter(object):
"""Maps each column using a function"""
def __init__(self):
self.columns = []
self.column_funcs = []
self.mask_parts = set()
def add_column(self, column):
"""Add a new column along with a formatting function."""
self.columns.append(column.name)
self.column_funcs.append(column.path)
if column.mask is not None:
self.mask_parts.add(column.mask)
def row(self, data):
"""Return a formatted row for the given data."""
for column in self.column_funcs:
if callable(column):
yield column(data)
else:
yield utils.lookup(data, *column)
def mask(self):
"""Returns a SoftLayer mask to fetch data needed for each column."""
return ','.join(self.mask_parts)
def get_formatter(columns):
"""This function returns a callback to use with click options.
The returned function parses a comma-separated value and returns a new
ColumnFormatter.
:param columns: a list of Column instances
"""
column_map = dict((column.name, column) for column in columns)
def validate(ctx, param, value):
"""Click validation function."""
if value == '':
raise click.BadParameter('At least one column is required.')
formatter = ColumnFormatter()
for column in [col.strip() for col in value.split(',')]:
if column in column_map:
formatter.add_column(column_map[column])
else:
formatter.add_column(Column(column, column.split('.')))
return formatter
return validate
|