src/eric7/QScintilla/Exporters/ExporterBase.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 9153
506e35e424d5
child 9221
bf71ee032bb4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/eric7/QScintilla/Exporters/ExporterBase.py	Thu Jul 07 11:23:56 2022 +0200
@@ -0,0 +1,79 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2007 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the exporter base class.
+"""
+
+import pathlib
+
+from PyQt6.QtCore import QObject, QCoreApplication
+
+from EricWidgets import EricMessageBox, EricFileDialog
+
+
+class ExporterBase(QObject):
+    """
+    Class implementing the exporter base class.
+    """
+    def __init__(self, editor, parent=None):
+        """
+        Constructor
+        
+        @param editor reference to the editor object (QScintilla.Editor.Editor)
+        @param parent parent object of the exporter (QObject)
+        """
+        super().__init__(parent)
+        self.editor = editor
+    
+    def _getFileName(self, fileFilter):
+        """
+        Protected method to get the file name of the export file from the user.
+        
+        @param fileFilter the filter string to be used (string). The filter for
+            "All Files (*)" is appended by this method.
+        @return file name entered by the user (string)
+        """
+        fileFilter += ";;"
+        fileFilter += QCoreApplication.translate('Exporter', "All Files (*)")
+        fn, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
+            self.editor,
+            QCoreApplication.translate('Exporter', "Export source"),
+            "",
+            fileFilter,
+            "",
+            EricFileDialog.DontConfirmOverwrite)
+        
+        if fn:
+            fpath = pathlib.Path(fn)
+            if not fpath.suffix:
+                ex = selectedFilter.split("(*")[1].split(")")[0]
+                if ex:
+                    fpath = fpath.with_suffix(ex)
+            if fpath.exists():
+                res = EricMessageBox.yesNo(
+                    self.editor,
+                    QCoreApplication.translate(
+                        'Exporter', "Export source"),
+                    QCoreApplication.translate(
+                        'Exporter',
+                        "<p>The file <b>{0}</b> already exists."
+                        " Overwrite it?</p>").format(fpath),
+                    icon=EricMessageBox.Warning)
+                if not res:
+                    return ""
+        
+        return str(fpath)
+    
+    def exportSource(self):
+        """
+        Public method performing the export.
+        
+        This method must be overridden by the real exporters.
+        
+        @exception NotImplementedError raised to indicate that this method
+            must be implemented by a subclass
+        """
+        raise NotImplementedError

eric ide

mercurial