
                         @   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl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
mZ  G d dej                         Z G d de	j$                        Z G d de      Zej(                  eej(                  dZddZddZd Zy)zUnified diff resource printer.    )absolute_import)division)unicode_literalsN)
exceptions)resource_printer_base)resource_projection_spec)resource_projector)resource_transform)yaml_printerc                   6     e Zd ZdZ fdZd ZddZd Z xZS )ACMDiffPrintera?  A printer for an ndiff of the first two projection columns.

  A unified diff of the first two projection columns.

  Printer attributes:
    format: The format of the diffed resources. Each resource is converted
      to this format and the diff of the converted resources is displayed.
      The default is 'yaml'.
  c                 t    t        t        | 
  |ddd| | j                  j	                  dd      | _        y )NT)
by_columnsnon_empty_projection_requiredformatyaml)superr   __init__
attributesget_print_format)selfargskwargs	__class__s      >lib/googlecloudsdk/api_lib/accesscontextmanager/acm_printer.pyr   zACMDiffPrinter.__init__,   s<    	.$(	$dNFLN,,Xv>D    c                    t        j                         }| j                  | j                  |      }|j	                  |       t        j                         }| j                  | j                  |      }|j	                  |       d}d}|.| j                  |j                         j                  d            }|.| j                  |j                         j                  d            }t        j                  ||      }t        j                  d      }	t        j                  d      }
|D ]=  }|s|d   dk7  s|	j                  |      r!|
j                  |      r3t        |       ? y)	a_  Prints a modified ndiff of formatter output for old and new.

    IngressPolicies:
     ingressFrom:
       sources:
         accessLevel: accessPolicies/123456789/accessLevels/my_level
        -resource: projects/123456789012
        +resource: projects/234567890123
    EgressPolicies:
      +egressTo:
        +operations:
          +actions:
            +action: method_for_all
            +actionType: METHOD
          +serviceName: chemisttest.googleapis.com
        +resources:
          +projects/345678901234
    Args:
      old: The old original resource.
      new: The new changed resource.
    )out N
z^\s*$z^(\+|-)\s+\{\}$r   ?)ioStringIOPrinterr   PrintSingleRecord(_FormatYamlPrinterLinesForDryRunDescribegetvaluesplitdifflibndiffrecompilematchprint)r   oldnewbuf_oldprinterbuf_new	lines_old	lines_new
lines_diffempty_line_patternempty_config_patternlines               r   _DiffzACMDiffPrinter._Diff1   s8   . kkmGll4--7l;Gc"kkmGll4--7l;Gc"II
??




"
"4
(*i
??




"
"4
(*i y)4JH-::&89 
$q'S.);)A)A
*,2248d r   c                     | j                   j                  d      }|r"| j                  j                  |       d| _        t        |      dkD  r| j                  |d   |d          yy)zImmediately prints the first two columns of record as a unified diff.

    Records with less than 2 columns are silently ignored.

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    titleN   r   )r   r   _outPrint_titlelenr;   )r   recorddelimitr=   s       r   
_AddRecordzACMDiffPrinter._AddRecordg   sX     OO(E
iiooedk
6{Q
jjF1I& r   c                 N    |D cg c]  }|j                  ddd       c}S c c}w )zTweak yaml printer formatted resources for ACM's dry run describe output.

    Args:
      lines: yaml printer formatted strings

    Returns:
      lines with no '-' prefix for yaml array elements.
    - r>   )replace)r   linesr:   s      r   r'   z7ACMDiffPrinter._FormatYamlPrinterLinesForDryRunDescribew   s)     388%$DLLc1%%888s   ")F)	__name__
__module____qualname____doc__r   r;   rE   r'   __classcell__)r   s   @r   r   r   !   s    ?
4l' 	9r   r   c                       e Zd ZdZy)ErrorzExceptions for this module.NrK   rL   rM   rN    r   r   rQ   rQ      s    #r   rQ   c                       e Zd ZdZy)UnknownFormatErrorzUnknown format name exception.NrR   rS   r   r   rU   rU      s    &r   rU   )defaultdiffr   c                 J    t        |||      }|r|j                  | |       yy)a  Prints the given resources.

  Args:
    resources: A singleton or list of JSON-serializable Python objects.
    print_format: The _FORMATTER name with optional projection expression.
    out: Output stream, log.out if None.
    defaults: Optional resource_projection_spec.ProjectionSpec defaults.
    single: If True then resources is a single item and not a list. For example,
      use this to print a single object as JSON.
  )r   defaultsN)r%   r@   )	resourcesprint_formatr   rY   singler3   s         r   r@   r@      s(     LcH='MM)V$ r   c           	      x   t        j                  | t        j                  |t	        j
                                     }|j                         j                         }|sy	 t        |   } |||t        ||      }|S # t        $ r3 t        dj                  dj                  t                     |            w xY w)aB  Returns a resource printer given a format string.

  Args:
    print_format: The _FORMATTERS name with optional attributes and projection.
    out: Output stream, log.out if None.
    defaults: Optional resource_projection_spec.ProjectionSpec defaults.
    console_attr: The console attributes for the output stream. Ignored by some
      printers. If None then printers that require it will initialize it to
      match out.

  Raises:
    UnknownFormatError: The print_format is invalid.

  Returns:
    An initialized ResourcePrinter class or None if printing is disabled.
  )rY   symbols)
expressionrY   Nz?  Format for acm_printer must be one of {0}; received [{1}].
  z, )r   namer3   	projectorconsole_attr)r	   Compiler   ProjectionSpecr
   GetTransforms
ProjectionName_FORMATTERSKeyErrorrU   r   joinSupportedFormatsr%   )r[   r   rY   rb   ra   printer_nameprinter_classr3   s           r   r%   r%      s    " !(('66%7%E%E%GIJ) %%',,.,	;-M 
!' 
. 
 ;
 fTYY')*L9; ;;s   !	A= =<B9c                       t        t              S )z0Returns a sorted list of supported format names.)sortedrh   rS   r   r   rk   rk      s    		r   )NNF)NNN)rN   
__future__r   r   r   r*   r#   r,   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r	   r
   r   ResourcePrinterr   rQ   rU   YamlPrinterrh   r@   r%   rk   rS   r   r   <module>ru      s    % &  '  	 	 * > A ; ; 5_9*:: _9D$J $' '
 ''$$%"&Rr   