Source code for dlab_core.plugins

# *****************************************************************************
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#
# ******************************************************************************

import abc
import inspect
import sys

import pkg_resources
import six

from dlab_core.cli import show_help
from dlab_core.domain.exceptions import DLabException
from dlab_core.routing import CLIRoute

"""cli entry_points group name for plugins in setup.py"""
[docs]CLI_ENTRY_POINTS_GROUP_NAME = 'dlab.plugin.cli'
[docs]class PluginLoadException(DLabException): pass
[docs]@six.add_metaclass(abc.ABCMeta) class BasePlugin(object):
[docs] _plugins = {}
@classmethod
[docs] def load_plugins(cls, group): """ Load external plugins.""" plugins = [] for ep in pkg_resources.iter_entry_points(group=group): key = '.'.join([group, ep.name]) val = cls.load_entry_point(ep) if key in cls._plugins: raise PluginLoadException cls._plugins[key] = val plugins.append(val) return plugins
@classmethod
[docs] def clear_plugins(cls): """ Clear plugins.""" cls._plugins = {}
@classmethod
[docs] def load_entry_point(cls, ep): """ Load EntryPoint :type ep: EntryPoint :param ep: Setup EntryPoint. :raises PluginLoadException: """ plugin = ep.load() if not (inspect.isclass(plugin) and any([issubclass(plugin, base) for base in cls.__bases__]) and plugin is not cls): raise PluginLoadException() return plugin()
[docs]@six.add_metaclass(abc.ABCMeta) class BaseCLIPlugin(BasePlugin): @property
[docs] def routes(self): return self.base_routes + self.ep_routes
@property @abc.abstractmethod
[docs] def base_routes(self): raise NotImplementedError
@property
[docs] def ep_group(self): return None
@property
[docs] def ep_routes(self): routes = [] if self.ep_group: for plugin in self.load_plugins(self.ep_group): routes.extend(plugin.routes) return routes
[docs]@six.add_metaclass(abc.ABCMeta) class BaseAPIPlugin(BasePlugin): pass
[docs]class CLIPlugin(BaseCLIPlugin): @property
[docs] def ep_group(self): return CLI_ENTRY_POINTS_GROUP_NAME
@property
[docs] def base_routes(self): route = CLIRoute(show_help, {0: sys.argv[0]}) return [route]