
                             S r SSKJ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S	K	Jr  S
r " S S\R                  5      rg)z4Custom table printer for CAI team's asset query API.    )absolute_import)division)print_function)unicode_literalsN)extra_types)custom_printer_base)resource_printer
cloudassetc                   @    \ rS rSrSr\S 5       rS rS rS r	S r
Srg	)
AssetQueryPrinter    zBPrints the asset query response in a custom human readable format.c                     [         R                  " [        [        5        U R                  R                  [        5        g)z3Register this custom printer with the given parser.N)r	   RegisterFormatterASSET_QUERY_PRINTER_FORMATr   display_info	AddFormat)parsers    ;lib/googlecloudsdk/command_lib/asset/asset_query_printer.pyRegisterAssetQueryPrinter.Register#   s-     &&"$57
!!"<=    c                    [         R                  R                  U5      nSU;   a+  SUS   ;   a  US   S	 SUS   ;   a  US   S	 US   (       d  US	 [        R                  " 5       n[
        R                  " USUS9  UR                  5       $ )NqueryResultrowsschemayamlout)r   encodingMessageToPyValueioStringIOr	   Printgetvalue)selfrespresp_message
string_bufs       r   _FormatMetadata!AssetQueryPrinter._FormatMetadata*   s    ''88>L$	<.	.'/	\-0	0'1-('J<Z@  r   c                    [        US5      (       a  [        UR                  S5      (       d  gUR                  R                  nUR                  R                  n/ nUR                  (       d  g[
        R                  " 5       nUR                  SR                  SR                  S UR                   5       5      5      5        U Hb  n[        R                  R                  U5      n[        R                  R                  U5      nUR                  U R                  XS5      5        Md     [
        R                  " 5       n	[        R                   " XER#                  5       U	S9  [$        R&                  " U	R#                  5       /5      $ )	u  Formats rows in a [QueryAssetsResponse]'s queryResults into a table.

Args:
  resp: The [QueryAssetsResponse] that contains 0 or more rows.

Returns:
  A 'Lines' custom printer object that corresponds to the formatted table
  when printed out.

The response.queryResult.rows in response:
{
  "jobReference":
  "CiBqb2JfdDR2SFgwa3BPNFpQVDFudVJJaW5TdVNfS1N0YxIBAxjH8ZmAo6qckik",
  "done": true,
  "queryResult": {
    "rows": [
      {
        "f": [
          {
            "v":
            "//cloudresourcemanager.googleapis.com/folders/417243649856"
          }
        ]
      }
    ],
    "schema": {
      "fields": [
        {
          "field": "name",
          "type": "STRING",
          "mode": "NULLABLE"
        }
      ]
    },
    "total_rows": 1
  }
}
Will return a custom printer Lines object holding the following string:
┌────────────────────────────────────────────────────────────┐
│                            name                            │
├────────────────────────────────────────────────────────────┤
│ //cloudresourcemanager.googleapis.com/folders/417243649856 │
└────────────────────────────────────────────────────────────┘
r   r   Nztable[box]({})z, c              3   n   #    U  H+  nS R                  UR                  UR                  5      v   M-     g7f)z{}:label={}N)formatfield).0r.   s     r   	<genexpr>4AssetQueryPrinter._FormatRowTable.<locals>.<genexpr>x   s/      6$"E 	U[[%++66"s   35Fr   )hasattrr   r   r   fieldsr!   r"   writer-   joinr   r   r    append_ConvertFromFVr	   r#   r$   cpLines)
r%   r&   r   r   row_list	schemabufrowrow_jsonschema_jsonraw_outs
             r   _FormatRowTable!AssetQueryPrinter._FormatRowTable:   s8   b 4''wt7G7G7?0A 0A$$F  DH ==IOO$++DII 6$]]6$ -$ % &  %%66s;h((99&Akood))+GH  kkmG8%7%7%9wG 88W%%'())r   c                 0   U(       d  gUR                  S/ 5       Vs/ s H  oDR                  SS5      PM     nn0 nUnU(       d  US   n[        Xu5       GH-  u  pSU;  a  [        S5      eUS   R                  5       S:X  a  UR                  S/ 5      n
UR                  SS	5      R                  5       S
:X  a:  U	 Vs/ s H%  nU R	                  XR                  SS5      S5      PM'     snXhS   '   M  U R	                  XS5      nU(       a	  XUS   '   M  SXhS   '   M  UR                  SS	5      R                  5       S
:X  a0  U	 Vs/ s H  oR                  SS5      PM     nnUb  UOSXhS   '   GM  U	(       a  U	OSXhS   '   GM0     U$ s  snf s  snf s  snf )a  Converts from FV format to values.

Args:
  schema: The schema struct within the queryResult struct in the response.
  row: A single row of the response's queryResult.rows message.
  is_record: True if the row object is a record within an actual row.

Returns:
  A dictionary mapping row keys to the values that may be a simple datatype,
  a record (struct) in the form of a dictionary, or a list of either simple
  data types or records (again, in the form of dictionaries).

Raises:
  IOError: An error occurred accessing the smalltable.
 fvr3   typez'Invalid response: missing type propertyRECORDmodeNULLABLEREPEATEDTr.   )getzipIOErrorupperr7   )r%   r   r<   	is_recordentryvaluesresult
new_schemar.   rE   	subfieldssubvaluecur_vals                r   r7    AssetQueryPrinter._ConvertFromFV   s     .1ggc2.>?.>UiiR .>F?FJ(#j
+	u	?@@	v			(	*IIh+	99VZ(..0J>  $( !!)\\#r-BDI$&w
  ''	d;'%,5>"%'F>"99VZ(..0J>9:;X<<R(;,3,?RW~ '(RW~3 ,4 MA @$ <s   F	=,FFc                    [        US5      (       d  [        US5      (       d  gU R                  U5      nU R                  U5      n/ nU(       a  UR                  U5        U(       a  UR                  U5        [        R
                  " USS9$ )zBTransforms a CAI [QueryAssetsResponse] into human-readable format.jobReferenceerrorN<   )max_column_width)r2   r)   r@   r6   r8   Section)r%   r&   metadatar   sections_lists        r   	TransformAssetQueryPrinter.Transform   sx     4((w1G1G##D)H%DM8$4 ::mb99r    N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r)   r@   r7   r`   __static_attributes__rb   r   r   r   r       s.    J> >! L*\3j:r   r   )rg   
__future__r   r   r   r   r!   apitools.base.pyr   googlecloudsdk.core.resourcer   r8   r	   r   CustomPrinterBaser   rb   r   r   <module>rn      s>    ; &  % ' 	 ( B 9 * k:,, k:r   