Skip to content

Commit

Permalink
Wrap Plugin calls with exception handling placeholders, new decorator…
Browse files Browse the repository at this point in the history
… for exception wrapping
  • Loading branch information
Rexeh committed Feb 6, 2024
1 parent 333d37f commit 4eec95e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
54 changes: 40 additions & 14 deletions joystick_diagrams/ui/plugin_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
""" Wrapper functionality for Plugins for the UI
"""

import functools
import logging
from dataclasses import dataclass, field
from pathlib import Path

from db import db_plugin_data
from utils import handle_bare_exception

from joystick_diagrams.exceptions import JoystickDiagramsException
from joystick_diagrams.input.profile_collection import ProfileCollection
from joystick_diagrams.plugins.plugin_interface import PluginInterface

_logger = logging.getLogger(__name__)


@dataclass
class PluginWrapper:
Expand All @@ -18,28 +29,43 @@ def __post_init__(self):
self.plugin_profile_collection = None
self.setup_plugin()

@handle_bare_exception
def process(self):
if self.path:
self.plugin_profile_collection = self.plugin.process()

"""Runs a specific plugin, attaching the result to the wrapper"""
try:
if self.path:
self.plugin_profile_collection = self.plugin.process()
except JoystickDiagramsException as e:
_logger.error(e)

@handle_bare_exception
def set_path(self, path: Path) -> bool:
set = self.plugin.set_path(path)
return set
"""Sets the path for a given plugin"""
try:
return self.plugin.set_path(path)
except JoystickDiagramsException as e:
_logger.error(e)

return False

@handle_bare_exception
def setup_plugin(self):
"""Sets up a pluginf or first use or restores existing state"""
"""Sets up a pluging on first use or restores existing state"""

existing_configuration = self.get_plugin_configuration(self.plugin.name)
try:
existing_configuration = self.get_plugin_configuration(self.plugin.name)

if existing_configuration:
self.enabled = existing_configuration[1]
else:
self.store_plugin_configuration()
if existing_configuration:
self.enabled = existing_configuration[1]
else:
self.store_plugin_configuration()

self.plugin.load_settings()
self.plugin.load_settings()

if self.plugin.path:
self.set_path(self.plugin.path)
if self.plugin.path:
self.set_path(self.plugin.path)
except JoystickDiagramsException as e:
_logger.error(e)

def get_plugin_configuration(self, plugin_name: str):
return db_plugin_data.get_plugin_configuration(plugin_name)
Expand Down
25 changes: 25 additions & 0 deletions joystick_diagrams/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import functools
import logging
from pathlib import Path

from joystick_diagrams.exceptions import JoystickDiagramsException

_logger = logging.getLogger(__name__)


Expand All @@ -10,3 +13,25 @@ def create_directory(directory) -> None: # pylint: disable=missing-function-doc
Path(directory).mkdir()
except OSError as error:
_logger.error(f"Failed to create directory: {directory} with {error}")


def handle_bare_exception(exception_type):
"""Handles bare exception by wrapping it in a Joystick Diagrams exception type.
Default type is JoystickDiagramsException
"""

if not exception_type:
exception_type = JoystickDiagramsException

def outer(func):
@functools.wraps(func)
def inner():
try:
return func()
except Exception as e:
raise exception_type(f"An error occured from within the plugin. {e}") from e

return inner

return outer

0 comments on commit 4eec95e

Please sign in to comment.