
    )                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ	 ddlm
Z
 dd	lmZ dd
lmZ dZ G d de	j                         Z G d de      Z G d de      Z G d de      Z G d de      Zd Z G d de      Zy)a  Resource printer base class.

Each printer has three main attributes, all accessible as strings in the
--format='NAME[ATTRIBUTES](PROJECTION)' option:

  NAME: str, The printer name.

  [ATTRIBUTES]: str, An optional [no-]name[=value] list of attributes. Unknown
    attributes are silently ignored. Attributes are added to a printer local
    dict indexed by name.

  (PROJECTION): str, List of resource names to be included in the output
    resource. Unknown names are silently ignored. Resource names are
    '.'-separated key identifiers with an implicit top level resource name.

Example:

  gcloud compute instances list       --format='table[box](name, networkInterfaces[0].networkIP)'
    )absolute_import)division)print_function)unicode_literalsN)
exceptions)log)
console_io)resource_property   c                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     9lib/googlecloudsdk/core/resource/resource_printer_base.pyr   r   6   s    #r   r   c                       e Zd ZdZy)ProjectionRequiredErrorz-Format missing required projection exception.Nr   r   r   r   r   r   :   s    5r   r   c                       e Zd ZdZd Zy)_ResourceMarkerz2A marker that can be injected into resource lists.c                      y)zYCalled by ResourcePrinter.Addrecord().

    Args:
      printer: The printer object.
    Nr   selfprinters     r   Actz_ResourceMarker.ActA   s     	r   Nr   r   r   r   r   r   r   r   r   r   >   s
    :	r   r   c                       e Zd ZdZd Zy)FinishMarkerzA resource list Finish marker.c                 $    |j                          y N)Finishr   s     r   r   zFinishMarker.ActM   s    NNr   Nr   r   r   r   r!   r!   J   s
    &r   r!   c                       e Zd ZdZd Zy)
PageMarkerzA resource list Page marker.c                 $    |j                          y r#   )Pager   s     r   r   zPageMarker.ActT   s    LLNr   Nr   r   r   r   r&   r&   Q   s
    $r   r&   c                 "    t        | t              S )z.Returns True if resource is a _ResourceMarker.)
isinstancer   )resources    r   IsResourceMarkerr,   X   s    	Ho	..r   c                   h    e Zd ZdZ	 	 	 	 ddZd Zd Zd ZddZddZ	d	 Z
d
 Zd Zd ZddZd Zy)ResourcePrinterae  Base class for printing JSON-serializable Python objects.

  Attributes:
    attributes: Optional printer attribute dict indexed by attribute name.
    column_attributes: Projection column attributes.
    _console_attr: The console attributes. May be ignored by some printers.
    _empty: True if there are no records.
    _heading: The list of column heading label strings.
    _name: Format name.
    _non_empty_projection_required: True if the printer requires a non-empty
      projection.
    _out: Output stream.
    _process_record: The function called to process each record passed to
      AddRecord() before calling _AddRecord(). It is called like this:
        record = process_record(record)
    _printer: The resource_printer.Printer method for nested formats.
    _is_column_visible: List of Boolean values indicating if indexed column is
      visible
  Nc                    ddl m} |	| _        d| _        d| _        || _        || _        |xs t        j                  | _	        || _
        d| _        d| _        |s|j                         }|xs |j                  | _        |j!                  |       |j#                  |       |j%                         }|r|j'                         xs i | _        || _        d| j(                  v rjt        j,                  j/                  dj1                  |j3                                      |j/                  t        j,                         ni | _        d| _        d| j(                  v rd}
|j5                  |
       d	| j(                  v r 	 | j                  j7                         | _	        d
| j(                  v r3|1| j                  | _        t;        j<                         | _	        d| _        | j?                          y# t8        $ r Y ]w xY w)a6  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.
      ignore_default_transforms: Ignore default projection transforms if True.
      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.
    r   )resource_projectorTNFdebugz{0} format projection:zno-undefinedprivatepager) googlecloudsdk.core.resourcer0   _console_attr_empty_heading_name_non_empty_projection_requiredr   out_out_printer_pager
_pager_outCompileEvaluate_process_recordSetByColumnsSetIgnoreDefaultTransforms
Projection
Attributes
attributescolumn_attributesstatusPrintformatNameSetRetainNoneValuesGetConsoleWriterStreamAttributeErrorioStringIO_SetVisibleColumns)r   r:   name	projector
by_columnsignore_default_transformsprocess_recordnon_empty_projection_requiredr   console_attrretain_none_valuesr0   
projections                r   __init__zResourcePrinter.__init__r   s   4 @ &DDKDMDJ*GD'swwDIDMDKDO$,,.i)?Y-?-?D:&(()BC%%'J"--/52do)d	DOO	#

1889JKL$do#d( !!"45DOO#II446	 $//!ck		do++-didk  s   ;G, ,	G87G8c                     | j                   rC| j                   j                         D cg c]  }|j                  j                    c}| _        yd| _        yc c}w )z@Sets visible columns list if column attributes have been loaded.N)rG   Columns	attributehidden_is_column_visible)r   columns     r   rQ   z"ResourcePrinter._SetVisibleColumns   sZ     ..668!8f %%
%8!d
 !%d!s   Ac                     | j                   s| j                          | j                   r.t        |      D cg c]  \  }}| j                   |   s| c}}S |S c c}}w )z.Returns a list of visible columns given a row.)r`   rQ   	enumerate)r   rowicols       r   RemoveHiddenColumnsz#ResourcePrinter.RemoveHiddenColumns   sU    ""
 )#Mfa$2I2I!2LcMMj Ns   AAc                     || _         y)zOverrides the default heading.

    If the printer does not support headings then this is a no-op.

    Args:
      heading: List of column heading strings that overrides the default
        heading.
    N)r7   )r   headings     r   
AddHeadingzResourcePrinter.AddHeading   s     DMr   c                      y)zFormat specific AddRecord().

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    Nr   r   recorddelimits      r   
_AddRecordzResourcePrinter._AddRecord   s     	r   c                     t        |      r|j                  |        yd| _        | j                  | j	                  |      |       y)a_  Adds a record for printing.

    Streaming formats (e.g., YAML) can print results at each AddRecord() call.
    Non-streaming formats (e.g., JSON, table(...)) may cache data at each
    AddRecord() call and not print until Finish() is called.

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    FN)r,   r   r6   ro   rA   rl   s      r   	AddRecordzResourcePrinter.AddRecord   s8     jjdk
ood**62G<r   c                     | j                   rH| j                  r;	 t        j                  | j                  j	                         | j
                         yyy# t        $ r Y yw xY w)z_Prints the results for non-streaming formats.

    Must be called via super if overridden.
    )r:   N)r=   r;   r	   Moregetvaluer>   rN   r   s    r   r$   zResourcePrinter.Finish   sP    
 {{tyy		**,$//B !{  s   9A 	A"!A"c                     | j                    S )zEReturns True if some resource items were printed or printer disabled.)r6   ru   s    r   ResourcesWerePrintedz$ResourcePrinter.ResourcesWerePrinted   s    {{?r   c                      y)z3Flushes intermediate results for streaming formats.Nr   ru   s    r   r(   zResourcePrinter.Page  s    r   c                 J    | j                  |d       | j                          y)zUPrint one record by itself.

    Args:
      record: A JSON-serializable object.
    Frn   N)rq   r$   )r   rm   s     r   PrintSingleRecordz!ResourcePrinter.PrintSingleRecord  s     	NN65N)KKMr   c                    d| j                   v rd| _        y| j                  rJ| j                  r| j                  j	                         s$t        dj                  | j                              	 |rC|st        j                  |      s| j                  ||       n|D ]  }| j                  |        |s| j                          yy# |s| j                          w w xY w)a  Prints resources using printer.AddRecord() and printer.Finish().

    Args:
      resources: A singleton or list of JSON-serializable Python objects.
      single: If True then resources is a single item and not a list.
        For example, use this to print a single object as JSON.
      intermediate: This is an intermediate call, do not call Finish().

    Raises:
      ProjectionRequiredError: If the projection is empty and the format
        requires a non-empty projection.
    disableFNztFormat [{0}] requires a non-empty projection. Use key parameters to specify a projection like so `{0}(foo, bar.baz)`rz   )rF   r6   r9   rG   r]   r   rJ   r8   r
   
IsListLikerq   r$   )r   	resourcessingleintermediater+   s        r   rI   zResourcePrinter.Print  s     DOO#dk**""$*@*@*H*H*J# A6$** 		*55i@
..L.
9#hNN8$ $  \ s   .AC Cc                 &     | j                   |i |S )zBCalls the resource_printer.Printer() method (for nested printers).)r<   )r   argskwargss      r   PrinterzResourcePrinter.Printer3  s    4==$)&))r   )
NNNFFNFNNF)T)FF)r   r   r   r   r[   rQ   rg   rj   ro   rq   r$   rw   r(   r{   rI   r   r   r   r   r.   r.   ]   sV    ( FK?C<@5:EN%		="
	#J*r   r.   )r   
__future__r   r   r   r   rO   googlecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.consoler	   r4   r
   STRUCTURED_INDENTATIONr   r   objectr   r!   r&   r,   r.   r   r   r   <module>r      s    * '  % ' 	 = # 2 :  $O!! $6e 6		f 		?  /
X*f X*r   