
    D                     j    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  G d dej                        Z	y)	zUnified diff resource printer.    )absolute_import)division)unicode_literalsN)resource_printer_basec                   0     e Zd ZdZ fdZd ZddZ xZS )DiffPrinteraJ  A printer for a unified diff 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 'flattened'.
  c                 t    t        t        | 
  |ddd| | j                  j	                  dd      | _        y )NT)
by_columnsnon_empty_projection_requiredformat	flattened)superr   __init__
attributesget_print_format)selfargskwargs	__class__s      0lib/googlecloudsdk/core/resource/diff_printer.pyr   zDiffPrinter.__init__&   s<    	+t%tDH0(.0 ,,X{CD    c                    ddl }t        j                         }| j                  | j                  |      }|j                  |       t        j                         }| j                  | j                  |      }|j                  |       |j                         j                  d      }|j                         j                  d      }|j                  ||      }	|	D ]  }
| j                  j                  |
        y)aO  Prints the unified diff of formatter output for old and new.

    Prints a unified diff, eg,
    ---

    +++

    @@ -27,6 +27,6 @@

     settings.pricingPlan:                             PER_USE
     settings.replicationType:                         SYNCHRONOUS
     settings.settingsVersion:                         1
    -settings.tier:                                    D1
    +settings.tier:                                    D0
     state:                                            RUNNABLE

    Args:
      old: The old original resource.
      new: The new changed resource.
    r   N)out
)difflibioStringIOPrinterr   PrintSingleRecordgetvaluesplitunified_diff_outPrint)r   oldnewr   buf_oldprinterbuf_new	lines_old	lines_new
lines_difflines              r   _DiffzDiffPrinter._Diff,   s    ,  kkmGll4--7l;Gc"kkmGll4--7l;Gc"  "((.I  "((.I%%i;J
iiood 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 colums are silently ignored.

    Args:
      record: A JSON-serializable object.
      delimit: Prints resource delimiters if True.
    titleN   r   )r   r   r$   r%   _titlelenr/   )r   recorddelimitr1   s       r   
_AddRecordzDiffPrinter._AddRecordT   sX     OO(E
iiooedk
6{Q
jjF1I& r   )F)__name__
__module____qualname____doc__r   r/   r7   __classcell__)r   s   @r   r   r      s    D&P'r   r   )
r;   
__future__r   r   r   r   googlecloudsdk.core.resourcer   ResourcePrinterr    r   r   <module>rA      s.     % &  ' 	 >G''77 G'r   