
    Q                     z    S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJr  Sr	 " S	 S
\R                  5      rg)z%Custom printer for gsutil-style JSON.    )absolute_import)division)unicode_literalsN)resource_printer)resource_printer_base
gsutiljsonc                   Z   ^  \ rS rSrSrSrSrU 4S jr\S 5       r	S
S jr
U 4S jrS	rU =r$ )GsutilJsonPrinter   aO  Prints resource records as single line JSON string, just like gsutil.

To use this resource printer, first call this class's Register() method in a
target command's Args() method to add it to the available formatters. Then,
use `gsutiljson[empty="Message"]` rather than the usual `json` formatter to
mimic gsutil JSON output.

Printer attributes:
  empty: Returns this value if the resource is empty or if `key` is missing.
      Defaults to printing ''.
  key: The key of the record to output. Only recommended when printing a
      single resource. The full record is printed by default.
  empty_prefix_key: The key of the record to use as a prefix for the `empty`
      string when the `key` attribute is specified and lacks a value.

Attributes:
  _empty: True if no records were output.
  _delimiter: Delimiter string before the next record.
[]c                 p   > [         [        U ]
  " USS0UD6  SU l        U R                  U l        SU l        g )Nretain_none_valuesT )superr
   __init___empty_BEGIN_DELIMITER
_delimiter_prefix)selfargskwargs	__class__s      Glib/googlecloudsdk/command_lib/storage/resources/gsutil_json_printer.pyr   GsutilJsonPrinter.__init__5   sC    	
T+	"&*0 DK++DODL    c                  B    [         R                  " [        [        5        g)z+Register this as a custom resource printer.N)r   RegisterFormatter_PRINTER_FORMATr
    r   r   RegisterGsutilJsonPrinter.Register=   s     &&*r   c                    UnSU R                   ;   a!  U R                   S   nUR                  US5      nU(       dE  SU R                   ;   a-  [        UR                  U R                   S   S5      5      U l        SU l        gSU l        SU R                   ;   a"  [
        R                  " X0R                   S   S9nO[
        R                  " USS	9nU(       a0  U R                  R                  U R                  U-   5        S
U l	        gU R                  U R                  :w  a6  U R                  R                  U R                  5        U R                  U l	        U R                  R                  U5        g)zPrints one element of a JSON-serializable Python object resource list.

Allows intermingled delimit=True and delimit=False.

Args:
  record: A JSON-serializable object.
  delimit: Dump one record if False, used by PrintSingleRecord().
keyr   empty_prefix_keyTNFindent)r'   )	sort_keys,)
attributesgetstrr   r   jsondumps_outwriter   r   _END_DELIMITER)r   recorddelimitelementr%   outputs         r   
_AddRecordGsutilJsonPrinter._AddRecordD   s    GOOE"cC$g	t	.6::doo6H&I2NO dkDK4??"zz'//(*CDfzz'T2f
iioodoo./do	D11	1		++,//
iioofr   c                   > U R                   (       aF  SU R                  ;   a5  U R                  R                  U R                  U R                  S   -   5        OPU R
                  U R                  :w  a6  U R                  R                  U R                  5        U R                  U l        U R                  R                  S5        [        [        U ]+  5         g )Nempty
)r   r*   r/   r0   r   r   r   r1   r   r
   Finish)r   r   s    r   r;   GsutilJsonPrinter.Finishg   s    {{	DOO	#		tw'??@	D11	1
iiood))*--doIIOOD	
T)+r   )r   r   r   )T)__name__
__module____qualname____firstlineno____doc__r   r1   r   staticmethodr"   r6   r;   __static_attributes____classcell__)r   s   @r   r
   r
      s>    ( .  !F, ,r   r
   )rA   
__future__r   r   r   r-   googlecloudsdk.core.resourcer   r   r    ResourcePrinterr
   r!   r   r   <module>rH      s6    , &  '  9 >R,-== R,r   