
    %(                         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ej8                        Z G d de      Z G d de      Z G d dej>                        Z  G d d ejB                        Z" G d! d"ejB                        Z# G d# d$ejH                        Z% G d% d&ejB                        Z& G d' d(ejB                        Z'i ejP                  ejR                  ejT                  ejV                  ejX                  e ejZ                  ej\                  ej^                  e"ej`                  ejH                  ejb                  ejd                  ejf                  ejh                  ejj                  ejl                  ejn                  e&ejp                  e#ejr                  ejt                  ejv                  ejx                  ejz                  e%ej|                  ej~                  ej                  ej>                  ZAi ZBd/d)ZCd/d*ZDd+ ZEd0d-ZFd1d.ZGy,)2a  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y)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     4lib/googlecloudsdk/core/resource/resource_printer.pyr   r   =   s    #r    r   c                       e Zd ZdZy)UnknownFormatErrorzUnknown format name exception.Nr   r   r    r!   r#   r#   A   s    &r    r#   c                       e Zd ZdZy)ProjectionFormatRequiredErrorz1Projection key missing required format attribute.Nr   r   r    r!   r%   r%   E   s    9r    r%   c                       e Zd ZdZy)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   s    r    r'   c                   "     e Zd ZdZ fdZ x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                 >    t        t        | 
  |i | ddi| _        y )Ndisable   )superr)   __init__
attributes)selfargskwargs	__class__s      r!   r.   zDisablePrinter.__init__Y   s"    	.$($9&9 !nDOr    )r   r   r   r   r.   __classcell__r3   s   @r!   r)   r)   Q   s    % %r    r)   c                       e Zd ZdZy)NonePrinterzqDisables formatted output and consumes the resources.

  Disables formatted output and consumes the resources.
  Nr   r   r    r!   r7   r7   ^       r    r7   c                       e Zd ZdZy)TextPrinterzIAn alias for FlattenedPrinter.

  An alias for the *flattened* format.
  Nr   r   r    r!   r:   r:   e   r8   r    r:   c                   *     e Zd ZdZ fdZddZ x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                    t        t        | 
  |i | g | _        | j                  j                         D ]  }|j                  j                  s8t        dj                  t        j                  |j                                    | j                  j                  |t        |j                  j                  | j                        f        y )Nz {key} requires format attribute.)key)out)r-   r<   r.   columnscolumn_attributesColumns	attribute	subformatr%   formatr   
GetKeyNamer>   appendPrinter_out)r0   r1   r2   colr3   s       r!   r.   zMultiPrinter.__init__|   s    	,&77DL%%--/]]$$+.55 ++CGG4 6 67 	7 ll//TYY?
@B 0r    c                    | j                   j                  dd      }t        | j                        D ]Y  \  }\  }}|dk7  s|r| j                  j                  |       |j                  t        j                  ||j                               [ | j                   j                  dd      }|r| j                  j                  |       y y )N	separator r   
terminator)
r/   get	enumerater@   rI   writePrintr   Getr>   )r0   recorddelimitrL   irJ   printerrN   s           r!   
_AddRecordzMultiPrinter._AddRecord   s    ##K4I&t||4>C	
a7			"mm%))&#'':; 5 $$\26J
iiooj! r    )T)r   r   r   r   r.   rX   r4   r5   s   @r!   r<   r<   l   s    B"r    r<   c                       e Zd ZdZy)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!   rZ   rZ      s    r    rZ   c                 .    |t         | <   |r
dt        | <   y y )NT_FORMATTERS_HIDDEN_FORMATTERS)format_namerW   hiddens      r!   RegisterFormatterra      s    $+k&*{# r    c                 X    t         D ci c]  }| s|t        vr
|t         |    c}S c c}w )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.
  r\   )r`   r_   s     r!   GetFormatRegistryrc      sF     
J
 5@;6H#H {;/
/

J J 
Js   'c                       t        t              S )z0Returns a sorted list of supported format names.)sortedr]   r   r    r!   SupportedFormatsrf      s    		r    Nc           	      t   ddl m} t        j                  j                  j
                  j                         }| j                  t        j                        r|r|}n| }t        j                  dj                  |             |j                  |t        j                  |t!        j"                                     }|j%                         j'                         }|sy	 t(        |   }	 |	||t2        ||	      }
|
S # t*        $ r3 t-        dj                  dj/                  t1                     |            w xY w)
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)
expressionri   NzhFormat must be one of {0}; received [{1}].

For information on output formats:
  $ gcloud topic formats
z, )r?   namerW   	projectorconsole_attr)googlecloudsdk.core.resourcerh   core_propertiesVALUEScoredefault_formatrS   endswithformatsDEFAULTr   debugrE   Compiler   ProjectionSpecr   GetTransforms
ProjectionNamer]   KeyErrorr#   joinrf   rH   )print_formatr?   ri   rn   rh   default_format_propertychosen_print_formatrm   printer_nameprinter_classrW   s              r!   rH   rH      s(   $ > ,2277FFJJL 7??+0G1&))&--.ABC (($'66%7%E%E%G ) ) %%',,.,	9-M c+")$-'3	5'
 
. 
 9
 
 F499%'(,79 99s   	C; ;<D7c                 J    t        |||      }|r|j                  | |       yy)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.
  )r?   ri   N)rH   rR   )	resourcesr   r?   ri   singlerW   s         r!   rR   rR     s(     LcH='MM)V$ r    )F)NNN)NNF)Hr   
__future__r   r   r   googlecloudsdk.corer   core_exceptionsr   r   rp   ro   r	   r
   r   r   r   r   r   r   r   r   ru   r   r   r   r   r   r   r#   r%   YamlPrinterr'   ResourcePrinterr)   r7   FlattenedPrinterr:   r<   rZ   CONFIGConfigPrinterCSV
CsvPrinterrv   DIFFDiffPrinterDISABLE	FLATTENEDGET
GetPrinterJSONJsonPrinterLISTListPrinterMULTINONEOBJECTObjectPrinterTABLETablePrinterTEXTVALUEValuePrinterYAMLr]   r^   ra   rc   rf   rH   rR   r   r    r!   <module>r      sR   * '  ' = # = 7 4 5 : 5 5 7 5 > J A : ; 6 5$O!! $' ':E :\-- 
%*:: 
%'77 #44 %"(88 %"P-== &NNN00KK'' OO^ LL,**	
 OO^ (99 KK'' LL,** LL,** MM< LL+ NNN00 MM=-- LL+ MM;++  LL,**!&  +
J6r%r    