o
    €Ïñ  ã                   @   sš   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
„ d
e
jƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z2Classes for runtime handling of concept arguments.é    )Úabsolute_import)Údivision)Úunicode_literals)Úparser_errors)Úutil)Ú
exceptionsNc                   @   s   e Zd ZdZdS )ÚErrorz%Base class for errors in this module.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úF/tmp/google-cloud-sdk/lib/googlecloudsdk/calliope/concepts/handlers.pyr      s    r   c                       ó    e Zd ZdZ‡ fdd„Z‡  ZS )Ú
ParseErrorz#Raised if a concept fails to parse.c                    s    d  ||¡}tt| ƒ |¡ d S )NzError parsing [{}].
{})ÚformatÚsuperr   Ú__init__)ÚselfÚpresentation_nameÚmessageÚmsg©Ú	__class__r   r   r   "   s   zParseError.__init__©r	   r
   r   r   r   Ú__classcell__r   r   r   r   r      ó    r   c                       r   )ÚRepeatedConceptNamezGRaised when adding a concept if one with the given name already exists.c                    s   d  |¡}tt| ƒ |¡ d S )NzRepeated concept name [{}].)r   r   r   r   )r   Úconcept_namer   r   r   r   r   *   s   
zRepeatedConceptName.__init__r   r   r   r   r   r   '   r   r   c                   @   sB   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ Zdd„ Zdd„ Z	dS )ÚRuntimeHandlerzäA 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                 C   s   d | _ i | _g | _d S ©N)Úparsed_argsÚ_arg_name_lookupÚ_all_concepts©r   r   r   r   r   6   s   
zRuntimeHandler.__init__c                 C   s   | j S )z=Basically a lazy property to use during lazy concept parsing.)r!   r$   r   r   r   Ú
ParsedArgs<   s   zRuntimeHandler.ParsedArgsTc                    sŠ   ddl m‰  G ‡ ‡‡fdd„dtƒ}t| ˆƒrtˆƒ‚t| ˆ||j| jƒƒ | j 	ˆ|ˆdœ¡ t
 |j¡D ]\}}|| jt |¡< q6dS )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                       s(   e Zd ZdZdd„ Z‡ ‡‡fdd„ZdS )z,RuntimeHandler.AddConcept.<locals>.LazyParsezBClass provided when accessing a concept to lazily parse from args.c                 S   s   || _ || _d S r    )ÚparseÚ
arg_getter)r   r'   r(   r   r   r   r   T   s   
z5RuntimeHandler.AddConcept.<locals>.LazyParse.__init__c              
      sN   z|   |  ¡ ¡W S  ˆ jy& } zˆrtˆt |¡ƒ‚W Y d }~d S d }~ww r    )r'   r(   ÚInitializationErrorr   ÚsixÚ	text_type)r   Úe©r&   ÚnameÚrequiredr   r   ÚParseX   s   €ýz2RuntimeHandler.AddConcept.<locals>.LazyParse.ParseN)r	   r
   r   r   r   r0   r   r-   r   r   Ú	LazyParseQ   s    r1   )r.   Úconcept_infor/   N)Ú googlecloudsdk.calliope.conceptsr&   ÚobjectÚhasattrr   Úsetattrr0   r%   r#   Úappendr*   Ú	iteritemsÚattribute_to_args_mapr"   r   ÚNormalizeFormat)r   r.   r2   r/   r1   Ú_Úarg_namer   r-   r   Ú
AddConcept@   s   
ýÿzRuntimeHandler.AddConceptc                 C   s   | j  t |¡¡S r    )r"   Úgetr   r:   )r   r<   r   r   r   ÚArgNameToConceptInfok   s   z#RuntimeHandler.ArgNameToConceptInfoc                 C   s   | j D ]}|d  ¡  qd S )Nr2   )r#   Ú
ClearCache)r   Úconcept_detailsr   r   r   ÚResetn   s   
ÿzRuntimeHandler.Resetc                 C   s,   zt | |ƒW S  ty   t d |¡¡‚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   Údestr   r   r   ÚGetValuer   s   ÿÿzRuntimeHandler.GetValueN)T)
r	   r
   r   r   r   r%   r=   r?   rB   rG   r   r   r   r   r   /   s    
+r   )r   Ú
__future__r   r   r   Úgooglecloudsdk.callioper   r3   r   Úgooglecloudsdk.corer   r*   r   r   r   r4   r   r   r   r   r   Ú<module>   s   