Music-Library/action_runner.py
2024-04-15 13:21:43 +00:00

69 lines
2.2 KiB
Python

from PyQt6.QtWidgets import (
QDialog, QLabel, QFrame,
QVBoxLayout
)
from PyQt6.QtCore import Qt
import subprocess, traceback, threading
class RunningDialog(QDialog):
def __init__(self, parent):
super().__init__(parent)
self.hide()
self.setObjectName("runningdialog")
self.setWindowTitle("Running")
# always on top
self.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)
self.setFixedSize(600, 1)
# disable resizing
self.setFixedSize(self.size())
# disable close button
self.setWindowFlag(Qt.WindowType.WindowCloseButtonHint, False)
self.setWindowFlag(Qt.WindowType.WindowContextHelpButtonHint, False)
class ActionRunner:
def __init__(self, command: str, args: list[str] = []):
self.command = command
self.args = args
def run(self):
running_dialog = RunningDialog(None)
running_dialog.show()
running_dialog.setWindowTitle("Running: " + self.command + " " + " ".join(self.args) )
try:
process=subprocess.Popen(
" ".join([self.command] + self.args),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
)
output = process.stdout.readlines()
err = process.stderr.readlines()
if output == []:
print("Output:", output)
print("err:")
for e in err:
print(e.decode("utf-8"))
raise Exception("Error while invoking command: " + self.command + " " + " ".join(self.args))
lines = []
for line in output:
l = line.decode("utf-8")
lines.append(l)
result = "".join(lines)
# remove last newline
if result.endswith("\n"):
result = result[:-1]
running_dialog.close()
return result
except Exception as e:
running_dialog.close()
print("### ActionRunner Error ###")
print(e)
traceback.print_exc()
return None