
    F                        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rSSKJ	r	  SSK
Jr  SS	K
J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 jr " S S\R(                  5      rS r " S S\R.                  " \R0                  5      5      r " S S\5      r " S S\R.                  " \R0                  5      5      rg)z'Generic functions for listing commands.    )absolute_import)division)print_function)unicode_literalsN)	cloud_api)errors)folder_util)plurality_checkable_iterator)wildcard_iterator)resource_reference)shim_format_utilc                 j    U(       a  U b  [         R                  " XS9$ [        R                  " U 5      $ )N)use_gsutil_style)r   get_human_readable_byte_valuesix	text_type)sizereadable_sizesr   s      3lib/googlecloudsdk/command_lib/storage/list_util.py#check_and_convert_to_readable_sizesr   #   s3     (99 
 ==    c                   (    \ rS rSrSrSrSrSrSrSr	g)	DisplayDetail/   z5Level of detail to display about items being printed.             N)
__name__
__module____qualname____firstlineno____doc__SHORTLONGFULLJSON__static_attributes__r   r   r   r   r   /   s    =
%	
$	
$	
$r   r   c                    U [         R                  :X  a!  U(       a  [        R                  R                  $ [         R                  [        R                  R                  [         R                  [        R                  R
                  [         R                  [        R                  R                  [         R                  [        R                  R                  0nX    $ )a	  Translates display details to fields scope equivalent.

Args:
  display_detail (DisplayDetail): Argument to translate.
  is_bucket_listing (bool): Buckets require special handling.

Returns:
  cloud_api.FieldsScope appropriate for the resources and display detail.
)r   r&   r   FieldsScoper%   NO_ACLr'   r(   )display_detailis_bucket_listingdisplay_detail_to_fields_scopes      r   )_translate_display_detail_to_fields_scoper0   8   s     }))).?  &&& 90066)//66)//44)//44	$  
(	77r   c                   H    \ rS rSrSr\R                  SSSSS4S jrS rSr	g)BaseFormatWrapperQ   zFor formatting how items are printed when listed.

Child classes should set _header_wrapper and _object_wrapper.

Attributes:
  resource (resource_reference.Resource): Item to be formatted for printing.
NFc                 X    Xl         X l        X0l        X@l        XPl        X`l        Xpl        g)am  Initializes wrapper instance.

Args:
  resource (resource_reference.Resource): Item to be formatted for printing.
  display_detail (DisplayDetail): Level of metadata detail for printing.
  full_formatter (base.FullResourceFormatter): Printing formatter used witch
    FULL DisplayDetail.
  include_etag (bool): Display etag string of resource.
  object_state (cloud_api.ObjectState): What versions of an object to query.
  readable_sizes (bool): Convert bytes to a more human readable format for
    long lising. For example, print 1024B as 1KiB.
  use_gsutil_style (bool): Outputs closer to the style of the gsutil CLI.
N)resource_display_detail_full_formatter_include_etag_object_state_readable_sizes_use_gsutil_style)selfr5   r-   full_formatterinclude_etagobject_stater   r   s           r   __init__BaseFormatWrapper.__init__Z   s-    . M))%%)-r   c                    U R                   [        R                  R                  [        R                  R                  4;   nU(       a\  U R
                  R                  R                  nSR                  [        R                  " U R
                  R                  5      5      nX#4$ U R
                  R                  R                  nSnX#4$ )Nz  metageneration={} )r9   r   ObjectStateLIVE_AND_NONCURRENTSOFT_DELETEDr5   storage_url
url_stringformatr   r   metagenerationversionless_url_string)r<   show_version_in_urlrH   metageneration_strings       r   _check_and_handles_versions-BaseFormatWrapper._check_and_handles_versionsy   s    ,,11**1  ==,,77j3::
--44
5 .. ==,,CCj ..r   )r6   r7   r8   r9   r:   r;   r5   )
r    r!   r"   r#   r$   r   r%   r@   rN   r)   r   r   r   r2   r2   Q   s+     #((.>/r   r2   c                   D   ^  \ rS rSrSr       SU 4S jjrS rSrU =r$ )NullFormatWrapper   zAFor formatting how containers are printed as headers when listed.c	                 :   > [         [        U ]  U5        AAAAAAAg N)superrQ   r@   )
r<   r5   container_sizer-   r=   r>   r?   r   r   	__class__s
            r   r@   NullFormatWrapper.__init__   s*     

T+H5r   c                     g)NrC   r   )r<   s    r   __str__NullFormatWrapper.__str__   s    r   r   )NNNNNNN)	r    r!   r"   r#   r$   r@   rZ   r)   __classcell__)rW   s   @r   rQ   rQ      s,    I
 , r   rQ   c                       \ rS rSrSrS\R                  SSSSSSSSSSSSSS4S jrS r SS jr	S	 r
S
 rS rS rS rS rS rSrg)BaseListExecutor   z<Abstract base class for list executors (e.g. for ls and du).FNTc                 $   Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        Xl	        Xl
        Xl        Xl        Xl        Xl        UU l        UU l        SU l        [$        U l        [$        U l        [$        U l        g)a  Initializes executor.

Args:
  cloud_urls ([storage_url.CloudUrl]): List of non-local filesystem URLs.
  buckets_flag (bool): If given a bucket URL, only return matching buckets
    ignoring normal recursion rules.
  display_detail (DisplayDetail): Determines level of metadata printed.
  exclude_patterns (Patterns|None): Don't return resources whose URLs or
    local file paths matched these regex patterns.
  fetch_encrypted_object_hashes (bool): Fall back to GET requests for
    encrypted objects in order to fetch their hash values.
  halt_on_empty_response (bool): Stops querying after empty list response.
    See CloudApi for details.
  include_etag (bool): Print etag string of resource, depending on other
    settings.
  include_managed_folders (bool): Includes managed folders in list results.
  next_page_token (str|None): Used to resume LIST calls.
  object_state (cloud_api.ObjectState): Versions of objects to query.
  readable_sizes (bool): Convert bytes to a more human readable format for
    long lising. For example, print 1024B as 1KiB.
  recursion_flag (bool): Recurse through all containers and format all
    container headers.
  total (bool): Add up the total size of all input sources.
  use_gsutil_style (bool): Outputs closer to the style of the gsutil CLI.
  zero_terminator (bool): Use null byte instead of newline as line
    terminator.
  soft_deleted_buckets (bool): If true, soft deleted buckets will be listed.
  list_filter (str|None): Performs list call with specified filter string.
N)_cloud_urls_buckets_flagr6   _exclude_patterns_fetch_encrypted_object_hashes_halt_on_empty_responser8   _include_managed_folders_next_page_tokenr9   r:   _recursion_flag_totalr;   _zero_terminator_soft_deleted_buckets_list_filterr7   rQ   _header_wrapper_container_summary_wrapper_object_wrapper)r<   
cloud_urlsbuckets_flagr-   exclude_patternsfetch_encrypted_object_hasheshalt_on_empty_responser>   include_managed_foldersnext_page_tokenr?   r   recursion_flagtotalr   zero_terminatorsoft_deleted_bucketslist_filters                     r   r@   BaseListExecutor.__init__   s    b "%)-*G'#9 %$;!+%))K-+!5D#DD,D&7D#,Dr   c                    UR                  S5      n[        U R                  SS9nU R                  (       a  [        R
                  R                  nO[        R
                  R                  n[        R                  " USU R                  U R                  UU R                  U[        R                  R                  U R                  U R                  U R                   U R"                  S9nU R%                  Xb5      $ )a
  For recursing into and retrieving the contents of a container.

Args:
  container_cloud_url (storage_url.CloudUrl): Container URL for recursing
    into.
  recursion_level (int): Determines if iterator should keep recursing.

Returns:
  BaseFormatWrapper generator.
*Fr.   )error_on_missing_keyrr   rs   fields_scopert   managed_folder_settingfolder_settingrv   r?   rz   r{   )joinr0   r6   rf   r	   ManagedFolderSettingLIST_AS_PREFIXESDO_NOT_LISTr   CloudWildcardIteratorrc   rd   re   FolderSettingrg   r9   rk   rl   _recursion_helper)r<   container_cloud_urlrecursion_levelnew_cloud_urlr   r   iterators          r   _get_container_iterator(BaseListExecutor._get_container_iterator   s     (,,S1M<L $$
  +??PP*??KK 66"//&*&I&I!#;;5"00AA--''!77%%H !!(<<r   c              #   :  #    U GH  n[         R                  " U5      (       Ga  US:  Ga  U R                  [        :w  aS  U R                  UU R                  U R
                  U R                  U R                  U R                  U R                  S9v   SnU R                  UR                  US-
  5      nU H<  nU R                  [        :w  a!  U(       a  U[        UR                  SS5      -  nUv   M>     U R                  [        :w  a5  U(       a+  U R                  UUU R                  U R                  S9v   GM7  GM:  GM=  U R                  UU R                  U R                  U R
                  U R                  U R                  U R                  S9v   GM     g7f)a-  For retrieving resources from URLs that potentially contain wildcards.

Args:
  iterator (Iterable[resource_reference.Resource]): For recursing through.
  recursion_level (int): Integer controlling how deep the listing recursion
    goes. "1" is the default, mimicking the actual OS ls, which lists the
    contents of the first level of matching subdirectories. Call with
    "float('inf')" for listing everything available.
  print_top_level_container (bool): Used by `du` to skip printing the top
    level bucket

Yields:
  BaseFormatWrapper generator.
r   )r-   r>   r?   r   r=   r   r   r   )r5   rV   r?   r   )r-   r=   r>   r?   r   r   N)r   !is_container_or_has_container_urlrm   rQ   r6   r8   r9   r:   r7   r;   r   rG   rn   getattrr5   ro   )r<   r   r   print_top_level_containerr5   rV   nested_iteratornested_resources           r   r   "BaseListExecutor._recursion_helper  s    "  
>
>x
H
H!#44$$!11----!11!11#55 %   66  /A"5
  /O--1BB+go&>&>JJN
  / ++/@@)//+--!11	 0   * A ""////++++//!33 # 
 	
W s   FFc                     A AAAAg rT   r   )r<   resource_urlonly_display_bucketsobject_counttotal_bytess        r    _print_summary_for_top_level_url1BaseListExecutor._print_summary_for_top_level_urla  s     	l0,r   c                    S=pEU R                   (       a  SOSn[        U5       H  u  px[        R                  " U5      n	[	        UR
                  [        R                  5      (       a&  US-  nXXR
                  R                  =(       d    S-  nU	(       d  Ms  US:X  a  U(       a  U	S   S:X  a  [        U	SS US9  M  [        XS9  M     U R                  UUUUS9  U$ )zBPrints ResourceWrapper objects in list with custom row formatting.r    
r   N)end)r   r   r   r   )rj   	enumerater   r   
isinstancer5   r   ObjectResourcer   printr   )
r<   resource_wrappersr   r   r   r   
terminatoriresource_wrapperresource_wrapper_strings
             r   _print_row_list BaseListExecutor._print_row_listf  s     "#"L..DJ():; #.> ?	

#
#%7%F%F
 
 	0055::$	
a$)@)Ct)K 	%ab)z:%6  <" 	))!1!	 *  r   c                 v    UR                  5       =(       d#    U R                  =(       a    UR                  5       $ rT   )is_providerrb   	is_bucket)r<   raw_cloud_urls     r   _should_only_display_buckets-BaseListExecutor._should_only_display_buckets  s1     $$& 8}668r   c                    [        U R                  UR                  5       S9nU R                  (       a  [        R
                  R                  nO[        R
                  R                  n[        R                  " [        R                  " USU R                  U R                  UU R                  U R                  U[        R                   R                  U R"                  U R$                  U R&                  U R(                  S95      nUR+                  5       (       a  [,        R.                  " S5      eU R1                  U5      nU(       a  U R3                  USS9nOU R4                  (       aF  SUR6                  ;  a6  SnUR9                  5       (       a  SnU R3                  U[;        S	5      U5      nOwUR=                  5       (       dR  [>        R@                  " URC                  5       5      (       a)  U RE                  URC                  5       RF                  SS9nOU R3                  US
S9nSnU R                  [H        RJ                  :X  a  U RM                  U5        U$ U RO                  XaU5      nU$ )zBRecursively create wildcard iterators to print all relevant items.r   F)r   rr   rs   r   get_bucket_metadatart   r   r   rv   r?   rz   r{   z$One or more URLs matched no objects.r   )r   z**Tinfr   )(r0   r6   r   rf   r	   r   r   r   r
   PluralityCheckableIteratorr   r   rc   rd   rb   re   r   rg   r9   rk   rl   is_emptyr   InvalidUrlErrorr   r   rh   rH   r   float	is_pluralr   r   peekr   rG   r   r(   _print_json_listr   )	r<   r   r   r   	resourcesr   resources_wrappersr   size_in_bytess	            r   	_list_urlBaseListExecutor._list_url  s&   <0I0I0KL $$*??PP*??KK,GG//!&!33*.*M*M% $ 2 2#'#?#?#9&44EE 11++!%!;!;))	
I$ ""#IJJ<<]K11)Q1O			$m.F.F"F"&		 	 	"	"$)!11
U5\#<   ""<<Y^^=MNN  77
..

&
& 8   11)Q1OM}111
./
  **
-Am r   c                     Ag rT   r   )r<   all_sources_total_bytess     r   _print_totalBaseListExecutor._print_total  s    r   c                     Ag rT   r   )r<   urls     r   _print_bucket_header%BaseListExecutor._print_bucket_header  s    r   c                 .   SnU R                    H`  nUR                  5       (       a  U R                  U5        U R                  (       a  XR	                  U5      -  nMO  U R	                  U5        Mb     U R                  (       a  U R                  U5        g g )Nr   )ra   r   r   ri   r   r   )r<   r   r   s      r   	list_urlsBaseListExecutor.list_urls  ss    	!!#&	>>##66s   {{
/0 r   )rb   ra   rn   r6   rc   rd   r7   re   rm   r8   rf   rl   rg   r9   ro   r:   rh   rk   ri   r;   rj   )T)r    r!   r"   r#   r$   r   r%   r@   r   r   r   r   r   r   r   r   r   r)   r   r   r   r^   r^      s~    D
 "(($)!# %G-R'=T BFD
LL
@=~ 
1r   r^   )FF)r$   
__future__r   r   r   r   abcenumgooglecloudsdk.api_lib.storager   "googlecloudsdk.command_lib.storager   r	   r
   r   ,googlecloudsdk.command_lib.storage.resourcesr   r   r   r   Enumr   r0   with_metaclassABCMetar2   rQ   r^   r   r   r   <module>r      s    . &  % ' 
  4 5 : K @ K I 
 27	DII 825/**3;;7 5/p) :z1s))#++6 z1r   