Skip to content

Commit

Permalink
chore: refactor create_screenshot script
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 18, 2024
1 parent fc637e6 commit 67ecc71
Showing 1 changed file with 71 additions and 26 deletions.
97 changes: 71 additions & 26 deletions scripts/create_screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,83 @@
# requires-python = ">=3.11"
# dependencies = [
# "prettyqt",
# "pyside6"
# "pyside6",
# "python-dateutil",
# "packaging",
# ]
# ///

import inspect
import pathlib
from typing import Any

from PySide6.QtWidgets import QWidget

from prettyqt import custom_widgets, widgets


module_dict = dict(custom_widgets=custom_widgets)
app = widgets.app()

for module in module_dict.values():
clsmembers = inspect.getmembers(module, inspect.isclass)
for _klass_name, Klass in clsmembers:
widget = None
match Klass:
case _:
try:
widget = Klass()
if not isinstance(widget, widgets.WidgetMixin):
continue
widget.show()
app.processEvents()
pixmap = widget.grab()
path = pathlib.Path().absolute() / "screenshots"
path.mkdir(parents=True, exist_ok=True)
filename = type(widget).__name__
pixmap.save(f"{path / filename}.png", "png")
except Exception as e:
print(e)
finally:
if hasattr(widget, "hide"):
widget.hide()
def create_widget_instance(widget_class: type[QWidget]) -> QWidget | None:
"""Create an instance of the given widget class.
Args:
widget_class: The class of the widget to instantiate.
Returns:
An instance of the widget if successful, None otherwise.
"""
try:
return widget_class()
except Exception as e:
print(f"Failed to create instance of {widget_class.__name__}: {e}")
return None


def save_widget_screenshot(widget: QWidget, save_path: pathlib.Path) -> None:
"""Save a screenshot of the given widget.
Args:
widget: The widget to capture.
save_path: The directory to save the screenshot in.
"""
widget.show()
app.processEvents()
pixmap = widget.grab()
filename = f"{type(widget).__name__}.png"
pixmap.save(str(save_path / filename), "png")


def process_widget(widget_class: type[QWidget], save_path: pathlib.Path) -> None:
"""Process a single widget: create instance, save screenshot, and clean up.
Args:
widget_class: The class of the widget to process.
save_path: The directory to save the screenshot in.
"""
widget = create_widget_instance(widget_class)
if widget is None or not isinstance(widget, widgets.WidgetMixin):
return

try:
save_widget_screenshot(widget, save_path)
except Exception as e:
print(f"Error processing {widget_class.__name__}: {e}")
finally:
if hasattr(widget, "hide"):
widget.hide()


def main() -> None:
"""Main function to process all widgets and save screenshots."""
module_dict: dict[str, Any] = {"custom_widgets": custom_widgets}
save_path = pathlib.Path().absolute() / "screenshots"
save_path.mkdir(parents=True, exist_ok=True)

for module in module_dict.values():
class_members = inspect.getmembers(module, inspect.isclass)
for _, widget_class in class_members:
process_widget(widget_class, save_path)


if __name__ == "__main__":
app = widgets.app()
main()

0 comments on commit 67ecc71

Please sign in to comment.