
                         D   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	  SSK
Jr  SSK
Jr  SS	K
Jr  SS
K
Jr  SSK
Jr   " S S\R                   5      r " S S\	R$                  5      r " S S\5      r\R(                  \\R(                  S.rSS jrSS jrS rg)zUnified diff resource printer.    )absolute_import)division)unicode_literalsN)
exceptions)resource_printer_base)resource_projection_spec)resource_projector)resource_transform)yaml_printerc                   B   ^  \ rS rSrSrU 4S jrS rSS jrS rSr	U =r
$ )	ACMDiffPrinter!   a3  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                 v   > [         [        U ]
  " USSS.UD6  U R                  R	                  SS5      U l        g )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   ACMDiffPrinter.__init__,   s<    	.$(	$dNFLN,,Xv>D    c                    [         R                  " 5       nU R                  U R                  US9nUR	                  U5        [         R                  " 5       nU R                  U R                  US9nUR	                  U5        SnSnUb.  U R                  UR                  5       R                  S5      5      nUb.  U R                  UR                  5       R                  S5      5      n[        R                  " Xg5      n[        R                  " S5      n	[        R                  " S5      n
U HR  nU(       d  M  US   S:w  d  M  U	R                  U5      (       a  M/  U
R                  U5      (       a  MG  [        U5        MT     g)	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   _DiffACMDiffPrinter._Diff1   s@   . 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8d r   c                     U R                   R                  S5      nU(       a"  U R                  R                  U5        SU l        [        U5      S:  a  U R                  US   US   5        gg)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   
_AddRecordACMDiffPrinter._AddRecordg   sX     OO(E
iiooedk
6{Q
jjF1I& r   c                 R    U Vs/ s H  o"R                  SSS5      PM     sn$ s  snf )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.
- rA   )replace)r   linesr<   s      r   r)   7ACMDiffPrinter._FormatYamlPrinterLinesForDryRunDescribew   s'     388%$LLc1%%888s   $)r   rD   )F)__name__
__module____qualname____firstlineno____doc__r   r=   rH   r)   __static_attributes____classcell__)r   s   @r   r   r   !   s"    ?
4l' 	9 	9r   r   c                       \ rS rSrSrSrg)Error   zExceptions for this module. NrP   rQ   rR   rS   rT   rU   rZ   r   r   rX   rX      s    #r   rX   c                       \ rS rSrSrSrg)UnknownFormatError   zUnknown format name exception.rZ   Nr[   rZ   r   r   r]   r]      s    &r   r]   )defaultdiffr   c                 J    [        XUS9nU(       a  UR                  X5        gg)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'   rC   )	resourcesprint_formatr!   rb   singler5   s         r   rC   rC      s$     LH='MM)$ r   c           	      x   [         R                  " U [        R                  " U[        R
                  " 5       S9S9nUR                  5       R                  5       nU(       d  g [        U   nU" UU[        UUS9nU$ ! [         a3    [        SR                  SR                  [        5       5      U5      5      ef = f)a*  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.
)rb   symbols)
expressionrb   Nz?  Format for acm_printer must be one of {0}; received [{1}].
  z, )r!   namer5   	projectorconsole_attr)r	   Compiler   ProjectionSpecr
   GetTransforms
ProjectionName_FORMATTERSKeyErrorr]   r   joinSupportedFormatsr'   )rd   r!   rb   rk   rj   printer_nameprinter_classr5   s           r   r'   r'      s    " !(('66%7%E%E%GIJ) %%',,.,	;-M 
!' 
. 
 ;
 fTYY')*L9; ;;s   #	A< <=B9c                       [        [        5      $ )z0Returns a sorted list of supported format names.)sortedrq   rZ   r   r   rt   rt      s    		r   )NNF)NNN)rT   
__future__r   r   r   r,   r%   r.   googlecloudsdk.corer   googlecloudsdk.core.resourcer   r   r	   r
   r   ResourcePrinterr   rX   r]   YamlPrinterrq   rC   r'   rt   rZ   r   r   <module>r~      s    % &  '  	 	 * > A ; ; 5_9*:: _9D$J $' '
 ''$$%"&Rr   