Skip to content

Commit

Permalink
Merge pull request #23 from simonsobs/plugin
Browse files Browse the repository at this point in the history
Accept external plugins
  • Loading branch information
TaiSakuma authored Jan 18, 2024
2 parents 2476ca7 + b71dbd9 commit a9ec151
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
14 changes: 4 additions & 10 deletions nextline/fsm/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@
from logging import getLogger
from typing import Any, Optional

import apluggy
from transitions import EventData

from nextline.plugin import build_hook
from nextline.spawned import Command
from nextline.types import InitOptions, ResetOptions
from nextline.utils.pubsub.broker import PubSub
from nextline.types import ResetOptions

from .factory import build_state_machine


class Machine:
'''The finite state machine of the nextline states.'''

def __init__(self, init_options: InitOptions, registry: PubSub[Any, Any]):
self._hook = build_hook()
self._hook.hook.init(
hook=self._hook, registry=registry, init_options=init_options
)

def __init__(self, hook: apluggy.PluginManager) -> None:
self._hook = hook
self._machine = build_state_machine(model=self)
self._machine.after_state_change = self.after_state_change.__name__ # type: ignore

assert self.state # type: ignore

def __repr__(self) -> str:
Expand Down
23 changes: 16 additions & 7 deletions nextline/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .continuous import Continuous
from .fsm import Machine
from .plugin import build_hook
from .spawned import PdbCommand
from .types import (
InitOptions,
Expand Down Expand Up @@ -66,32 +67,40 @@ def __init__(
trace_modules=trace_modules,
)
self._continuous = Continuous(self)
self._registry = PubSub[Any, Any]()
self._pubsub = PubSub[Any, Any]()
self._timeout_on_exit = timeout_on_exit
self._started = False
self._closed = False
self._hook = build_hook()

def __repr__(self) -> str:
# e.g., "<Nextline 'running'>"
return f'<{self.__class__.__name__} {self.state!r}>'

def register(self, plugin: Any) -> str | None:
return self._hook.register(plugin)

async def start(self) -> None:
if self._started:
return
self._started = True
logger = getLogger(__name__)
logger.debug(f'self._init_options: {self._init_options}')
self._machine = Machine(
init_options=self._init_options, registry=self._registry
self._hook.hook.init(
nextline=self,
hook=self._hook,
registry=self._pubsub,
init_options=self._init_options,
)
self._machine = Machine(hook=self._hook)
await self._continuous.start()
await self._machine.initialize() # type: ignore

async def close(self) -> None:
if self._closed:
return
self._closed = True
await self._registry.close()
await self._pubsub.close()
await self._machine.close() # type: ignore
await self._continuous.close()

Expand Down Expand Up @@ -219,7 +228,7 @@ def subscribe_trace_ids(self) -> AsyncIterator[tuple[int, ...]]:
return self.subscribe("trace_nos")

def get_source(self, file_name: Optional[str] = None) -> list[str]:
if not file_name or file_name == self._registry.latest("script_file_name"):
if not file_name or file_name == self._pubsub.latest("script_file_name"):
return self.get("statement").split("\n")
return [e.rstrip() for e in linecache.getlines(file_name)]

Expand Down Expand Up @@ -271,10 +280,10 @@ async def _subscribe_prompt_info_for(
yield info

def get(self, key: Any) -> Any:
return self._registry.latest(key)
return self._pubsub.latest(key)

def subscribe(self, key: Any, last: Optional[bool] = True) -> AsyncIterator[Any]:
return self._registry.subscribe(key, last=last)
return self._pubsub.subscribe(key, last=last)

def subscribe_stdout(self) -> AsyncIterator[StdoutInfo]:
return self.subscribe("stdout", last=False)
Expand Down
10 changes: 8 additions & 2 deletions nextline/plugin/spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Optional
from typing import TYPE_CHECKING, Any, Callable, Optional

import apluggy

Expand All @@ -7,6 +7,9 @@
from nextline.utils import ExitedProcess, RunningProcess
from nextline.utils.pubsub.broker import PubSub

if TYPE_CHECKING:
from nextline import Nextline

PROJECT_NAME = 'nextline_main'


Expand All @@ -16,7 +19,10 @@

@hookspec
def init(
hook: apluggy.PluginManager, registry: PubSub, init_options: InitOptions
nextline: 'Nextline',
hook: apluggy.PluginManager,
registry: PubSub,
init_options: InitOptions,
) -> None:
pass

Expand Down
28 changes: 28 additions & 0 deletions tests/main/test_register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time

from nextline import Nextline
from nextline.plugin.spec import hookimpl
from nextline.spawned import OnStartPrompt


def func():
time.sleep(0.001)


class Plugin:
@hookimpl
def init(self, nextline: Nextline) -> None:
self._nextline = nextline

@hookimpl
async def on_start_prompt(self, event: OnStartPrompt) -> None:
await self._nextline.send_pdb_command('next', event.prompt_no, event.trace_no)


async def test_one() -> None:
nextline = Nextline(func, trace_modules=True)
assert nextline.register(Plugin())
async with nextline:
async with nextline.run_session():
pass
nextline.exception()

0 comments on commit a9ec151

Please sign in to comment.