qtwrap 1.0.0__tar.gz

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-1.0.0/LICENSE ADDED
@@ -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.
qtwrap-1.0.0/PKG-INFO ADDED
@@ -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
qtwrap-1.0.0/README.md ADDED
@@ -0,0 +1 @@
1
+ # qtwrap
@@ -0,0 +1,30 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.11.32,<0.12"]
3
+ build-backend = "uv_build"
4
+
5
+
6
+ [project]
7
+ name = "qtwrap"
8
+ version = "1.0.0"
9
+ requires-python = ">=3.12"
10
+ dependencies = [
11
+ "pyqt6==6.11.0",
12
+ "pyqt6-sip>=13.11.1",
13
+ ]
14
+ description = "A PyQt6 toolkit for wrapping scripts in a GUI with live stdout capture"
15
+ readme = "README.md"
16
+ authors = [
17
+ {name = "malomr", email = "malomr@proton.me"}
18
+ ]
19
+ license = "MIT"
20
+ license-files = ["LICEN[CS]E*"]
21
+ keywords = ["pyqt6", "gui", "stdout"]
22
+ classifiers = [
23
+ "Programming Language :: Python :: 3 :: Only",
24
+ "License :: OSI Approved :: MIT License",
25
+ "Operating System :: OS Independent",
26
+ "Development Status :: 4 - Beta",
27
+ ]
28
+
29
+ [project.urls]
30
+ Homepage = "https://gitlab.com/malomr/qtwrap"
@@ -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)
@@ -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)
@@ -0,0 +1,3 @@
1
+ from .StatusBar import StatusBar
2
+ from .Choice import Choice
3
+ from .ScriptCaptureWindow import ScriptCaptureWindow
@@ -0,0 +1,6 @@
1
+ from enum import Enum
2
+
3
+ class LogLevel(Enum):
4
+ INFO = (255, 255, 255)
5
+ WARNING = (255, 255, 0)
6
+ ERROR = (255, 0, 0)
@@ -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)))
@@ -0,0 +1,2 @@
1
+ from .DirectorySelector import DirectorySelector
2
+ from .FileSelector import FileSelector
@@ -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()
@@ -0,0 +1,11 @@
1
+ from PyQt6.QtCore import QObject, pyqtSignal
2
+
3
+
4
+ class StreamRedirector(QObject):
5
+ text_written = pyqtSignal(str)
6
+
7
+ def write(self, text):
8
+ self.text_written.emit(str(text))
9
+
10
+ def flush(self):
11
+ pass
@@ -0,0 +1,4 @@
1
+ from .StreamRedirector import StreamRedirector
2
+ from .OutputCaptureWorker import OutputCaptureWorker
3
+
4
+ from .commons import *
@@ -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()])