
    z                     ~    d 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	 ddl
Z
 G d d	e	j                        Zy)
z4Backend rewrite tool for Cloud Datastore operations.    )absolute_import)division)unicode_literalsN)	constants)resource_expr_rewritec                   |     e Zd ZdZddddddddZddiZdej                  d	iiZ fd
Z	d fd	Z
d Zd Zd Z xZS )OperationsRewriteBackendz<Rewrites for Cloud Datastore server side filter expressions.zmetadata.common.labels.\1z#metadata.entity_filter.namespace_idzmetadata.common.operation_typezmetadata.entity_filter.kind)z^label\.(.*)z^labels\.(.*)z^namespace$z^namespaceId$z^type$z^operationType$z^kind$:= c                     | j                  |      }| j                  |      }| j                  ||      }t        t        |   ||||      S )a  Rewrites a <key op operand> term of a filter expression.

    Args:
      key: The key, a string.
      op: The op, a string.
      operand: The operand, a string or list of strings.
      key_type: The key type, unknown if None.

    Returns:
      the new term, as a string.
    )_RewriteKey
_RewriteOp_RewriteOperandsuperr	   RewriteTerm)selfkeyopoperandkey_type	__class__s        7lib/googlecloudsdk/api_lib/datastore/rewrite_backend.pyr   z$OperationsRewriteBackend.RewriteTerm4   sS     

3
C		B""30G)4<R($ $    c                 <    |xs | }t         t        |   ||      S )a  Returns value or value "..." quoted with C-style escapes if needed.

    Defers to BackendBase.Quote for everything but the empty string, which it
    force quotes.

    Args:
      value: The string value to quote if needed.
      always: Always quote non-numeric value if True.

    Returns:
      A string: value or value "..." quoted with C-style escapes if needed or
      requested.
    )always)r   r	   Quote)r   valuer   r   s      r   r   zOperationsRewriteBackend.QuoteF   s)       5yF)46uV6LLr   c                     t        |t              r |D cg c]  }| j                  ||       c}S | j                  j	                  |i       j	                  ||      S c c}w N)
isinstancelistr   _KEY_OPERAND_MAPPINGget)r   r   r   operand_items       r   r   z(OperationsRewriteBackend._RewriteOperandY   sb    '4 FMFMl$

sL
1g  $$((b155gwGGs   Ac                     t        j                  | j                        D ]4  \  }}t        j                  ||      st        j
                  |||      c S  |S r    )six	iteritems_KEY_MAPPINGrematchsub)r   r   regexreplacements       r   r   z$OperationsRewriteBackend._RewriteKey`   sH    !mmD,=,=>{	%	vve[#.. ? Jr   c                 :    | j                   j                  ||      S r    )_OPERATOR_MAPPINGr$   )r   r   s     r   r   z#OperationsRewriteBackend._RewriteOpf   s    !!%%b"--r   )F)__name__
__module____qualname____doc__r)   r0   r   DEFAULT_NAMESPACEr#   r   r   r   r   r   __classcell__)r   s   @r   r	   r	      sk    D 44:<09-, 
3 ,

%
%r.$$M&H.r   r	   )r4   
__future__r   r   r   r*    googlecloudsdk.api_lib.datastorer   googlecloudsdk.core.resourcer   r'   Backendr	    r   r   <module>r<      s4    ; &  ' 	 6 > 
K.4<< K.r   