Skip to content

Commit

Permalink
internal class for exposing loaded plugins for external access in run…
Browse files Browse the repository at this point in the history
…time by class name
  • Loading branch information
dotchetter committed Jan 7, 2024
1 parent bd947a9 commit a5cbca1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pyttman/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,38 @@ class PyttmanApp(PrettyReprMixin):
"""
__repr_fields__ = ("name", "client", "hooks")

@dataclass
class LoadedPluginContainer:
"""
Access point for loaded plugins during the app runtime, e.g:
`from pyttman import app`
`my_plugin = app.loaded_plugins.SomePluginName`
"""

def ingest(self, plugin):
setattr(self, plugin.__class__.__name__, plugin)

client: Any
name: str | None = field(default=None)
settings: Settings | None = field(default=None)
hooks: LifecycleHookRepository = field(
default_factory=lambda: LifecycleHookRepository())
_abilities: set = field(default_factory=set)
plugins: list[PyttmanPlugin] = field(default_factory=list)
loaded_plugins: LoadedPluginContainer = field(default_factory=LoadedPluginContainer)

def start(self):
"""
Start a Pyttman application.
"""
# noinspection PyBroadException
# Execute plugin hooks
self.execute_plugins_before_start()
try:
self.client.run_client()
except Exception:
warnings.warn(traceback.format_exc())
self.execute_plugins_after_stop()

@property
def abilities(self):
Expand Down

0 comments on commit a5cbca1

Please sign in to comment.