o
    D                     @   sR   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 dejZ	dS )	zUnified diff resource printer.    )absolute_import)division)unicode_literalsN)resource_printer_basec                       s2   e Zd ZdZ fddZdd Zd	ddZ  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                    s0   t t| j|ddd| | jdd| _d S )NT)
by_columnsnon_empty_projection_requiredformat	flattened)superr   __init__
attributesget_print_format)selfargskwargs	__class__ F/tmp/google-cloud-sdk/lib/googlecloudsdk/core/resource/diff_printer.pyr   &   s   zDiffPrinter.__init__c                 C   s   ddl }t }| j| j|d}|| t }| j| j|d}|| | d}| d}|||}	|	D ]}
| j	
|
 q<dS )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liner   r   r   _Diff,   s   

zDiffPrinter._DiffFc                 C   sJ   | j d}|r| j| d| _t|dkr#| |d |d  dS dS )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delimitr-   r   r   r   
_AddRecordT   s   	zDiffPrinter._AddRecord)F)__name__
__module____qualname____doc__r   r,   r3   __classcell__r   r   r   r   r      s
    
(r   )
r7   
__future__r   r   r   r   googlecloudsdk.core.resourcer   ResourcePrinterr   r   r   r   r   <module>   s   