Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/sio3pack/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class SIO3PackException(Exception):
"""A wrapper for all exceptions raised by SIO3Pack."""

def __init__(self, message, original_exception=None):
super().__init__(message)
self.original_exception = original_exception
32 changes: 32 additions & 0 deletions src/sio3pack/packages/package/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
from sio3pack.utils.archive import Archive
from sio3pack.utils.classinit import RegisteredSubclassesBase

from sio3pack.exceptions import SIO3PackException


def wrap_exceptions(func):
"""Decorator to catch exceptions and re-raise them as SIO3PackException."""

def decorator(*args, **kwargs):
try:
return func(*args, **kwargs)
except SIO3PackException:
raise # Do not wrap SIO3PackExceptions again
except Exception as e:
raise SIO3PackException(f"SIO3Pack raised an exception in {func.__name__} function.", e)

return decorator


class Package(RegisteredSubclassesBase):
"""
Expand All @@ -22,13 +38,15 @@ def __init__(self):
super().__init__()

@classmethod
@wrap_exceptions
def identify(cls, file: LocalFile):
"""
Identify if the package is of this type.
"""
raise NotImplementedError()

@classmethod
@wrap_exceptions
def from_file(cls, file: LocalFile, django_settings=None):
"""
Create a package from a file.
Expand All @@ -49,6 +67,7 @@ def _from_file(self, file: LocalFile):
self.is_archive = False

@classmethod
@wrap_exceptions
def identify_db(cls, problem_id: int):
"""
Identify if the package is of this type. Should check if there
Expand All @@ -57,6 +76,7 @@ def identify_db(cls, problem_id: int):
raise NotImplementedError()

@classmethod
@wrap_exceptions
def from_db(cls, problem_id: int):
"""
Create a package from the database. If sio3pack isn't installed with Django
Expand Down Expand Up @@ -92,39 +112,51 @@ def _setup_django_handler(self, problem_id: int):
self.django_enabled = False
self.django = NoDjangoHandler()

@wrap_exceptions
def get_task_id(self) -> str:
pass

@wrap_exceptions
def get_titles(self) -> dict[str, str]:
pass

@wrap_exceptions
def get_title(self, lang: str | None = None) -> str:
pass

@wrap_exceptions
def get_statements(self) -> dict[str, File]:
pass

@wrap_exceptions
def get_statement(self, lang: str | None = None) -> File:
pass

@wrap_exceptions
def get_config(self) -> dict[str, Any]:
pass

@wrap_exceptions
def get_tests(self) -> list[Test]:
pass

@wrap_exceptions
def get_test(self, test_id: str) -> Test:
pass

@wrap_exceptions
def get_unpack_graph(self) -> GraphOperation | None:
pass

@wrap_exceptions
def get_run_graph(self, file: File, tests: list[Test] | None = None) -> GraphOperation | None:
pass

@wrap_exceptions
def get_save_outs_graph(self, tests: list[Test] | None = None) -> GraphOperation | None:
pass

@wrap_exceptions
def save_to_db(self, problem_id: int):
"""
Save the package to the database. If sio3pack isn't installed with Django
Expand Down