(g)ULP!
Loading...
Searching...
No Matches
gulp.plugin Namespace Reference

Classes

class  PluginBase
 

Functions

str _get_plugin_path (str plugin, GulpPluginType plugin_type=GulpPluginType.INGESTION)
 
list[str] get_plugin_tags (str plugin)
 
list[dict] list_plugins ()
 
PluginBase load_plugin (str plugin, GulpPluginType plugin_type=GulpPluginType.INGESTION, bool ignore_cache=False, **kwargs)
 
None plugin_cache_add (ModuleType m, str name)
 
None plugin_cache_clear ()
 
ModuleType plugin_cache_get (str plugin)
 
None plugin_cache_remove (str plugin)
 
None unload_plugin (PluginBase mod)
 

Variables

dict _cache = {}
 

Detailed Description

Gulp plugin base class and plugin utilities.

Function Documentation

◆ _get_plugin_path()

str _get_plugin_path ( str plugin,
GulpPluginType plugin_type = GulpPluginType.INGESTION )
protected

Definition at line 1017 of file plugin.py.

1019) -> str:
1020
1021 # try plain .py first
1022 # TODO: on license manager, disable plain .py load (only encrypted pyc)
1023 # get path according to plugin type
1024 path_plugins = config.path_plugins(plugin_type)
1025
1026 ppy = muty.file.safe_path_join(path_plugins, plugin + ".py")
1027 if not muty.file.exists(ppy):
1028 # try pyc
1029 ppyc = muty.file.safe_path_join(path_plugins, plugin + ".pyc")
1030 if not muty.file.exists(ppyc):
1031 raise ObjectNotFound(
1032 "plugin %s not found (tried %s, %s)" % (plugin, ppy, ppyc)
1033 )
1034 return ppyc
1035 return ppy
1036
1037
Here is the caller graph for this function:

◆ get_plugin_tags()

list[str] get_plugin_tags ( str plugin)
Get the tags for a given (ingestion) plugin.

Args:
    plugin (str): The name of the plugin to get the tags for.
Returns:
    list[str]: The tags for the given plugin.

Definition at line 1131 of file plugin.py.

1131async def get_plugin_tags(plugin: str) -> list[str]:
1132 """
1133 Get the tags for a given (ingestion) plugin.
1134
1135 Args:
1136 plugin (str): The name of the plugin to get the tags for.
1137 Returns:
1138 list[str]: The tags for the given plugin.
1139 """
1140 p = load_plugin(plugin)
1141 tags = p.tags()
1142 return tags
1143
1144
Here is the call graph for this function:

◆ list_plugins()

list[dict] list_plugins ( )
List all available plugins.

Returns:
    list[dict]: The list of available plugins.

Definition at line 1095 of file plugin.py.

1095async def list_plugins() -> list[dict]:
1096 """
1097 List all available plugins.
1098
1099 Returns:
1100 list[dict]: The list of available plugins.
1101 """
1102 path_plugins = config.path_plugins()
1103 files = await muty.file.list_directory_async(path_plugins, "*.py*", recursive=True)
1104 l = []
1105 for f in files:
1106 if "__init__" not in f and "__pycache__" not in f:
1107 try:
1108 p = load_plugin(f)
1109 n = {
1110 "display_name": p.name(),
1111 "type": str(p.type()),
1112 "desc": p.desc(),
1113 "filename": os.path.basename(p.path),
1114 "internal": p.internal(),
1115 "options": [o.to_dict() for o in p.options()],
1116 "depends_on": p.depends_on(),
1117 "tags": p.tags(),
1118 "event_type_field": p.event_type_field(),
1119 "version": p.version(),
1120 }
1121 l.append(n)
1122 unload_plugin(p)
1123 except Exception as ex:
1124 logger().exception(ex)
1125 logger().error("could not load plugin %s" % (f))
1126 continue
1127
1128 return l
1129
1130
Here is the call graph for this function:

◆ load_plugin()

PluginBase load_plugin ( str plugin,
GulpPluginType plugin_type = GulpPluginType.INGESTION,
bool ignore_cache = False,
** kwargs )
Load a plugin from a given path or from the default plugin path.

Args:
    plugin (str): The name or path of the plugin to load.
    plugin_type (GulpPluginType, optional): The type of the plugin to load. Defaults to GulpPluginType.INGESTION.
        this is ignored if the plugin is an absolute path or if "plugin_cache" is enabled and the plugin is already cached.
    ignore_cache (bool, optional): Whether to ignore the plugin cache. Defaults to False.
    **kwargs (dict, optional): Additional keyword arguments.
Returns:
    PluginBase: The loaded plugin.

Raises:
    Exception: If the plugin could not be loaded.

Definition at line 1038 of file plugin.py.

1043) -> PluginBase:
1044 """
1045 Load a plugin from a given path or from the default plugin path.
1046
1047 Args:
1048 plugin (str): The name or path of the plugin to load.
1049 plugin_type (GulpPluginType, optional): The type of the plugin to load. Defaults to GulpPluginType.INGESTION.
1050 this is ignored if the plugin is an absolute path or if "plugin_cache" is enabled and the plugin is already cached.
1051 ignore_cache (bool, optional): Whether to ignore the plugin cache. Defaults to False.
1052 **kwargs (dict, optional): Additional keyword arguments.
1053 Returns:
1054 PluginBase: The loaded plugin.
1055
1056 Raises:
1057 Exception: If the plugin could not be loaded.
1058 """
1059 logger().debug("load_plugin %s ..." % (plugin))
1060
1061 if not '/' in plugin and (plugin.lower().endswith(".py") or plugin.lower().endswith(".pyc")):
1062 # remove extension
1063 plugin = plugin.rsplit(".", 1)[0]
1064
1065 m = plugin_cache_get(plugin)
1066 if ignore_cache:
1067 logger().debug("ignoring cache for plugin %s" % (plugin))
1068 m = None
1069
1070 if m is not None:
1071 return m.Plugin(plugin, **kwargs)
1072
1073 if "/" in plugin:
1074 # plugins is an absolute path
1075 path = muty.file.abspath(plugin)
1076 else:
1077 # uses plugin_type to load from the correct subfolder
1078 path = _get_plugin_path(plugin, plugin_type=plugin_type)
1079
1080 try:
1081 m: PluginBase = muty.dynload.load_dynamic_module_from_file(path)
1082 except Exception as ex:
1083 raise Exception(
1084 "load_dynamic_module_from_file() failed, could not load plugin %s !"
1085 % (path)
1086 ) from ex
1087
1088 mod: PluginBase = m.Plugin(path, **kwargs)
1089 logger().debug("loaded plugin: %s" % (mod.name()))
1090 plugin_cache_add(m, plugin)
1091
1092 return mod
1093
1094
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plugin_cache_add()

None plugin_cache_add ( ModuleType m,
str name )
Add a plugin to the process's own plugin cache.

Args:
    m (ModuleType): The plugin module to add to the cache.
    name (str): The name/path of the plugin.

Returns:
    None

Definition at line 1206 of file plugin.py.

1206def plugin_cache_add(m: ModuleType, name: str) -> None:
1207 """
1208 Add a plugin to the process's own plugin cache.
1209
1210 Args:
1211 m (ModuleType): The plugin module to add to the cache.
1212 name (str): The name/path of the plugin.
1213
1214 Returns:
1215 None
1216 """
1217 global _cache
1218 if not config.plugin_cache_enabled():
1219 return
1220
1221 mm = _cache.get(name, None)
1222 if mm is None:
1223 logger().debug("adding plugin %s (%s) to cache" % (name, m))
1224 _cache[name] = m
1225
1226
Here is the caller graph for this function:

◆ plugin_cache_clear()

None plugin_cache_clear ( )
Clear the process's own plugin cache.

Returns:
    None

Definition at line 1170 of file plugin.py.

1170def plugin_cache_clear() -> None:
1171 """
1172 Clear the process's own plugin cache.
1173
1174 Returns:
1175 None
1176 """
1177 global _cache
1178 if not config.plugin_cache_enabled():
1179 return
1180
1181 _cache = {}
1182
1183

◆ plugin_cache_get()

ModuleType plugin_cache_get ( str plugin)
Retrieve a plugin from the process's own plugin cache.

Args:
    plugin (str): The name/path of the plugin to retrieve.

Returns:
    ModuleType: The plugin module if found in the cache, otherwise None.

Definition at line 1227 of file plugin.py.

1227def plugin_cache_get(plugin: str) -> ModuleType:
1228 """
1229 Retrieve a plugin from the process's own plugin cache.
1230
1231 Args:
1232 plugin (str): The name/path of the plugin to retrieve.
1233
1234 Returns:
1235 ModuleType: The plugin module if found in the cache, otherwise None.
1236 """
1237 global _cache
1238 if not config.plugin_cache_enabled():
1239 return None
1240
1241 p = _cache.get(plugin, None)
1242 if p is not None:
1243 logger().debug("found plugin %s in cache" % (plugin))
1244 else:
1245 logger().warning("plugin %s not found in cache" % (plugin))
1246 return p
Here is the caller graph for this function:

◆ plugin_cache_remove()

None plugin_cache_remove ( str plugin)
Remove a plugin from the process's own plugin cache.

Args:
    plugin (str): The name/path of the plugin to remove from the cache.

Returns:
    None

Definition at line 1184 of file plugin.py.

1184def plugin_cache_remove(plugin: str) -> None:
1185 """
1186 Remove a plugin from the process's own plugin cache.
1187
1188 Args:
1189 plugin (str): The name/path of the plugin to remove from the cache.
1190
1191 Returns:
1192 None
1193 """
1194 global _cache
1195 if not config.plugin_cache_enabled():
1196 return
1197
1198 if plugin in _cache:
1199 logger().debug("removing plugin %s from cache" % (plugin))
1200
1201 # cleanup module and delete
1202 m = _cache[plugin]
1203 del _cache[plugin]
1204
1205

◆ unload_plugin()

None unload_plugin ( PluginBase mod)
Unloads a plugin module by calling its `unload` method and deletes the module object

NOTE: mod is **no more valid** after this function returns.

Args:
    mod (PluginBase): The plugin module to unload.
    run_gc (bool): if set, garbage collector is called after unloading the module. Defaults to True.

Returns:
    None

Definition at line 1145 of file plugin.py.

1145def unload_plugin(mod: PluginBase) -> None:
1146 """
1147 Unloads a plugin module by calling its `unload` method and deletes the module object
1148
1149 NOTE: mod is **no more valid** after this function returns.
1150
1151 Args:
1152 mod (PluginBase): The plugin module to unload.
1153 run_gc (bool): if set, garbage collector is called after unloading the module. Defaults to True.
1154
1155 Returns:
1156 None
1157 """
1158 if config.plugin_cache_enabled():
1159 return
1160
1161 if mod is not None:
1162 # delete from cache if any
1163 # plugin_cache_delete(mod)
1164
1165 logger().debug("unloading plugin: %s" % (mod.name()))
1166 mod.cleanup()
1167 del mod
1168
1169
Here is the caller graph for this function:

Variable Documentation

◆ _cache

dict _cache = {}
protected

Definition at line 43 of file plugin.py.