diff -r 16b243bdb12f -r 3139cbc98a14 eric6/E5Network/E5Ftp.py --- 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":