MCPcopy Create free account
hub / github.com/blacklanternsecurity/bbot / load_module

Method load_module

bbot/core/modules.py:491–535  ·  view source on GitHub ↗

Loads a BBOT module by its name. Imports the module from its namespace, locates its class, and returns it. Identifies modules based on the presence of `watched_events` and `produced_events` attributes. Args: module_name (str): The name of the module to load.

(self, module_name)

Source from the content-addressed store, hash-verified

489 return modules
490
491 def load_module(self, module_name):
492 """Loads a BBOT module by its name.
493
494 Imports the module from its namespace, locates its class, and returns it.
495 Identifies modules based on the presence of `watched_events` and `produced_events` attributes.
496
497 Args:
498 module_name (str): The name of the module to load.
499
500 Returns:
501 object: The loaded module class object.
502
503 Examples:
504 >>> module = load_module("example_module")
505 >>> isinstance(module, object)
506 True
507 """
508 preloaded = self._preloaded[module_name]
509 namespace = preloaded["namespace"]
510 try:
511 module_path = preloaded["path"]
512 except KeyError:
513 module_path = preloaded["cache_key"][0]
514 full_namespace = f"{namespace}.{module_name}"
515
516 spec = importlib.util.spec_from_file_location(full_namespace, module_path)
517 module = importlib.util.module_from_spec(spec)
518 sys.modules[full_namespace] = module
519 spec.loader.exec_module(module)
520
521 # for every top-level variable in the .py file
522 for variable in module.__dict__.keys():
523 # get its value
524 value = getattr(module, variable)
525 with suppress(AttributeError):
526 # if it has watched_events and produced_events
527 if all(
528 type(a) == list
529 for a in (getattr(value, "watched_events", None), getattr(value, "produced_events", None))
530 ):
531 # and if its variable name matches its filename
532 if value.__name__.lower() == module_name.lower():
533 value._name = module_name
534 # then we have a module
535 return value
536
537 def check_dependency(self, event_type, modname, produced):
538 if event_type not in produced:

Callers 1

load_modulesMethod · 0.95

Calls 1

keysMethod · 0.80

Tested by

no test coverage detected