Skip to content

Commit

Permalink
Reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlegiantJGC committed Jun 1, 2024
1 parent 5dc3716 commit f46b25a
Show file tree
Hide file tree
Showing 25 changed files with 217 additions and 76 deletions.
8 changes: 5 additions & 3 deletions src/amulet_editor/application/_invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def invoke(cls, method: Callable[[], T], parent: QObject | None = None) -> T:
# Connect slot
self.start_signal.connect(
self.execute,
Qt.ConnectionType.DirectConnection
if thread is QThread.currentThread()
else Qt.ConnectionType.BlockingQueuedConnection,
(
Qt.ConnectionType.DirectConnection
if thread is QThread.currentThread()
else Qt.ConnectionType.BlockingQueuedConnection
),
)

# Start execute. This will block execution of this function until it is finished.
Expand Down
2 changes: 1 addition & 1 deletion src/amulet_editor/application/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
qInstallMessageHandler,
QtMsgType,
QLocale,
QMessageLogContext
QMessageLogContext,
)
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QSurfaceFormat
Expand Down
2 changes: 1 addition & 1 deletion src/amulet_editor/application/appearance/_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _format_style_sheet(raw_style_sheet: str, theme_json: dict[str, Any]) -> str
color
for color in re.findall("{(.+?)}", style_sheet)
if color not in theme_json["material_colors"]
and color.rsplit(".", 2)[0] in theme_json["material_colors"]
and color.rsplit(".", 2)[0] in theme_json["material_colors"]
]
)
for modified_color in modified_colors:
Expand Down
12 changes: 9 additions & 3 deletions src/amulet_editor/data/_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def _process_msg(self) -> None:
payload[37:],
)
if is_response:
log.debug(f"New response from {self.socket} {identifier.decode()}")
log.debug(
f"New response from {self.socket} {identifier.decode()}"
)
_, (success_callback, error_callback) = self._calls.pop(
identifier
)
Expand Down Expand Up @@ -300,7 +302,9 @@ def _process_msg(self) -> None:
self.socket.write(payload)

except Exception:
log.debug(f"Exception processing request {identifier.decode()}")
log.debug(
f"Exception processing request {identifier.decode()}"
)
payload = self.encode_error_response(
identifier, traceback.format_exc()
)
Expand All @@ -312,7 +316,9 @@ def _add_size(payload: bytes) -> bytes:
return struct.pack(">I", payload_size) + payload

@classmethod
def encode_request(cls, identifier: bytes, address: str, args: Any, kwargs: Any) -> bytes:
def encode_request(
cls, identifier: bytes, address: str, args: Any, kwargs: Any
) -> bytes:
"""
Encode an RPC request.
Expand Down
32 changes: 21 additions & 11 deletions src/amulet_editor/data/plugin/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,22 @@ def _validate_import(imported_name: str, frame: FrameType) -> None:

# Find what imported the module
frame_: FrameType | None = frame
while frame_ is not None and frame_.f_globals.get("__name__", "").split(".")[0] == "importlib":
while (
frame_ is not None
and frame_.f_globals.get("__name__", "").split(".")[0] == "importlib"
):
# Skip over the import mechanisms
frame_ = frame_.f_back

if frame_ is not None and frame_.f_globals.get("__name__") == __name__ and frame_.f_code.co_name in {
"_enable_plugin",
"wrap_importer_import",
}:
if (
frame_ is not None
and frame_.f_globals.get("__name__") == __name__
and frame_.f_code.co_name
in {
"_enable_plugin",
"wrap_importer_import",
}
):
return

assert frame_ is not None
Expand Down Expand Up @@ -202,7 +210,7 @@ def __getitem__(self, imported_name: str) -> ModuleType:
"collections",
"collections.abc",
"inspect",
"dataclasses"
"dataclasses",
}:
frame = frame.f_back
if frame is not None:
Expand All @@ -225,9 +233,8 @@ def __call__(
globals: Mapping[str, object] | None = None,
locals: Mapping[str, object] | None = None,
fromlist: Sequence[str] = (),
level: int = 0
) -> ModuleType:
...
level: int = 0,
) -> ModuleType: ...


def wrap_importer(imp: ImportProtocol) -> ImportProtocol:
Expand All @@ -236,7 +243,7 @@ def wrap_importer_import(
globals: Mapping[str, object] | None = None,
locals: Mapping[str, object] | None = None,
fromlist: Sequence[str] = (),
level: int = 0
level: int = 0,
) -> ModuleType:
if level:
assert globals is not None
Expand All @@ -252,6 +259,7 @@ def wrap_importer_import(
assert frame is not None
_validate_import(imported_name, frame)
return imp(name, globals=globals, locals=locals, fromlist=fromlist, level=level)

return wrap_importer_import


Expand Down Expand Up @@ -348,7 +356,9 @@ def get_plugins_state() -> dict[LibraryUID, bool]:
return {}


def _set_plugin_state(plugin_container: PluginContainer, plugin_state: PluginState) -> None:
def _set_plugin_state(
plugin_container: PluginContainer, plugin_state: PluginState
) -> None:
plugin_container.state = plugin_state
uid = plugin_container.data.uid
identifier = uid.identifier
Expand Down
50 changes: 41 additions & 9 deletions src/amulet_editor/models/plugin/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ def from_path(cls, plugin_path: str) -> PluginContainer:
plugin_path is the system path to a directory containing a plugin.json file.
"""
with open(os.path.join(plugin_path, "plugin.json")) as f:
plugin_data = dynamic_cast(json.load(f), dict, "plugin.json must be a dictionary.")
plugin_data = dynamic_cast(
json.load(f), dict, "plugin.json must be a dictionary."
)

# Get the plugin identifier
format_version = dynamic_cast(plugin_data.get("format_version", 1), int, "plugin.json[format_version] must be an int")
format_version = dynamic_cast(
plugin_data.get("format_version", 1),
int,
"plugin.json[format_version] must be an int",
)
try:
cls2 = _plugin_classes[format_version]
except KeyError:
Expand Down Expand Up @@ -117,33 +123,59 @@ def from_data(
plugin_path is the system path to a directory containing a plugin.json file.
"""
# Get the plugin identifier
plugin_identifier = dynamic_cast(plugin_data.get("identifier"), str, "plugin.json[identifier] must be a string")
plugin_identifier = dynamic_cast(
plugin_data.get("identifier"),
str,
"plugin.json[identifier] must be a string",
)
if not plugin_identifier.isidentifier():
raise ValueError(
"plugin.json[identifier] must be a valid python identifier"
)

# Get the plugin version
plugin_version_string = dynamic_cast(plugin_data.get("version"), str, "plugin.json[version] must be a PEP 440 version string")
plugin_version_string = dynamic_cast(
plugin_data.get("version"),
str,
"plugin.json[version] must be a PEP 440 version string",
)
plugin_version = Version(plugin_version_string)

# Get the plugin name
plugin_name = dynamic_cast(plugin_data.get("name"), str, "plugin.json[name] must be a string")
plugin_name = dynamic_cast(
plugin_data.get("name"), str, "plugin.json[name] must be a string"
)

# Get the plugin dependencies
depends_raw = dynamic_cast(plugin_data.get("depends"), dict, 'plugin.json[depends] must be a dictionary of the form {"python": "~=3.9", "library": [], "plugin": []}')
depends_raw = dynamic_cast(
plugin_data.get("depends"),
dict,
'plugin.json[depends] must be a dictionary of the form {"python": "~=3.9", "library": [], "plugin": []}',
)

python_raw = dynamic_cast(depends_raw.get("python"), str, 'plugin.json[depends][python] must be a string of the form "~=3.9"')
python_raw = dynamic_cast(
depends_raw.get("python"),
str,
'plugin.json[depends][python] must be a string of the form "~=3.9"',
)
python = SpecifierSet(python_raw)

library_depends_raw = dynamic_cast(depends_raw.get("library", []), list, "plugin.json[depends][library] must be a list")
library_depends_raw = dynamic_cast(
depends_raw.get("library", []),
list,
"plugin.json[depends][library] must be a list",
)
if not all(isinstance(d, str) for d in library_depends_raw):
raise TypeError(
'plugin.json[depends][library] must be a list of string identifiers and version specifiers if defined.\nEg. ["PySide6_Essentials~=6.4"]'
)
library_depends = tuple(map(Requirement.from_string, library_depends_raw))

plugin_depends_raw = dynamic_cast(depends_raw.get("plugin", []), list, "plugin.json[depends][plugin] must be a list")
plugin_depends_raw = dynamic_cast(
depends_raw.get("plugin", []),
list,
"plugin.json[depends][plugin] must be a list",
)
if not all(isinstance(d, str) for d in plugin_depends_raw):
raise TypeError(
'plugin.json[depends][plugin] must be a list of string identifiers and version specifiers if defined.\nEg. ["plugin_1~=1.0", "plugin_2~=1.3"]'
Expand Down
1 change: 1 addition & 0 deletions src/amulet_editor/models/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# from ._card import ALinkCard, QPixCard
from ._draggable import ADragContainer
from ._icon import AStylableSvgWidget, AIconButton, ATooltipIconButton

# from ._label import QElidedLabel
# from ._menu import QMenuWidget
# from ._line import QHLine
10 changes: 9 additions & 1 deletion src/amulet_editor/models/widgets/_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
from amulet_editor.resources import get_resource
from amulet_editor.models.widgets._label import QHoverLabel
from PySide6.QtCore import QEvent, QSize, Qt
from PySide6.QtGui import QColor, QEnterEvent, QPainter, QPaintEvent, QImage, QPixmap, QIcon
from PySide6.QtGui import (
QColor,
QEnterEvent,
QPainter,
QPaintEvent,
QImage,
QPixmap,
QIcon,
)
from PySide6.QtWidgets import QWidget, QStyleOption, QStyle, QVBoxLayout, QPushButton
from PySide6.QtSvgWidgets import QSvgWidget

Expand Down
26 changes: 22 additions & 4 deletions src/amulet_editor/models/widgets/traceback_dialog/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
main_logger = logging.getLogger()


def display_exception_blocking(title: str = "", error: str = "", traceback: str = "") -> None:
def display_exception_blocking(
title: str = "", error: str = "", traceback: str = ""
) -> None:
"""
Display an exception window.
This must be called from the main thread.
Expand Down Expand Up @@ -44,15 +46,26 @@ class DisplayException:
It will also log the exception to the logging module and optionally suppress the exception.
"""

def __init__(self, msg: str, *, suppress: bool = False, log: logging.Logger = logging.getLogger()) -> None:
def __init__(
self,
msg: str,
*,
suppress: bool = False,
log: logging.Logger = logging.getLogger()
) -> None:
self._msg = msg
self._suppress = suppress
self._log = log

def __enter__(self) -> None:
pass

def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> bool:
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool:
if exc_type and isinstance(exc_val, Exception):
self._log.exception(exc_val)
display_exception(
Expand All @@ -73,7 +86,12 @@ class CatchException:
def __enter__(self) -> None:
pass

def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> bool:
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> bool:
if isinstance(exc_val, Exception):
main_logger.exception(exc_val)
display_exception(
Expand Down
11 changes: 10 additions & 1 deletion src/builtin_plugins/amulet_team_default_layout/_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

from amulet_editor.data.level import get_level
from amulet_editor.models.plugin import PluginV1
from amulet_team_main_window import register_layout, unregister_layout, WidgetConfig, WidgetStackConfig, WindowConfig, LayoutConfig, ButtonProxy, create_layout_button
from amulet_team_main_window import (
register_layout,
unregister_layout,
WidgetConfig,
WidgetStackConfig,
WindowConfig,
LayoutConfig,
ButtonProxy,
create_layout_button,
)
from amulet_team_home_page import HomeWidget

import tablericons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def _get_locales() -> tuple[tuple[tuple[str, QLocale], ...], int]:


class HomePage(Ui_HomePage):
def __init__(self, parent: QWidget | None = None, f: Qt.WindowType = Qt.WindowType.Widget):
def __init__(
self, parent: QWidget | None = None, f: Qt.WindowType = Qt.WindowType.Widget
):
super().__init__(parent, f)
amulet_logo = QPixmap(QImage(get_resource("icons/amulet/amulet_logo.png")))
amulet_logo = amulet_logo.scaledToHeight(128)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


class OpenWorldPage(Ui_OpenWorldPage):
def __init__(self, parent: QWidget | None = None, f: Qt.WindowType = Qt.WindowType.Widget):
def __init__(
self, parent: QWidget | None = None, f: Qt.WindowType = Qt.WindowType.Widget
):
super().__init__(parent, f)
self.btn_back.setIcon(QIcon(tablericons.arrow_left))
self.load_file_button.clicked.connect(self.open_file)
Expand Down
13 changes: 12 additions & 1 deletion src/builtin_plugins/amulet_team_main_window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

# Public functions and classes
from ._tab_engine import TabWidget
from ._layout import SplitterConfig, WidgetConfig, WidgetStackConfig, WindowConfig, LayoutConfig, register_layout, unregister_layout, activate_layout, active_layout, create_layout_button
from ._layout import (
SplitterConfig,
WidgetConfig,
WidgetStackConfig,
WindowConfig,
LayoutConfig,
register_layout,
unregister_layout,
activate_layout,
active_layout,
create_layout_button,
)
from ._widget import register_widget, unregister_widget
from ._main_window.toolbar import ButtonProxy
Loading

0 comments on commit f46b25a

Please sign in to comment.