
    ;                         d Z ddlmZ ddlmZ ddlmZ ddlZ G d dej                        Z G d d	ej                        Zd
 Z	ddZ
y)z9Utilities for interacting with folders in gcloud storage.    )absolute_import)division)unicode_literalsNc                        e Zd ZdZdZdZdZdZy)ManagedFolderSettingz+Indicates how to deal with managed folders.do_not_listlist_as_prefixeslist_with_objectslist_without_objectsN)__name__
__module____qualname____doc__DO_NOT_LISTLIST_AS_PREFIXESLIST_WITH_OBJECTSLIST_WITHOUT_OBJECTS     5lib/googlecloudsdk/command_lib/storage/folder_util.pyr   r      s$    3 + ( * 0r   r   c                        e Zd ZdZdZdZdZdZy)FolderSettingz'Indicates how to deal with HNS folders.r   r	   list_as_foldersr   N)r   r   r   r   r   r   LIST_AS_FOLDERSr   r   r   r   r   r   ,   s#    / + ( &/ 0r   r   c                     | j                   }|j                   }| j                  }|j                  |      |z   }|j                  |      S )z3Checks containment based on string representations.)versionless_url_string	delimiterrstrip
startswith)potential_container_urlpotential_containee_urlpotential_container_stringpotential_containee_stringr   prefixs         r   	_containsr%   @   sM    6MM6MM
 &//)%,,Y7)C&	#	.	.v	66r   c              #      K   |sd }g }| D ]D  }	 |rt         ||d          ||            r|j                  |       3|j                          B |r|j                          |ryyw)ab  Reorders resources so containees are yielded before containers.

  For example, an iterator with the following:
  [
      gs://bucket/prefix/,
      gs://bucket/prefix/object,
      gs://bucket/prefix/object2,
      gs://bucket/prefix2/,
      gs://bucket/prefix2/object,
  ]

  Will become:
  [
      gs://bucket/prefix/object,
      gs://bucket/prefix/object2,
      gs://bucket/prefix/,
      gs://bucket/prefix2/object,
      gs://bucket/prefix2/,
  ]

  Args:
    ordered_iterator (Iterable[object]): Yields objects containing resources
      s.t. container resources are yielded before and contiguous with all of
      their containee resources. Bucket/folder/object resources ordered
      alphabetically by storage URL safisfy this constraint.
    get_url_function (None|object -> storage_url.StorageUrl): Maps objects
      yielded by `iterator` to storage URLs. Defaults to assuming yielded
      objects are URLs. Similar to the `key` attribute on the built-in
      list.sort() method.

  Yields:
    Resources s.t. containees are yielded before their containers, and
      contiguous with other containees.
  c                     | S Nr   )urls    r   <lambda>z+reverse_containment_order.<locals>.<lambda>q   s    3r   N)r%   appendpop)ordered_iteratorget_url_functionstackresource_containers       r   reverse_containment_orderr2   M   s     F 
&
 %,
i
59
%
-
. 	'(iik  - 	
))+ 	s   A'A,*A,r(   )r   
__future__r   r   r   enumEnumr   r   r%   r2   r   r   r   <module>r6      sA    @ &  ' 0499 0(0DII 0(
75r   