def DisplaySchemaAndRows(schema_and_rows):
    """Display a SchemaAndRows object as a gcloud table.

  The heading for each column is a field name.

  Args:
    schema_and_rows: The SchemaAndRows object.
  """
    # Convert the iterable over TableRow objects into a list of lists, where
    # each inner list corresponds to a row, and contains JsonValue objects
    # for the values in the row. This is the point at which we invoke the
    # generator created by the call on list_pager.YieldFromList from
    # GetJobSchemaAndRows, so this is where we catch any exception from the API
    # call made by that generator.
    try:
        rows = [[cell.v for cell in tr.f] for tr in schema_and_rows.rows]
    except apitools_base.HttpError as server_error:
        raise bigquery.Error.ForHttpError(server_error)

    def CreateColumnFetcher(col_num):
        def ColumnFetcher(row):
            return row[col_num].string_value if row[col_num] else 'null'

        return ColumnFetcher

    col_fetchers = [(field_schema.name.upper(), CreateColumnFetcher(i))
                    for i, field_schema in enumerate(schema_and_rows.schema)]
    # The variable col_fetchers is a list of pairs. The first component of each
    # pair is the name of a field, and the second component of each pair is a
    # function to which a row is passed. Each row passed to such a function is a
    # list of JsonValue objects, and each of these JsonValue objects has its
    # string_value field set.

    console_io.PrintExtendedList(rows, col_fetchers)
Ejemplo n.º 2
0
    def Display(self, args, query_results):
        """This method is called to print the result of the Run() method.

    Args:
      args: The arguments that the command was run with.
      query_results: A bigquery.QueryResults object.
    """
        console_io.PrintExtendedList(query_results,
                                     query_results.GetColumnFetchers())
Ejemplo n.º 3
0
def PrintResourceList(collection, items):
    """Print a list of cloud resources.

  Args:
    collection: str, The name of the collection to which the items belong.
    items: iterable, A list or otherwise iterable object that generates the
        rows of the list.
  """
    console_io.PrintExtendedList(items, COLLECTION_COLUMNS[collection])
Ejemplo n.º 4
0
 def Display(self, unused_args, repos):
     if repos:
         console_io.PrintExtendedList(repos,
                                      [('REPOSITORY', lambda x: x),
                                       ('LAST_UPDATE', List._LastUpdate)])
     else:
         log.status.write(
             'You have no registered component repositories.  To add one, run:\n'
             '  $ gcloud components repositories add URL\n\n')
Ejemplo n.º 5
0
def PrintResourceList(collection, items):
  """Print a list of cloud resources.

  Args:
    collection: str, The name of the collection to which the items belong.
    items: iterable, A list or otherwise iterable object that generates the
        rows of the list.
  """
  options = []
  cache = remote_completion.RemoteCompletion()

  class Iter(object):
    """Create an iterator that steals the names of objects.

    Args:
      items: List of items to iterate
      options: List of names of the items created by iterator.
    """

    def __init__(self, items, options):
      self.items = items
      self.options = options

    def next(self):
      item = self.items.next()
      fun = cache.ITEM_NAME_FUN['sql']
      if fun:
        self.options.append(fun(item))
      return item

    def __iter__(self):
      return self

  if cache.ResourceIsCached(collection):
    items = Iter(iter(items), options)
  console_io.PrintExtendedList(items, COLLECTION_COLUMNS[collection])
  if options:
    cache.StoreInCache(collection, options, None)