Exemplo n.º 1
0
def PrintTable(header, resource_list):
  """Print a table of results with the specified columns.

  Prints a table whose columns are the proto fields specified in the
  header list. Any fields which cannot be found are printed as empty.

  Args:
    header: A list of strings which are the field names to include in the
        table. Must match field names in the resource_list items.
    resource_list: A list of resource objects, each corresponding to a row
        in the table to print.
  """
  printer = resource_printer.TablePrinter(out=log.out)
  printer.AddRow(header)
  for resource in resource_list:
    printer.AddRow([resource[column] if column in resource else ''
                    for column in header])
  printer.Print()
Exemplo n.º 2
0
def PrintTable(resources, table_cols):
  """Prints a table of the given resources."""
  # TODO(aryann): Switch over to console_io.TablePrinter once the
  # class is refactored to support tables without ASCII borders.
  printer = resource_printer.TablePrinter(out=log.out)

  header = []
  for name, _ in table_cols:
    header.append(name)
  printer.AddRow(header)

  try:
    for resource in resources:
      row = []
      for _, action in table_cols:
        if isinstance(action, property_selector.PropertyGetter):
          row.append(action.Get(resource) or '')
        elif callable(action):
          row.append(action(resource))
      printer.AddRow(row)
  finally:
    printer.Print()
Exemplo n.º 3
0
def PrintTable(resources, resource_type):
    """Prints a table of the given resources.

  Args:
    resources: a list of resources to print into a table
    resource_type: the type of the resources to print, e.g. 'replica' or
      'replica-pool'

  Raises:
    ValueError: if an unsupported resource_type is provided
  """
    printer = resource_printer.TablePrinter(out=log.out)

    if not resources:
        return

    if resource_type == 'replica':
        header = ['name', 'status', 'templateVersion']
        printer.AddRow(header)
        for resource in resources:
            row = []
            row.append(resource['name'])
            row.append(resource['status']['state'])
            row.append(resource['status']['templateVersion'])
            printer.AddRow(row)
    elif resource_type == 'replica-pool':
        header = ['name', 'currentNumReplicas']
        printer.AddRow(header)
        for resource in resources:
            row = []
            row.append(resource['name'])
            row.append(str(resource['currentNumReplicas']))
            printer.AddRow(row)
    else:
        raise ValueError(
            'Unsupported resource_type: {0}'.format(resource_type))

    printer.Print()