
    8                     l   S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSKJ
r
  SS	KJr   " S
 S\	R                  5      r " S S\5      r " S S\\5      r " S S\\R"                  S9r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS S jrS S jrS rg)!a  Classes to handle dependencies for concepts.

At runtime, resources can be parsed and initialized using the information given
in the Deps object. All the information given by the user in the command line is
available in the Deps object. It may also access other information (such as
information provided by the user during a prompt or properties that are changed
during runtime before the Deps object is used) when Get() is called for a given
attribute, depending on the fallthroughs.
    )absolute_import)division)unicode_literalsN)util)
exceptions)
properties)	resourcesc                       \ rS rSrSrSrg)Error%   z$Base exception type for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       ,lib/googlecloudsdk/calliope/concepts/deps.pyr   r   %   s    ,r   r   c                       \ rS rSrSrSrg)FallthroughNotFoundError)   zDRaised when an attribute value is not found by a Fallthrough object.r   Nr   r   r   r   r   r   )   s    Lr   r   c                       \ rS rSrSrSrg)AttributeNotFoundError-   z@Raised when an attribute value cannot be found by a Deps object.r   Nr   r   r   r   r   r   -   s    Hr   r   c                   n    \ rS rSrSrSS jrS r\R                  S 5       r	S r
\S 5       rS rS	 rS
rg)_FallthroughBase1   a  Represents a way to get information about a concept's attribute.

Specific implementations of Fallthrough objects must implement the method:

  _Call():
    Get a value from information given to the fallthrough.

GetValue() is used by the Deps object to attempt to find the value of an
attribute. The hint property is used to provide an informative error when an
attribute can't be found.
c                 (    Xl         X l        X0l        g)a  Initializes a fallthrough to an arbitrary function.

Args:
  hint: str | list[str], The user-facing message for the fallthrough
    when it cannot be resolved.
  active: bool, True if the fallthrough is considered to be "actively"
    specified, i.e. on the command line.
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
    resource argument is plural (i.e. parses to a list).
N_hintactiveplural)selfhintr#   r$   s       r   __init___FallthroughBase.__init__>   s     JKKr   c                 h    U R                  U5      nU(       a  U R                  U5      $ [        5       e)zGets a value from information given to the fallthrough.

Args:
  parsed_args: the argparse namespace.

Raises:
  FallthroughNotFoundError: If the attribute is not found.

Returns:
  The value of the attribute.
)_Call
_Pluralizer   )r%   parsed_argsvalues      r   GetValue_FallthroughBase.GetValueN   s-     JJ{#E__U##
"
$$r   c                     g Nr   r%   r,   s     r   r*   _FallthroughBase._Call_   s    r   c                 j    U R                   (       a  [        U[        5      (       a  U$ U(       a  U/$ / $ )zCPluralize the result of calling the fallthrough. May be overridden.r$   
isinstancelistr%   r-   s     r   r+   _FallthroughBase._Pluralizec   s)    ;;*UD11lE7##r   c                     U R                   $ )zCString representation of the fallthrough for user-facing messaging.)r"   r%   s    r   r&   _FallthroughBase.hinti   s     ::r   c                 X    [        U R                  5      [        U R                  5      -   $ r1   )hashr&   r#   r;   s    r   __hash___FallthroughBase.__hash__n   s    		?T$++...r   c                     [        XR                  5      =(       aY    UR                  U R                  :H  =(       a9    UR                  U R                  :H  =(       a    UR                  U R                  :H  $ r1   )r6   	__class__r&   r#   r$   r%   others     r   __eq___FallthroughBase.__eq__q   sU    unn- H%**		2I HLLDKK'H,1LLDKK,GIr   r!   NFF)r   r   r   r   r   r'   r.   abcabstractmethodr*   r+   propertyr&   r?   rE   r   r   r   r   r   r   1   sO    
 %" 	 	$  /Ir   r   )	metaclassc                   H   ^  \ rS rSrSrSU 4S jjrS rU 4S jrS rSr	U =r
$ )	Fallthroughv   zIA fallthrough that can get an attribute value from an arbitrary function.c                 Z   > U(       d  [        S5      e[        [        U ]  X#US9  Xl        g)a  Initializes a fallthrough to an arbitrary function.

Args:
  function: f() -> value, A no argument function that returns the value of
    the argument or None if it cannot be resolved.
  hint: str, The user-facing message for the fallthrough when it cannot be
    resolved. Should start with a lower-case letter.
  active: bool, True if the fallthrough is considered to be "actively"
    specified, i.e. on the command line.
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
    resource argument is plural (i.e. parses to a list).

Raises:
  ValueError: if no hint is provided
zHint must be provided.r#   r$   N)
ValueErrorsuperrM   r'   	_function)r%   functionr&   r#   r$   rB   s        r   r'   Fallthrough.__init__y   s-    " /00	+t%d&%INr   c                 $    AU R                  5       $ r1   rS   r2   s     r   r*   Fallthrough._Call   s    >>r   c                 j   > [         [        U ]  U5      =(       a    UR                  U R                  :H  $ r1   )rR   rM   rE   rS   )r%   rD   rB   s     r   rE   Fallthrough.__eq__   s,    +t+E2 .OOt~~-/r   c                 ,    [        U R                  5      $ r1   )r>   rS   r;   s    r   r?   Fallthrough.__hash__   s    r   rW   rG   r   r   r   r   r   r'   r*   rE   r?   r   __classcell__rB   s   @r   rM   rM   v   s    Q,/   r   rM   c                   B   ^  \ rS rSrSrSU 4S jjrS rS rS rSr	U =r
$ )	ValueFallthrough   "Gets an attribute from a property.c                 b   > Uc  SR                  U5      OUn[        [        U ]  X#US9  Xl        g)a  Initializes a fallthrough for the property associated with the attribute.

Args:
  value: str, Denoting the fixed value to provide to the attribute.
  hint: str, Optional, If provided, used over default help_text.
  active: bool, Optional, whether the value is specified by the user on
    the command line.
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
    resource argument is plural (i.e. parses to a list).
NzThe default is `{}`rP   )formatrR   ra   r'   r-   )r%   r-   r&   r#   r$   rB   s        r   r'   ValueFallthrough.__init__   s6     37, ''.DD	
D*4v*NJr   c                     AU R                   $ r1   r-   r2   s     r   r*   ValueFallthrough._Call   s    ::r   c                 j    [        XR                  5      (       d  gUR                  U R                  :H  $ NF)r6   rB   r-   rC   s     r   rE   ValueFallthrough.__eq__   s'    e^^,,;;$**$$r   c                 ,    [        U R                  5      $ r1   )r>   r-   r;   s    r   r?   ValueFallthrough.__hash__   s    

r   rh   )NFFr]   r_   s   @r   ra   ra      s    *"%
 r   ra   c                   B   ^  \ rS rSrSrSU 4S jjrS rS rS rSr	U =r
$ )	PropertyFallthrough   rc   c                 V   > SR                  U5      n[        [        U ]  X2S9  Xl        g)aY  Initializes a fallthrough for the property associated with the attribute.

Args:
  prop: googlecloudsdk.core.properties._Property, a property.
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
    resource argument is plural (i.e. parses to a list).
zset the property `{}`)r$   N)re   rR   rp   r'   rJ   )r%   propr$   r&   rB   s       r   r'   PropertyFallthrough.__init__   s-     #))$/D	
t-d-BMr   c                     A U R                   R                  5       $ ! [        R                  [        R                  4 a     g f = fr1   )rJ   	GetOrFailr   InvalidValueErrorRequiredPropertyErrorr2   s     r   r*   PropertyFallthrough._Call   s@    ]]$$&&((**J*JK s    $AAc                 j    [        XR                  5      (       d  gUR                  U R                  :H  $ rk   )r6   rB   rJ   rC   s     r   rE   PropertyFallthrough.__eq__   '    e^^,,>>T]]**r   c                 ,    [        U R                  5      $ r1   )r>   rJ   r;   s    r   r?   PropertyFallthrough.__hash__       r   )rJ   Fr]   r_   s   @r   rp   rp      s    *+
 r   rp   c                   H   ^  \ rS rSrSrS	U 4S jjrS rS rS rS r	Sr
U =r$ )
ArgFallthrough   z?Gets an attribute from the argparse parsed values for that arg.c                 V   > [         [        U ]  SR                  U5      SUS9  Xl        g)aP  Initializes a fallthrough for the argument associated with the attribute.

Args:
  arg_name: str, the name of the flag or positional.
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
    resource argument is plural (i.e. parses to a list).
z-provide the argument `{}` on the command lineTrP   N)rR   r   r'   re   arg_name)r%   r   r$   rB   s      r   r'   ArgFallthrough.__init__   s4     
.$(7>>xH )  Mr   c                 \    [        U[        R                  " U R                  5      S 5      nU$ r1   )getattrr   NamespaceFormatr   )r%   r,   	arg_values      r   r*   ArgFallthrough._Call   s%    T%9%9$--%H$OIr   c                     U R                   (       d%  [        U[        5      (       a  U(       a  US   $ S $ U$ U(       a  [        U[        5      (       d  U/$ U(       a  U$ / $ )Nr   r5   r8   s     r   r+   ArgFallthrough._Pluralize   sQ    ;; 
E4	 	  uQx*d*lZt,,Wn5!r!r   c                 j    [        XR                  5      (       d  gUR                  U R                  :H  $ rk   )r6   rB   r   rC   s     r   rE   ArgFallthrough.__eq__   r|   r   c                 ,    [        U R                  5      $ r1   )r>   r   r;   s    r   r?   ArgFallthrough.__hash__  r   r   )r   r   )r   r   r   r   r   r'   r*   r+   rE   r?   r   r^   r_   s   @r   r   r      s$    G	"+
 r   r   c                   J   ^  \ rS rSrSr S	U 4S jjrS rS rS rS r	Sr
U =r$ )
FullySpecifiedAnchorFallthroughi  zAA fallthrough that gets a parameter from the value of the anchor.c                   > U(       a  SnOSnU Vs/ s H  ofR                    SU 3PM     nn[        S U 5       5      n[        [        U ]  XxUS9  X0l        X l        [        U5      U l        [        R                  R                  5       U l        U R                  R                  U R                  R                  U R                  R                  5        gs  snf )a  Initializes a fallthrough getting a parameter from the anchor.

For anchor arguments which can be plural, returns the list.

Args:
  fallthroughs: list[_FallthroughBase], any fallthrough for an anchor arg.
  collection_info: the info of the collection to parse the anchor as.
  parameter_name: str, the name of the parameter
  plural: bool, whether the expected result should be a list. Should be
    False for everything except the "anchor" arguments in a case where a
zwith fully specified nameszwith a fully specified name c              3   8   #    U  H  oR                   v   M     g 7fr1   )r#   ).0fs     r   	<genexpr>;FullySpecifiedAnchorFallthrough.__init__.<locals>.<genexpr>   s     0<a<s   rP   N)r&   allrR   r   r'   parameter_namecollection_infotuple_fallthroughsr	   REGISTRYClone
_resourcesRegisterApiByNameapi_nameapi_version)
r%   fallthroughsr   r   r$   hint_suffixr   r&   r#   rB   s
            r   r'   (FullySpecifiedAnchorFallthrough.__init__
  s      0k1k/;<|!vvha}%|D<0<00F	
)49F : ,(*|,D((..0DOOO%%d&:&:&C&C&*&:&:&F&FH =s   Cc                      U R                   R                  XR                  R                  U R                  R                  S9n[        X R                  S5      $ ! [
        R                   a     g[         a     gf = f)z<Returns the parameter value from the parsed anchor resource.)
collectionr   N)
r   Parser   	full_namer   r	   r   AttributeErrorr   r   )r%   anchor_valueresource_refs      r   _GetFromAnchor.FullySpecifiedAnchorFallthrough._GetFromAnchor*  sx    
__**
#7#7#A#A**66 + 8l <!4!4d;; ??   s   AA A<0	A<;A<c                      [        U R                  XR                  S9nU R	                  U5      $ ! [         a     g f = f)Nattribute_name)GetFromFallthroughsr   r   r   r   )r%   r,   r   s      r   r*   %FullySpecifiedAnchorFallthrough._Call9  sJ    (


k:M:MOl |,, " s   1 
>>c                     [        XR                  5      =(       aY    UR                  U R                  :H  =(       a9    UR                  U R                  :H  =(       a    UR                  U R                  :H  $ r1   )r6   rB   r   r   r   rC   s     r   rE   &FullySpecifiedAnchorFallthrough.__eq__A  s`    unn- 84#5#558!!T%9%998   D$7$779r   c           	          [        [        [        U R                  [	        U R
                  5      U R                  /5      5      $ r1   )summapr>   r   strr   r   r;   s    r   r?   (FullySpecifiedAnchorFallthrough.__hash__G  s>    D$$%
 	 r   )r   r   r   r   r   )r   r   r   r   r   r'   r   r*   rE   r?   r   r^   r_   s   @r   r   r     s+    I 	H@<-9 r   r   c                 :    UR                  U / 5      n[        X2U S9$ )a  Gets the value of an attribute based on fallthrough information.

  If the attribute value is not provided by any of the fallthroughs, an
  error is raised with a list of ways to provide information about the
  attribute.

Args:
  attribute_name: str, the name of the attribute.
  attribute_to_fallthroughs_map: {str: [_FallthroughBase], a map of attribute
    names to lists of fallthroughs.
  parsed_args: a parsed argparse namespace.

Returns:
  the value of the attribute.

Raises:
  AttributeNotFoundError: if no value can be found.
r   )getr   )r   attribute_to_fallthroughs_mapr,   r   s       r   Getr   P  s*    & /22>2F,	
@ @r   c                 L   U  H  n UR                  U5      s  $    [        U 5      nSR                  U Vs/ s H  nSR	                  U5      PM     sn5      n[        SR	                  Uc  SU5      5      eSR	                  U5      U5      5      e! [         a     M  f = fs  snf )a  Gets the value of an attribute based on fallthrough information.

  If the attribute value is not provided by any of the fallthroughs, an
  error is raised with a list of ways to provide information about the
  attribute.

Args:
  fallthroughs: [_FallthroughBase], list of fallthroughs.
  parsed_args: a parsed argparse namespace.
  attribute_name: str, the name of the attribute. Used for error message,
    omitted if not provided.

Returns:
  the value of the attribute.

Raises:
  AttributeNotFoundError: if no value can be found.

z- {}zOFailed to find attribute{}. The attribute can be set in the following ways: 
{} z [{}])r.   r   GetHintsjoinre   r   )r   r,   r   fallthroughhintsr&   fallthroughs_summarys          r   r   r   h  s    & "k!!+.. " <
 %',-utv}}Tu-/#V&"
 	! ! -4NN>,J
 	! ! $  .s   BB!
BBc                     0 nU  HO  n[        UR                  [        5      (       a  UR                  OUR                  /nU H  nXA;   a  M
  SX'   M     MQ     [        UR                  5       5      $ )z0Gathers deduped hints from list of fallthroughs.T)r6   r&   r7   keys)r   	hints_setr   	new_hintsr&   s        r   r   r     sb     )a$QVVT22I		io   
inn	r   r1   )r   
__future__r   r   r   rH    googlecloudsdk.calliope.conceptsr   googlecloudsdk.corer   r   r	   r   r   r   r   objectABCMetar   rM   ra   rp   r   r   r   r   r   r   r   r   <module>r      s    '  ' 
 1 * * )-J -Mu MIUN IBIv BIJ" " " J' B* B'% 'TF&6 FR@0!!H r   