o
    K                     @   sd   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
G dd	 d	eZdS )
z-Calliope argparse argument completer objects.    )absolute_import)division)unicode_literalsN)resource_cache)console_attrc                   @   sf   e Zd ZdZdddZedd Zedd Zdd	d
Z	dddZ
dddZdddZ	dddZdS )ArgumentCompletera  Argument completer wrapper to delay instantiation until first use.

  Attributes:
    _argument: The argparse argument object.
    _completer_class: The uninstantiated completer class.
    _parsed_args: argparse parsed_args, used here if not known at __call__ time.
  Nc                 C   s@   ddl m} || _|| _|| _dtjv r|j| _d S |j	| _d S )Nr   )progress_tracker_ARGCOMPLETE)
googlecloudsdk.core.consoler   _completer_class	_argument_parsed_argsosenvironCompletionProgressTracker_progress_trackerProgressTracker)selfcompleter_classparsed_argsargumentr    r   E/tmp/google-cloud-sdk/lib/googlecloudsdk/calliope/parser_completer.py__init__&   s   
zArgumentCompleter.__init__c                 C   s   | j S N)r   )r   r   r   r   r   4   s   z!ArgumentCompleter.completer_classc                    s&   t  }| \ } fdd|D S )z@Returns a msgs list that will display 1 per line as completions.c                    s$   g | ]}| d  t | d  qS )    )len).0msgwidthr   r   
<listcomp>>   s   $ zBArgumentCompleter._MakeCompletionErrorMessages.<locals>.<listcomp>)r   GetConsoleAttrGetTermSize)clsmsgsattr_r   r    r   _MakeCompletionErrorMessages8   s   z.ArgumentCompleter._MakeCompletionErrorMessagesc              	   C   sB   |rt |dr|j}n| jj}| d||d|t|gS )a  Handles completer errors by crafting two "completions" from exception.

    Fatal completer errors return two "completions", each an error
    message that is displayed by the shell completers, and look more
    like a pair of error messages than completions. This is much better than
    the default that falls back to the file completer with no indication of
    errors, typically yielding the list of all files in the current directory.

    NOTICE: Each message must start with different characters, otherwise they
    will be taken as valid completions. Also, the messages are sorted in the
    display, so the messages here are displayed with ERROR first and REASON
    second.

    Args:
      exception: The completer exception.
      prefix: The current prefix string to be matched by the completer.
      completer: The instantiated completer object or None.

    Returns:
      Two "completions" crafted from the completer exception.
    
collectionz&{}ERROR: {} resource completer failed.z{}REASON: {})hasattrr*   r   __name__r)   formatsix	text_type)r   	exceptionprefix	completercompleter_namer   r   r   _HandleCompleterException@   s   
z+ArgumentCompleter._HandleCompleterException c              	   K   s   t | jts| j|dS |s| j}|  , t }| j|||dW  d   W  d   S 1 s4w   Y  W d   dS 1 sDw   Y  dS )z+A completer function suitable for argparse.r1   )r1   cacher   N)	
isinstancer   type_CompleteFromFunctionr   r   r   ResourceCache_CompleteFromCompleterClass)r   r1   r   kwargsr7   r   r   r   __call___   s   

"zArgumentCompleter.__call__c              
   C   s@   z|  |W S  ty } z| j||dW  Y d}~S d}~ww )z-Helper to complete from a function completer.r6   Nr   BaseExceptionr4   )r   r1   er   r   r   r:   l   s   z'ArgumentCompleter._CompleteFromFunctionc              
   C   sN   d}z
|   }||dW S  ty& } z| j|||dW  Y d}~S d}~ww )z=Helper to complete from a class that isn't a cache completer.Nr6   r1   r2   r?   )r   r1   r2   rA   r   r   r   "_CompleteFromGenericCompleterClasss   s   z4ArgumentCompleter._CompleteFromGenericCompleterClassc              
   C   s   |rt | jjdkrdh}nt }d}z| j||d}||| j}|||W S  t	yW } z!t
|trE|sE| j|dW  Y d}~S | j|||dW  Y d}~S d}~ww )z Helper to complete from a class.   r*   N)r7   qualified_parameter_namesr6   rB   )r   _GetCommandaipositional_completerssetr   ParameterInfor   Completer@   r8   	TypeErrorrC   r4   )r   r1   r7   r   rE   r2   parameter_inforA   r   r   r   r<   ~   s(   z-ArgumentCompleter._CompleteFromCompleterClass)NNr   )r5   N)r5   )r5   NN)r,   
__module____qualname____doc__r   propertyr   classmethodr)   r4   r>   r:   rC   r<   r   r   r   r   r      s    






r   )rP   
__future__r   r   r   r   googlecloudsdk.core.cacher   r
   r   r.   objectr   r   r   r   r   <module>   s   