
    8#                     R    d Z ddlZddlmZ d Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zy)z7Utilities for generating and updating fallthrough maps.    N)depsc                    |D ]  }| j                  |j                  g       }|j                  |j                        x}rt        j                  |      }nd}|r|D cg c]
  }||k7  s	| }}|g|z   }	n|}	|	| |j                  <    yc c}w )a  Adds flag fallthroughs to fallthrough map.

  Iterates through each attribute and prepends a flag fallthrough.
  This allows resource attributes to be resolved to flag first. For example:

    {'book': [deps.ValueFallthrough('foo')]}

  will update to something like...

    {
        'book': [
            deps.ArgFallthrough('--foo'),
            deps.ValueFallthrough('foo')
        ]
    }

  Args:
    base_fallthroughs_map: {str: [deps._FallthroughBase]}, A map of attribute
      names to fallthroughs
    attributes: list[concepts.Attribute], list of attributes associated
      with the resource
    attribute_to_args_map: {str: str}, A map of attribute names to the names
      of their associated flags.
  N)getnamedeps_libArgFallthrough)
base_fallthroughs_map
attributesattribute_to_args_map	attributecurrent_fallthroughsarg_namearg_fallthroughffiltered_fallthroughsfallthroughss
             5lib/googlecloudsdk/calliope/concepts/deps_map_util.pyAddFlagFallthroughsr      s    4 i044Y^^RH(,,Y^^<<x< //9oo)C)Q/-A!)  C%&)>>l)l,8)..) Cs   
B	(B	c                     |D ]q  }| j                  |j                  g       }t        j                  |||j                        }||k7  r|D cg c]
  }||k7  s	| }	}|g|	z   }
n|}
|
| |j                  <   s yc c}w )a  Adds fully specified fallthroughs to fallthrough map.

  Iterates through each attribute and prepends a fully specified fallthrough.
  This allows resource attributes to resolve to the fully specified anchor
  value first. For example:

    {'book': [deps.ValueFallthrough('foo')]}

  will udpate to something like...

    {
        'book': [
            deps.FullySpecifiedAnchorFallthrough(anchor_fallthroughs),
            deps.ValueFallthrough('foo')
        ]
    }

  Args:
    base_fallthroughs_map: {str: [deps._FallthroughBase]}, A map of attribute
      names to fallthroughs
    attributes: list[concepts.Attribute], list of attributes associated
      with the resource
    anchor: concepts.Attribute, attribute that the other attributes should
      resolve to if fully specified
    collection_info: the info of the collection to parse the anchor as
    anchor_fallthroughs: list[deps._FallthroughBase], fallthroughs used to
      resolve the anchor value
  N)r   r   r   FullySpecifiedAnchorFallthrough
param_name)r	   r
   anchorcollection_infoanchor_fallthroughsr   r   anchor_based_fallthroughr   r   r   s              r   AddAnchorFallthroughsr   A   s    > i044Y^^RH'GG_i.B.B D F)L)Q2J-J!)  L./2GGl)l,8)..) Ls   
A8A8c                 L    |syt        || |      \  }}|rt        | |||       yy)aF  Shortens fallthrough list to a single deps.ValueFallthrough.

  Used to replace the attribute_name entry in a fallthrough map to a
  single ValueFallthrough. For example:

    {'book': [deps.Fallthrough(lambda: 'foo')]}

  will update to something like...

    {'book': [deps.ValueFallthrough('foo')]}

  Args:
    base_fallthroughs_map: {str: [deps._FallthroughBase]}, A map of attribute
      names to fallthroughs we are updating
    attribute_name: str, entry in fallthrough map we are updating
    parsed_args: Namespace | None, used to derive the value for ValueFallthrough
  N)_GetFallthroughAndValue_UpdateMapWithValueFallthrough)r	   attribute_nameparsed_argsattribute_valueattribute_fallthroughs        r   UpdateWithValueFallthroughr$   n   s>    & 

+B+[,:(/( "     c                     t        || |      \  }}g }|s|S |D ]%  }i | }t        ||||       |j                  |       ' |S )a"  Generates a list of fallthrough maps for each anchor value in a list.

  For each anchor value, generate a fallthrough map. For example, if user
  provides anchor values ['foo', 'bar'] and a base fallthrough like...

    {'book': [deps.ArgFallthrough('--book')]}

  will generate something like...

    [
        {'book': [deps.ValueFallthrough('foo')]},
        {'book': [deps.ValueFallthrough('bar')]}
    ]

  Args:
    base_fallthroughs_map: {str: [deps._FallthroughBase]}, A map of attribute
      names to fallthroughs we are updating
    attribute_name: str, entry in fallthrough map we are updating
    parsed_args: Namespace | None, used to derive the value for ValueFallthrough

  Returns:
    list[{str: deps._FallthroughBase}], a list of fallthrough maps for
    each parsed anchor value
  )r   r   append)r	   r    r!   attribute_valuesr#   map_listvaluenew_maps           r   CreateValueFallthroughMapListr,      sg    4 -D+[-:)) (	Oe'&'G"(=?OOG	  
 
/r%   c                 j    | j                  |g       }|D cg c]  }t        |       c}| |<   yc c}w )a  Updates fallthrough map entry to make fallthroughs plural.

  For example:

    {'book': [deps.ArgFallthrough('--foo')]}

  will update to something like...

    {'book': [deps.ArgFallthrough('--foo'), plural=True]}

  Args:
    base_fallthroughs_map: {str: [deps.Fallthrough]}, A map of attribute
      names to fallthroughs we are updating
    attribute_name: str, entry in fallthrough map we are updating
  N)r   _PluralizeFallthrough)r	   r    given_fallthroughsfallthroughs       r   PluralizeFallthroughsr1      sF      -00D ,+++ K(++' +s   0c                 >    t        j                  |       }d|_        |S )NT)copydeepcopyplural)r0   plural_fallthroughs     r   r.   r.      s     }}[1"	r%   c                 h    t        j                  ||j                  |j                        }|g| |<   y )N)active)r   ValueFallthroughhintr8   )r	   r*   r    r#   value_fallthroughs        r   r   r      s8    //  "))+ ,=*='r%   c                     |j                  | g       D ]  }	 |j                  |      }||fc S  y# t        j                  $ r Y 2w xY w)z=Derives value and fallthrough used to derives value from map.)NN)r   GetValuer   FallthroughNotFoundError)r    fallthroughs_mapr!   possible_fallthroughr*   s        r   r   r      s[    .22>2F"++K8e)** G  ,, s   0AAc                    | j                         D ]r  \  }}d}|D ]e  }|rR|j                  rF|j                  j                  }|j                  j                  }d| d| d| d| d| d}d|fc c S |j                  rd|}g  y	 y)
a  Validates fallthrough map to ensure fallthrough map is not invalid.

  Fallthrough maps are only invalid if an inactive fallthrough comes before
  an active fallthrough. It could result in an active fallthrough that can
  never be reached.

  Args:
    fallthroughs_map: {str: [deps._FallthroughBase]}, A map of attribute
      names to fallthroughs we are validating

  Returns:
    (bool, str), bool for whether fallthrough map is valid and str for
      the error message
  Nz-Invalid Fallthrough Map: Fallthrough map at [z!] contains inactive fallthrough [z] before active fallthrough [z-]. Fix the order so that active fallthrough [z-] is reachable or remove active fallthrough [z].F)TN)itemsr8   	__class____name__)r?   attrr   inactive_fallthroughr0   
active_strinactive_strmsgs           r   ValidateFallthroughMaprJ      s      -224dL#	+"4"4 **33
+55>>>tf E((4~ 6)l +)l +)l"	.
 cz* $ ! 5r%   )__doc__r3    googlecloudsdk.calliope.conceptsr   r   r   r   r$   r,   r1   r.   r   r   rJ    r%   r   <module>rN      s@    >  =(9V*9Z>&R0>	 r%   