Skip to content

Commit efbcb64

Browse files
committed
Introduce TrinityBootInfo to cleanup API
1 parent 61a9a37 commit efbcb64

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

trinity/extensibility/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BaseSyncStopPlugin,
1010
DebugPlugin,
1111
PluginContext,
12+
TrinityBootInfo,
1213
)
1314
from trinity.extensibility.plugin_manager import ( # noqa: F401
1415
BaseManagerProcessScope,

trinity/extensibility/plugin.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import (
1616
Any,
1717
Dict,
18+
NamedTuple
1819
)
1920

2021
from lahja import (
@@ -49,6 +50,12 @@
4950
)
5051

5152

53+
class TrinityBootInfo(NamedTuple):
54+
args: Namespace
55+
trinity_config: TrinityConfig
56+
boot_kwargs: Dict[str, Any] = None
57+
58+
5259
class PluginContext:
5360
"""
5461
The ``PluginContext`` holds valuable contextual information such as the parsed
@@ -58,11 +65,11 @@ class PluginContext:
5865
Each plugin gets a ``PluginContext`` injected during startup.
5966
"""
6067

61-
def __init__(self, endpoint: Endpoint) -> None:
68+
def __init__(self, endpoint: Endpoint, boot_info: TrinityBootInfo) -> None:
6269
self.event_bus = endpoint
63-
self.boot_kwargs: Dict[str, Any] = None
64-
self.args: Namespace = None
65-
self.trinity_config: TrinityConfig = None
70+
self.boot_kwargs: Dict[str, Any] = boot_info.boot_kwargs
71+
self.args: Namespace = boot_info.args
72+
self.trinity_config: TrinityConfig = boot_info.trinity_config
6673

6774
def shutdown_host(self, reason: str) -> None:
6875
self.event_bus.broadcast(

trinity/extensibility/plugin_manager.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
BasePlugin,
3838
BaseSyncStopPlugin,
3939
PluginContext,
40+
TrinityBootInfo,
4041
)
4142

4243

@@ -58,9 +59,7 @@ def is_responsible_for_plugin(self, plugin: BasePlugin) -> bool:
5859
@abstractmethod
5960
def create_plugin_context(self,
6061
plugin: BasePlugin,
61-
args: Namespace,
62-
trinity_config: TrinityConfig,
63-
boot_kwargs: Dict[str, Any]) -> PluginContext:
62+
boot_info: TrinityBootInfo) -> PluginContext:
6463
"""
6564
Create the ``PluginContext`` for a given plugin.
6665
"""
@@ -78,19 +77,14 @@ def is_responsible_for_plugin(self, plugin: BasePlugin) -> bool:
7877

7978
def create_plugin_context(self,
8079
plugin: BasePlugin,
81-
args: Namespace,
82-
trinity_config: TrinityConfig,
83-
boot_kwargs: Dict[str, Any]) -> PluginContext:
80+
boot_info: TrinityBootInfo) -> PluginContext:
8481

8582
if isinstance(plugin, BaseIsolatedPlugin):
8683
# Isolated plugins get an entirely new endpoint to be passed into that new process
87-
context = PluginContext(
88-
self.event_bus.create_endpoint(plugin.name)
84+
return PluginContext(
85+
self.event_bus.create_endpoint(plugin.name),
86+
boot_info,
8987
)
90-
context.args = args
91-
context.trinity_config = trinity_config
92-
context.boot_kwargs = boot_kwargs
93-
return context
9488

9589
# A plugin that overtakes the main process never gets far enough to even get a context.
9690
# For now it should be safe to just return `None`. Maybe reconsider in the future.
@@ -107,16 +101,10 @@ def is_responsible_for_plugin(self, plugin: BasePlugin) -> bool:
107101

108102
def create_plugin_context(self,
109103
plugin: BasePlugin,
110-
args: Namespace,
111-
trinity_config: TrinityConfig,
112-
boot_kwargs: Dict[str, Any]) -> PluginContext:
104+
boot_info: TrinityBootInfo) -> PluginContext:
113105

114106
# Plugins that run in a shared process all share the endpoint of the plugin manager
115-
context = PluginContext(self.endpoint)
116-
context.args = args
117-
context.trinity_config = trinity_config
118-
context.boot_kwargs = boot_kwargs
119-
return context
107+
return PluginContext(self.endpoint, boot_info)
120108

121109

122110
class PluginManager:
@@ -173,7 +161,10 @@ def prepare(self,
173161
if not self._scope.is_responsible_for_plugin(plugin):
174162
continue
175163

176-
context = self._scope.create_plugin_context(plugin, args, trinity_config, boot_kwargs)
164+
context = self._scope.create_plugin_context(
165+
plugin,
166+
TrinityBootInfo(args, trinity_config, boot_kwargs)
167+
)
177168
plugin.set_context(context)
178169
plugin.ready()
179170

0 commit comments

Comments
 (0)