|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the exporter base class. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QFileInfo, QObject, QCoreApplication |
|
13 |
|
14 from E5Gui import E5MessageBox, E5FileDialog |
|
15 |
|
16 import Utilities |
|
17 |
|
18 |
|
19 class ExporterBase(QObject): |
|
20 """ |
|
21 Class implementing the exporter base class. |
|
22 """ |
|
23 def __init__(self, editor, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param editor reference to the editor object (QScintilla.Editor.Editor) |
|
28 @param parent parent object of the exporter (QObject) |
|
29 """ |
|
30 super(ExporterBase, self).__init__(parent) |
|
31 self.editor = editor |
|
32 |
|
33 def _getFileName(self, fileFilter): |
|
34 """ |
|
35 Protected method to get the file name of the export file from the user. |
|
36 |
|
37 @param fileFilter the filter string to be used (string). The filter for |
|
38 "All Files (*)" is appended by this method. |
|
39 @return file name entered by the user (string) |
|
40 """ |
|
41 fileFilter += ";;" |
|
42 fileFilter += QCoreApplication.translate('Exporter', "All Files (*)") |
|
43 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
|
44 self.editor, |
|
45 QCoreApplication.translate('Exporter', "Export source"), |
|
46 "", |
|
47 fileFilter, |
|
48 "", |
|
49 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
|
50 |
|
51 if fn: |
|
52 ext = QFileInfo(fn).suffix() |
|
53 if not ext: |
|
54 ex = selectedFilter.split("(*")[1].split(")")[0] |
|
55 if ex: |
|
56 fn += ex |
|
57 if QFileInfo(fn).exists(): |
|
58 res = E5MessageBox.yesNo( |
|
59 self.editor, |
|
60 QCoreApplication.translate( |
|
61 'Exporter', "Export source"), |
|
62 QCoreApplication.translate( |
|
63 'Exporter', |
|
64 "<p>The file <b>{0}</b> already exists." |
|
65 " Overwrite it?</p>").format(fn), |
|
66 icon=E5MessageBox.Warning) |
|
67 if not res: |
|
68 return "" |
|
69 |
|
70 fn = Utilities.toNativeSeparators(fn) |
|
71 |
|
72 return fn |
|
73 |
|
74 def exportSource(self): |
|
75 """ |
|
76 Public method performing the export. |
|
77 |
|
78 This method must be overridden by the real exporters. |
|
79 |
|
80 @exception NotImplementedError raised to indicate that this method |
|
81 must be implemented by a subclass |
|
82 """ |
|
83 raise NotImplementedError |