# eric6.UI.DiffDialog

Module implementing a dialog to compare two files.

### Global Attributes

None

### Classes

DiffDialog | Class implementing a dialog to compare two files.
DiffWindow | Main window class for the standalone dialog.

### Functions

context_diff | Compare two sequences of lines; generate the delta as a context diff.
unified_diff | Compare two sequences of lines; generate the delta as a unified diff.

## DiffDialog

Class implementing a dialog to compare two files.

### Derived from

QWidget, Ui_DiffDialog

### Class Attributes

None

### Class Methods

None

### Methods

DiffDialog | Constructor
__appendText | Private method to append text to the end of the contents pane.
__fileChanged | Private slot to enable/disable the Compare button.
__generateContextDiff | Private slot to generate a context diff output.
__generateUnifiedDiff | Private slot to generate a unified diff output.
on_buttonBox_clicked | Private slot called by a button of the button box clicked.
on_diffButton_clicked | Private slot to handle the Compare button press.
on_saveButton_clicked | Private slot to handle the Save button press.
show | Public slot to show the dialog.

### Static Methods

None

#### DiffDialog (Constructor)

DiffDialog(parent=None)

Constructor

parent
  reference to the parent widget (QWidget)

#### DiffDialog.__appendText

__appendText(txt)

Private method to append text to the end of the contents pane.

txt
  text to insert (string)

#### DiffDialog.__fileChanged

__fileChanged()

Private slot to enable/disable the Compare button.

#### DiffDialog.__generateContextDiff

__generateContextDiff(a, b, fromfile, tofile, fromfiledate, tofiledate)

Private slot to generate a context diff output.

a
  first sequence of lines (list of strings)
b
  second sequence of lines (list of strings)
fromfile
  filename of the first file (string)
tofile
  filename of the second file (string)
fromfiledate
  modification time of the first file (string)
tofiledate
  modification time of the second file (string)

#### DiffDialog.__generateUnifiedDiff

__generateUnifiedDiff(a, b, fromfile, tofile, fromfiledate, tofiledate)

Private slot to generate a unified diff output.

a
  first sequence of lines (list of strings)
b
  second sequence of lines (list of strings)
fromfile
  filename of the first file (string)
tofile
  filename of the second file (string)
fromfiledate
  modification time of the first file (string)
tofiledate
  modification time of the second file (string)

#### DiffDialog.on_buttonBox_clicked

on_buttonBox_clicked(button)

Private slot called by a button of the button box clicked.

button
  button that was clicked (QAbstractButton)

#### DiffDialog.on_diffButton_clicked

on_diffButton_clicked()

Private slot to handle the Compare button press.

#### DiffDialog.on_saveButton_clicked

on_saveButton_clicked()

Private slot to handle the Save button press.

It saves the diff shown in the dialog to a file in the local filesystem.

#### DiffDialog.show

show(filename=None)

Public slot to show the dialog.

filename
  name of a file to use as the first file (string)

Up

## DiffWindow

Main window class for the standalone dialog.

### Derived from

E5MainWindow

### Class Attributes

None

### Class Methods

None

### Methods

DiffWindow | Constructor
eventFilter | Public method to filter events.

### Static Methods

None

#### DiffWindow (Constructor)

DiffWindow(parent=None)

Constructor

parent
  reference to the parent widget (QWidget)

#### DiffWindow.eventFilter

eventFilter(obj, event)

Public method to filter events.

obj
  reference to the object the event is meant for (QObject)
event
  reference to the event object (QEvent)

Returns:
  flag indicating, whether the event was handled (boolean)

Up

## context_diff

context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')

Compare two sequences of lines; generate the delta as a context diff.

Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.

By default, the diff control lines (those with *** or ---) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines.

For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free.

The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the format returned by time.ctime(). If not specified, the strings default to blanks.

Example:

        >>> print ''.join(
        ...       context_diff('one\ntwo\nthree\nfour\n'.splitlines(1),
        ...       'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current',
        ...       'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:22:46 2003')),
        *** Original Sat Jan 26 23:30:50 1991
        --- Current Fri Jun 06 10:22:46 2003
        ***************
        *** 1,4 ****
          one
        ! two
        ! three
          four
        --- 1,4 ----
        + zero
          one
        ! tree
          four

a
  first sequence of lines (list of strings)
b
  second sequence of lines (list of strings)
fromfile
  filename of the first file (string)
tofile
  filename of the second file (string)
fromfiledate
  modification time of the first file (string)
tofiledate
  modification time of the second file (string)
n
  number of lines of context (integer)
lineterm
  line termination string (string)

Returns:
  a generator yielding lines of differences

Up

## unified_diff

unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')

Compare two sequences of lines; generate the delta as a unified diff.

Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.

By default, the diff control lines (those with ---, +++, or @@) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines.

For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free.

The unidiff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the format returned by time.ctime().

Example:

        >>> for line in unified_diff('one two three four'.split(),
        ...             'zero one tree four'.split(), 'Original', 'Current',
        ...             'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
        ...             lineterm=''):
        ...     print line
        --- Original Sat Jan 26 23:30:50 1991
        +++ Current Fri Jun 06 10:20:52 2003
    @ -1,4 +1,4 @@
        +zero
         one
        -two
        -three
        +tree
         four

a
  first sequence of lines (list of strings)
b
  second sequence of lines (list of strings)
fromfile
  filename of the first file (string)
tofile
  filename of the second file (string)
fromfiledate
  modification time of the first file (string)
tofiledate
  modification time of the second file (string)
n
  number of lines of context (integer)
lineterm
  line termination string (string)

Returns:
  a generator yielding lines of differences

Up