Helpviewer/Sync/DirectorySyncHandler.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2847
1843ef6e2656
parent 3002
6ffc581f00f1
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
23 class DirectorySyncHandler(SyncHandler): 23 class DirectorySyncHandler(SyncHandler):
24 """ 24 """
25 Class implementing a synchronization handler using a shared directory. 25 Class implementing a synchronization handler using a shared directory.
26 26
27 @signal syncStatus(type_, message) emitted to indicate the synchronization 27 @signal syncStatus(type_, message) emitted to indicate the synchronization
28 status (string one of "bookmarks", "history", "passwords", "useragents" or 28 status (string one of "bookmarks", "history", "passwords",
29 "speeddial", string) 29 "useragents" or "speeddial", string)
30 @signal syncError(message) emitted for a general error with the error message (string) 30 @signal syncError(message) emitted for a general error with the error
31 @signal syncMessage(message) emitted to send a message about synchronization (string) 31 message (string)
32 @signal syncFinished(type_, done, download) emitted after a synchronization has 32 @signal syncMessage(message) emitted to send a message about
33 finished (string one of "bookmarks", "history", "passwords", "useragents" or 33 synchronization (string)
34 "speeddial", boolean, boolean) 34 @signal syncFinished(type_, done, download) emitted after a
35 synchronization has finished (string one of "bookmarks", "history",
36 "passwords", "useragents" or "speeddial", boolean, boolean)
35 """ 37 """
36 syncStatus = pyqtSignal(str, str) 38 syncStatus = pyqtSignal(str, str)
37 syncError = pyqtSignal(str) 39 syncError = pyqtSignal(str)
38 syncMessage = pyqtSignal(str) 40 syncMessage = pyqtSignal(str)
39 syncFinished = pyqtSignal(str, bool, bool) 41 syncFinished = pyqtSignal(str, bool, bool)
51 53
52 def initialLoadAndCheck(self, forceUpload): 54 def initialLoadAndCheck(self, forceUpload):
53 """ 55 """
54 Public method to do the initial check. 56 Public method to do the initial check.
55 57
56 @keyparam forceUpload flag indicating a forced upload of the files (boolean) 58 @keyparam forceUpload flag indicating a forced upload of the files
59 (boolean)
57 """ 60 """
58 if not Preferences.getHelp("SyncEnabled"): 61 if not Preferences.getHelp("SyncEnabled"):
59 return 62 return
60 63
61 self.__forceUpload = forceUpload 64 self.__forceUpload = forceUpload
62 65
63 self.__remoteFilesFound = [] 66 self.__remoteFilesFound = []
64 67
65 # check the existence of the shared directory; create it, if it is not there 68 # check the existence of the shared directory; create it, if it is
69 # not there
66 if not os.path.exists(Preferences.getHelp("SyncDirectoryPath")): 70 if not os.path.exists(Preferences.getHelp("SyncDirectoryPath")):
67 try: 71 try:
68 os.makedirs(Preferences.getHelp("SyncDirectoryPath")) 72 os.makedirs(Preferences.getHelp("SyncDirectoryPath"))
69 except OSError as err: 73 except OSError as err:
70 self.syncError.emit( 74 self.syncError.emit(
77 def __downloadFile(self, type_, fileName, timestamp): 81 def __downloadFile(self, type_, fileName, timestamp):
78 """ 82 """
79 Private method to downlaod the given file. 83 Private method to downlaod the given file.
80 84
81 @param type_ type of the synchronization event (string one 85 @param type_ type of the synchronization event (string one
82 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 86 of "bookmarks", "history", "passwords", "useragents" or
87 "speeddial")
83 @param fileName name of the file to be downloaded (string) 88 @param fileName name of the file to be downloaded (string)
84 @param timestamp time stamp in seconds of the file to be downloaded (int) 89 @param timestamp time stamp in seconds of the file to be downloaded
90 (integer)
85 """ 91 """
86 self.syncStatus.emit(type_, self._messages[type_]["RemoteExists"]) 92 self.syncStatus.emit(type_, self._messages[type_]["RemoteExists"])
87 try: 93 try:
88 f = open(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 94 f = open(os.path.join(Preferences.getHelp("SyncDirectoryPath"),
89 self._remoteFiles[type_]), "rb") 95 self._remoteFiles[type_]), "rb")
94 self.trUtf8("Cannot read remote file.\n{0}").format(str(err))) 100 self.trUtf8("Cannot read remote file.\n{0}").format(str(err)))
95 self.syncFinished.emit(type_, False, True) 101 self.syncFinished.emit(type_, False, True)
96 return 102 return
97 103
98 QCoreApplication.processEvents() 104 QCoreApplication.processEvents()
99 ok, error = self.writeFile(QByteArray(data), fileName, type_, timestamp) 105 ok, error = self.writeFile(QByteArray(data), fileName, type_,
106 timestamp)
100 if not ok: 107 if not ok:
101 self.syncStatus.emit(type_, error) 108 self.syncStatus.emit(type_, error)
102 self.syncFinished.emit(type_, ok, True) 109 self.syncFinished.emit(type_, ok, True)
103 110
104 def __uploadFile(self, type_, fileName): 111 def __uploadFile(self, type_, fileName):
105 """ 112 """
106 Private method to upload the given file. 113 Private method to upload the given file.
107 114
108 @param type_ type of the synchronization event (string one 115 @param type_ type of the synchronization event (string one
109 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 116 of "bookmarks", "history", "passwords", "useragents" or
117 "speeddial")
110 @param fileName name of the file to be uploaded (string) 118 @param fileName name of the file to be uploaded (string)
111 """ 119 """
112 QCoreApplication.processEvents() 120 QCoreApplication.processEvents()
113 data = self.readFile(fileName, type_) 121 data = self.readFile(fileName, type_)
114 if data.isEmpty(): 122 if data.isEmpty():
120 f = open(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 128 f = open(os.path.join(Preferences.getHelp("SyncDirectoryPath"),
121 self._remoteFiles[type_]), "wb") 129 self._remoteFiles[type_]), "wb")
122 f.write(bytes(data)) 130 f.write(bytes(data))
123 f.close() 131 f.close()
124 except IOError as err: 132 except IOError as err:
125 self.syncStatus.emit(type_, 133 self.syncStatus.emit(
126 self.trUtf8("Cannot write remote file.\n{0}").format(str(err))) 134 type_,
135 self.trUtf8("Cannot write remote file.\n{0}").format(
136 str(err)))
127 self.syncFinished.emit(type_, False, False) 137 self.syncFinished.emit(type_, False, False)
128 return 138 return
129 139
130 self.syncFinished.emit(type_, True, False) 140 self.syncFinished.emit(type_, True, False)
131 141
132 def __initialSyncFile(self, type_, fileName): 142 def __initialSyncFile(self, type_, fileName):
133 """ 143 """
134 Private method to do the initial synchronization of the given file. 144 Private method to do the initial synchronization of the given file.
135 145
136 @param type_ type of the synchronization event (string one 146 @param type_ type of the synchronization event (string one
137 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 147 of "bookmarks", "history", "passwords", "useragents" or
148 "speeddial")
138 @param fileName name of the file to be synchronized (string) 149 @param fileName name of the file to be synchronized (string)
139 """ 150 """
140 if not self.__forceUpload and \ 151 if not self.__forceUpload and \
141 os.path.exists(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 152 os.path.exists(
142 self._remoteFiles[type_])) and \ 153 os.path.join(Preferences.getHelp("SyncDirectoryPath"),
143 QFileInfo(fileName).lastModified() <= \ 154 self._remoteFiles[type_])) and \
144 QFileInfo(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 155 QFileInfo(fileName).lastModified() <= QFileInfo(
145 self._remoteFiles[type_])).lastModified(): 156 os.path.join(Preferences.getHelp("SyncDirectoryPath"),
157 self._remoteFiles[type_])).lastModified():
146 self.__downloadFile(type_, fileName, 158 self.__downloadFile(type_, fileName,
147 QFileInfo(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 159 QFileInfo(os.path.join(
160 Preferences.getHelp("SyncDirectoryPath"),
148 self._remoteFiles[type_])).lastModified().toTime_t()) 161 self._remoteFiles[type_])).lastModified().toTime_t())
149 else: 162 else:
150 if os.path.exists(os.path.join(Preferences.getHelp("SyncDirectoryPath"), 163 if os.path.exists(
151 self._remoteFiles[type_])): 164 os.path.join(Preferences.getHelp("SyncDirectoryPath"),
152 self.syncStatus.emit(type_, self._messages[type_]["RemoteMissing"]) 165 self._remoteFiles[type_])):
166 self.syncStatus.emit(
167 type_, self._messages[type_]["RemoteMissing"])
153 else: 168 else:
154 self.syncStatus.emit(type_, self._messages[type_]["LocalNewer"]) 169 self.syncStatus.emit(
170 type_, self._messages[type_]["LocalNewer"])
155 self.__uploadFile(type_, fileName) 171 self.__uploadFile(type_, fileName)
156 172
157 def __initialSync(self): 173 def __initialSync(self):
158 """ 174 """
159 Private slot to do the initial synchronization. 175 Private slot to do the initial synchronization.
160 """ 176 """
161 QCoreApplication.processEvents() 177 QCoreApplication.processEvents()
162 # Bookmarks 178 # Bookmarks
163 if Preferences.getHelp("SyncBookmarks"): 179 if Preferences.getHelp("SyncBookmarks"):
164 self.__initialSyncFile("bookmarks", 180 self.__initialSyncFile(
165 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().getFileName()) 181 "bookmarks",
182 Helpviewer.HelpWindow.HelpWindow.bookmarksManager()\
183 .getFileName())
166 184
167 QCoreApplication.processEvents() 185 QCoreApplication.processEvents()
168 # History 186 # History
169 if Preferences.getHelp("SyncHistory"): 187 if Preferences.getHelp("SyncHistory"):
170 self.__initialSyncFile("history", 188 self.__initialSyncFile(
171 Helpviewer.HelpWindow.HelpWindow.historyManager().getFileName()) 189 "history",
190 Helpviewer.HelpWindow.HelpWindow.historyManager()\
191 .getFileName())
172 192
173 QCoreApplication.processEvents() 193 QCoreApplication.processEvents()
174 # Passwords 194 # Passwords
175 if Preferences.getHelp("SyncPasswords"): 195 if Preferences.getHelp("SyncPasswords"):
176 self.__initialSyncFile("passwords", 196 self.__initialSyncFile(
177 Helpviewer.HelpWindow.HelpWindow.passwordManager().getFileName()) 197 "passwords",
198 Helpviewer.HelpWindow.HelpWindow.passwordManager()
199 .getFileName())
178 200
179 QCoreApplication.processEvents() 201 QCoreApplication.processEvents()
180 # User Agent Settings 202 # User Agent Settings
181 if Preferences.getHelp("SyncUserAgents"): 203 if Preferences.getHelp("SyncUserAgents"):
182 self.__initialSyncFile("useragents", 204 self.__initialSyncFile(
183 Helpviewer.HelpWindow.HelpWindow.userAgentsManager().getFileName()) 205 "useragents",
206 Helpviewer.HelpWindow.HelpWindow.userAgentsManager()
207 .getFileName())
184 208
185 QCoreApplication.processEvents() 209 QCoreApplication.processEvents()
186 # Speed Dial Settings 210 # Speed Dial Settings
187 if Preferences.getHelp("SyncSpeedDial"): 211 if Preferences.getHelp("SyncSpeedDial"):
188 self.__initialSyncFile("speeddial", 212 self.__initialSyncFile("speeddial",
194 def __syncFile(self, type_, fileName): 218 def __syncFile(self, type_, fileName):
195 """ 219 """
196 Private method to synchronize the given file. 220 Private method to synchronize the given file.
197 221
198 @param type_ type of the synchronization event (string one 222 @param type_ type of the synchronization event (string one
199 of "bookmarks", "history", "passwords", "useragents" or "speeddial") 223 of "bookmarks", "history", "passwords", "useragents" or
224 "speeddial")
200 @param fileName name of the file to be synchronized (string) 225 @param fileName name of the file to be synchronized (string)
201 """ 226 """
202 self.syncStatus.emit(type_, self._messages[type_]["Uploading"]) 227 self.syncStatus.emit(type_, self._messages[type_]["Uploading"])
203 self.__uploadFile(type_, fileName) 228 self.__uploadFile(type_, fileName)
204 229

eric ide

mercurial