示例#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()
示例#2
0
def PrintTable(resources, table_cols):
  """Prints a table of the given resources."""
  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()
示例#3
0
def PrintTable(resources, table_cols):
  """Prints a table of the given resources."""
  # TODO(user): 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()
示例#4
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']['templateVersion'])
            row.append(resource['status']['state'])
            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)
    elif resource_type == 'zone-operation':
        header = [
            'name', 'type', 'target', 'httpStatus', 'status', 'timestamp'
        ]
        printer.AddRow(header)
        for resource in resources:
            row = []
            row.append(resource['name'])
            row.append(resource['operationType'])

            # get the last two parts of the targetLink. e.g. instances/instance-foo
            target_link = resource['targetLink'].split('/', 9)[-1]
            row.append(target_link)

            # extract http status
            http_status = ''
            if resource['status'] == 'DONE':
                http_status = resource.get('httpErrorStatusCode') or '200'
            row.append(http_status)

            row.append(resource['status'])
            row.append(resource['insertTime'])

            printer.AddRow(row)
    elif resource_type == 'managed-instance-group':
        header = ['name', 'instanceTemplate', 'currentSize', 'targetSize']
        printer.AddRow(header)
        for resource in resources:
            row = []
            row.append(resource['name'])
            template = resource['instanceTemplate'].split('/')[-1]
            row.append(template)
            row.append(str(resource['currentSize']))
            row.append(str(resource['targetSize']))

            printer.AddRow(row)
    else:
        raise ValueError(
            'Unsupported resource_type: {0}'.format(resource_type))

    printer.Print()