
                             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dlZ G d	 d
e
j                        Z G d de      Z G d de      Z G d de      Zy)z2Classes for runtime handling of concept arguments.    )absolute_import)division)unicode_literals)parser_errors)util)
exceptionsNc                       e Zd ZdZy)Errorz%Base class for errors in this module.N)__name__
__module____qualname____doc__     0lib/googlecloudsdk/calliope/concepts/handlers.pyr
   r
      s    -r   r
   c                   "     e Zd ZdZ fdZ xZS )
ParseErrorz#Raised if a concept fails to parse.c                 P    dj                  ||      }t        t        |   |       y )NzError parsing [{}].
{})formatsuperr   __init__)selfpresentation_namemessagemsg	__class__s       r   r   zParseError.__init__"   s%    
#
*
*+<g
FC	*d$S)r   r   r   r   r   r   __classcell__r   s   @r   r   r      s    +* *r   r   c                   "     e Zd ZdZ fdZ xZS )RepeatedConceptNamezGRaised when adding a concept if one with the given name already exists.c                 N    dj                  |      }t        t        |   |       y )NzRepeated concept name [{}].)r   r   r!   r   )r   concept_namer   r   s      r   r   zRepeatedConceptName.__init__*   s#    
'
.
.|
<C	
t-c2r   r   r   s   @r   r!   r!   '   s    O3 3r   r!   c                   6    e Zd ZdZd Zd Zd	dZd Zd Zd Z	y)
RuntimeHandlerzA handler to hold information about all concept arguments in a command.

  The handler is assigned to 'CONCEPTS' in the argparse namespace and has an
  attribute to match the name of each concept argument in lower snake case.
  c                 .    d | _         i | _        g | _        y N)parsed_args_arg_name_lookup_all_conceptsr   s    r   r   zRuntimeHandler.__init__6   s    DDDr   c                     | j                   S )z=Basically a lazy property to use during lazy concept parsing.)r(   r+   s    r   
ParsedArgszRuntimeHandler.ParsedArgs<   s    r   c                    ddl m  G fddt              }t        |       rt	              t        |  ||j                  | j                               | j                  j                  |d       t        j                  |j                        D ]'  \  }}|| j                  t        j                  |      <   ) y)a  Adds a concept handler for a given concept.

    Args:
      name: str, the name to be used for the presentation spec.
      concept_info: ConceptInfo, the object that holds dependencies of the
        concept.
      required: bool, True if the concept must be parseable, False if not.

    Raises:
      RepeatedConceptName: If the given "name" has already been used with a
        concept.
    r   )conceptsc                   &    e Zd ZdZd Z fdZy),RuntimeHandler.AddConcept.<locals>.LazyParsezBClass provided when accessing a concept to lazily parse from args.c                      || _         || _        y r'   )parse
arg_getter)r   r3   r4   s      r   r   z5RuntimeHandler.AddConcept.<locals>.LazyParse.__init__T   s    
$r   c                     	 | j                  | j                               S # j                  $ r+}rt        t	        j
                  |            Y d }~y d }~ww xY wr'   )r3   r4   InitializationErrorr   six	text_type)r   er/   namerequireds     r   Parsez2RuntimeHandler.AddConcept.<locals>.LazyParse.ParseX   sP    	DOO-.
.++ 	T3==#344	s   " A!AAN)r   r   r   r   r   r<   )r/   r:   r;   s   r   	LazyParser1   Q   s    N%r   r=   )r:   concept_infor;   N) googlecloudsdk.calliope.conceptsr/   objecthasattrr!   setattrr<   r-   r*   appendr7   	iteritemsattribute_to_args_mapr)   r   NormalizeFormat)r   r:   r>   r;   r=   _arg_namer/   s    ` `   @r   
AddConceptzRuntimeHandler.AddConcept@   s     : F  tT%%D$	,"4"4dooFG$ 
 }}\%G%GH8>JdD00:; Ir   c                 ^    | j                   j                  t        j                  |            S r'   )r)   getr   rF   )r   rH   s     r   ArgNameToConceptInfoz#RuntimeHandler.ArgNameToConceptInfok   s$      $$T%9%9(%CDDr   c                 L    | j                   D ]  }|d   j                           y )Nr>   )r*   
ClearCache)r   concept_detailss     r   ResetzRuntimeHandler.Resetn   s#    --n%002 .r   c                 ~    	 t        | |      S # t        $ r% t        j                  dj	                  |            w xY w)a7  Returns the value of the argument registered for dest.

    Based on argparse.Namespace.GetValue().

    Args:
      dest: The dest of a registered argument.

    Raises:
      UnknownDestinationException: If no arg is registered for dest.

    Returns:
      The value of the argument registered for dest.
    z/No registered concept arg for destination [{}].)getattrAttributeErrorr   UnknownDestinationExceptionr   )r   dests     r   GetValuezRuntimeHandler.GetValuer   sI    JT4   J55
;
B
B4
HJ JJs    .<N)T)
r   r   r   r   r   r-   rI   rL   rP   rV   r   r   r   r%   r%   /   s)    )KVE3Jr   r%   )r   
__future__r   r   r   googlecloudsdk.callioper   r?   r   googlecloudsdk.corer   r7   r
   r   r!   r@   r%   r   r   r   <module>rZ      sY    9 &  ' 1 1 * 
.J .* *3% 3UJV UJr   