
                         d    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mZ ddlm	Z	 d Z
d	 ZddZy
)z)Helper methods for interactive prompting.    )absolute_import)division)unicode_literals)errors)resource_args)
console_ioc                 ~  
 g }	  |       }t        j                  |    }|r@d|j                  z  }t        j                  ||gz   d|z        }|t        |      k  r||   S t        j                  |       

fd}d|j                  z   }d|z  }	t        j                  |||	      S # t         j                  $ r Y w xY w)a  Prompts the user to select a resource.

  Args:
    name: the name of the resource. For example, "environment" or "developer".
    long_name: a longer form of `name` which the user will see in prompts.
      Should explain the context in which the resource will be used. For
      example, "the environment to be updated".
    list_func: a function that returns the names of existing resources.

  Returns:
    The resource's identifier if successful, or None if not.
  z(some other %s)z
Select %s:)prompt_stringc                 *    j                  |       d uS N)search)responsevalid_patterns    0lib/googlecloudsdk/command_lib/apigee/prompts.py<lambda>z,ResourceFromFreeformPrompt.<locals>.<lambda>:   s    }33H=TI    z'Doesn't match the expected format of a z
Enter %s: )
r   RequestErrorr   ENTITIES	docs_namer   PromptChoicelenValidPatternForEntityPromptWithValidator)name	long_name	list_funcresource_listentity_namesenter_manuallychoice	validator	error_strprompt_messager   s             @r   ResourceFromFreeformPromptr$      s     -	KM
 ''-,&)?)??N$$(("Y.0F
 M""6""55d;-I)7,:P:PP))+.		'	'	9n	MM) 
		 		s   B& &B<;B<c                    g }d}|t        |      k  r|t        |      k  r|d| ||dz   d z   }n4|t        |      k(  r&t        j                  |       }|j                  |       |D cg c]  }d|z  	 }}|j                  |       |j                  |rdn|       t        j                  |      }|t        |      k  r|S c c}w )a7  Returns a list of strings inputted by the user.

  Args:
    message: the message to display when prompting for a new string.
    add_message: the menu option for adding a new string to the list.
    empty_done_message: the menu option to display for the "Done" option if no
      strings have been selected.
  r   N   Remove `%s`Done)r   r   PromptResponseappendr   )messageadd_messageempty_done_messagechosenmenu_option
input_dataitemoptionss           r   ListFromFreeformPromptr3   A   s     &+s6{"S[ l{#f[1_-=&>>f	F	#,,W5jmmJ067}t#G7NN;NNV6);<))'2K 	s6{" 
-	 8s   (CNc                     |       }|s:t         j                  |    j                  }t        j                  d|d|d      g }d}t        |      dz   }|t        |      k7  r|t        |      k  r|d| ||dz   d z   }n0|t        |      k  r"|t        |      z
  }|j                  ||          |D 	cg c]	  }	|	|vs|	 }}	|D 	cg c]  }	d|	z  	 }
}	|
|D 	cg c]  }	d|	z  	 c}	z  }
|r&d	d
j                  |      z  }|
j                  d       n3dt         j                  |    j                  z  }||
j                  |       t        j                  |
|      }|t        |      k7  r|S c c}	w c c}	w c c}	w )a&  Returns a list of resources selected by the user.

  Args:
    name: the entity name for the resources being selected.
    list_func: a zero argument function that will return a list of existing
      resources.
    end_empty_message: text for the menu option that will return an empty list.
  zCould not find any z$ to select. Check that at least one z4 has been created and is properly configued for use.)r+   Nr&   r'   zAdd `%s`zCurrently selected: %sz, r(   zNo %s selected yet)
r   r   r   r   EntityNotFoundErrorr   r*   joinr   r   )r   r   end_empty_messager   r   r.   	availabler/   indexr1   r2   r+   s               r   ResourceListFromPromptr:   Z   s    +-	&&t,66I

$
$ Y(* *
 &)M"Q&+s=))S[  l{#f[1_-=&>>f	s=)	)CK'emmIe$%"/F-$4v3E-IF067}t#G7i8id
T!i88G(499V+<<gnnV$}'='=d'C'M'MMg		&()))'7CK) 	s=))* 
- G78s   6	E2 E2
E7E<r   )__doc__
__future__r   r   r   !googlecloudsdk.command_lib.apigeer   r   googlecloudsdk.core.consoler   r$   r3   r:    r   r   <module>r@      s-    0 &  ' 4 ; 2$NN2)r   