~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/gui2/tweak_book/diff/main.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-27 07:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 74.
  • Revision ID: package-import@ubuntu.com-20140227074806-64wdebb3ptosxhhx
Tags: upstream-1.25.0+dfsg
ImportĀ upstreamĀ versionĀ 1.25.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
__license__ = 'GPL v3'
7
7
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
8
8
 
9
 
import sys, os
 
9
import sys, os, re
10
10
from functools import partial
11
11
 
12
12
from PyQt4.Qt import (
93
93
        added_names.add(name)
94
94
    return cache, changed_names, renamed_names, removed_names, added_names
95
95
 
 
96
 
96
97
def get_decoded_raw(name):
97
98
    from calibre.ebooks.chardet import xml_to_unicode, force_encoding
98
99
    with open(name, 'rb') as f:
107
108
        if syntax in {'html', 'xml'}:
108
109
            raw = xml_to_unicode(raw, verbose=True)[0]
109
110
        else:
110
 
            enc = force_encoding(raw, verbose=True)
 
111
            m = re.search(r"coding[:=]\s*([-\w.]+)", raw[:1024], flags=re.I)
 
112
            if m is not None:
 
113
                enc = m.group(1)
 
114
            else:
 
115
                enc = force_encoding(raw, verbose=True)
111
116
            try:
112
117
                raw = raw.decode(enc)
113
118
            except ValueError:
180
185
    revert_requested = pyqtSignal()
181
186
    line_activated = pyqtSignal(object, object, object)
182
187
 
183
 
    def __init__(self, revert_button_msg=None, parent=None, show_open_in_editor=False):
 
188
    def __init__(self, revert_button_msg=None, parent=None, show_open_in_editor=False, show_as_window=False):
184
189
        self.context = 3
185
190
        self.beautify = False
186
191
        self.apply_diff_calls = []
187
192
        self.show_open_in_editor = show_open_in_editor
188
193
        self.revert_button_msg = revert_button_msg
189
194
        Dialog.__init__(self, _('Differences between books'), 'diff-dialog', parent=parent)
 
195
        if show_as_window:
 
196
            self.setWindowFlags(Qt.Window)
190
197
        self.view.line_activated.connect(self.line_activated)
191
198
 
192
199
    def sizeHint(self):
257
264
        for i in (3, 5, 10, 50):
258
265
            cm.addAction(_('Show %d lines of context') % i, partial(self.change_context, i))
259
266
        cm.addAction(_('Show all text'), partial(self.change_context, None))
260
 
        m.addAction(_('Beautify files before comparing them'), partial(self.change_beautify, True))
 
267
        self.beautify_action = m.addAction('', self.toggle_beautify)
 
268
        self.set_beautify_action_text()
261
269
        m.addMenu(cm)
262
270
        l.addWidget(b, r, 7)
263
271
 
268
276
 
269
277
        self.bb.setStandardButtons(self.bb.Close)
270
278
        if self.revert_button_msg is not None:
271
 
            self.rvb = b = self.bb.addButton(self.revert_button_msg, self.bb.RejectRole)
272
 
            b.setIcon(QIcon(I('edit-undo.png')))
 
279
            self.rvb = b = self.bb.addButton(self.revert_button_msg, self.bb.ActionRole)
 
280
            b.setIcon(QIcon(I('edit-undo.png'))), b.setAutoDefault(False)
273
281
            b.clicked.connect(self.revert_requested)
 
282
            b.clicked.connect(self.reject)
274
283
        self.bb.button(self.bb.Close).setDefault(True)
275
284
        self.hl.addWidget(self.bb, r)
276
285
 
306
315
                self.view.add_diff(*args, **kwargs)
307
316
            self.view.finalize()
308
317
 
309
 
    def change_beautify(self, beautify):
310
 
        if beautify == self.beautify:
311
 
            return
312
 
        self.beautify = beautify
 
318
    def toggle_beautify(self):
 
319
        self.beautify = not self.beautify
 
320
        self.set_beautify_action_text()
313
321
        self.refresh()
314
322
 
 
323
    def set_beautify_action_text(self):
 
324
        self.beautify_action.setText(
 
325
            _('Beautify files before comparing them') if not self.beautify else
 
326
            _('Do not beautify files before comparing'))
 
327
 
315
328
    def __enter__(self):
316
329
        self.stacks.setCurrentIndex(0)
317
330
        self.busy.pi.startAnimation()
434
447
    else:
435
448
        attr = 'file_diff'
436
449
    app = QApplication([])
437
 
    d = Diff()
 
450
    d = Diff(show_as_window=True)
438
451
    d.show()
439
452
    getattr(d, attr)(left, right)
440
 
    app.exec_()
 
453
    return app.exec_()
441
454
 
442
455
if __name__ == '__main__':
443
456
    main()