Coverage for girder/utility/plugin_utilities : 82%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: utf-8 -*-
############################################################################### # Copyright Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###############################################################################
The purpose of this module is to provide utility functions related to loading and registering plugins into the system. The "loadPlugins" and "loadPlugin" functions are used by the core system to actually load the plugins and create the pseudo-packages required for easily referencing them. The other functions in this class are general utility functions designed to be used within the plugins themselves to help them register with the application. """
""" Loads a set of plugins into the application. The list passed in should not already contain dependency information; dependent plugins will be loaded automatically.
:param plugins: The set of plugins to load, by directory name. :type plugins: list :param root: The root node of the server tree. :param appconf: The server's cherrypy configuration object. :type appconf: dict :returns: A list of plugins that were actually loaded, once dependencies were resolved and topological sort was performed. """ # Register a pseudo-package for the root of all plugins. This must be # present in the system module list in order to avoid import warnings. '__path__': os.path.join(ROOT_DIR, 'plugins'), '__package__': ROOT_PLUGINS_PACKAGE, '__name__': ROOT_PLUGINS_PACKAGE })()
for pluginName, info in findAllPlugins().iteritems() if pluginName in plugins}
except: print TerminalColor.error( 'ERROR: Failed to load plugin "{}":'.format(plugin)) traceback.print_exc()
""" Loads a plugin into the application. This means allowing it to create endpoints within its own web API namespace, and to register its event listeners, and do anything else it might want to do.
:param name: The name of the plugin (i.e. its directory name) :type name: str :param root: The root node of the web API. :param appconf: The cherrypy configuration for the server. :type appconf: dict """ raise Exception('Plugin directory does not exist: {}'.format(pluginDir)) # This plugin does not have any server-side python code. return
# If the plugin has mail templates, add them to the lookup path mail_utils.addTemplateDirectory(mailTemplatesDir)
'name': name, 'config': appconf, 'serverRoot': root, 'apiRoot': root.api.v1 }) finally: fp.close()
""" Walks the plugins directory to find all of the plugins. If the plugin has a plugin.json file, this reads that file to determine dependencies. """ os.path.join(pluginsDir, dir))]
'name': data.get('name', plugin), 'description': data.get('description', ''), 'dependencies': set(data.get('dependencies', [])) }
""" General-purpose topoligical sort function. Dependencies are expressed as a dictionary whose keys are items and whose values are a set of dependent items. Output is a list of sets in topological order. This is a generator function that returns a sequence of sets in topological order.
:param data: The dependency information. :type data: dict :returns: Yields a list of sorted sets representing the sorted order. """
# Ignore self dependencies.
# Find all items that don't depend on anything. set.union, data.itervalues()) - set(data.iterkeys()) # Add empty dependences where needed
# Perform the toposort. for item, dep in data.iteritems() if item not in ordered} # Detect any cycles in the dependency graph. raise Exception('Cyclic dependencies detected:\n%s' % '\n'.join( repr(x) for x in data.iteritems()))
""" Use this to build paths to your plugin's endpoints.
:param node: The parent node to add the child node to. :param name: The name of the child node in the URL path. :type name: str :param obj: The object to place at this new node, or None if this child should not be exposed as an endpoint, instead just used as an intermediary hidden node. :type obj: object or None :returns: The node that was created. """ if obj: setattr(node, name, obj) return obj else: hiddenNode = type('', (), dict(exposed=False))() setattr(node, name, hiddenNode) return hiddenNode |