importabcfromtypingimportUnion,Type"""The deephaven.plugin module provides an API and registration mechanism to add new behavior to the Deephavenserver. Plugins should be registered by adding a Registration instance as an entrypoint to the Python package."""__version__="0.6.0"DEEPHAVEN_PLUGIN_ENTRY_KEY="deephaven.plugin"DEEPHAVEN_PLUGIN_REGISTRATION_CLASS="registration_cls"
[docs]classCallback(abc.ABC):""" An instance of Callback will be passed to Registration.register_into, to allow any number of plugins to be registered. """
[docs]@abc.abstractmethoddefregister(self,plugin:Union[Plugin,Type[Plugin]])->None:""" Registers a given plugin type for use in the Deephaven server. Should be called from from a Registration's register_into method, so that it is available when the server expects it. :param plugin: the plugin or plugin type to register on the server :return: """pass
[docs]classRegistration(abc.ABC):""" Registration types should be set as the registration_cls for deephaven.plugin entrypoints for their package to ensure that they are all run on server startup. """
[docs]@classmethod@abc.abstractmethoddefregister_into(cls,callback:Callback)->None:""" Implement this method and reference this Registration type from the package's entrypoint to ensure that any provided plugins are available at server startup. Invoke callback.register() once for each provided plugin. :param callback: invoke this once per plugin to register them for use in the server. :return: """pass
defcollect_registration_entrypoints():importsysifsys.version_info<(3,8):fromimportlib_metadataimportentry_pointselifsys.version_info<(3,10):fromimportlib.metadataimportentry_pointsasepdefentry_points(group,name):# Looks to be a bug in 3.8, 3.9 where entries are doubled upentries=set(ep()[group]or[])return[eforeinentriesife.name==name]else:fromimportlib.metadataimportentry_pointsreturn(entry_points(group=DEEPHAVEN_PLUGIN_ENTRY_KEY,name=DEEPHAVEN_PLUGIN_REGISTRATION_CLASS)or[])defcollect_registration_classes():return[e.load()foreincollect_registration_entrypoints()]defregister_all_into(callback:Callback):forregistration_clsincollect_registration_classes():registration_cls.register_into(callback)deflist_registrations():output="Listing Deephaven Plugin registrations:"forregistration_clsincollect_registration_classes():output+=f"\n{registration_cls}"print(output)deflist_plugins():output="Listing Deephaven Plugins:"forregistration_clsincollect_registration_classes():output+=f"\n{registration_cls}"plugins=registration_cls.collect_plugins()output+="".join([f"\n{plugin}"forplugininplugins])print(output)
[docs]deflist_registrations_console():""" Entrypoint for the console script deephaven-plugin-list-registrations """list_registrations()
[docs]deflist_plugins_console():""" Entrypoint for the console script deephaven-plugin-list-plugins """list_plugins()