7 Module implementing a synchronization handler using FTP. |
7 Module implementing a synchronization handler using FTP. |
8 """ |
8 """ |
9 |
9 |
10 import ftplib # secok |
10 import ftplib # secok |
11 import io |
11 import io |
|
12 import contextlib |
12 |
13 |
13 from PyQt5.QtCore import ( |
14 from PyQt5.QtCore import ( |
14 pyqtSignal, QTimer, QFileInfo, QCoreApplication, QByteArray |
15 pyqtSignal, QTimer, QFileInfo, QCoreApplication, QByteArray |
15 ) |
16 ) |
16 |
17 |
81 self.__idleTimer.timeout.connect(self.__idleTimeout) |
82 self.__idleTimer.timeout.connect(self.__idleTimeout) |
82 |
83 |
83 self.__ftp = E5Ftp() |
84 self.__ftp = E5Ftp() |
84 |
85 |
85 # do proxy setup |
86 # do proxy setup |
86 if not Preferences.getUI("UseProxy"): |
87 proxyType = ( |
87 proxyType = E5FtpProxyType.NoProxy |
88 E5FtpProxyType.NoProxy |
88 else: |
89 if not Preferences.getUI("UseProxy") else |
89 proxyType = Preferences.getUI("ProxyType/Ftp") |
90 Preferences.getUI("ProxyType/Ftp") |
|
91 ) |
90 if proxyType != E5FtpProxyType.NoProxy: |
92 if proxyType != E5FtpProxyType.NoProxy: |
91 self.__ftp.setProxy( |
93 self.__ftp.setProxy( |
92 proxyType, |
94 proxyType, |
93 Preferences.getUI("ProxyHost/Ftp"), |
95 Preferences.getUI("ProxyHost/Ftp"), |
94 Preferences.getUI("ProxyPort/Ftp")) |
96 Preferences.getUI("ProxyPort/Ftp")) |
167 urlInfo = self.__dirLineParser.parseLine(line) |
169 urlInfo = self.__dirLineParser.parseLine(line) |
168 except FtpDirLineParserError: |
170 except FtpDirLineParserError: |
169 # silently ignore parser errors |
171 # silently ignore parser errors |
170 urlInfo = None |
172 urlInfo = None |
171 |
173 |
172 if urlInfo and urlInfo.isValid() and urlInfo.isFile(): |
174 if ( |
173 if urlInfo.name() in self._remoteFiles.values(): |
175 urlInfo and |
174 self.__remoteFilesFound[urlInfo.name()] = ( |
176 urlInfo.isValid() and |
175 urlInfo.lastModified() |
177 urlInfo.isFile() and |
176 ) |
178 urlInfo.name() in self._remoteFiles.values() |
|
179 ): |
|
180 self.__remoteFilesFound[urlInfo.name()] = urlInfo.lastModified() |
177 |
181 |
178 QCoreApplication.processEvents() |
182 QCoreApplication.processEvents() |
179 |
183 |
180 def __downloadFile(self, type_, fileName, timestamp): |
184 def __downloadFile(self, type_, fileName, timestamp): |
181 """ |
185 """ |
389 Public method to shut down the handler. |
393 Public method to shut down the handler. |
390 """ |
394 """ |
391 if self.__idleTimer.isActive(): |
395 if self.__idleTimer.isActive(): |
392 self.__idleTimer.stop() |
396 self.__idleTimer.stop() |
393 |
397 |
394 try: |
398 with contextlib.suppress(ftplib.all_errors): |
395 if self.__connected: |
399 if self.__connected: |
396 self.__ftp.quit() |
400 self.__ftp.quit() |
397 except ftplib.all_errors: |
|
398 pass # ignore FTP errors because we are shutting down anyway |
|
399 self.__connected = False |
401 self.__connected = False |
400 |
402 |
401 def __idleTimeout(self): |
403 def __idleTimeout(self): |
402 """ |
404 """ |
403 Private slot to prevent a disconnect from the server. |
405 Private slot to prevent a disconnect from the server. |