
                         d    d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d de      Z G d d	e      Z	y
)a  Defines a registry for storing per-runtime information.

A registry is essentially a wrapper around a Python dict that stores a mapping
from (runtime, environment) to arbitrary data. Its main feature is that it
supports lookups by matching both the runtime and the environment.
    )absolute_import)division)unicode_literals)mapc                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
RegistryEntryzAn entry in the Registry.

  Attributes:
    runtime: str or re.RegexObject, the runtime to be staged
    envs: set(env.Environment), the environments to be staged
  c                      || _         || _        y Nruntimeenvs)selfr   r   s      2lib/googlecloudsdk/api_lib/app/runtime_registry.py__init__zRegistryEntry.__init__&   s    DLDI    c                 v    	 | j                   j                  |      S # t        $ r | j                   |k(  cY S w xY wr
   )r   matchAttributeError)r   r   s     r   _RuntimeMatcheszRegistryEntry._RuntimeMatches*   s:    %\\(( %\\W$$%s    88c                     || j                   v S r
   )r   )r   envs     r   _EnvMatcheszRegistryEntry._EnvMatches0   s    $))r   c                 J    | j                  |      xr | j                  |      S )a  Returns True iff the given runtime and environment match this entry.

    The runtime matches if it is an exact string match.

    The environment matches if it is an exact Enum match or if this entry has a
    "wildcard" (that is, None) for the environment.

    Args:
      runtime: str, the runtime to match
      env: env.Environment, the environment to match

    Returns:
      bool, whether the given runtime and environment match.
    )r   r   )r   r   r   s      r   MatcheszRegistryEntry.Matches3   s%     (BT-=-=c-BBr   c                     t        | j                  t        t        t	        t         | j
                                    f      S r
   )hashr   sumsortedr   r   )r   s    r   __hash__zRegistryEntry.__hash__D   s+    s6#dDII*>#?@ABBr   c                 j    | j                   |j                   k(  xr | j                  |j                  k(  S r
   r   r   others     r   __eq__zRegistryEntry.__eq__H   s'    <<5==(DTYY%**-DDr   c                 &    | j                  |       S r
   )r#   r!   s     r   __ne__zRegistryEntry.__ne__K   s    {{5!!!r   N)__name__
__module____qualname____doc__r   r   r   r   r   r#   r%    r   r   r   r      s-    %C"CE"r   r   c                       e Zd ZdZddZd Zy)Registrya  A registry to store values for various runtimes and environments.

  The registry is a map from (runtime, app-engine-environment) to
  user-specified values. As an example, storing Booleans for different
  runtimes/environments would look like:

  REGISTRY = {
    RegistryEntry('php72', {env.STANDARD}): True,
    RegistryEntry('php55', {env.STANDARD}): False,
    RegistryEntry('nodejs8', {env.FLEX}): False,
  }

  Attributes:
    mappings: dict, where keys are RegistryEntry objects and values can be
      of any type
    override: object or None; if specified, this value will always be returned
      by Get()
    default: object or None; if specified, will be returned if Get() could not
      find a matching registry entry
  Nc                 6    |xs i | _         || _        || _        y r
   )mappingsoverridedefault)r   r.   r/   r0   s       r   r   zRegistry.__init__e   s    NDMDMDLr   c                     | j                   r| j                   S | j                  j                         D ]  \  }}|j                  ||      s|c S  | j                  | j                  S y)aZ  Return the associated value for the given runtime/environment.

    Args:
      runtime: str, the runtime to get a stager for
      env: env, the environment to get a stager for

    Returns:
      object, the matching entry, or override if one was specified. If no
        match is found, will return default if specified or None otherwise.
    N)r/   r.   itemsr   r0   )r   r   r   entryvalues        r   GetzRegistry.Getj   s\     }}]]++-u	w	$ . ||\\r   )NNN)r&   r'   r(   r)   r   r5   r*   r   r   r,   r,   O   s    *
r   r,   N)
r)   
__future__r   r   r   	six.movesr   objectr   r,   r*   r   r   <module>r9      s1    '  ' ."F ."b0v 0r   