Modernized some more code E5Ftp).

Sun, 02 May 2021 18:13:23 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 02 May 2021 18:13:23 +0200
changeset 8283
3139cbc98a14
parent 8282
16b243bdb12f
child 8284
eeaed80810ff

Modernized some more code E5Ftp).

eric6/E5Network/E5Ftp.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/NetworkPage.py file | annotate | diff | comparison | revisions
eric6/Preferences/__init__.py file | annotate | diff | comparison | revisions
eric6/WebBrowser/Sync/FtpSyncHandler.py file | annotate | diff | comparison | revisions
--- a/eric6/E5Network/E5Ftp.py	Sun May 02 17:26:47 2021 +0200
+++ b/eric6/E5Network/E5Ftp.py	Sun May 02 18:13:23 2021 +0200
@@ -8,6 +8,7 @@
 proxies.
 """
 
+import enum
 import ftplib           # secok
 from socket import _GLOBAL_DEFAULT_TIMEOUT
 
@@ -39,22 +40,20 @@
     pass
 
 
-# TODO: convert to Enum
-#       note: it is used as a preference item as well => IntEnum
-class E5FtpProxyType:
+class E5FtpProxyType(enum.Enum):
     """
     Class defining the supported FTP proxy types.
     """
-    NoProxy = 0                     # no proxy
-    NonAuthorizing = 1              # non authorizing proxy
-    UserAtServer = 2                # proxy login first, than user@remote.host
-    Site = 3                        # proxy login first, than use SITE command
-    Open = 4                        # proxy login first, than use OPEN command
-    UserAtProxyuserAtServer = 5     # one login for both
-    ProxyuserAtServer = 6
+    NO_PROXY = 0                    # no proxy
+    NON_AUTHORIZING = 1             # non authorizing proxy
+    USER_SERVER = 2                 # proxy login first, than user@remote.host
+    SITE = 3                        # proxy login first, than use SITE command
+    OPEN = 4                        # proxy login first, than use OPEN command
+    USER_PROXYUSER_SERVER = 5       # one login for both
+    PROXYUSER_SERVER = 6
     # proxy login with remote host given, than normal remote login
-    AuthResp = 7  # authenticate to proxy with AUTH and RESP commands
-    Bluecoat = 8                    # bluecoat proxy
+    AUTH_RESP = 7  # authenticate to proxy with AUTH and RESP commands
+    BLUECOAT = 8                    # bluecoat proxy
 
 
 class E5Ftp(ftplib.FTP):
@@ -63,23 +62,34 @@
     proxies.
     """
     def __init__(self, host="", user="", password="", acct="",          # secok
-                 proxyType=E5FtpProxyType.NoProxy, proxyHost="",
+                 proxyType=E5FtpProxyType.NO_PROXY, proxyHost="",
                  proxyPort=ftplib.FTP_PORT, proxyUser="", proxyPassword="",
                  proxyAccount="", timeout=_GLOBAL_DEFAULT_TIMEOUT):
         """
         Constructor
         
-        @param host name of the FTP host (string)
-        @param user user name for login to FTP host (string)
-        @param password password for login to FTP host (string)
-        @param acct account for login to FTP host (string)
-        @param proxyType type of the FTP proxy (integer 0 to 8)
-        @param proxyHost name of the FTP proxy (string)
-        @param proxyPort port of the FTP proxy (integer)
-        @param proxyUser user name for login to the proxy (string)
-        @param proxyPassword password for login to the proxy (string)
-        @param proxyAccount accounting info for the proxy (string)
-        @param timeout timeout in seconds for blocking operations (integer)
+        @param host name of the FTP host
+        @type str
+        @param user user name for login to FTP host
+        @type str
+        @param password password for login to FTP host
+        @type str
+        @param acct account for login to FTP host
+        @type str
+        @param proxyType type of the FTP proxy
+        @type E5FtpProxyType
+        @param proxyHost name of the FTP proxy
+        @type str
+        @param proxyPort port of the FTP proxy
+        @type int
+        @param proxyUser user name for login to the proxy
+        @type str
+        @param proxyPassword password for login to the proxy
+        @type str
+        @param proxyAccount accounting info for the proxy
+        @type str
+        @param timeout timeout in seconds for blocking operations
+        @type int
         """
         super().__init__()
         
@@ -103,18 +113,24 @@
             if user:
                 self.login(user, password, acct)
     
-    def setProxy(self, proxyType=E5FtpProxyType.NoProxy, proxyHost="",
+    def setProxy(self, proxyType=E5FtpProxyType.NO_PROXY, proxyHost="",
                  proxyPort=ftplib.FTP_PORT, proxyUser="", proxyPassword="",
                  proxyAccount=""):
         """
         Public method to set the proxy configuration.
         
-        @param proxyType type of the FTP proxy (integer 0 to 8)
-        @param proxyHost name of the FTP proxy (string)
-        @param proxyPort port of the FTP proxy (integer)
-        @param proxyUser user name for login to the proxy (string)
-        @param proxyPassword password  for login to the proxy (string)
-        @param proxyAccount accounting info for the proxy (string)
+        @param proxyType type of the FTP proxy
+        @type E5FtpProxyType
+        @param proxyHost name of the FTP proxy
+        @type str
+        @param proxyPort port of the FTP proxy
+        @type int
+        @param proxyUser user name for login to the proxy
+        @type str
+        @param proxyPassword password  for login to the proxy
+        @type str
+        @param proxyAccount accounting info for the proxy
+        @type str
         """
         self.__proxyType = proxyType
         self.__proxyHost = proxyHost
@@ -128,9 +144,12 @@
         """
         Public method to set the proxy authentication info.
         
-        @param proxyUser user name for login to the proxy (string)
-        @param proxyPassword password  for login to the proxy (string)
-        @param proxyAccount accounting info for the proxy (string)
+        @param proxyUser user name for login to the proxy
+        @type str
+        @param proxyPassword password  for login to the proxy
+        @type str
+        @param proxyAccount accounting info for the proxy
+        @type str
         """
         self.__proxyUser = proxyUser
         self.__proxyPassword = proxyPassword
@@ -144,10 +163,14 @@
         if a proxy is to be used. It throws an exception, if the proxy data
         is incomplete.
         
-        @param host name of the FTP host (string)
-        @param port port of the FTP host (integer)
-        @param timeout timeout in seconds for blocking operations (integer)
-        @return welcome message of the server (string)
+        @param host name of the FTP host
+        @type str
+        @param port port of the FTP host
+        @type int
+        @param timeout timeout in seconds for blocking operations
+        @type int
+        @return welcome message of the server
+        @rtype str
         @exception E5FtpProxyError raised to indicate a proxy related issue
         """
         if host:
@@ -157,7 +180,7 @@
         if timeout != -999:
             self.__timeout = timeout
         
-        if self.__proxyType != E5FtpProxyType.NoProxy:
+        if self.__proxyType != E5FtpProxyType.NO_PROXY:
             if not self.__proxyHost:
                 raise E5FtpProxyError(
                     "990 Proxy usage requested, but no proxy host given.")
@@ -187,24 +210,24 @@
         </table>
         
         <dl>
-          <dt>E5FtpProxyType.NoProxy:</dt>
+          <dt>E5FtpProxyType.NO_PROXY:</dt>
           <dd>
             USER user<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.NonAuthorizing:</dt>
+          <dt>E5FtpProxyType.NON_AUTHORIZING:</dt>
           <dd>
             USER user@remote.host<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.UserAtServer:</dt>
+          <dt>E5FtpProxyType.USER_SERVER:</dt>
           <dd>
             USER pruser<br/>
             PASS prpass<br/>
             USER user@remote.host<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.Site:</dt>
+          <dt>E5FtpProxyType.SITE:</dt>
           <dd>
             USER pruser<br/>
             PASS prpass<br/>
@@ -212,7 +235,7 @@
             USER user<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.Open:</dt>
+          <dt>E5FtpProxyType.OPEN:</dt>
           <dd>
             USER pruser<br/>
             PASS prpass<br/>
@@ -220,26 +243,26 @@
             USER user<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.UserAtProxyuserAtServer:</dt>
+          <dt>E5FtpProxyType.USER_PROXYUSER_SERVER:</dt>
           <dd>
             USER user@pruser@remote.host<br/>
             PASS pass@prpass
           </dd>
-          <dt>E5FtpProxyType.ProxyuserAtServer:</dt>
+          <dt>E5FtpProxyType.PROXYUSER_SERVER:</dt>
           <dd>
             USER pruser@remote.host<br/>
             PASS prpass<br/>
             USER user<br/>
             PASS pass
           </dd>
-          <dt>E5FtpProxyType.AuthResp:</dt>
+          <dt>E5FtpProxyType.AUTH_RESP:</dt>
           <dd>
             USER user@remote.host<br/>
             PASS pass<br/>
             AUTH pruser<br/>
             RESP prpass
           </dd>
-          <dt>E5FtpProxyType.Bluecoat:</dt>
+          <dt>E5FtpProxyType.BLUECOAT:</dt>
           <dd>
             USER user@remote.host pruser<br/>
             PASS pass<br/>
@@ -247,10 +270,14 @@
           </dd>
         </dl>
         
-        @param user username for the remote host (string)
-        @param password password for the remote host (string)
-        @param acct accounting information for the remote host (string)
-        @return response sent by the remote host (string)
+        @param user username for the remote host
+        @type str
+        @param password password for the remote host
+        @type str
+        @param acct accounting information for the remote host
+        @type str
+        @return response sent by the remote host
+        @rtype str
         @exception E5FtpProxyError raised to indicate a proxy related issue
         @exception ftplib.error_reply raised to indicate an FTP error reply
         """
@@ -265,8 +292,8 @@
         if user == "anonymous" and password in {'', '-'}:
             password += "anonymous@"
         
-        if self.__proxyType != E5FtpProxyType.NoProxy:
-            if self.__proxyType != E5FtpProxyType.NonAuthorizing:
+        if self.__proxyType != E5FtpProxyType.NO_PROXY:
+            if self.__proxyType != E5FtpProxyType.NON_AUTHORIZING:
                 # check, if a valid proxy configuration is known
                 if not self.__proxyUser:
                     raise E5FtpProxyError(
@@ -276,22 +303,22 @@
                         "992 Proxy usage requested, but no proxy password"
                         " given")
             
-            if self.__proxyType in [E5FtpProxyType.NonAuthorizing,
-                                    E5FtpProxyType.AuthResp,
-                                    E5FtpProxyType.Bluecoat]:
+            if self.__proxyType in [E5FtpProxyType.NON_AUTHORIZING,
+                                    E5FtpProxyType.AUTH_RESP,
+                                    E5FtpProxyType.BLUECOAT]:
                 user += "@" + self.__host
-                if self.__proxyType == E5FtpProxyType.Bluecoat:
+                if self.__proxyType == E5FtpProxyType.BLUECOAT:
                     user += " " + self.__proxyUser
                     acct = self.__proxyPassword
-            elif self.__proxyType == E5FtpProxyType.UserAtProxyuserAtServer:
+            elif self.__proxyType == E5FtpProxyType.USER_PROXYUSER_SERVER:
                 user = "{0}@{1}@{2}".format(
                     user, self.__proxyUser, self.__host)
                 password = "{0}@{1}".format(password, self.__proxyPassword)
             else:
                 pruser = self.__proxyUser
-                if self.__proxyType == E5FtpProxyType.UserAtServer:
+                if self.__proxyType == E5FtpProxyType.USER_SERVER:
                     user += "@" + self.__host
-                elif self.__proxyType == E5FtpProxyType.ProxyuserAtServer:
+                elif self.__proxyType == E5FtpProxyType.PROXYUSER_SERVER:
                     pruser += "@" + self.__host
                 
                 # authenticate to the proxy first
@@ -305,14 +332,14 @@
                         "9{0}0 Error authorizing at proxy\n{1}".format(
                             presp[0], presp))
                 
-                if self.__proxyType == E5FtpProxyType.Site:
+                if self.__proxyType == E5FtpProxyType.SITE:
                     # send SITE command
                     presp = self.sendcmd("SITE " + self.__host)
                     if presp[0] != "2":
                         raise E5FtpProxyError(
                             "9{0}0 Error sending SITE command\n{1}".format(
                                 presp[0], presp))
-                elif self.__proxyType == E5FtpProxyType.Open:
+                elif self.__proxyType == E5FtpProxyType.OPEN:
                     # send OPEN command
                     presp = self.sendcmd("OPEN " + self.__host)
                     if presp[0] != "2":
@@ -329,7 +356,7 @@
         if resp[0] != "2":
             raise ftplib.error_reply(resp)          # secok
         
-        if self.__proxyType == E5FtpProxyType.AuthResp:
+        if self.__proxyType == E5FtpProxyType.AUTH_RESP:
             # authorize to the FTP proxy
             presp = self.sendcmd("AUTH " + self.__proxyUser)
             if presp[0] == "3":
--- a/eric6/Preferences/ConfigurationPages/NetworkPage.py	Sun May 02 17:26:47 2021 +0200
+++ b/eric6/Preferences/ConfigurationPages/NetworkPage.py	Sun May 02 18:13:23 2021 +0200
@@ -41,25 +41,25 @@
         self.downloadDirPicker.setMode(E5PathPickerModes.DirectoryMode)
         
         self.ftpProxyTypeCombo.addItem(
-            self.tr("No FTP Proxy"), E5FtpProxyType.NoProxy)
+            self.tr("No FTP Proxy"), E5FtpProxyType.NO_PROXY)
         self.ftpProxyTypeCombo.addItem(
             self.tr("No Proxy Authentication required"),
-            E5FtpProxyType.NonAuthorizing)
+            E5FtpProxyType.NON_AUTHORIZING)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("User@Server"), E5FtpProxyType.UserAtServer)
+            self.tr("User@Server"), E5FtpProxyType.USER_SERVER)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("SITE"), E5FtpProxyType.Site)
+            self.tr("SITE"), E5FtpProxyType.SITE)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("OPEN"), E5FtpProxyType.Open)
+            self.tr("OPEN"), E5FtpProxyType.OPEN)
         self.ftpProxyTypeCombo.addItem(
             self.tr("User@Proxyuser@Server"),
-            E5FtpProxyType.UserAtProxyuserAtServer)
+            E5FtpProxyType.USER_PROXYUSER_SERVER)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("Proxyuser@Server"), E5FtpProxyType.ProxyuserAtServer)
+            self.tr("Proxyuser@Server"), E5FtpProxyType.PROXYUSER_SERVER)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("AUTH and RESP"), E5FtpProxyType.AuthResp)
+            self.tr("AUTH and RESP"), E5FtpProxyType.AUTH_RESP)
         self.ftpProxyTypeCombo.addItem(
-            self.tr("Bluecoat Proxy"), E5FtpProxyType.Bluecoat)
+            self.tr("Bluecoat Proxy"), E5FtpProxyType.BLUECOAT)
         
         # set initial values
         self.downloadDirPicker.setText(Preferences.getUI("DownloadPath"))
@@ -204,8 +204,7 @@
             self.ftpProxyPortSpin.value())
         Preferences.setUI(
             "ProxyType/Ftp",
-            self.ftpProxyTypeCombo.itemData(
-                self.ftpProxyTypeCombo.currentIndex()))
+            self.ftpProxyTypeCombo.currentData())
         Preferences.setUI(
             "ProxyUser/Ftp",
             self.ftpProxyUserEdit.text())
@@ -232,17 +231,17 @@
         @param index index of the selected item (integer)
         """
         proxyType = self.ftpProxyTypeCombo.itemData(index)
-        self.ftpProxyHostEdit.setEnabled(proxyType != E5FtpProxyType.NoProxy)
-        self.ftpProxyPortSpin.setEnabled(proxyType != E5FtpProxyType.NoProxy)
+        self.ftpProxyHostEdit.setEnabled(proxyType != E5FtpProxyType.NO_PROXY)
+        self.ftpProxyPortSpin.setEnabled(proxyType != E5FtpProxyType.NO_PROXY)
         self.ftpProxyUserEdit.setEnabled(
-            proxyType not in [E5FtpProxyType.NoProxy,
-                              E5FtpProxyType.NonAuthorizing])
+            proxyType not in [E5FtpProxyType.NO_PROXY,
+                              E5FtpProxyType.NON_AUTHORIZING])
         self.ftpProxyPasswordEdit.setEnabled(
-            proxyType not in [E5FtpProxyType.NoProxy,
-                              E5FtpProxyType.NonAuthorizing])
+            proxyType not in [E5FtpProxyType.NO_PROXY,
+                              E5FtpProxyType.NON_AUTHORIZING])
         self.ftpProxyAccountEdit.setEnabled(
-            proxyType not in [E5FtpProxyType.NoProxy,
-                              E5FtpProxyType.NonAuthorizing])
+            proxyType not in [E5FtpProxyType.NO_PROXY,
+                              E5FtpProxyType.NON_AUTHORIZING])
     
 
 def create(dlg):
--- a/eric6/Preferences/__init__.py	Sun May 02 17:26:47 2021 +0200
+++ b/eric6/Preferences/__init__.py	Sun May 02 18:13:23 2021 +0200
@@ -201,7 +201,7 @@
         "ProxyPassword/Http": "",
         "ProxyPassword/Https": "",
         "ProxyPassword/Ftp": "",
-        "ProxyType/Ftp": E5FtpProxyType.NoProxy,
+        "ProxyType/Ftp": E5FtpProxyType.NO_PROXY,
         "ProxyAccount/Ftp": "",
         "ProxyExceptions": "localhost,127.0.0.,::1",
         
@@ -1933,12 +1933,15 @@
             "UI/" + key, prefClass.uiDefaults[key]))
     elif key in ["TabViewManagerFilenameLength", "CaptionFilenameLength",
                  "ProxyPort/Http", "ProxyPort/Https", "ProxyPort/Ftp",
-                 "ProxyType/Ftp", "OpenOnStartup",
-                 "PerformVersionCheck", "RecentNumber", "NotificationTimeout",
-                 "SidebarDelay", "KeyboardInputInterval",
-                 "BackgroundServiceProcesses", "MinimumMessageTypeSeverity"]:
+                 "OpenOnStartup", "PerformVersionCheck", "RecentNumber",
+                 "NotificationTimeout", "SidebarDelay",
+                 "KeyboardInputInterval", "BackgroundServiceProcesses",
+                 "MinimumMessageTypeSeverity"]:
         return int(prefClass.settings.value(
             "UI/" + key, prefClass.uiDefaults[key]))
+    elif key in ["ProxyType/Ftp", ]:
+        return E5FtpProxyType(int(prefClass.settings.value(
+            "UI/" + key, prefClass.uiDefaults[key])))
     elif key in ["ProxyPassword/Http", "ProxyPassword/Https",
                  "ProxyPassword/Ftp", ]:
         from Utilities.crypto import pwConvert
@@ -2015,6 +2018,9 @@
                  "ProxyPassword/Ftp", ]:
         from Utilities.crypto import pwConvert
         prefClass.settings.setValue("UI/" + key, pwConvert(value, encode=True))
+    elif key in ["ProxyType/Ftp", ]:
+        # value is an enum.Enum derived item
+        prefClass.settings.setValue("UI/" + key, value.value)
     else:
         prefClass.settings.setValue("UI/" + key, value)
     
--- a/eric6/WebBrowser/Sync/FtpSyncHandler.py	Sun May 02 17:26:47 2021 +0200
+++ b/eric6/WebBrowser/Sync/FtpSyncHandler.py	Sun May 02 18:13:23 2021 +0200
@@ -85,16 +85,16 @@
         
         # do proxy setup
         proxyType = (
-            E5FtpProxyType.NoProxy
+            E5FtpProxyType.NO_PROXY
             if not Preferences.getUI("UseProxy") else
             Preferences.getUI("ProxyType/Ftp")
         )
-        if proxyType != E5FtpProxyType.NoProxy:
+        if proxyType != E5FtpProxyType.NO_PROXY:
             self.__ftp.setProxy(
                 proxyType,
                 Preferences.getUI("ProxyHost/Ftp"),
                 Preferences.getUI("ProxyPort/Ftp"))
-            if proxyType != E5FtpProxyType.NonAuthorizing:
+            if proxyType != E5FtpProxyType.NON_AUTHORIZING:
                 self.__ftp.setProxyAuthentication(
                     Preferences.getUI("ProxyUser/Ftp"),
                     Preferences.getUI("ProxyPassword/Ftp"),

eric ide

mercurial