
    7                         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
      Z G d de
      Zd Zy)zClasses that manage concepts and dependencies.

For usage examples, see
googlecloudsdk/command_lib/concepts/all_concepts/base.py.
    )absolute_import)division)unicode_literals)base)dependency_managers)namesNc                   (    e Zd ZdZd Zd Zd Zd Zy)ConceptManagera  A manager that contains all concepts (v2) for a given command.

  This object is responsible for registering all concepts, creating arguments
  for the concepts, and creating a RuntimeParser which will be responsible
  for parsing the concepts.

  Attributes:
    concepts: [base.Concept], a list of concepts.
    runtime_parser: RuntimeParser, the runtime parser manager for all concepts.
  c                 .    g | _         d | _        i | _        y )N)conceptsruntime_parser_command_level_fallthroughsselfs    ;lib/googlecloudsdk/command_lib/concepts/concept_managers.py__init__zConceptManager.__init__-   s    DMD')D$    c                 :    | j                   j                  |       y)zAdd a single concept.

    This method adds a concept to the ConceptManager. It does not immediately
    have any effect on the command's argparse parser.

    Args:
      concept: base.Concept, an instantiated concept.
    N)r   append)r   concepts     r   
AddConceptzConceptManager.AddConcept2   s     	MM!r   c                     | j                   D cg c]  }|j                          }}| j                  ||       t        |      | _        |j                  | j                         yc c}w )a  Adds concept arguments and concept RuntimeParser to argparse parser.

    For each concept, the Attribute() method is called, and all resulting
    attributes and attribute groups are translated into arguments for the
    argparse parser.

    Additionally, a concept-specific RuntimeParser is created with all of the
    resulting attributes from the first step. (This will be responsible for
    parsing the concepts.) It is registered to the argparse parser, and will
    appear in the final parsed namespace under CONCEPT_ARGS.

    Args:
      parser: googlecloudsdk.calliope.parser_arguments.ArgumentInterceptor, the
        argparse parser to which to add argparse arguments.
    N)r   	Attribute_AddToArgparseRuntimeParserr   add_concepts)r   parserr   
attributess       r   AddToParserzConceptManager.AddToParser=   s\      6:]]C]''##%]JC
F+'
3D
++, Ds   A&c                 :   |D ]  }t        |t        j                        r( |j                  |j                  fi |j
                   E |j                  |j
                  j                  d      fi |j
                  }| j                  |j                  |        y)z0Recursively add an arg definition to the parser.helpN)

isinstancer   r   add_argumentarg_namekwargsadd_argument_grouppopr   r   )r   r   r   	attributegroups        r   r   zConceptManager._AddToArgparseR   s    		It~~	.I..C)2B2BC'f''	(8(8(<(<V(D <*3*:*:<e
)..6  r   N)__name__
__module____qualname____doc__r   r   r   r    r   r   r
   r
   !   s    	*
	"-*7r   r
   c                   "    e Zd ZdZd Zd Zd Zy)r   a  An object to manage parsing all concepts via their attributes.

  Once argument parsing is complete and ParseConcepts is called, each parsed
  concept is stored on this runtime parser as an attribute, named after the
  name of that concept.

  Attributes:
    parsed_args: the argparse namespace after arguments have been parsed.
    <CONCEPT_NAME> (the namespace format of each top level concept, such as
      "foo_bar"): the parsed concept corresponding to that name.
  c                     d | _         i | _        |D ]f  }t        j                  |j                  j                               }|| j                  v rt        dj                  |            || j                  |<   h y )NzDAttempting to add two concepts with the same presentation name: [{}])parsed_args_attributesr   ConvertToNamespaceNamer   GetPresentationName
ValueErrorformat)r   r   r(   	attr_names       r   r   zRuntimeParser.__init__j   sz    DD	..



/
/
13i	d&&	& 3396)3DF 	F$-dy!  r   c                 .   i }t        j                  | j                        D ]=  \  }}t        j                  j                  |      }t        || j                        ||<   ? t        j                  |      D ]  \  }}t        | j                  ||        y)a   Parse all concepts.

    Stores the result of parsing concepts, keyed to the namespace format of
    their presentation name. Afterward, will be accessible as
    args.<LOWER_SNAKE_CASE_NAME>.

    Raises:
      googlecloudsdk.command_lib.concepts.exceptions.Error: if parsing fails.
    N)
six	iteritemsr2   r   DependencyNodeFromAttribute
FinalParse
ParsedArgssetattrr1   )r   finalr7   r(   dependenciesnamevalues          r   ParseConceptszRuntimeParser.ParseConceptsu   s~     E #d.>.> ?	9(77EEiPl#L$//BeI !@ }}U+ede, ,r   c                     | j                   S )zA lazy property to use during concept parsing.

    Returns:
      googlecloudsdk.calliope.parser_extensions.Namespace: the parsed argparse
        namespace | None, if the parser hasn't been registered to the namespace
        yet.
    )r1   r   s    r   r>   zRuntimeParser.ParsedArgs   s     r   N)r*   r+   r,   r-   r   rD   r>   r.   r   r   r   r   ]   s    
	.-0r   r   c                 \    t        j                  |       } |       }|j                  |      S )a  Lazy parser stored under args.CONCEPT_ARGS.

  Args:
    dependencies: dependency_managers.DependencyNode, the root of the tree of
      the concept's dependencies.
    arg_getter: Callable, a function that returns the parsed args namespace.

  Raises:
      googlecloudsdk.command_lib.concepts.exceptions.Error: if parsing fails.

  Returns:
    the result of parsing the root concept.
  )r   DependencyManagerParseConcept)rA   
arg_getterdependency_managerr1   s       r   r=   r=      s-     +<<\J+		(	(	55r   )r-   
__future__r   r   r   #googlecloudsdk.command_lib.conceptsr   r   r   r9   objectr
   r   r=   r.   r   r   <module>rN      s@     '  ' 4 C 5 
97V 97x8F 8v6r   