def __init__(self, out=None, name=None, attributes=None, column_attributes=None, by_columns=False, process_record=None): """Constructor. Args: out: The output stream, log.out if None. name: The format name. attributes: Optional printer attribute dict indexed by attribute name. column_attributes: Projection ColumnAttributes(). by_columns: True if AddRecord() expects a list of columns. process_record: The function called to process each record passed to AddRecord() before calling _AddRecord(). It is called like this: record = process_record(record) """ self._attributes = attributes or {} self._by_columns = by_columns self._column_attributes = column_attributes self._heading = None self._name = name self._out = out or log.out self._process_record = (process_record or resource_projector.Compile().Evaluate)
def Printer(print_format, out=None, defaults=None): """Returns a resource printer given a format string. Args: print_format: The _FORMATTERS name with optional attributes and projection. out: Output stream, log.out if None. defaults: Optional resource_projection_spec.ProjectionSpec defaults. Raises: UnknownFormatError: The print_format is invalid. Returns: An initialized ResourcePrinter class. """ projector = resource_projector.Compile( expression=print_format, defaults=defaults, symbols=resource_transform.Builtins()) projection = projector.Projection() printer_name = projection.Name() printer_class = _FORMATTERS.get(printer_name, None) if not printer_class: raise UnknownFormatError( 'Format must be one of {0}; received [{1}]'.format( ', '.join(SupportedFormats()), printer_name)) printer = printer_class(out=out, name=printer_name, attributes=projection.Attributes(), column_attributes=projection, process_record=projector.Evaluate) projector.SetByColumns(printer.ByColumns()) return printer
def __init__(self, out=None, name=None, projector=None, by_columns=False, process_record=None, non_empty_projection_required=False, printer=None, console_attr=None, retain_none_values=False): """Constructor. Args: out: The output stream, log.out if None. If the 'private' attribute is set and the output stream is a log._ConsoleWriter then the underlying stream is used instead to disable output to the log file. name: The format name. projector: Optional resource Projector. by_columns: True if AddRecord() expects a list of columns. process_record: The function called to process each record passed to AddRecord() before calling _AddRecord(). It is called like this: record = process_record(record) non_empty_projection_required: True if the printer requires a non-empty projection. printer: The resource_printer.Printer method for nested formats. console_attr: The console attributes for the output stream. Ignored by some printers. If None then printers that require it will initialize it to match out. retain_none_values: Retain resurce dict entries with None values. """ self._console_attr = console_attr self._empty = True self._heading = None self._is_legend_done = False self._name = name self._non_empty_projection_required = non_empty_projection_required self._out = out or log.out self._printer = printer if not projector: projector = resource_projector.Compile() self._process_record = process_record or projector.Evaluate projector.SetByColumns(by_columns) projector.SetRetainNoneValues(retain_none_values) projection = projector.Projection() if projection: self.attributes = projection.Attributes() or {} self.column_attributes = projection else: self.attributes = {} self.column_attributes = None if 'private' in self.attributes: try: # Disable log file writes by printing directly to the console stream. self._out = self._out.GetConsoleWriterStream() except AttributeError: pass
def TransformFormat(r, projection, fmt, *args): """Formats a sub-projection of r. Args: r: A JSON-serializable object. projection: The parent ProjectionSpec. fmt: The format string with {0} ... {nargs-1} references to the resource key arg values. *args: The resource key args to format. The args values form a projection on r. The projection symbols and aliases are available in the sub-projection. Returns: The formatted string. Example: --format='value(format("{0:f.1}/{0:f.1}", q.CPU.default, q.CPU.limit))' """ columns = resource_projector.Compile('(' + ','.join(args) + ')', by_columns=True, defaults=projection).Evaluate(r) return fmt.format(*columns)
def Printer(print_format, out=None, defaults=None, console_attr=None): """Returns a resource printer given a format string. Args: print_format: The _FORMATTERS name with optional attributes and projection. out: Output stream, log.out if None. defaults: Optional resource_projection_spec.ProjectionSpec defaults. console_attr: The console attributes for the output stream. Ignored by some printers. If None then printers that require it will initialize it to match out. Raises: UnknownFormatError: The print_format is invalid. Returns: An initialized ResourcePrinter class or None if printing is disabled. """ projector = resource_projector.Compile( expression=print_format, defaults=resource_projection_spec.ProjectionSpec( defaults=defaults, symbols=resource_transform.GetTransforms())) printer_name = projector.Projection().Name() if not printer_name: return None try: printer_class = _FORMATTERS[printer_name] except KeyError: raise UnknownFormatError("""\ Format must be one of {0}; received [{1}]. For information on output formats: $ gcloud topic formats """.format(', '.join(SupportedFormats()), printer_name)) # TODO(user): move to top-level gcloud exception handler printer = printer_class(out=out, name=printer_name, printer=Printer, projector=projector, console_attr=console_attr) return printer
def Printer(print_format, out=None, defaults=None, console_attr=None): """Returns a resource printer given a format string. Args: print_format: The _FORMATTERS name with optional attributes and projection. out: Output stream, log.out if None. defaults: Optional resource_projection_spec.ProjectionSpec defaults. console_attr: The console attributes for the output stream. Ignored by some printers. If None then printers that require it will initialize it to match out. Raises: UnknownFormatError: The print_format is invalid. Returns: An initialized ResourcePrinter class or None if printing is disabled. """ projector = resource_projector.Compile( expression=print_format, defaults=defaults, symbols=resource_transform.GetTransforms()) projection = projector.Projection() printer_name = projection.Name() try: printer_class = _FORMATTERS[printer_name] except KeyError: raise UnknownFormatError( 'Format must be one of {0}; received [{1}]'.format( ', '.join(SupportedFormats()), printer_name)) printer = printer_class(out=out, name=printer_name, attributes=projection.Attributes(), column_attributes=projection, process_record=projector.Evaluate, printer=Printer, console_attr=console_attr) projector.SetByColumns(printer.ByColumns()) return printer
def __init__(self, out=None, name=None, attributes=None, column_attributes=None, by_columns=False, process_record=None): """Constructor. Args: out: The output stream, log.out if None. If the 'private' attribute is set and the output stream is a log._ConsoleWriter then the underlying stream is used instead to disable output to the log file. name: The format name. attributes: Optional printer attribute dict indexed by attribute name. column_attributes: Projection ColumnAttributes(). by_columns: True if AddRecord() expects a list of columns. process_record: The function called to process each record passed to AddRecord() before calling _AddRecord(). It is called like this: record = process_record(record) """ self.attributes = attributes or {} self._by_columns = by_columns self.column_attributes = column_attributes self._empty = True self._heading = None self._name = name self._out = out or log.out if 'private' in self.attributes: try: # Disable log file writes by printing directly to the console stream. self._out = self._out.GetConsoleWriterStream() except AttributeError: pass self._process_record = (process_record or resource_projector.Compile().Evaluate)
def SerializeCommand(command): return resource_projector.Compile().Evaluate(command)