Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project api hooks #136

Merged
merged 3 commits into from
Jul 24, 2024
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
21 changes: 21 additions & 0 deletions cassini/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Dict,
ClassVar,
Optional,
Callable,
cast,
)
from warnings import warn
Expand Down Expand Up @@ -831,6 +832,14 @@ def __init__(
self._rank_map: Dict[Type[TierABC], int] = {}
self._hierarchy: List[Type[TierABC]] = []

self.__before_setup_files__: List[Callable[[Project], None]] = []
self.__after_setup_files__: List[Callable[[Project], None]] = []

self.__before_launch__: List[
Callable[[Project, Union[LabApp, None]], None]
] = []
self.__after_launch__: List[Callable[[Project, Union[LabApp, None]], None]] = []

self.hierarchy: List[Type[TierABC]] = hierarchy

project_folder_path = Path(project_folder).resolve()
Expand Down Expand Up @@ -950,6 +959,9 @@ def setup_files(self) -> TierABC:

Will put everything you need in `project_folder` to get going.
"""
for func in self.__before_setup_files__:
func(self)

home = self.home

if home.exists():
Expand Down Expand Up @@ -978,6 +990,9 @@ def setup_files(self) -> TierABC:
home.setup_files()
print("Success")

for func in self.__after_setup_files__:
func(self)

return home

def launch(
Expand All @@ -998,6 +1013,9 @@ def launch(
patch_pythonpath : bool
Add `self.project_folder` to the `PYTHONPATH`? (defaults to `True`)
"""
for func in self.__before_launch__:
func(self, app)

self.setup_files()

if patch_pythonpath:
Expand All @@ -1012,6 +1030,9 @@ def launch(

app.launch_instance()

for func in self.__after_launch__:
func(self, app)

return app

def parse_name(self, name: str) -> Tuple[str, ...]:
Expand Down
60 changes: 60 additions & 0 deletions tests/test_extensibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from unittest.mock import Mock
from typing import Any

from utils import patch_project


def test_setup_files_hook(patch_project):
Tier, project = patch_project

home = project.home
home.exists = lambda: False

call_order = []

def stamp_call(label):
def wrapped(*args, **kwargs):
call_order.append(label)
return wrapped

home.setup_files = Mock(side_effect=stamp_call('home'))

before_mock = Mock(side_effect=stamp_call('before'))
after_mock = Mock(side_effect=stamp_call('after'))

project.__before_setup_files__.append(before_mock)
project.__after_setup_files__.append(after_mock)

project.setup_files()

assert before_mock.call_args.args == (project,)
assert after_mock.call_args.args == (project,)
assert call_order == ['before', 'home', 'after']


def test_launch_hook(patch_project):
Tier, project = patch_project
project.exists = lambda self: False

call_order = []

def stamp_call(label):
def wrapped(*args, **kwargs):
call_order.append(label)
return wrapped

project.setup_files = Mock(side_effect=stamp_call('project'))

before_mock = Mock(side_effect=stamp_call('before'))
after_mock = Mock(side_effect=stamp_call('after'))

project.__before_launch__.append(before_mock)
project.__after_launch__.append(after_mock)

app = Mock()

project.launch(app)

assert before_mock.call_args.args == (project, app)
assert after_mock.call_args.args == (project, app)
assert call_order == ['before', 'project', 'after']
Loading