qtwrap 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qtwrap/Choice.py +31 -0
- qtwrap/ScriptCaptureWindow.py +108 -0
- qtwrap/StatusBar.py +14 -0
- qtwrap/__init__.py +3 -0
- qtwrap/enums/LogLevel.py +6 -0
- qtwrap/enums/__init__.py +1 -0
- qtwrap/path/BrowsingSelector.py +40 -0
- qtwrap/path/DirectorySelector.py +20 -0
- qtwrap/path/FileSelector.py +22 -0
- qtwrap/path/__init__.py +2 -0
- qtwrap/utils/OutputCaptureWorker.py +32 -0
- qtwrap/utils/StreamRedirector.py +11 -0
- qtwrap/utils/__init__.py +4 -0
- qtwrap/utils/commons.py +22 -0
- qtwrap-1.0.0.dist-info/METADATA +20 -0
- qtwrap-1.0.0.dist-info/RECORD +18 -0
- qtwrap-1.0.0.dist-info/WHEEL +4 -0
- qtwrap-1.0.0.dist-info/licenses/LICENSE +21 -0
qtwrap/Choice.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QButtonGroup, QLabel, QRadioButton
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Choice(QWidget):
|
|
5
|
+
def __init__(self, *options, title: str = "Choice:", inline: bool = True, exclusive: bool = True):
|
|
6
|
+
super().__init__()
|
|
7
|
+
|
|
8
|
+
self.__parent_layout = QVBoxLayout()
|
|
9
|
+
self.__child_layout = QHBoxLayout() if inline else QVBoxLayout()
|
|
10
|
+
self.__child_widget = QWidget()
|
|
11
|
+
|
|
12
|
+
self.__title = QLabel(title)
|
|
13
|
+
self.__btn_group = QButtonGroup()
|
|
14
|
+
self.__btn_group.setExclusive(exclusive)
|
|
15
|
+
|
|
16
|
+
self.add_options(*options)
|
|
17
|
+
self.__child_widget.setLayout(self.__child_layout)
|
|
18
|
+
|
|
19
|
+
self.__parent_layout.addWidget(self.__title)
|
|
20
|
+
self.__parent_layout.addWidget(self.__child_widget)
|
|
21
|
+
self.setLayout(self.__parent_layout)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def add_options(self, *options):
|
|
25
|
+
for option in options:
|
|
26
|
+
btn = QRadioButton(option)
|
|
27
|
+
self.__btn_group.addButton(btn)
|
|
28
|
+
self.__child_layout.addWidget(btn)
|
|
29
|
+
|
|
30
|
+
def choice(self) -> list[str]:
|
|
31
|
+
return [btn.text() for btn in self.__btn_group.buttons() if btn.isChecked()]
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from typing import cast
|
|
3
|
+
|
|
4
|
+
from PyQt6.QtCore import QThread, Qt
|
|
5
|
+
from PyQt6.QtGui import QColor
|
|
6
|
+
from PyQt6.QtWidgets import QMainWindow, QTextEdit, QWidget, QMenuBar, QFileDialog
|
|
7
|
+
|
|
8
|
+
from . import StatusBar
|
|
9
|
+
from .enums import LogLevel
|
|
10
|
+
from .utils import OutputCaptureWorker, log_file_path, StreamRedirector
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ScriptCaptureWindow(QMainWindow):
|
|
14
|
+
def __init__(self, func):
|
|
15
|
+
super().__init__()
|
|
16
|
+
|
|
17
|
+
self.__func = func
|
|
18
|
+
self.__thread = None
|
|
19
|
+
self.__worker = None
|
|
20
|
+
self.__output = None
|
|
21
|
+
self.__old_stdout = None
|
|
22
|
+
|
|
23
|
+
self.__redirector = StreamRedirector()
|
|
24
|
+
self.__redirector.text_written.connect(
|
|
25
|
+
self.__append_text, type=Qt.ConnectionType.QueuedConnection
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
self.__status_bar = StatusBar()
|
|
29
|
+
self.setStatusBar(self.__status_bar)
|
|
30
|
+
|
|
31
|
+
self.__menu_bar = QMenuBar()
|
|
32
|
+
self.setMenuBar(self.__menu_bar)
|
|
33
|
+
|
|
34
|
+
self.__save_menu = self.__menu_bar.addMenu("&Save")
|
|
35
|
+
self.__save_menu.addAction("Save Output", self.__save_output)
|
|
36
|
+
|
|
37
|
+
def _started(self, args: tuple = (), kwargs: dict | None = None):
|
|
38
|
+
if kwargs is None:
|
|
39
|
+
kwargs = {}
|
|
40
|
+
if self.__output is None: raise Exception("The output field must be set.")
|
|
41
|
+
if self.__thread is not None and self.__thread.isRunning():
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
self.__status_bar.clearMessage()
|
|
45
|
+
|
|
46
|
+
self.__output.clear()
|
|
47
|
+
self.__set_inputs_enabled(False)
|
|
48
|
+
self.__output.setTextColor(QColor(*LogLevel.INFO.value))
|
|
49
|
+
|
|
50
|
+
self.__old_stdout = sys.stdout
|
|
51
|
+
sys.stdout = self.__redirector
|
|
52
|
+
|
|
53
|
+
self.__thread = QThread()
|
|
54
|
+
self.__worker = OutputCaptureWorker(self.__func, args, kwargs)
|
|
55
|
+
self.__worker.moveToThread(self.__thread)
|
|
56
|
+
|
|
57
|
+
self.__thread.started.connect(self.__worker.run)
|
|
58
|
+
self.__worker.error_signal.connect(
|
|
59
|
+
lambda text: self.__append_text(text=text, level=LogLevel.ERROR),
|
|
60
|
+
type=Qt.ConnectionType.QueuedConnection,
|
|
61
|
+
)
|
|
62
|
+
self.__worker.finished_signal.connect(self.__finished)
|
|
63
|
+
|
|
64
|
+
self.__thread.start()
|
|
65
|
+
|
|
66
|
+
def __finished(self):
|
|
67
|
+
sys.stdout = self.__old_stdout
|
|
68
|
+
self.__thread.quit()
|
|
69
|
+
self.__thread.wait()
|
|
70
|
+
self.__worker.deleteLater()
|
|
71
|
+
self.__thread.deleteLater()
|
|
72
|
+
self.__worker = None
|
|
73
|
+
self.__thread = None
|
|
74
|
+
self.__set_inputs_enabled(True)
|
|
75
|
+
|
|
76
|
+
def __append_text(self, text: str, level: LogLevel = LogLevel.INFO):
|
|
77
|
+
if text and self.__output:
|
|
78
|
+
self.__output.setTextColor(QColor(*level.value))
|
|
79
|
+
self.__output.insertPlainText(text)
|
|
80
|
+
self.__output.ensureCursorVisible()
|
|
81
|
+
|
|
82
|
+
if level == LogLevel.ERROR:
|
|
83
|
+
self.status(msg=f"Script Error: \"{text.split(chr(10))[-2]}\"", level=level)
|
|
84
|
+
|
|
85
|
+
def __set_inputs_enabled(self, enabled: bool = True):
|
|
86
|
+
central = self.centralWidget()
|
|
87
|
+
if central is None:
|
|
88
|
+
return
|
|
89
|
+
for obj in central.findChildren(QWidget):
|
|
90
|
+
widget = cast(QWidget, obj)
|
|
91
|
+
if widget is not self.__output:
|
|
92
|
+
widget.setEnabled(enabled)
|
|
93
|
+
|
|
94
|
+
def __save_output(self):
|
|
95
|
+
file_path, _ = QFileDialog.getSaveFileName(self, 'Save Script Output', log_file_path())
|
|
96
|
+
if file_path:
|
|
97
|
+
with open(file_path, 'w') as save_file:
|
|
98
|
+
save_file.write(self.__output.toPlainText())
|
|
99
|
+
|
|
100
|
+
def get_output_field(self) -> QTextEdit:
|
|
101
|
+
return self.__output
|
|
102
|
+
|
|
103
|
+
def set_output_field(self, output_field: QTextEdit):
|
|
104
|
+
self.__output = output_field
|
|
105
|
+
self.__output.setReadOnly(True)
|
|
106
|
+
|
|
107
|
+
def status(self, msg: str, level: LogLevel = LogLevel.INFO):
|
|
108
|
+
self.__status_bar.log_status(msg, level)
|
qtwrap/StatusBar.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from PyQt6.QtWidgets import QStatusBar
|
|
2
|
+
|
|
3
|
+
from .enums import LogLevel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class StatusBar(QStatusBar):
|
|
7
|
+
def __init__(self):
|
|
8
|
+
super().__init__()
|
|
9
|
+
|
|
10
|
+
def log_status(self, status: str, level: LogLevel = LogLevel.INFO):
|
|
11
|
+
self.setStyleSheet(f"""
|
|
12
|
+
color: rgb{str(level.value)}
|
|
13
|
+
""")
|
|
14
|
+
self.showMessage(status)
|
qtwrap/__init__.py
ADDED
qtwrap/enums/LogLevel.py
ADDED
qtwrap/enums/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .LogLevel import LogLevel
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from PyQt6.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QSizePolicy, QHBoxLayout, QVBoxLayout
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class _BrowsingSelector(QWidget):
|
|
5
|
+
def __init__(self, field_label, btn_label, inline):
|
|
6
|
+
if type(self) is _BrowsingSelector: raise Exception("Class _BrowsingSelector can't be instantiated.")
|
|
7
|
+
super().__init__()
|
|
8
|
+
|
|
9
|
+
self._label = QLabel(field_label)
|
|
10
|
+
self._path_input = QLineEdit()
|
|
11
|
+
self._browse_btn = QPushButton(btn_label)
|
|
12
|
+
|
|
13
|
+
self._label.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
|
|
14
|
+
self._browse_btn.clicked.connect(self._selector_dialog)
|
|
15
|
+
|
|
16
|
+
if inline:
|
|
17
|
+
layout = QHBoxLayout()
|
|
18
|
+
layout.addWidget(self._label)
|
|
19
|
+
layout.addWidget(self._path_input, 1)
|
|
20
|
+
layout.addWidget(self._browse_btn)
|
|
21
|
+
else:
|
|
22
|
+
layout = QVBoxLayout()
|
|
23
|
+
top_row = QHBoxLayout()
|
|
24
|
+
top_row.addWidget(self._label)
|
|
25
|
+
top_row.addStretch(1)
|
|
26
|
+
|
|
27
|
+
bottom_row = QHBoxLayout()
|
|
28
|
+
bottom_row.addWidget(self._path_input, 1)
|
|
29
|
+
bottom_row.addWidget(self._browse_btn)
|
|
30
|
+
|
|
31
|
+
layout.addLayout(top_row)
|
|
32
|
+
layout.addLayout(bottom_row)
|
|
33
|
+
|
|
34
|
+
self.setLayout(layout)
|
|
35
|
+
|
|
36
|
+
def _selector_dialog(self) -> None:
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
def path(self) -> str:
|
|
40
|
+
return self._path_input.text()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from PyQt6.QtCore import QDir
|
|
4
|
+
from PyQt6.QtWidgets import QFileDialog
|
|
5
|
+
|
|
6
|
+
from .BrowsingSelector import _BrowsingSelector
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DirectorySelector(_BrowsingSelector):
|
|
10
|
+
def __init__(self, field_label: str = "Select Directory:", btn_label: str = "Browse", inline: bool = True):
|
|
11
|
+
super().__init__(field_label=field_label, btn_label=btn_label, inline=inline)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _selector_dialog(self):
|
|
15
|
+
dir_path = QFileDialog.getExistingDirectory(
|
|
16
|
+
self,
|
|
17
|
+
self._label.text(),
|
|
18
|
+
QDir.currentPath()
|
|
19
|
+
)
|
|
20
|
+
if dir_path: self._path_input.setText(str(Path(dir_path)))
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from PyQt6.QtCore import QDir
|
|
4
|
+
from PyQt6.QtWidgets import QFileDialog
|
|
5
|
+
|
|
6
|
+
from .BrowsingSelector import _BrowsingSelector
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class FileSelector(_BrowsingSelector):
|
|
10
|
+
def __init__(self, field_label: str = "Select File:", btn_label: str = "Browse", inline: bool = True, file_filter: str = '*'):
|
|
11
|
+
super().__init__(field_label=field_label, btn_label=btn_label, inline=inline)
|
|
12
|
+
self.__file_filter = file_filter
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _selector_dialog(self):
|
|
16
|
+
file_path, _ = QFileDialog.getOpenFileName(
|
|
17
|
+
self,
|
|
18
|
+
self._label.text(),
|
|
19
|
+
QDir.currentPath(),
|
|
20
|
+
self.__file_filter
|
|
21
|
+
)
|
|
22
|
+
if file_path: self._path_input.setText(str(Path(file_path)))
|
qtwrap/path/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import threading
|
|
3
|
+
import traceback
|
|
4
|
+
|
|
5
|
+
from PyQt6.QtCore import QObject, pyqtSignal
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class OutputCaptureWorker(QObject):
|
|
9
|
+
finished_signal = pyqtSignal()
|
|
10
|
+
error_signal = pyqtSignal(str)
|
|
11
|
+
|
|
12
|
+
def __init__(self, func, args: tuple, kwargs: dict):
|
|
13
|
+
super().__init__()
|
|
14
|
+
self.__func = func
|
|
15
|
+
self.__args = args
|
|
16
|
+
self.__kwargs = kwargs
|
|
17
|
+
|
|
18
|
+
def __thread_excepthook(self, args: threading.ExceptHookArgs):
|
|
19
|
+
tb = "".join(traceback.format_exception(args.exc_type, args.exc_value, args.exc_traceback))
|
|
20
|
+
thread_name = args.thread.name if args.thread else "unknown thread"
|
|
21
|
+
self.error_signal.emit(f"Exception in {thread_name}:\n{tb}")
|
|
22
|
+
|
|
23
|
+
def run(self):
|
|
24
|
+
old_excepthook = threading.excepthook
|
|
25
|
+
threading.excepthook = self.__thread_excepthook
|
|
26
|
+
try:
|
|
27
|
+
self.__func(*self.__args, **self.__kwargs)
|
|
28
|
+
except Exception:
|
|
29
|
+
self.error_signal.emit(traceback.format_exc())
|
|
30
|
+
finally:
|
|
31
|
+
threading.excepthook = old_excepthook
|
|
32
|
+
self.finished_signal.emit()
|
qtwrap/utils/__init__.py
ADDED
qtwrap/utils/commons.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
|
|
4
|
+
from PyQt6.QtCore import QDir
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def script_name() -> str:
|
|
8
|
+
return (
|
|
9
|
+
sys.argv[0]
|
|
10
|
+
.replace('\\', '/')
|
|
11
|
+
.split('/')[-1]
|
|
12
|
+
.split('.')[0]
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
def timestamp() -> str:
|
|
16
|
+
return datetime.now().strftime('%Y%m%d-%H%M%S')
|
|
17
|
+
|
|
18
|
+
def log_file_name() -> str:
|
|
19
|
+
return '_'.join([script_name(), timestamp()]) + ".log"
|
|
20
|
+
|
|
21
|
+
def log_file_path() -> str:
|
|
22
|
+
return '/'.join([QDir.currentPath().replace('\\', '/'), log_file_name()])
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: qtwrap
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A PyQt6 toolkit for wrapping scripts in a GUI with live stdout capture
|
|
5
|
+
Keywords: pyqt6,gui,stdout
|
|
6
|
+
Author: malomr
|
|
7
|
+
Author-email: malomr <malomr@proton.me>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Requires-Dist: pyqt6==6.11.0
|
|
15
|
+
Requires-Dist: pyqt6-sip>=13.11.1
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Project-URL: Homepage, https://gitlab.com/malomr/qtwrap
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# qtwrap
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
qtwrap/Choice.py,sha256=xlBJKjM3F70umd34ffb0RI6iIVZKw7qAGEswo8uWbA4,1116
|
|
2
|
+
qtwrap/ScriptCaptureWindow.py,sha256=EuokuUr06mqNN1q9DYmfwIZ2rbPM7S13coPG7K27zdE,3677
|
|
3
|
+
qtwrap/StatusBar.py,sha256=mkS808X_Fhln1G0LkZ9u1PFcOgbIkCqfmj-unvNEYpw,342
|
|
4
|
+
qtwrap/__init__.py,sha256=zx91RxAaqGt_x14DhSz7faLkrZ6Q-Ftj2FzXGutUbQ0,113
|
|
5
|
+
qtwrap/enums/LogLevel.py,sha256=m9XyJDQaf4eIEbSOIWfEr9M6rTjs5Nc03xmgFWZJxa0,123
|
|
6
|
+
qtwrap/enums/__init__.py,sha256=0YuNLCHrvgHfx9YYq9sO0MhXEqYzzD3OGCiwc_yKaw4,30
|
|
7
|
+
qtwrap/path/BrowsingSelector.py,sha256=TlU7Brb15bbiC7z_qZGx7ktmFOQHlgqYN_97RHVw29Y,1355
|
|
8
|
+
qtwrap/path/DirectorySelector.py,sha256=60e-wm6yyqci8rnJXSjF_DtZ9sTo0MTSPw-naJmSolQ,634
|
|
9
|
+
qtwrap/path/FileSelector.py,sha256=_M5V7W6f4o9Kv7UqwS2MuI1zQWsZjHvElI9HYgknzJs,722
|
|
10
|
+
qtwrap/path/__init__.py,sha256=Ypd7dNbmIExC9ci0qBeIgwjwa8FD6XFhMBwuo2ZI-PU,88
|
|
11
|
+
qtwrap/utils/OutputCaptureWorker.py,sha256=KZlckLg57jkfegOgLJ3wLjPkRvoaPkS6WjHTx5ro44c,1063
|
|
12
|
+
qtwrap/utils/StreamRedirector.py,sha256=npYyWaobMweRFZhJR8tTB8pOXpEyVzidkveZndkjo14,219
|
|
13
|
+
qtwrap/utils/__init__.py,sha256=Hr8sIXFo5ipQpzxCIr7iT1hs_BTVoOjHCFq1gSP7Inw,123
|
|
14
|
+
qtwrap/utils/commons.py,sha256=cdkVtM4EQSNnJbJnllmTuvNZ5bjYeXX0k2Y5I43ZNBY,485
|
|
15
|
+
qtwrap-1.0.0.dist-info/licenses/LICENSE,sha256=MCoWQ_vWg2IfQW1b3H1Z8pjDpa5B-55et0eg0YvNYbI,1079
|
|
16
|
+
qtwrap-1.0.0.dist-info/WHEEL,sha256=l3MmIxu8qaet7ng2J9fFoJnYGj8IREj7jXTbgsuzmy4,81
|
|
17
|
+
qtwrap-1.0.0.dist-info/METADATA,sha256=vNpY7bNkJWOKD8nv7Ts_8YUl50ME-Fv44rFzkdlQt58,647
|
|
18
|
+
qtwrap-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Malo Massieu--Rocabois
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|