
    	                       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Zddlm	Z	 dd	l
mZ 	 ej                  Z G d
 de	j                        ZdZdddZddZddZddZddZ G d dej.                        Zy# e$ r eZY Kw xY w)z-Wrapper for Cloud Run InstanceSplit messages.    )absolute_import)annotations)division)print_function)unicode_literalsN)
exceptions)run_v1_messagesc                      e Zd ZdZy)&InvalidInstanceSplitSpecificationErrorz:Error to indicate an invalid instance split specification.N)__name__
__module____qualname____doc__     0lib/googlecloudsdk/api_lib/run/instance_split.pyr   r   "   s    Br   r   LATESTc                h    |t         k(  r| j                  d|      }|S | j                  ||      }|S )a)  Creates a new InstanceSplit.

  Args:
    messages: The message module that defines InstanceSplit.
    key: The key for the instance split assignment in the InstanceSplits
      mapping.
    percent: Optional percent of instance split to assign.

  Returns:
    The newly created InstanceSplit.
  T)latestRevisionpercent)revisionNamer   )LATEST_REVISION_KEYInstanceSplit)messageskeyr   results       r   NewInstanceSplitr   -   sE     	##4#IF 
- ##g#FF	-r   c                >    | j                   rt        S | j                  S )zReturns the key for a InstanceSplit.

  Args:
    split: InstanceSplit, the InstanceSplit to check

  Returns:
    LATEST_REVISION_KEY if split is for the latest revison or
    split.revisionName if not.
  )r   r   r   )splits    r   GetKeyr    @   s     !& 4 4	L%:L:LLr   c                ,    | t         k(  rd| f}|S d| f}|S )a}  Sorted key function to order InstanceSplit keys.

  InstanceSplits keys are one of:
  o revisionName
  o LATEST_REVISION_KEY

  Note LATEST_REVISION_KEY is not a str so its ordering with respect
  to revisionName keys is hard to predict.

  Args:
    key: Key for a InstanceSplits dictionary.

  Returns:
    A value that sorts by revisionName with LATEST_REVISION_KEY
    last.
        )r   )r   r   s     r   SortKeyFromKeyr$   M   s-    " 	XF 
- XF	-r   c                .    t        |       }t        |      S )zSorted key function to order InstanceSplit objects by key.

  Args:
    split: A InstanceSplit.

  Returns:
    A value that sorts by revisionName with LATEST_REVISION_KEY
    last.
  )r    r$   )r   r   s     r   SortKeyFromSplitr&   e   s     	u#		r   c                >    | j                   r| j                   }|S d}|S )zBKey function for sorting InstanceSplit objects during __getitem__.r   )r   )r   r   s     r   _GetItemSortKeyr(   s   s$     #]]EMM'	. 12'	.r   c                  \    e Zd ZdZd Zd Zd Zd Zd Ze	d        Z
d Zd	 Zd
 Zd Zd Zy)InstanceSplitsan  Wraps a repeated InstanceSplit message and provides dict-like access.

  The dictionary key is one of
     LATEST_REVISION_KEY for the latest revision
     InstanceSplit.revisionName for InstanceSplits with a revision name.

  The dictionary value is a list of all instance split assignments referencing
  the same revision, either by name or the latest revision.
  c                V    || _         || _        | j                   j                  | _        y)a  Constructs a new InstanceSplits instance.

    The InstanceSplits instance wraps the to_wrap argument, which is a repeated
    proto message. Operations that mutate to_wrap will usually occur through
    this class, but that is not a requirement. Callers can directly mutate
    to_wrap by accessing the proto directly.

    Args:
      messages_module: The message module that defines InstanceSplit.
      to_wrap: The instance split assignments to wrap.
    N)	_messages_mr   _instance_split_cls)selfmessages_moduleto_wraps      r   __init__zInstanceSplits.__init__   s$     %DNDG#~~;;Dr   c                j    t        fd| j                  D        t              }|st              |S )a  Gets a sorted list of instance split assignments associated with the given key.

    Allows accessing instance split assignments based on the revision they
    reference
    (either directly by name or the latest ready revision by specifying
    "LATEST" as the key).

    Returns a sorted list of instance split assignments to support comparison
    operations on
    InstanceSplits objects which should be independent of the order of the
    instance split assignments for a given key.

    Args:
      key: A revision name or "LATEST" to get the instance split assignments
        for.

    Returns:
      A sorted list of instance split assignments associated with the given key.

    Raises:
      KeyError: If this object does not contain the given key.
    c              3  @   K   | ]  }t        |      k(  s|  y wNr    ).0ir   s     r   	<genexpr>z-InstanceSplits.__getitem__.<locals>.<genexpr>   s     0GqvayC/Gs   )r   )sortedr-   r(   KeyError)r/   r   r   s    ` r   __getitem__zInstanceSplits.__getitem__   s1    . 0DGG0oF SMMr   c                    t               zNot implemented for now.NotImplementedError)r/   r   s     r   __delitem__zInstanceSplits.__delitem__       

r   c                    t               r>   r?   )r/   r   
new_splitss      r   __setitem__zInstanceSplits.__setitem__   rB   r   c                F    | j                   D ]  }|t        |      k(  s y y)z(Implements evaluation of `item in self`.TF)r-   r    )r/   r   r   s      r   __contains__zInstanceSplits.__contains__   s$    	u	  r   c                :    t        d | j                  D              S )z$A set containing the mapping's keys.c              3  2   K   | ]  }t        |        y wr5   r6   )r7   r8   s     r   r9   z*InstanceSplits._key_set.<locals>.<genexpr>   s     *'Qvay's   )setr-   r/   s    r   _key_setzInstanceSplits._key_set   s     *$''***r   c                ,    t        | j                        S )z%Implements evaluation of `len(self)`.)lenrL   rK   s    r   __len__zInstanceSplits.__len__   s    t}}r   c                ,    t        | j                        S )z<Returns an iterator over the instance split assignment keys.)iterrL   rK   s    r   __iter__zInstanceSplits.__iter__   s    r   c                    | j                   S r5   )r-   rK   s    r   MakeSerializablezInstanceSplits.MakeSerializable   s    77Nr   c                X    dj                  d | j                         D              }d|z  S )Nz, c              3  F   K   | ]  \  }}d j                  ||        yw)z{}: {}N)format)r7   kvs      r   r9   z*InstanceSplits.__repr__.<locals>.<genexpr>   s      G,$!Q1-,s   !z[%s])joinitems)r/   contents     r   __repr__zInstanceSplits.__repr__   s'    iiG$**,GGGGr   c                d    | j                   j                  t        | j                  ||             y)zAdd a new instance split assignments for the given key.

    Mainly for testing.

    Args:
      key: Name of the revision (or "LATEST") to set the percent for.
      percent: Percent of instance split to set.
    N)r-   appendr   r,   )r/   r   r   s      r   AddSplitzInstanceSplits.AddSplit   s"     	GGNN#DNNCABr   N)r   r   r   r   r2   r<   rA   rE   rG   propertyrL   rO   rR   rT   r]   r`   r   r   r   r*   r*   }   sN    < <   + +	Cr   r*   r5   )r   str)r   zrun_v1_messages.InstanceSplit)r   
__future__r   r   r   r   r   collectionsgooglecloudsdk.corer   ,googlecloudsdk.generated_clients.apis.run.v1r	   abccollections_abcAttributeErrorErrorr   r   r   r    r$   r&   r(   MutableMappingr*   r   r   r   <module>rl      s    4 & "  % '  * H OO/
Z-=-=   &
M0eC_33 eC   / s   B   B
	B
