
    ^                     z    d 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Z	 G d	 d
ej                        Zy)z%Custom printer for gsutil-style JSON.    )absolute_import)division)unicode_literalsN)resource_printer)resource_printer_base
gsutiljsonc                   L     e Zd ZdZdZdZ fdZed        ZddZ	 fdZ
 xZS )	GsutilJsonPrinteram  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                 n    t        t        | 
  |ddi| d| _        | j                  | _        d| _        y )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   zGsutilJsonPrinter.__init__5   sC    	
T+	"&*0 DK++DODL    c                  D    t        j                  t        t        d       y)z+Register this as a custom resource printer.T)hiddenN)r   RegisterFormatter_PRINTER_FORMATr
    r   r   RegisterzGsutilJsonPrinter.Register=   s     &&*4r   c                    |}d| j                   v r!| j                   d   }|j                  |d      }|sCd| j                   v r-t        |j                  | j                   d   d            | _        d| _        yd| _        d| j                   v r%t        j                  || j                   d         }nt        j                  |d	      }|r0| j                  j                  | j                  |z          d
| _	        y| j                  | j                  k7  r6| j                  j                  | j                         | j                  | _	        | j                  j                  |       y)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   
_AddRecordz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                    | j                   rDd| j                  v r| j                  j                  | j                  | j                  d   z          nO| j
                  | j                  k7  r6| j                  j                  | j                         | j                  | _        | j                  j                  d       t        t        | +          y )Nempty
)r   r(   r-   r.   r   r   r   r/   r   r
   Finish)r   r   s    r   r8   zGsutilJsonPrinter.Finishg   s    {{	DOO	#		tw'??@	D11	1
iiood))*--doIIOOD	
T)+r   )T)__name__
__module____qualname____doc__r   r/   r   staticmethodr!   r4   r8   __classcell__)r   s   @r   r
   r
      s>    ( .  !F, ,r   r
   )r<   
__future__r   r   r   r+   googlecloudsdk.core.resourcer   r   r   ResourcePrinterr
   r    r   r   <module>rB      s6    , &  '  9 >R,-== R,r   