
    F                        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Zddl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Z	 ddZ G d dej(                        Zd Z G d d ej.                  ej0                              Z G d de      Z G d d ej.                  ej0                              Zy)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                 b    |r| t        j                  | |      S t        j                  |       S )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   #   s6     (99/ 
 ==    c                        e Zd ZdZdZdZdZdZy)DisplayDetailz5Level of detail to display about items being printed.            N)__name__
__module____qualname____doc__SHORTLONGFULLJSON r   r   r   r   /   s    =
%	
$	
$	
$r   r   c                    | t         j                  k(  r|rt        j                  j                  S t         j                  t        j                  j                  t         j                  t        j                  j
                  t         j                  t        j                  j                  t         j                  t        j                  j                  i}||    S )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_scoper-   8   s     }))).?  &&& 90066)//66)//44)//44	$  
(	77r   c                   >    e Zd ZdZej
                  dddddfdZd Zy)BaseFormatWrapperzFor 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                 f    || _         || _        || _        || _        || _        || _        || _        y)a  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)selfr1   r*   full_formatterinclude_etagobject_stater   r   s           r   __init__zBaseFormatWrapper.__init__Z   s;    . DM)D)D%D%D)D-Dr   c                    | j                   t        j                  j                  t        j                  j                  fv }|r\| j
                  j                  j                  }dj                  t        j                  | j
                  j                              }||fS | j
                  j                  j                  }d}||fS )Nz  metageneration={} )r5   r   ObjectStateLIVE_AND_NONCURRENTSOFT_DELETEDr1   storage_url
url_stringformatr   r   metagenerationversionless_url_string)r8   show_version_in_urlrC   metageneration_strings       r   _check_and_handles_versionsz-BaseFormatWrapper._check_and_handles_versionsy   s    ,,11**1  ==,,77j3::
--44
5 -.. ==,,CCj -..r   )r   r   r    r!   r   r"   r<   rI   r&   r   r   r/   r/   Q   s+     #((.>/r   r/   c                   8     e Zd ZdZ	 	 	 	 	 	 	 d fd	Zd Z xZS )NullFormatWrapperzAFor formatting how containers are printed as headers when listed.c	                 :    t         t        |   |       ~~~~~~~y N)superrK   r<   )
r8   r1   container_sizer*   r9   r:   r;   r   r   	__class__s
            r   r<   zNullFormatWrapper.__init__   s*     

T+H5r   c                      y)Nr>   r&   )r8   s    r   __str__zNullFormatWrapper.__str__   s    r   )NNNNNNN)r   r   r    r!   r<   rR   __classcell__)rP   s   @r   rK   rK      s'    I
 ,r   rK   c                       e Zd ZdZdej
                  ddddddddddddddfdZd Z	 ddZd Z	d	 Z
d
 Zd Zd Zd Zd Zy)BaseListExecutorz<Abstract base class for list executors (e.g. for ls and du).FNTc                 B   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        || _        || _        || _        || _        || _        d| _        t$        | _        t$        | _        t$        | _        y)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_flagr2   _exclude_patterns_fetch_encrypted_object_hashes_halt_on_empty_responser4   _include_managed_folders_next_page_tokenr5   r6   _recursion_flag_totalr7   _zero_terminator_soft_deleted_buckets_list_filterr3   rK   _header_wrapper_container_summary_wrapper_object_wrapper)r8   
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<   zBaseListExecutor.__init__   s    b "D%D)D-D*GD'#9D %D$;D!+D%D)D)DDK-D+D!5D#DD,D&7D#,Dr   c                    |j                  d      }t        | j                  d      }| j                  rt        j
                  j                  }nt        j
                  j                  }t        j                  |d| j                  | j                  || j                  |t        j                  j                  | j                  | j                  | j                   | j"                        }| j%                  ||      S )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_keyrh   ri   fields_scoperj   managed_folder_settingfolder_settingrl   r;   rp   rq   )joinr-   r2   r\   r	   ManagedFolderSettingLIST_AS_PREFIXESDO_NOT_LISTr   CloudWildcardIteratorrY   rZ   r[   FolderSettingr]   r5   ra   rb   _recursion_helper)r8   container_cloud_urlrecursion_levelnew_cloud_urlrv   rw   iterators          r   _get_container_iteratorz(BaseListExecutor._get_container_iterator   s     (,,S1M<L $$
  +??PP*??KK 66"//&*&I&I!#;;5"00AA--''!77%%H !!(O<<r   c           
   #     K   |D ]}  }t        j                  |      r|dkD  r| j                  t        k7  rV| j                  || j                  | j
                  | j                  | j                  | j                  | j                         d}| j                  |j                  |dz
        }|D ]5  }| j                  t        k7  r|r|t        |j                  dd      z  }| 7 | j                  t        k7  s|s| j                  ||| j                  | j                         (| j                  || j                  | j                  | j
                  | j                  | j                  | j                          yw)aY  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   r9   r   r   r   )r1   rO   r;   r   )r*   r9   r:   r;   r   r   N)r   !is_container_or_has_container_urlrc   rK   r2   r4   r5   r6   r3   r7   r   rB   rd   getattrr1   re   )r8   r   r   print_top_level_containerr1   rO   nested_iteratornested_resources           r   r   z"BaseListExecutor._recursion_helper  s    "  
>
>x
H!#44$$!11----!11!11#55 %   66  /A"5
  /O--1BB+go&>&>JJN
  / ++/@@)//+--!11	 0   ""////++++//!33 # 
 	
W s   C:F=F BFc                      ~ ~~~~y rM   r&   )r8   resource_urlonly_display_bucketsobject_counttotal_bytess        r    _print_summary_for_top_level_urlz1BaseListExecutor._print_summary_for_top_level_urla  s     	l0,r   c                    dx}}| j                   rdnd}t        |      D ]  \  }}t        j                  |      }	t	        |j
                  t        j                        r"|dz  }||j
                  j                  xs dz  }|	sd|dk(  r|r|	d   dk(  rt        |	dd |       t        |	|        | j                  ||||       |S )zBPrints ResourceWrapper objects in list with custom row formatting.r    
r   N)end)r   r   r   r   )r`   	enumerater   r   
isinstancer1   r   ObjectResourcer   printr   )
r8   resource_wrappersr   r   r   r   
terminatoriresource_wrapperresource_wrapper_strings
             r   _print_row_listz BaseListExecutor._print_row_listf  s     "#"L;..DJ():; #.> ?	

#
#%7%F%F
 	'0055::$	
a$)@)Ct)K 	%ab)z:%:6  <" 	))!1!	 *  r   c                 b    |j                         xs | j                  xr |j                         S rM   )is_providerrX   	is_bucket)r8   raw_cloud_urls     r   _should_only_display_bucketsz-BaseListExecutor._should_only_display_buckets  s1     $$& 8}668r   c                    t        | j                  |j                               }| j                  rt        j
                  j                  }nt        j
                  j                  }t        j                  t        j                  |d| j                  | j                  || j                  | j                  |t        j                   j                  | j"                  | j$                  | j&                  | j(                              }|j+                         rt-        j.                  d      | j1                  |      }|r| j3                  |d      }n| j4                  r?d|j6                  vr1d}|j9                         rd}| j3                  |t;        d	      |      }nr|j=                         sOt?        j@                  |jC                               r,| jE                  |jC                         jF                  d      }n| j3                  |d
      }d}| j                  tH        jJ                  k(  r| jM                  |       |S | jO                  |||      }|S )zBRecursively create wildcard iterators to print all relevant items.rt   F)ru   rh   ri   rv   get_bucket_metadatarj   rw   rx   rl   r;   rp   rq   z$One or more URLs matched no objects.r   )r   z**Tinfr   )(r-   r2   r   r\   r	   rz   r{   r|   r
   PluralityCheckableIteratorr   r}   rY   rZ   rX   r[   r~   r]   r5   ra   rb   is_emptyr   InvalidUrlErrorr   r   r^   rC   r   float	is_pluralr   r   peekr   rB   r   r%   _print_json_listr   )	r8   r   rv   rw   	resourcesr   resources_wrappersr   size_in_bytess	            r   	_list_urlz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  77
..

&
& 8   11)Q1OM}111
./
  **
m-Am r   c                      ~y rM   r&   )r8   all_sources_total_bytess     r   _print_totalzBaseListExecutor._print_total  s    r   c                      ~y rM   r&   )r8   urls     r   _print_bucket_headerz%BaseListExecutor._print_bucket_header  s    r   c                    d}| j                   D ]U  }|j                         r| j                  |       | j                  r|| j	                  |      z  }E| j	                  |       W | j                  r| j                  |       y y )Nr   )rW   r   r   r_   r   r   )r8   r   r   s      r   	list_urlszBaseListExecutor.list_urls  ss    	!!#&	4>>##66s   {{
/0 r   )T)r   r   r    r!   r   r"   r<   r   r   r   r   r   r   r   r   r   r&   r   r   rU   rU      s~    D
 "(($)!# %G-R'=T BFD
LL
@=~ 
1r   rU   )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   r-   with_metaclassABCMetar/   rK   rU   r&   r   r   <module>r      s    . &  % ' 
  4 5 : K @ K I 
 27	DII 825/***3;;7 5/p) :z1)s))#++6 z1r   