def validate_player(ctx, param, value): if not value: return value file_info = QFileInfo(value) if file_info and file_info.isExecutable(): return file_info.filePath() file_info = QFileInfo(shutil.which(value)) if file_info and file_info.isExecutable(): return file_info.filePath() if is_windows(): from contextlib import suppress from pathlib import Path import winreg import itertools filename = Path(value.lower()).stem with suppress(WindowsError), winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths", ) as k: for i in itertools.count(): subkey = winreg.EnumKey(k, i) if Path(subkey.lower()).stem == filename: with winreg.OpenKey(k, subkey) as filekey: val, _ = winreg.QueryValueEx(filekey, None) file_info = QFileInfo(val) if file_info and file_info.isExecutable(): return file_info.filePath() raise click.BadParameter("Player must an executable")
def on_btnInfo_isExec_clicked(self): self.__showBtnInfo(self.sender()) fileInfo = QFileInfo(self.ui.editFile.text()) if fileInfo.isExecutable(): self.ui.textEdit.appendPlainText("True \n") else: self.ui.textEdit.appendPlainText("False \n")
def accept(self): if self.txtName.text().strip() == "": QMessageBox.information(self, self.windowTitle(), self.tr("请填写快捷方式的名称。")) self.txtName.setFocus(Qt.OtherFocusReason) return path = self.txtPath.text().strip() if path == "": QMessageBox.information(self, self.windowTitle(), self.tr("请填写目标文件/程序。")) self.txtPath.setFocus(Qt.OtherFocusReason) self.txtPath.selectAll() return if not os.path.exists(path): QMessageBox.information(self, self.windowTitle(), self.tr("目标文件/程序不存在。")) self.txtPath.setFocus(Qt.OtherFocusReason) self.txtPath.selectAll() return openwith = self.txtOpenwith.text().strip() if openwith != "": if not os.path.exists(openwith): QMessageBox.information( self, self.windowTitle(), self.tr("编辑程序不存在。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setFocus(Qt.OtherFocusReason) self.txtOpenwith.selectAll() return fi = QFileInfo(openwith) if not fi.isExecutable(): QMessageBox.information( self, self.windowTitle(), self.tr("编辑程序必须是一个可执行文件。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setFocus(Qt.OtherFocusReason) self.txtOpenwith.selectAll() return dir = self.txtDir.text().strip() if dir == "": QMessageBox.information( self, self.windowTitle(), self.tr("请填写运行目录。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return if not os.path.exists(dir): QMessageBox.information( self, self.windowTitle(), self.tr("运行目录不存在。请重新选择。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return if not os.path.isdir(dir): QMessageBox.information( self, self.windowTitle(), self.tr("运行目录必须是一个目录,而非文件。请重新选择。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return QDialog.accept(self)
def ask_executable(caption, parent=None, starting_path=''): (chosen_path, _) = QFileDialog.getOpenFileName(parent, caption, starting_path, '') if len(chosen_path) == 0: # user pressed Cancel return '' qfi = QFileInfo(chosen_path) if not qfi.isExecutable(): return None return chosen_path
def accept(self): if self.txtName.text().strip() == "": QMessageBox.information(self, self.windowTitle(), self.tr("请填写快捷方式的名称。")) self.txtName.setFocus(Qt.OtherFocusReason) return path = self.txtPath.text().strip() if path == "": QMessageBox.information(self, self.windowTitle(), self.tr("请填写目标文件/程序。")) self.txtPath.setFocus(Qt.OtherFocusReason) self.txtPath.selectAll() return if not os.path.exists(path): QMessageBox.information(self, self.windowTitle(), self.tr("目标文件/程序不存在。")) self.txtPath.setFocus(Qt.OtherFocusReason) self.txtPath.selectAll() return openwith = self.txtOpenwith.text().strip() if openwith != "": if not os.path.exists(openwith): QMessageBox.information(self, self.windowTitle(), self.tr("编辑程序不存在。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setFocus(Qt.OtherFocusReason) self.txtOpenwith.selectAll() return fi = QFileInfo(openwith) if not fi.isExecutable(): QMessageBox.information(self, self.windowTitle(), self.tr("编辑程序必须是一个可执行文件。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setFocus(Qt.OtherFocusReason) self.txtOpenwith.selectAll() return dir = self.txtDir.text().strip() if dir == "": QMessageBox.information(self, self.windowTitle(), self.tr("请填写运行目录。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return if not os.path.exists(dir): QMessageBox.information(self, self.windowTitle(), self.tr("运行目录不存在。请重新选择。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return if not os.path.isdir(dir): QMessageBox.information(self, self.windowTitle(), self.tr("运行目录必须是一个目录,而非文件。请重新选择。可以使用“默认运行目录”按钮恢复默认的运行目录。")) self.txtDir.setFocus(Qt.OtherFocusReason) self.txtDir.selectAll() return QDialog.accept(self)
def ask_executable(caption, parent=None, starting_path=''): (chosen_path, _) = QFileDialog.getOpenFileName( parent, caption, starting_path,'' ) if len(chosen_path) == 0 : # user pressed Cancel return '' qfi = QFileInfo(chosen_path) if not qfi.isExecutable() : return None return chosen_path
def browseOpenwith(self): filename, selectedFilter = QFileDialog.getOpenFileName(self, self.windowTitle()) if not filename: return fi = QFileInfo(filename) if fi.isSymLink(): filename = fi.symLinkTarget() if not os.path.exists(filename): QMessageBox.information(self, self.windowTitle(), self.tr("快捷方式所指向的程序不正确。")) return fi = QFileInfo(filename) if not fi.isExecutable(): QMessageBox.information(self, self.windowTitle(), self.tr("编辑程序必须是一个可执行文件。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setText(fi.absoluteFilePath())
def browseOpenwith(self): filename, selectedFilter = QFileDialog.getOpenFileName( self, self.windowTitle()) if not filename: return fi = QFileInfo(filename) if fi.isSymLink(): filename = fi.symLinkTarget() if not os.path.exists(filename): QMessageBox.information(self, self.windowTitle(), self.tr("快捷方式所指向的程序不正确。")) return fi = QFileInfo(filename) if not fi.isExecutable(): QMessageBox.information( self, self.windowTitle(), self.tr("编辑程序必须是一个可执行文件。请重新选择。该选项是选填项,并不一定要填写。")) self.txtOpenwith.setText(fi.absoluteFilePath())
def select_new_player(self): logger.info("Selecting new player") if self.settings_widget.dlg_select_player.exec_() == QDialog.Accepted: files = self.settings_widget.dlg_select_player.selectedFiles() if len(files) > 0: file_path = files[0] file_info = QFileInfo(file_path) if not file_info or not file_info.isExecutable(): logger.error("Bad file selected: {}".format(file_path)) message_box( title="Invalid file", text="File must be an executable binary", icon=QMessageBox.Warning, buttons=QMessageBox.Ok, ) else: logger.info("Selected file: {}".format(file_path)) self.local_player.set_player(file_path) self.update_player_info()
def getMetadataForFileList(filenames): '''Takes a list of filenames, returns a list of metadata associated with all files in that list that are readable tracks''' metadata = [] for filename in filenames: info = QFileInfo(filename) if info.isDir() and info.isExecutable(): print(filename) dir = QDir(filename) print(dir.entryList(QDir.AllEntries | QDir.NoDotAndDotDot)) metadata.extend( getMetadataForFileList([ i.filePath() for i in dir.entryInfoList(QDir.AllEntries | QDir.NoDotAndDotDot) ])) elif info.isFile() and info.isReadable(): print(filename) metadata.extend(processFile(filename)) return metadata