eric7/EricNetwork/EricSslCertificatesDialog.py

branch
eric7
changeset 9152
8a68afaf1ba2
parent 8881
54e42bc2437a
child 9153
506e35e424d5
--- a/eric7/EricNetwork/EricSslCertificatesDialog.py	Wed Jun 15 09:44:07 2022 +0200
+++ b/eric7/EricNetwork/EricSslCertificatesDialog.py	Thu Jun 16 18:28:59 2022 +0200
@@ -8,10 +8,9 @@
 """
 
 import contextlib
+import pathlib
 
-from PyQt6.QtCore import (
-    pyqtSlot, Qt, QByteArray, QFile, QFileInfo, QIODevice
-)
+from PyQt6.QtCore import pyqtSlot, Qt, QByteArray
 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem
 with contextlib.suppress(ImportError):
     from PyQt6.QtNetwork import QSslCertificate, QSslConfiguration, QSsl
@@ -219,7 +218,7 @@
                             """ Skipping.</p>""")
                         .format(Utilities.decodeString(commonStr)))
                 else:
-                    pems.append(cert.toPem() + '\n')
+                    pems.append(cert.toPem() + b'\n')
             if server not in certificateDict:
                 certificateDict[server] = QByteArray()
             certificateDict[server].append(pems)
@@ -433,8 +432,10 @@
         """
         Private slot to export a certificate.
         
-        @param name default file name without extension (string)
-        @param cert certificate to be exported (QSslCertificate)
+        @param name default file name without extension
+        @type str
+        @param cert certificate to be exported encoded as PEM
+        @type QByteArray
         """
         if cert is not None:
             fname, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
@@ -447,12 +448,12 @@
                 EricFileDialog.DontConfirmOverwrite)
             
             if fname:
-                ext = QFileInfo(fname).suffix()
-                if not ext or ext not in ["pem", "der"]:
+                fpath = pathlib.Path(fname)
+                if not fpath.suffix:
                     ex = selectedFilter.split("(*")[1].split(")")[0]
                     if ex:
-                        fname += ex
-                if QFileInfo(fname).exists():
+                        fpath = fpath.with_suffix(ex)
+                if fpath.exists():
                     res = EricMessageBox.yesNo(
                         self,
                         self.tr("Export Certificate"),
@@ -462,29 +463,31 @@
                     if not res:
                         return
                 
-                f = QFile(fname)
-                if not f.open(QIODevice.OpenModeFlag.WriteOnly):
+                if fpath.suffix == ".pem":
+                    crt = bytes(cert)
+                else:
+                    crt = bytes(
+                        QSslCertificate.fromData(
+                            crt, QSsl.EncodingFormat.Pem)[0].toDer()
+                    )
+                try:
+                    with fpath.open("wb") as f:
+                        f.write(crt)
+                except OSError as err:
                     EricMessageBox.critical(
                         self,
                         self.tr("Export Certificate"),
                         self.tr(
                             """<p>The certificate could not be written"""
                             """ to file <b>{0}</b></p><p>Error: {1}</p>""")
-                        .format(fname, f.errorString()))
-                    return
-                
-                if fname.endswith(".pem"):
-                    crt = cert.toPem()
-                else:
-                    crt = cert.toDer()
-                f.write(crt)
-                f.close()
+                        .format(str(fpath), str(err)))
     
     def __importCertificate(self):
         """
         Private method to read a certificate.
         
-        @return certificates read (list of QSslCertificate)
+        @return certificates read
+        @rtype list of QSslCertificate
         """
         fname = EricFileDialog.getOpenFileName(
             self,
@@ -494,23 +497,23 @@
                     "All Files (*)"))
         
         if fname:
-            f = QFile(fname)
-            if not f.open(QIODevice.OpenModeFlag.ReadOnly):
+            try:
+                with pathlib.Path(fname).open("rb") as f:
+                    crt = QByteArray(f.read())
+                cert = QSslCertificate.fromData(
+                    crt, QSsl.EncodingFormat.Pem)
+                if not cert:
+                    cert = QSslCertificate.fromData(
+                        crt, QSsl.EncodingFormat.Der)
+                
+                return cert
+            except OSError as err:
                 EricMessageBox.critical(
                     self,
-                    self.tr("Export Certificate"),
+                    self.tr("Import Certificate"),
                     self.tr(
                         """<p>The certificate could not be read from file"""
                         """ <b>{0}</b></p><p>Error: {1}</p>""")
-                    .format(fname, f.errorString()))
-                return []
-            
-            crt = f.readAll()
-            f.close()
-            cert = QSslCertificate.fromData(crt, QSsl.EncodingFormat.Pem)
-            if not cert:
-                cert = QSslCertificate.fromData(crt, QSsl.EncodingFormat.Der)
-            
-            return cert
+                    .format(fname, str(err)))
         
         return []

eric ide

mercurial