From 2fbc920ebc31a1368f6b258ffe811fb57cb808c8 Mon Sep 17 00:00:00 2001 From: jerlendds Date: Sun, 30 Jul 2023 11:44:39 -0600 Subject: [PATCH] feature: add new plugin loading method --- requirements.txt | 2 +- src/osintbuddy/__init__.py | 4 ++- src/osintbuddy/entities/INDEX.md | 4 --- src/osintbuddy/plugins.py | 59 +++++++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 14 deletions(-) delete mode 100644 src/osintbuddy/entities/INDEX.md diff --git a/requirements.txt b/requirements.txt index a0757e9..28ce6bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ selenium>=4.9.0 pydantic>=1.10.8 -httpx>=0.24.1 +httpx>=0.23.3 SQLAlchemy>=2.0.12 gremlinpy @ git+https://github.com/jerlendds/gremlinpy.git@7d3033e6a55ed9cb1f982ec3b58ca233e01c58e3 \ No newline at end of file diff --git a/src/osintbuddy/__init__.py b/src/osintbuddy/__init__.py index c82c715..0ce7e7d 100644 --- a/src/osintbuddy/__init__.py +++ b/src/osintbuddy/__init__.py @@ -8,7 +8,9 @@ OBPlugin as Plugin, OBAuthorUse as PluginUse, discover_plugins, - transform + transform, + load_plugin, + load_plugins ) __version__ = "0.0.4" diff --git a/src/osintbuddy/entities/INDEX.md b/src/osintbuddy/entities/INDEX.md deleted file mode 100644 index 62f09df..0000000 --- a/src/osintbuddy/entities/INDEX.md +++ /dev/null @@ -1,4 +0,0 @@ -# OSINTBuddy Entities - -@todo add core set of plugins here... - diff --git a/src/osintbuddy/plugins.py b/src/osintbuddy/plugins.py index 3eec8ee..1fad9ec 100644 --- a/src/osintbuddy/plugins.py +++ b/src/osintbuddy/plugins.py @@ -1,21 +1,18 @@ -import os -import importlib +import os, imp, importlib, sys from typing import List, Any, Callable from collections import defaultdict from pydantic import create_model, BaseModel -import undetected_chromedriver as uc # from osintbuddy.utils import slugify from osintbuddy.elements.base import BaseElement from osintbuddy.errors import OBPluginError from osintbuddy.utils import to_snake_case -def driver() -> uc.Chrome: - pass # @todo add permission system and display what parts of system plugin can access class OBAuthorUse(BaseModel): - get_driver: Callable[[], uc.Chrome] - get_graph: Callable[[], None] # @todo + # @todo + get_driver: Callable[[], None] + get_graph: Callable[[], None] class OBRegistry(type): @@ -57,8 +54,54 @@ async def get_plugin(cls, plugin_label: str): return cls.plugins[idx] return None + @classmethod + def get_plug(cls, plugin_label: str): + """ + Returns the corresponding plugin class for a given plugin_label or + 'None' if not found. + + :param plugin_label: The label of the plugin to be returned. + :return: The plugin class or None if not found. + """ + for idx, label in enumerate(cls.labels): + if to_snake_case(label) == to_snake_case(plugin_label): + return cls.plugins[idx] + return None + def __getitem__(self, i): - return self.get_plugin[i] + return self.get_plug[i] + +# https://stackoverflow.com/a/7548190 +def load_plugin( + mod_name: str, + plugin_code: str, +): + """ + Load plugins from a string of code + + :param module_name: The desired module name of the plugin. + :param plugin_code: The code of the plugin. + :return: + """ + new_mod = imp.new_module(mod_name) + exec(plugin_code, new_mod.__dict__) + return OBRegistry.plugins + + +def load_plugins( + entities: list +): + """ + Loads plugins from the osintbuddy db + + :param entities: list of entities from the db + :return: + """ + for entity in entities: + mod_name = to_snake_case(entity.label) + new_mod = imp.new_module(mod_name) + exec(entity.source, new_mod.__dict__) + return OBRegistry.plugins def discover_plugins(