Helpviewer/Sync/FtpSyncHandler.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2525
8b507a9a2d40
parent 3002
6ffc581f00f1
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 11
12 import ftplib 12 import ftplib
13 import io 13 import io
14 14
15 from PyQt4.QtCore import pyqtSignal, QTimer, QFileInfo, QCoreApplication, QByteArray 15 from PyQt4.QtCore import pyqtSignal, QTimer, QFileInfo, QCoreApplication, \
16 QByteArray
16 17
17 from E5Network.E5Ftp import E5Ftp, E5FtpProxyType, E5FtpProxyError 18 from E5Network.E5Ftp import E5Ftp, E5FtpProxyType, E5FtpProxyError
18 19
19 from .SyncHandler import SyncHandler 20 from .SyncHandler import SyncHandler
20 21
28 class FtpSyncHandler(SyncHandler): 29 class FtpSyncHandler(SyncHandler):
29 """ 30 """
30 Class implementing a synchronization handler using FTP. 31 Class implementing a synchronization handler using FTP.
31 32
32 @signal syncStatus(type_, message) emitted to indicate the synchronization 33 @signal syncStatus(type_, message) emitted to indicate the synchronization
33 status (string one of "bookmarks", "history", "passwords", "useragents" or 34 status (string one of "bookmarks", "history", "passwords",
34 "speeddial", string) 35 "useragents" or "speeddial", string)
35 @signal syncError(message) emitted for a general error with the error message (string) 36 @signal syncError(message) emitted for a general error with the error
36 @signal syncMessage(message) emitted to send a message about synchronization (string) 37 message (string)
37 @signal syncFinished(type_, done, download) emitted after a synchronization has 38 @signal syncMessage(message) emitted to send a message about
38 finished (string one of "bookmarks", "history", "passwords", "useragents" or 39 synchronization (string)
39 "speeddial", boolean, boolean) 40 @signal syncFinished(type_, done, download) emitted after a
41 synchronization has finished (string one of "bookmarks", "history",
42 "passwords", "useragents" or "speeddial", boolean, boolean)
40 """ 43 """
41 syncStatus = pyqtSignal(str, str) 44 syncStatus = pyqtSignal(str, str)
42 syncError = pyqtSignal(str) 45 syncError = pyqtSignal(str)
43 syncMessage = pyqtSignal(str) 46 syncMessage = pyqtSignal(str)
44 syncFinished = pyqtSignal(str, bool, bool) 47 syncFinished = pyqtSignal(str, bool, bool)
59 62
60 def initialLoadAndCheck(self, forceUpload): 63 def initialLoadAndCheck(self, forceUpload):
61 """ 64 """
62 Public method to do the initial check. 65 Public method to do the initial check.
63 66
64 @keyparam forceUpload flag indicating a forced upload of the files (boolean) 67 @keyparam forceUpload flag indicating a forced upload of the files
68 (boolean)
65 """ 69 """
66 if not Preferences.getHelp("SyncEnabled"): 70 if not Preferences.getHelp("SyncEnabled"):
67 return 71 return
68 72
69 self.__state = "initializing" 73 self.__state = "initializing"
71 75
72 self.__dirLineParser = FtpDirLineParser() 76 self.__dirLineParser = FtpDirLineParser()
73 self.__remoteFilesFound = {} 77 self.__remoteFilesFound = {}
74 78
75 self.__idleTimer = QTimer(self) 79 self.__idleTimer = QTimer(self)
76 self.__idleTimer.setInterval(Preferences.getHelp("SyncFtpIdleTimeout") * 1000) 80 self.__idleTimer.setInterval(
81 Preferences.getHelp("SyncFtpIdleTimeout") * 1000)
77 self.__idleTimer.timeout.connect(self.__idleTimeout) 82 self.__idleTimer.timeout.connect(self.__idleTimeout)
78 83
79 self.__ftp = E5Ftp() 84 self.__ftp = E5Ftp()
80 85
81 # do proxy setup 86 # do proxy setup
164 # silently ignore parser errors 169 # silently ignore parser errors
165 urlInfo = None 170 urlInfo = None
166 171
167 if urlInfo and urlInfo.isValid() and urlInfo.isFile(): 172 if urlInfo and urlInfo.isValid() and urlInfo.isFile():
168 if urlInfo.name() in self._remoteFiles.values(): 173 if urlInfo.name() in self._remoteFiles.values():
169 self.__remoteFilesFound[urlInfo.name()] = urlInfo.lastModified() 174 self.__remoteFilesFound[urlInfo.name()] = \
175 urlInfo.lastModified()
170 176
171 QCoreApplication.processEvents() 177 QCoreApplication.processEvents()
172 178
173 def __downloadFile(self, type_, fileName, timestamp): 179 def __downloadFile(self, type_, fileName, timestamp):
174 """ 180 """
175 Private method to downlaod the given file. 181 Private method to downlaod the given file.
176 182
177 @param type_ type of the synchronization event (string one 183 @param type_ type of the synchronization event (string one
178 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 184 of "bookmarks", "history", "passwords", "useragents" or
185 "speeddial")
179 @param fileName name of the file to be downloaded (string) 186 @param fileName name of the file to be downloaded (string)
180 @param timestamp time stamp in seconds of the file to be downloaded (int) 187 @param timestamp time stamp in seconds of the file to be downloaded
188 (integer)
181 """ 189 """
182 self.syncStatus.emit(type_, self._messages[type_]["RemoteExists"]) 190 self.syncStatus.emit(type_, self._messages[type_]["RemoteExists"])
183 buffer = io.BytesIO() 191 buffer = io.BytesIO()
184 try: 192 try:
185 self.__ftp.retrbinary( 193 self.__ftp.retrbinary(
209 def __uploadFile(self, type_, fileName): 217 def __uploadFile(self, type_, fileName):
210 """ 218 """
211 Private method to upload the given file. 219 Private method to upload the given file.
212 220
213 @param type_ type of the synchronization event (string one 221 @param type_ type of the synchronization event (string one
214 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 222 of "bookmarks", "history", "passwords", "useragents" or
223 "speeddial")
215 @param fileName name of the file to be uploaded (string) 224 @param fileName name of the file to be uploaded (string)
216 @return flag indicating success (boolean) 225 @return flag indicating success (boolean)
217 """ 226 """
218 res = False 227 res = False
219 data = self.readFile(fileName, type_) 228 data = self.readFile(fileName, type_)
237 def __initialSyncFile(self, type_, fileName): 246 def __initialSyncFile(self, type_, fileName):
238 """ 247 """
239 Private method to do the initial synchronization of the given file. 248 Private method to do the initial synchronization of the given file.
240 249
241 @param type_ type of the synchronization event (string one 250 @param type_ type of the synchronization event (string one
242 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 251 of "bookmarks", "history", "passwords", "useragents" or
252 "speeddial")
243 @param fileName name of the file to be synchronized (string) 253 @param fileName name of the file to be synchronized (string)
244 """ 254 """
245 if not self.__forceUpload and \ 255 if not self.__forceUpload and \
246 self._remoteFiles[type_] in self.__remoteFilesFound: 256 self._remoteFiles[type_] in self.__remoteFilesFound:
247 if QFileInfo(fileName).lastModified() < \ 257 if QFileInfo(fileName).lastModified() < \
248 self.__remoteFilesFound[self._remoteFiles[type_]]: 258 self.__remoteFilesFound[self._remoteFiles[type_]]:
249 self.__downloadFile(type_, fileName, 259 self.__downloadFile(
250 self.__remoteFilesFound[self._remoteFiles[type_]].toTime_t()) 260 type_, fileName,
261 self.__remoteFilesFound[self._remoteFiles[type_]]
262 .toTime_t())
251 else: 263 else:
252 self.syncStatus.emit(type_, self.trUtf8("No synchronization required.")) 264 self.syncStatus.emit(
265 type_, self.trUtf8("No synchronization required."))
253 self.syncFinished.emit(type_, True, True) 266 self.syncFinished.emit(type_, True, True)
254 else: 267 else:
255 if self._remoteFiles[type_] not in self.__remoteFilesFound: 268 if self._remoteFiles[type_] not in self.__remoteFilesFound:
256 self.syncStatus.emit(type_, self._messages[type_]["RemoteMissing"]) 269 self.syncStatus.emit(
270 type_, self._messages[type_]["RemoteMissing"])
257 else: 271 else:
258 self.syncStatus.emit(type_, self._messages[type_]["LocalNewer"]) 272 self.syncStatus.emit(
273 type_, self._messages[type_]["LocalNewer"])
259 self.__uploadFile(type_, fileName) 274 self.__uploadFile(type_, fileName)
260 275
261 def __initialSync(self): 276 def __initialSync(self):
262 """ 277 """
263 Private slot to do the initial synchronization. 278 Private slot to do the initial synchronization.
264 """ 279 """
265 # Bookmarks 280 # Bookmarks
266 if Preferences.getHelp("SyncBookmarks"): 281 if Preferences.getHelp("SyncBookmarks"):
267 self.__initialSyncFile("bookmarks", 282 self.__initialSyncFile(
268 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().getFileName()) 283 "bookmarks",
284 Helpviewer.HelpWindow.HelpWindow.bookmarksManager()
285 .getFileName())
269 286
270 # History 287 # History
271 if Preferences.getHelp("SyncHistory"): 288 if Preferences.getHelp("SyncHistory"):
272 self.__initialSyncFile("history", 289 self.__initialSyncFile(
273 Helpviewer.HelpWindow.HelpWindow.historyManager().getFileName()) 290 "history",
291 Helpviewer.HelpWindow.HelpWindow.historyManager()
292 .getFileName())
274 293
275 # Passwords 294 # Passwords
276 if Preferences.getHelp("SyncPasswords"): 295 if Preferences.getHelp("SyncPasswords"):
277 self.__initialSyncFile("passwords", 296 self.__initialSyncFile(
278 Helpviewer.HelpWindow.HelpWindow.passwordManager().getFileName()) 297 "passwords",
298 Helpviewer.HelpWindow.HelpWindow.passwordManager()
299 .getFileName())
279 300
280 # User Agent Settings 301 # User Agent Settings
281 if Preferences.getHelp("SyncUserAgents"): 302 if Preferences.getHelp("SyncUserAgents"):
282 self.__initialSyncFile("useragents", 303 self.__initialSyncFile(
283 Helpviewer.HelpWindow.HelpWindow.userAgentsManager().getFileName()) 304 "useragents",
305 Helpviewer.HelpWindow.HelpWindow.userAgentsManager()
306 .getFileName())
284 307
285 # Speed Dial Settings 308 # Speed Dial Settings
286 if Preferences.getHelp("SyncSpeedDial"): 309 if Preferences.getHelp("SyncSpeedDial"):
287 self.__initialSyncFile("speeddial", 310 self.__initialSyncFile(
311 "speeddial",
288 Helpviewer.HelpWindow.HelpWindow.speedDial().getFileName()) 312 Helpviewer.HelpWindow.HelpWindow.speedDial().getFileName())
289 313
290 self.__forceUpload = False 314 self.__forceUpload = False
291 315
292 def __syncFile(self, type_, fileName): 316 def __syncFile(self, type_, fileName):
293 """ 317 """
294 Private method to synchronize the given file. 318 Private method to synchronize the given file.
295 319
296 @param type_ type of the synchronization event (string one 320 @param type_ type of the synchronization event (string one
297 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 321 of "bookmarks", "history", "passwords", "useragents" or
322 "speeddial")
298 @param fileName name of the file to be synchronized (string) 323 @param fileName name of the file to be synchronized (string)
299 """ 324 """
300 if self.__state == "initializing": 325 if self.__state == "initializing":
301 return 326 return
302 327
304 if self.__connected: 329 if self.__connected:
305 self.__idleTimeout() 330 self.__idleTimeout()
306 if not self.__connected or self.__ftp.sock is None: 331 if not self.__connected or self.__ftp.sock is None:
307 ok = self.__connectAndLogin() 332 ok = self.__connectAndLogin()
308 if not ok: 333 if not ok:
309 self.syncStatus.emit(type_, self.trUtf8("Cannot log in to FTP host.")) 334 self.syncStatus.emit(
335 type_, self.trUtf8("Cannot log in to FTP host."))
310 return 336 return
311 337
312 # upload the changed file 338 # upload the changed file
313 self.__state = "uploading" 339 self.__state = "uploading"
314 self.syncStatus.emit(type_, self._messages[type_]["Uploading"]) 340 self.syncStatus.emit(type_, self._messages[type_]["Uploading"])
315 if self.__uploadFile(type_, fileName): 341 if self.__uploadFile(type_, fileName):
316 self.syncStatus.emit(type_, self.trUtf8("Synchronization finished.")) 342 self.syncStatus.emit(
343 type_, self.trUtf8("Synchronization finished."))
317 self.__state = "idle" 344 self.__state = "idle"
318 345
319 def syncBookmarks(self): 346 def syncBookmarks(self):
320 """ 347 """
321 Public method to synchronize the bookmarks. 348 Public method to synchronize the bookmarks.

eric ide

mercurial