
    +                        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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rSrSR1                  5       r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\R>                  5      r  " S S\!5      r" " S S\5      r# " S S \5      r$ " S! S"\5      r%S# r&S$ r'g)%zShell tab completion.    )absolute_import)print_function)division)unicode_literalsN)CannedACLStrings)IsFileUrlString)StorageUrlFromString)StripOneSlash)GetTabCompletionCacheFilename)GetTabCompletionLogFilename)CreateWildcardIterator   i  z
Tab completion aborted (took >%ss), you may complete the command manually.
The timeout can be adjusted in the gsutil configuration file.
c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
CompleterType/   cloud_bucketcloud_objectcloud_or_local_objectlocal_objectlocal_object_or_canned_aclno_op N)__name__
__module____qualname____firstlineno__CLOUD_BUCKETCLOUD_OBJECTCLOUD_OR_LOCAL_OBJECTLOCAL_OBJECTLOCAL_OBJECT_OR_CANNED_ACLNO_OP__static_attributes__r       %platform/gsutil/gslib/tab_complete.pyr   r   /   s"    ,,1,;
%r$   r   c                   $    \ rS rSrSrS rS rSrg)LocalObjectCompleter8   z!Completer object for local files.c                 (    SSK Jn  U" 5       U l        g )Nr   )FilesCompleter)argcomplete.completersr*   files_completer)selfr*   s     r%   __init__LocalObjectCompleter.__init__;   s     6)+Dr$   c                 (    U R                   " U40 UD6$ Nr,   )r-   prefixkwargss      r%   __call__LocalObjectCompleter.__call__B   s    1&11r$   r2   Nr   r   r   r   __doc__r.   r5   r#   r   r$   r%   r'   r'   8   s    ),2r$   r'   c                   $    \ rS rSrSrS rS rSrg)LocalObjectOrCannedACLCompleterF   zxCompleter object for local files and canned ACLs.

Currently, only Google Cloud Storage canned ACL names are supported.
c                 "    [        5       U l        g r1   )r'   local_object_completer)r-   s    r%   r.   (LocalObjectOrCannedACLCompleter.__init__L   s    "6"8Dr$   c                     U R                   " U40 UD6n[         Vs/ s H  oDR                  U5      (       d  M  UPM     nnX5-   $ s  snf r1   )r=   r   
startswith)r-   r3   r4   local_objectsaclcanned_aclss         r%   r5   (LocalObjectOrCannedACLCompleter.__call__O   sE    //A&AM"2M"23nnV6L3"2KM&& Ns
   AA)r=   Nr7   r   r$   r%   r:   r:   F   s    
9'r$   r:   c                   @    \ rS rSrSrS r\S 5       rS rS r	S r
Srg	)
TabCompletionCacheU   z!Cache for tab completion results.c                 4    Xl         X l        X0l        X@l        g r1   )r3   results	timestamppartial_results)r-   r3   rI   rJ   rK   s        r%   r.   TabCompletionCache.__init__X   s    KLN*r$   c                     [        U S5       n[        R                  " UR                  5       5      nUS   nUS   nUS   nUS   nSSS5        [        WWWW5      $ ! , (       d  f       N= f! [         a    Sn/ nSnSn N3f = f)	zInstantiates the cache from a file.

Args:
  filename: The file to load.
Returns:
  TabCompletionCache instance with loaded data or an empty cache
      if the file cannot be loaded
rr3   rI   rJ   partial-resultsNr   F)openjsonloadsread	ExceptionrF   )filenamefp
cache_dictr3   rI   rJ   rK   s          r%   LoadFromFileTabCompletionCache.LoadFromFile^   s    #"ZZ	*
H%Y'{+	$%67  fgy/JJ    fgios.   A. 9AA. 
A+'A. +A. .BBc                    [         R                   " 5       nX R                  -
  [        :  a  gSnXR                  :X  a  U R                  nOU R
                  (       d  UR                  U R                  5      (       aa  UR                  S5      U R                  R                  S5      :X  a3  U R                   Vs/ s H  oDR                  U5      (       d  M  UPM     nnUb  [         R                   " 5       U l        U$ gs  snf )z>Returns the cached results for prefix or None if not in cache.N/)timerJ   TAB_COMPLETE_CACHE_TTLr3   rI   rK   r@   count)r-   r3   current_timerI   xs        r%   GetCachedResults#TabCompletionCache.GetCachedResultsz   s    99;Lnn$(>>Gg""v'8'8'E'E
,,s
t{{005
5 LLALqLL,@LgA
 yy{dnn  Bs   8C=C=c                 \    Xl         X l        X0l        [        R                  " 5       U l        g)zBUpdates the in-memory cache with the results for the given prefix.N)r3   rI   rK   r\   rJ   )r-   r3   rI   rK   s       r%   UpdateCacheTabCompletionCache.UpdateCache   s    KL*YY[DNr$   c                    [         R                  " U R                  U R                  U R                  U R
                  S.5      n [        US5       nUR                  U5        SSS5        g! , (       d  f       g= f! [         a     gf = f)z'Writes out the cache to the given file.)r3   rI   rO   rJ   wN)	rQ   dumpsr3   rI   rK   rJ   rP   writeIOError)r-   rU   json_strrV   s       r%   WriteToFileTabCompletionCache.WriteToFile   sm    zz++<<//^^	 H#"
  
s0   A= A,#A= ,
A:6A= :A= =
B
	B
)rK   r3   rI   rJ   N)r   r   r   r   r8   r.   staticmethodrX   ra   rd   rl   r#   r   r$   r%   rF   rF   U   s/    )+ K K6,!r$   rF   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )CloudListingRequestThread   z@Thread that performs a listing request for the given URL string.c                 ^   > [         [        U ]  5         SU l        Xl        X l        SU l        g)zInstantiates Cloud listing request thread.

Args:
  wildcard_url_str: The URL to list.
  gsutil_api: gsutil Cloud API instance to use.
TN)superrp   r.   daemon_wildcard_url_str_gsutil_apirI   )r-   wildcard_url_str
gsutil_api	__class__s      r%   r.   "CloudListingRequestThread.__init__   s-     

#T35DK-!DLr$   c                     [        U R                  U R                  5      R                  S/S9n[        R
                  " U[        5       Vs/ s H  n[        U5      PM     snU l        g s  snf )Nname)bucket_listing_fields)	r   ru   rv   IterAll	itertoolsislice_TAB_COMPLETE_MAX_RESULTSstrrI   )r-   itcs      r%   runCloudListingRequestThread.run   sd    	
!''A  "((-FGG1AGDL s   A*)rv   ru   rt   rI   )	r   r   r   r   r8   r.   r   r#   __classcell__)ry   s   @r%   rp   rp      s    H r$   rp   c                       \ rS rSrSrg)TimeoutError   r   N)r   r   r   r   r#   r   r$   r%   r   r      s    r$   r   c                   .    \ rS rSrSrSS jrS rS rSrg)	CloudObjectCompleter   z Completer object for Cloud URLs.c                     Xl         X l        g)zInstantiates completer for Cloud URLs.

Args:
  gsutil_api: gsutil Cloud API instance to use.
  bucket_only: Whether the completer should only match buckets.
N)rv   _bucket_only)r-   rx   bucket_onlys      r%   r.   CloudObjectCompleter.__init__   s     "#r$   c                     [        XR                  5      nUR                  5         UR                  U5        UR	                  5       (       a&  SSKnUR                  [        U-  5        [        5       eUR                  nU$ )a  Perform a remote listing request for the given wildcard URL.

Args:
  wildcard_url: The wildcard URL to list.
  timeout: Time limit for the request.
Returns:
  Cloud resources matching the given wildcard URL.
Raises:
  TimeoutError: If the listing does not finish within the timeout.
r   N)
rp   rv   startjoinis_aliveargcompletewarn_TIMEOUT_WARNINGr   rI   )r-   wildcard_urltimeoutrequest_threadr   rI   s         r%   _PerformCloudListing)CloudObjectCompleter._PerformCloudListing   sn     /|=M=MNN    ''12N$$GNr$   c                 .   U(       d  SnO[        U5      (       a  / $ US-   n[        U5      nU R                  (       a  UR                  5       (       d  / $ [        R
                  R                  SSS5      nUS:X  a  / $ [        R                  " 5       n[        R                  [        5       5      nUR                  U5      nSn	Ub  Un
Sn	Oe U R                  X55      n
U R                  (       a  [        U
5      S	:X  a  [        U
S   5      /n
[        U
5      [        :H  nUR!                  XU5        UR%                  [        5       5        [        R                  " 5       n[        U
5      nX-
  n['        SXUX-  U4-  5        U
$ ! ["         a    S
n	/ n
 Ncf = f)Nzgs://*GSUtiltab_completion_timeout   r    z (from cache)   z (request timeout)z:%s results%s in %.2fs, %.2f results/second for prefix: %s
)r   r	   r   IsBucketbotoconfiggetintr\   rF   rX   r   ra   r   lenr
   r   rd   r   rl   _WriteTimingLog)r-   r3   r4   r   urlr   
start_timecachecached_resultstiming_log_entry_typerI   rK   end_timenum_resultselapsed_secondss                  r%   r5   CloudObjectCompleter.__call__   s~   f		 	 iC<L
|
,Cikk  +CQGG!|iJ++,I,KLE++F3N!g-++LBW!2"71:./'w<+DD&?;
 
356yy{Hg,K+OE	_		&	0	01
 N   4s   A$F FF)r   rv   N)F)	r   r   r   r   r8   r.   r   r5   r#   r   r$   r%   r   r      s    ($6-r$   r   c                   $    \ rS rSrSrS rS rSrg)CloudOrLocalObjectCompleteri  zCompleter object for Cloud URLs or local files.

Invokes the Cloud object completer if the input looks like a Cloud URL and
falls back to local file completer otherwise.
c                 B    [        U5      U l        [        5       U l        g r1   )r   cloud_object_completerr'   r=   )r-   rx   s     r%   r.   $CloudOrLocalObjectCompleter.__init__#  s    "6z"BD"6"8Dr$   c                 f    [        U5      (       a  U R                  nOU R                  nU" U40 UD6$ r1   )r   r=   r   )r-   r3   r4   	completers       r%   r5   $CloudOrLocalObjectCompleter.__call__'  s3    v--i--iV&v&&r$   )r   r=   Nr7   r   r$   r%   r   r     s    9'r$   r   c                       \ rS rSrSrS rSrg)NoOpCompleteri/  z(Completer that always returns 0 results.c                     / $ r1   r   )r-   unused_prefixunused_kwargss      r%   r5   NoOpCompleter.__call__2  s    Ir$   r   N)r   r   r   r   r8   r5   r#   r   r$   r%   r   r   /  s
    0r$   r   c                    U [         R                  :X  a  [        U5      $ U [         R                  :X  a
  [	        5       $ U [         R
                  :X  a
  [        5       $ U [         R                  :X  a
  [        USS9$ U [         R                  :X  a  [        U5      $ U [         R                  :X  a
  [        5       $ [        SU -  5      e)zCreate a completer instance of the given type.

Args:
  completer_type: The type of completer to create.
  gsutil_api: gsutil Cloud API instance to use.
Returns:
  A completer instance.
Raises:
  RuntimeError: if completer type is not supported.
T)r   zUnknown completer "%s")r   r   r   r    r'   r!   r:   r   r   r   r"   r   RuntimeError)completer_typerx   s     r%   MakeCompleterr   6  s     }:::&z22333!!AAA*,,333
==333
++,,,?
/.@
AAr$   c                     [         R                  R                  SSS5      (       a/  [        [	        5       S5       nUR                  U 5        SSS5        gg! , (       d  f       g= f)zAWrite an entry to the tab completion timing log, if it's enabled.r   tab_completion_time_logsFabN)r   r   getboolrP   r   ri   )messagerV   s     r%   r   r   Q  sM    	[[#=uEE	)+T	2bhhw 
3	2 F	2	2s   A
A%)(r8   
__future__r   r   r   r   r   rQ   	threadingr\   r   boto.gs.aclr   gslib.storage_urlr   r	   r
   gslib.utils.boto_utilr   r   gslib.wildcard_iteratorr   r]   r   rstripr   objectr   r'   r:   rF   Threadrp   rT   r   r   r   r   r   r   r   r$   r%   <module>r      s     & %  '      ( - 2 + ? = :    FH F 26 2'f 'O Od	 0 0 29 U6 Up'& '&F B6r$   