
    9                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  \R                  " S5      rS rSS	 jrS
 rS rS rS rS rS rS rSS jrS rS rS rg)zResource property Get.    )absolute_import)division)unicode_literalsN)rangez9((?<=[a-z0-9])[A-Z]+(?=[A-Z][a-z]|$)|(?!^)[A-Z](?=[a-z]))c                      U  H  nUR                  U5      U:X  d  M  Us  $    g! [        [        [        [        4 a     gf = f)a^  Gets the dict in items that contains key==value.

A metadict object is a list of dicts of the form:
  [
    {key: value-1, ...},
    {key: value-2, ...},
    ...
  ]

Args:
  items: A list of dicts.
  key: The dict key name.
  value: The dict key value.

Returns:
  The dict in items that contains key==value or None if no match or not a
  metadict.
N)getAttributeError
IndexError	TypeError
ValueError)itemskeyvalueitems       5lib/googlecloudsdk/core/resource/resource_property.py_GetMetaDictr   !   sK    &		#%	 
 
 *i	< 			s   % % % AAc                     [        U SU5      nUc  gUR                  SS5      nU(       a   [        R                  " U5      $ U$ ! [        [
        4 a     U$ f = f)a/  Gets the metadata value for the item in items with key == name.

A metadata object is a list of dicts of the form:
  [
    {'key': key-name-1, 'value': field-1-value-string},
    {'key': key-name-2, 'value': field-2-value-string},
    ...
  ]

Examples:
  x.metadata[windows-keys].email
    Deserializes the 'windows-keys' metadata value and gets the email value.
  x.metadata[windows-keys]
    Gets the 'windows-key' metadata string value.
  x.metadata[windows-keys][]
    Gets the deserialized 'windows-key' metadata value.

Args:
  items: The metadata items list.
  name: The metadata name (which must match one of the 'key' values).
  deserialize: If True then attempt to deserialize a compact JSON string.

Returns:
  The metadata value for name or None if not found or if items is not a
  metadata dict list.
r   Nr   )r   r   jsonloadsr   r   )r   namedeserializer   r   s        r   _GetMetaDataValuer   =   sf    6 
eUD	)$	\
((7D
!%ZZ 
, z" 
	,s   A AAc                 f    U R                  S5      nUS   SR                  S USS  5       5      -   $ )z&Converts snake_case name to camelCase._r    c              3   @   #    U  H  oR                  5       v   M     g 7fN)title).0xs     r   	<genexpr>%ConvertToCamelCase.<locals>.<genexpr>g   s     7h7799hs      N)splitjoin)r   parts     r   ConvertToCamelCaser'   d   s3    	C$	a2777d12h77	77    c                 J    [         R                  SU 5      R                  5       $ )z&Converts camelCase name to snake_case._\1)	_SNAKE_REsublowerr   s    r   ConvertToSnakeCaser/   j       	vt	$	*	*	,,r(   c                 J    [         R                  SU 5      R                  5       $ )z,Converts camelCase name to ANGRY_SNAKE_CASE.r*   )r+   r,   upperr.   s    r   ConvertToAngrySnakeCaser3   o   r0   r(   c                     U" U 5      (       a  U $ [        U [        R                  5      (       d  g[        [        4 H  nU" U 5      nU" U5      (       d  M  Us  $    g)z6Returns index converted to a case that satisfies func.N
isinstancesixstring_typesr'   r/   )indexfuncconvertr   s       r   GetMatchingIndexr<   t   sR    	%[[L	E3++	,	,$&89g5>DDzzk : 
r(   c                     U" U 5      nU(       a  U$ [        U [        R                  5      (       d  g[        [        4 H  nU" U" U 5      5      nU(       d  M  Us  $    g)z?Returns the first non-None func value for case-converted index.Nr5   )r9   r:   r   r;   s       r   GetMatchingIndexValuer>      sV    
u+%
L	E3++	,	,$&89g Eul : 
r(   c                    / nU  H  n[        U[        R                  5      (       d  M$  S [        [        4 H'  nU" U5      n UR                  U5      R                  n  O   [        SR                  U5      5      eUR                  U5        M     U[        R                  :X  a  [        nX4$ ! [        [        4 a     M  f = f)aY  Returns the messages module type for key in message and the actual key.

Handles camelCase/snake_case key name variants for OnePlatform compatibility.
Indices and slices in resource_key are ignored -- they are not needed for
repeated field queries.

Args:
  resource_key: Ordered list of key names/indices, applied left to right. Each
    element in the list may be one of:
      str - A resource property name. This could be a class attribute name or
        a dict index.
      int - A list index. Selects one member is the list. Negative indices
        count from the end of the list, starting with -1 for the last element
        in the list. An out of bounds index is not an error; it produces the
        value None.
      None - A list slice. Selects all members of a list or dict like object.
        A slice of an empty dict or list is an empty dict or list.
  message: The known proto message type if not None.

Raises:
  KeyError: If key is not in message.

Returns:
  (type, actual_key), the messages module type for key in message and the
    actual key (names in the proper case, indices omitted).
c                     U $ r    r    s    r   <lambda>%GetMessageFieldType.<locals>.<lambda>   s    ar(   zField {} not in message.)r6   r7   r8   r'   r/   field_by_nametyper	   KeyErrorformatappendinteger_typesint)resource_keymessage
actual_keyr   r;   actual_names         r   GetMessageFieldTyperP      s    6 *ddC,,--!35GHDMk''499 	 I /66t<==k"  !!! G		 h' s   B..CCc           
      L   S [         [        4 H  nU  Vs/ s H,  n[        U[        R                  5      (       a  U" U5      OUPM.     nnSR                  U Vs/ s H&  n[        U[        R                  5      (       d  M$  UPM(     sn5      nXQ;   d  M  Us  $    gs  snf s  snf )aF  Returns the actual_key match of resource_key in fields.

Handles camelCase/snake_case key name variants for OnePlatform compatibility.
Indices and slices in resource_key are ignored to normalize the lookup. This
means that the lookup can determine the existence of an attribute name, but
not a specific value among all repeated values.

Args:
  resource_key: Ordered list of key names/indices, applied left to right. Each
    element in the list may be one of:
      str - A resource property name. This could be a class attribute name or
        a dict index.
      int - A list index. Selects one member is the list. Negative indices
        count from the end of the list, starting with -1 for the last element
        in the list. An out of bounds index is not an error; it produces the
        value None.
      None - A list slice. Selects all members of a list or dict like object.
        A slice of an empty dict or list is an empty dict or list.
  fields: The set of dotted field names to match against.

Returns:
  The actual_key match of resource_key in fields or None if no match.
c                     U $ r   rA   rB   s    r   rC   LookupField.<locals>.<lambda>   s    Ar(   .N)r'   r/   r6   r7   r8   r%   )rL   fieldsr;   r   rN   
lookup_keys         r   LookupFieldrW      s    0 13EFg*,*d $.dC4D4D#E#E'$-4O*  ,J BJD(s/?/?@  J B CJ G 
,Bs   3B#B!
B!
c                 n  ^
^ [        U5      m
U mT
(       Ga  T
R                  S5      n[        T[        5      (       a  [	        T5      mTc  U$ [        TS5      (       ah  Uc+  T
(       a"  T Vs/ s H  n[        TU/T
-   U5      PM     sn$ T$ [        UU4S j5      nU(       a  TU   mM  ST;   a  U
U4S jn[        X65      mM  U$ [        U[        R                  5      (       a8  [        UU4S j5      nU(       a!  [        TXR5      n[        U5      (       d  UmGM  [        TS5      (       d   [        T[        R                  5      (       Ga  Uc=  T
(       a4  [        [        T5      5       Vs/ s H  n[        TU/T
-   U5      PM     sn$ T$ [        U[        R                  5      (       Gd   [        U[        R                  5      (       a  [        T[         5      (       a  [        T5      (       a  [        TS   [         5      (       a  T
(       a*  [#        TUT
S   5      nUb  UmT
R                  S5      nGM+  O[%        TU5      nUb  U$ UTS   ;   a4  [        [        T5      5       Vs/ s H  n[        TXC/T
-   U5      PM     sn$ T Vs/ s H  oR'                  U5      PM     sn V	s/ s H  o(       d  M  U	PM     sn	=(       d    U$ U$ U[        [        T5      * [        T5      5      ;   a  TU   mGM  U$ [        T[        5      (       a  [	        T5      mT$ s  snf s  snf s  snf s  snf s  sn	f )a  Gets the value referenced by key in the object resource.

Since it is common for resource instances to be sparse it is not an error if
a key is not present in a particular resource instance, or if an index does
not match the resource type.

Args:
  resource_obj: The resource object possibly containing a value for key.
  resource_key: Ordered list of key names/indices, applied left to right. Each
    element in the list may be one of:
      str - A resource property name. This could be a class attribute name or
        a dict index.
      int - A list index. Selects one member is the list. Negative indices
        count from the end of the list, starting with -1 for the last element
        in the list. An out of bounds index is not an error; it produces the
        value None.
      None - A list slice. Selects all members of a list or dict like object.
        A slice of an empty dict or list is an empty dict or list.
  default: Get() returns this value if key is not in resource.

Returns:
  The value, None if any of the given keys are not found. This is
    intentionally not an error. In this context a value can be any data
    object: dict, list, tuple, class, str, int, float, ...
r   r   c                    > U T;   $ r   rA   r    resources    r   rC   Get.<locals>.<lambda>  s	    qH}r(   c                 2   > [        TS   U [        T5      S9$ )Nr   )r   )r   bool)r9   r   r[   s    r   	_GetValueGet.<locals>._GetValue$  s     "wDI? ?r(   c                    > [        TU 5      $ r   )hasattrrZ   s    r   rC   r\   0  s    wx/Cr(   __iter__)listpopr6   setsortedrb   Getr<   r>   r7   r8   getattrcallabler   lenrJ   dictr   r   r   )resource_objrL   defaultr9   kr   r_   rdfr   r[   s             @@r   rh   rh      s   4 	\#( GGAJE (C  !h nx!!	=EFX#hc	73XF
Fe%<=d	D>	H		?
 ):n%))**e%CDd	Hd,{{(
x$$
8S=M=M(N(N	 !X/1/a hc	73/1 1 s0011eS--..:h3M3M]]z(1+t<<xA7a
	  $He4a#  %S]353! (QJ$4g>35 5 8@ @x!ux @F @1AQ @F   	%XH6	6E? N #hH	/y GN165 !AFs$   -L/L#,L(L--
L2;L2c                     [        XS5      SL$ )z*True if resource contains key, else False.N)rh   )r[   r   s     r   ResourceContainsKeyrt   t  s    	XD	!	--r(   c                 $   U (       d  g[        U [        R                  5      (       a   [        UR	                  U 5      5      $ [        U [        [        45      (       a$   [        UR	                  [        U 5      5      5      $  [        R                  " U 5       H0  u  p4UR                  S5      (       a  M  [        XAU5      (       d  M0    g    [        R                  " U R                  5       H0  u  p4UR                  S5      (       a  M  [        XAU5      (       d  M0    g   g! [
         a     Nf = f! [
         a     Nf = f! [         a2     U  H  n[        XAU5      (       d  M     g    g! [
         a      Nf = ff = f! [         a     gf = f)aS  Returns True if any attribute value in resource matches the RE pattern.

This function is called to evaluate a global restriction on a resource. For
example, --filter="Foo.Bar" results in a call like this on each resource item:

  resource_property.EvaluateGlobalRestriction(
    resource,
    'Foo.Bar',
    re.compile(re.escape('Foo.Bar'), re.IGNORECASE),
  )

Args:
  resource: The object to check.
  restriction: The global restriction string.
  pattern: The global restriction pattern for matcing resource values.

Returns:
  True if any attribute value in resource matches the RE pattern.
Fr   T)r6   r7   r8   r^   searchr   floatrK   str	iteritems
startswithEvaluateGlobalRestrictionr	   __dict__)r[   restrictionpatternr   r   s        r   r{   r{   y  sp   ( 
#**++'..*++ 5#,'''..X/00mmH-
^^C  %>
g&' &' .	mmH$5$56
^^C  %>
g&' &' 7 
7  

  
 
 %$UAA   
 
 			s   D# "D3 /E 6E 	E E 9F F F  F #
D0/D03
E ?E 
E?E-&E-)E--
E;7E?:E;;E?
FFc                     [        U [        5      =(       d2    [        U S5      =(       a    [        U S5      =(       d    [        U S5      $ )zChecks if resource is a list-like iterable object.

Args:
  resource: The object to check.

Returns:
  True if resource is a list-like iterable object.
rc   next__next__)r6   rd   rb   )r[   s    r   
IsListLiker     s@     Xt
$ I8Z( HHf%F:)FJr(   )Fr   )__doc__
__future__r   r   r   r   rer7   	six.movesr   compiler+   r   r   r'   r/   r3   r<   r>   rP   rW   rh   rt   r{   r   rA   r(   r   <module>r      s{      &  '  	 
  JJ?A	8$N8-
-

/dDNb.
4nJr(   