def deleteFile(self, path, name): file = QFile(path + name + ".tmp") print(file.fileName()) if file.exists(): print(file.remove()) file.setFileName(path + name + ".tmp.cfg") if file.exists(): print(file.remove())
def backupSavedSessions(self): if not QFile.exists(self._lastActiveSessionPath): return if QFile.exists(self._firstBackupSession): QFile.remove(self._secondBackupSession) QFile.copy(self._firstBackupSession, self._secondBackupSession) QFile.remove(self._firstBackupSession) QFile.copy(self._lastActiveSessionPath, self._firstBackupSession)
def _sessionMetaData(self, withBackups=True): ''' @brief: return all session meta info list with backup sessions. @return: QList<SessionMetaData> ''' self._fillSessionsMetaDataListIfNeeded() out = self._sessionsMetaDataList[:] if withBackups and QFile.exists(self._firstBackupSession): data = self.SessionMetaData() data.name = _('Backup 1') data.filePath = self._firstBackupSession data.isBackup = True out.append(data) if withBackups and QFile.exists(self._secondBackupSession): data = self.SessionMetaData() data.name = _('Backup 2') data.filePath = self._secondBackupSession data.isBackup = True out.append(data) return out
def load(self): if not QFile.exists(self.filename): print ('-------file not exist : ',self.filename) return None fh = QFile(self.filename) if not fh.open(QFile.ReadOnly): print ('echec ouverture fichier') return None data = fh.readAll() codec = QTextCodec.codecForHtml(data) unistr = codec.toUnicode(data) if QtCore.Qt.mightBeRichText(unistr): self.setHtml(unistr) else: self.setPlainText(unistr)
def _saveSession(self): ''' @note: not used yet ''' sessionName, ok = QInputDialog.getText(gVar.app.getWindow(), _('Save Session'), _('Please enter a name to save session:'), QLineEdit.Normal, _('Saved Session (%s)') % QDateTime.currentDateTime().toString('yyyy MM dd HH-mm-ss')) if not ok: return filePath = '%s/%s.dat' % (DataPaths.path(DataPaths.Sessions), sessionName) if QFile.exists(filePath): QMessageBox.information(gVar.app.activeWindow(), _('Error!'), _('The session file "%s" exists. Please enter another name.') % sessionName) self._saveSession() return self.writeCurrentSession(filePath)
def _finished(self): self._progress(100) # File scheme watcher if self.url().scheme() == 'file': info = QFileInfo(self.url().toLocalFile()) if info.isFile(): if not self._fileWatcher: self._fileWatcher = DelayedFileWatcher(self) self._fileWatcher.delayedFileChanged.connect(self._watchedFileChanged) filePath = self.url().toLocalFile() if QFile.exists(filePath) and filePath not in self._fileWatcher.files(): self._fileWatcher.addPath(filePath) elif self._fileWatcher and self._fileWatcher.files(): self._fileWatcher.removePathes(self._fileWatcher.files()) # AutoFill self._autoFillUsernames = gVar.app.autoFill().completePage(self, self.url())
def _renameSession(self, sessionFilePath='', flags=0): if not sessionFilePath: action = self.sender() if not isinstance(action, QAction): return sessionFilePath = action.data() suggestedName = QFileInfo(sessionFilePath).completeBaseName() + \ (flags & self.CloneSession) and _('_cloned') or _('_renamed') newName, ok = QInputDialog.getText(gVar.app.activeWindow(), (flags & self.CloneSession) and _('Clone Session') or _('Rename Session'), _('Please enter a new name:'), QLineEdit.Normal, suggestedName) if not ok: return newSessionPath = '%s/%s.dat' % (DataPaths.path(DataPaths.Sessions), newName) if QFile.exists(newSessionPath): QMessageBox.information(gVar.app.activeWindow(), _('Error!'), _('The session file "%s" exists. Please enter another name.') % newName) self._renameSession(sessionFilePath, flags) return if flags & self.CloneSession: if not QFile.copy(sessionFilePath, newSessionPath): QMessageBox.information(gVar.app.activeWindow(), _('Error!'), _('An error occurred when cloning session file.')) return else: if not QFile.rename(sessionFilePath, newSessionPath): QMessageBox.information(gVar.app.activeWindow(), _('Error!'), _('An error occurred when renaming session file.')) return if self._isActive(sessionFilePath): self._lastActiveSessionPath = newSessionPath self._sessionsMetaDataList.clear()
def _newSession(self): sessionName, ok = QInputDialog.getText(gVar.app.getWindow(), _('New Session'), _('Please enter a name to create new session:'), QLineEdit.Normal, _('New Session (%s)') % QDateTime.currentDateTime().toString('yyyy MM dd HH-mm-ss')) if not ok: return filePath = '%s/%s.dat' % (DataPaths.path(DataPaths.Sessions), sessionName) if QFile.exists(filePath): QMessageBox.information(gVar.app.activeWindow(), _('Error!'), _('The session file "%1" exists. Please enter another name.') % sessionName) self._newSession() return self.writeCurrentSession(self._lastActiveSessionPath) window = gVar.app.createWindow(const.BW_NewWindow) for win in gVar.app.windows(): if win != window: win.close() self._lastActiveSessionPath = filePath self.autoSaveLastSession()
def createFromFile(cls, fpath): ''' @param: fpath QString @return: data RestoreData ''' data = RestoreData() if not QFile.exists(fpath): return data recoveryFile = QFile(fpath) if not recoveryFile.open(QIODevice.ReadOnly): return data stream = QDataStream(recoveryFile) version = stream.readInt() if version == const.sessionVersion: byteData = stream.readBytes() data = pickle.loads(byteData) else: print('WARNING: Unsupported session file version', version, 'path', fpath) return data