def outputIndexAs(self): widget = QApplication.focusWidget() extensions = [] for extension, desc in EXPORT_EXTENSIONS.items(): extensions.append("{} (*{})".format(desc, extension)) with Lib.Qt.DisableUI(*self.window.widgets(), forModalDialog=True): form = QFileDialog( self.window, "Output As — {}".format(QApplication.applicationName())) form.setNameFilters(extensions) form.setAcceptMode(QFileDialog.AcceptSave) form.setDirectory(self.state.outputPath) form.selectFile(str(pathlib.Path(self.state.model.filename).stem)) if form.exec_(): filename = form.selectedFiles()[0] extension = form.selectedNameFilter() if filename: # Must have some extension if not re.match(r"^.*[.].+$", filename): if extension: filename += EXTENSION_EXTRACT_RX.sub( r"\1", extension) else: filename += ".rtf" self._outputIndex(filename, widget) Lib.restoreFocus(widget)
def choosePackages(parent=None, caption="", directory="", selectMode="single"): dialog = QFileDialog(parent, caption, osp.normcase(directory)) dialog.setFileMode(QFileDialog.Directory) dialog.setFilter(QDir.NoDotAndDotDot | QDir.Dirs)# | QDir.Files) dialog.setNameFilters(["Packages ( pkg_* lyr_* )"]) dialog.setOption(QFileDialog.ReadOnly) bMultiSelect = (selectMode == "multi") treeView = dialog.findChild(QTreeView) if bMultiSelect: treeView.setSelectionMode(QAbstractItemView.ExtendedSelection) if bMultiSelect: listView = dialog.itemDelegate().parent() listView.setSelectionMode(QAbstractItemView.ExtendedSelection) treeView.model().setNameFilterDisables(True) proxyModel = PackSortProxyModel(dialog) dialog.setProxyModel(proxyModel) res = dialog.exec_() if res and treeView.selectionModel().hasSelection(): sDirList = dialog.selectedFiles() try: sDirList.remove(dialog.directory().absolutePath()) except ValueError: pass return sDirList if bMultiSelect else sDirList[0] return [] if bMultiSelect else ""
def restore_backup(self): self._clear_log() self._log("Restore procedure started") url = self.url_edit.text() psql_path = configuration.get("Commands", "psql") if not psql_path: self._log_error( "The Commands/psql path is not set in the server.cfg") self._log("Please fix the configuration file (on the right)") return if not configuration.get("Commands", "pg_restore"): self._log_error( "The Commands/pg_restore path is not set in the server.cfg") self._log("Please fix the configuration file (on the right)") return if not configuration.get("Backup", "backup_directory"): self._log( "The Backup/backup_directory path is not set in the server.cfg" ) self._log("I'm setting it myself.") configuration.set("Backup", "backup_directory", get_data_dir()) configuration.set("DocumentsDatabase", "documents_root", os.path.join(get_data_dir(), "documents")) configuration.save() self.edit_config.load_configuration() login_clt, password_clt, dummy, dummy, dummy = self._extract_db_params_from_url( configuration.get("Database", "url")) login_adm, password_adm, dbname, host, port = self._extract_db_params_from_url( configuration.get("Database", "admin_url")) self._log("{} / {}".format(login_adm, password_adm)) full_path_backup = None d = "" if configuration.get("Backup", "backup_directory"): d = configuration.get("Backup", "backup_directory") if platform.system() == "Windows": if configuration.get("Backup", "backup_directory"): d = configuration.get("Backup", "backup_directory") # Using the static method gives a more native FileDialog. # with support for network backup_file = QFileDialog.getOpenFileName( self, _("Please select a backup file"), d, "{} database backup (*.pgbackup)".format( configuration.get("Globals", "name")))[0] if not backup_file: self._log("Restore aborted") return full_path_backup = backup_file if not os.path.isdir(full_path_backup): self._log( "{} is not a directory, so I'll go up a level".format( full_path_backup)) full_path_backup = os.path.dirname(full_path_backup) if not os.path.isdir(full_path_backup): self._log_error( "{} is not a directory either. Aborting restore.". format(full_path_backup)) return elif platform.system() == "Linux": d = AskWindowsShare(None) d.exec_() if d.result() == QDialog.Accepted: # //192.168.0.6/postgresqlbackup script_path = "/tmp/horse_mount.sh" script = open(script_path, "w") script.write("""#!/bin/bash echo "Creating transfer directory" mkdir /tmp/backup_win echo "Unmounting previous transfer directory (can fail)" umount /tmp/backup_win echo "Mouting the backup directory" mount -t cifs -ousername={},password={} {} /tmp/backup_win """.format(d.user.text().strip(), d.password.text().strip(), d.address.text().strip())) script.close() import stat os.chmod(script_path, stat.S_IEXEC | stat.S_IWRITE | stat.S_IREAD) cmd = [ 'gksudo', '--sudo-mode', '--message', 'Allow Koi to connect to the backup server.', script_path ] # gksudo seems to like to have the DISPLAY set. So I basically copy # it from the calling environment. ret, dummy, dummy = self._run_shell( cmd, {'DISPLAY': os.environ.get('DISPLAY')}) if ret > 0: self._log_error( "The mount operation failed. Please review the parameters you've given." ) self._log_error( "Network address : {}, windows user name : {}".format( d.address.text() or "?", d.user.text() or "?")) return full_path_backup = "/tmp/backup_win" else: dialog = QFileDialog(self) dialog.setFileMode(QFileDialog.Directory) dialog.setNameFilters(['Koi database backup (*.pgbackup)']) dialog.setWindowTitle("Please select a backup file") if configuration.get("Backup", "backup_directory"): dialog.setDirectory( configuration.get("Backup", "backup_directory")) if dialog.exec_(): full_path_backup = dialog.selectedFiles()[0] else: self._log_error( "Without proper source directory, I can't continue !") return else: self._log_error("Unsupported operating system") # At this poitn full_path_backup is the path to the backup # directory of Horse that we want to restore. # It is different than the current backup directory. if full_path_backup: full_restore(configuration, full_path_backup, backup_file, True, mainlog) self._log_success("Backup successfully restored !")