o
    (                     @   sn  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mZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd dejZG dd deZG dd deZG dd dejZ G dd  d ej!Z"G d!d" d"ej!Z#G d#d$ d$ej$Z%G d%d& d&ej!Z&G d'd( d(ej!Z'i ej(ej)ej*ej+ej,e ej-ej.ej/e"ej0ej$ej1ej2ej3ej4ej5ej6ej7e&ej8e#ej9ej:ej;ej<ej=e%ej>ej?ej@ejZAi ZBd)d* ZCd5d,d-ZDd.d/ ZEd6d1d2ZFd7d3d4ZGd0S )8a  Methods for formatting and printing Python objects.

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)unicode_literals)
exceptions)log)
properties)config_printer)csv_printer)diff_printer)flattened_printer)json_printer)list_printer)object_printer)resource_lex)resource_printer_base)resource_printer_types)resource_projection_spec)resource_property)resource_transform)table_printer)yaml_printerc                   @      e Zd ZdZdS )ErrorzExceptions for this module.N__name__
__module____qualname____doc__ r   r   J/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_printer.pyr   =       r   c                   @   r   )UnknownFormatErrorzUnknown format name exception.Nr   r   r   r   r   r!   A   r    r!   c                   @   r   )ProjectionFormatRequiredErrorz1Projection key missing required format attribute.Nr   r   r   r   r   r"   E   r    r"   c                   @   r   )DefaultPrinterzAn alias for YamlPrinter.

  An alias for the *yaml* format. To override use *gcloud config set
  core/default_format* property.
  Nr   r   r   r   r   r#   I   r    r#   c                       s    e Zd ZdZ fddZ  ZS )DisablePrinterzDisables formatted output and does not consume the resources.

  Disables formatted output and does not consume the resources. Equivalent to
  the *none* format, but also short-circuits early for commands that return
  pageable lists.
  c                    s$   t t| j|i | ddi| _d S )Ndisable   )superr$   __init__
attributes)selfargskwargs	__class__r   r   r(   Y   s   zDisablePrinter.__init__)r   r   r   r   r(   __classcell__r   r   r-   r   r$   Q   s    r$   c                   @   r   )NonePrinterzqDisables formatted output and consumes the resources.

  Disables formatted output and consumes the resources.
  Nr   r   r   r   r   r0   ^   r    r0   c                   @   r   )TextPrinterzIAn alias for FlattenedPrinter.

  An alias for the *flattened* format.
  Nr   r   r   r   r   r1   e   r    r1   c                       s*   e Zd ZdZ fddZdddZ  ZS )MultiPrintera  A printer that prints different formats for each projection key.

  Each projection key must have a subformat defined by the
  :format=FORMAT-STRING attribute. For example,

    `--format="multi(data:format=json, info:format='table[box](a, b, c)')"`

  formats the *data* field as JSON and the *info* field as a boxed table.

  Printer attributes:
    separator: Separator string to print between each format. If multiple
      resources are provided, the separator is also printed between each
      resource.
  c                    sn   t t| j|i | g | _| j D ]!}|jjs%tdj	t
|jd| j|t|jj| jdf qd S )Nz {key} requires format attribute.)key)out)r'   r2   r(   columnscolumn_attributesColumns	attribute	subformatr"   formatr   
GetKeyNamer3   appendPrinter_out)r*   r+   r,   colr-   r   r   r(   |   s   
zMultiPrinter.__init__Tc                 C   sx   | j dd}t| jD ]\}\}}|dks|r| j| |t||j	 q| j dd}|r:| j| d S d S )N	separator r   
terminator)
r)   get	enumerater5   r>   writePrintr   Getr3   )r*   recorddelimitr@   ir?   printerrB   r   r   r   
_AddRecord   s   zMultiPrinter._AddRecord)T)r   r   r   r   r(   rL   r/   r   r   r-   r   r2   l   s    r2   c                   @   r   )PrinterAttributesa$  Attributes for all printers. This docstring is used to generate topic docs.

  All formats have these attributes.

  Printer attributes:
    disable: Disables formatted output and does not consume the resources.
    json-decode: Decodes string values that are JSON compact encodings of list
      and dictionary objects. This may become the default.
    pager: If True, sends output to a pager.
    private: Disables log file output. Use this for sensitive resource data
      that should not be displayed in log files. Explicit command line IO
      redirection overrides this attribute.
    transforms: Apply projection transforms to the resource values. The default
      is format specific; table-like formats may define default transforms to
      certain columns. Use *no-transforms* to disable.
  Nr   r   r   r   r   rM      r    rM   c                 C   s   |t | < dt| < d S )NT)_FORMATTERS_HIDDEN_FORMATTERS)format_namerK   r   r   r   RegisterFormatter   s   rQ   Fc                    s    fddt D S )zReturns the (format-name => Printer) format registry dictionary.

  Args:
    hidden: bool, if True, include the hidden formatters.

  Returns:
    The (format-name => Printer) format registry dictionary.
  c                    s"   i | ]} s
|t vr|t| qS r   )rO   rN   ).0rP   hiddenr   r   
<dictcomp>   s    z%GetFormatRegistry.<locals>.<dictcomp>)rN   rS   r   rS   r   GetFormatRegistry   s   
	rV   c                   C   s   t tS )z0Returns a sorted list of supported format names.)sortedrN   r   r   r   r   SupportedFormats   s   rX   Nc              	   C   s   ddl m} tjjj }| tj	r|r|}n| }t
d| |j|tj|t dd}|  }|s:dS zt| }	W n tyS   tddt |w |	||t||d	}
|
S )
aB  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.
  r   )resource_projectorzChosen display Format:{})defaultssymbols)
expressionrZ   NzhFormat must be one of {0}; received [{1}].

For information on output formats:
  $ gcloud topic formats
z, )r4   namerK   	projectorconsole_attr)googlecloudsdk.core.resourcerY   core_propertiesVALUEScoredefault_formatrG   endswithformatsDEFAULTr   debugr:   Compiler   ProjectionSpecr   GetTransforms
ProjectionNamerN   KeyErrorr!   joinrX   r=   )print_formatr4   rZ   r_   rY   default_format_propertychosen_print_formatr^   printer_nameprinter_classrK   r   r   r   r=      s:   r=   c                 C   s&   t |||d}|r|| | dS dS )a  Prints the given resources.

  Args:
    resources: A singleton or list of JSON-serializable Python objects.
    print_format: The _FORMATTER name with optional projection expression.
    out: Output stream, log.out if None.
    defaults: Optional resource_projection_spec.ProjectionSpec defaults.
    single: If True then resources is a single item and not a list.
      For example, use this to print a single object as JSON.
  )r4   rZ   N)r=   rF   )	resourcesrp   r4   rZ   singlerK   r   r   r   rF     s   rF   )F)NNN)NNF)Hr   
__future__r   r   r   googlecloudsdk.corer   core_exceptionsr   r   ra   r`   r   r	   r
   r   r   r   r   r   r   r   rf   r   r   r   r   r   r   r!   r"   YamlPrinterr#   ResourcePrinterr$   r0   FlattenedPrinterr1   r2   rM   CONFIGConfigPrinterCSV
CsvPrinterrg   DIFFDiffPrinterDISABLE	FLATTENEDGET
GetPrinterJSONJsonPrinterLISTListPrinterMULTINONEOBJECTObjectPrinterTABLETablePrinterTEXTVALUEValuePrinterYAMLrN   rO   rQ   rV   rX   r=   rF   r   r   r   r   <module>   s   (	


9