def get_plugins(baseclass='GeoHealthCheck.plugin.Plugin', filters=None): """ Class method to get list of Plugins of particular baseclass (optional), default is all Plugins. filters is a list of tuples to filter out Plugins with class var values: (class var, value), e.g. `filters=[('RESOURCE_TYPE', 'OGC:*'), ('RESOURCE_TYPE', 'OGC:WMS')]`. """ result = [] baseclass = Factory.create_class(baseclass) def add_result(plugin_name, class_obj): if not filters: result.append(plugin_name) else: vars = Factory.get_class_vars(class_obj) for filter in filters: if vars[filter[0]] == filter[1]: result.append(plugin_name) break plugins = App.get_plugins() for plugin_name in plugins: try: # Assume module first module = Factory.create_module(plugin_name) for name in dir(module): class_obj = getattr(module, name) # Must be a class object inheriting from baseclass # but not the baseclass itself if inspect.isclass(class_obj) \ and baseclass in inspect.getmro(class_obj) and \ baseclass != class_obj: add_result('%s.%s' % (plugin_name, name), class_obj) except Exception: # Try for full classname try: class_obj = Factory.create_class(plugin_name) if baseclass in inspect.getmro(class_obj) \ and baseclass != class_obj: add_result(plugin_name, class_obj) except Exception: LOGGER.warn('cannot create obj class=%s' % plugin_name) return result
def get_plugins(baseclass='GeoHealthCheck.plugin.Plugin', filters=None): """ Class method to get list of Plugins of particular baseclass (optional), default is all Plugins. filters is a list of tuples to filter out Plugins with class var values: (class var, value), e.g. `filters=[('RESOURCE_TYPE', 'OGC:*'), ('RESOURCE_TYPE', 'OGC:WMS')]`. """ result = [] baseclass = Factory.create_class(baseclass) def add_result(plugin_name, class_obj): if not filters: result.append(plugin_name) else: vars = Factory.get_class_vars(class_obj) for filter in filters: if vars[filter[0]] == filter[1]: result.append(plugin_name) break plugins = App.get_plugins() for plugin_name in plugins: try: # Assume module first module = Factory.create_module(plugin_name) for name in dir(module): class_obj = getattr(module, name) # Must be a class object inheriting from baseclass # but not the baseclass itself if inspect.isclass(class_obj) \ and baseclass in inspect.getmro(class_obj) and \ baseclass != class_obj: add_result('%s.%s' % (plugin_name, name), class_obj) except Exception: # Try for full classname try: class_obj = Factory.create_class(plugin_name) if baseclass in inspect.getmro(class_obj) \ and baseclass != class_obj: add_result(plugin_name, class_obj) except Exception: print('cannot create obj class=%s' % plugin_name) return result
def testPluginsPresent(self): plugins = Plugin.get_plugins('GeoHealthCheck.probe.Probe') for plugin in plugins: plugin = Factory.create_obj(plugin) self.assertIsNotNone(plugin) # Must have run_request method self.assertIsNotNone(plugin.run_request) plugins = Plugin.get_plugins('GeoHealthCheck.check.Check') for plugin in plugins: plugin = Factory.create_obj(plugin) self.assertIsNotNone(plugin) # Must have perform method self.assertIsNotNone(plugin.perform) plugins = Plugin.get_plugins( 'GeoHealthCheck.resourceauth.ResourceAuth') for plugin in plugins: plugin = Factory.create_obj(plugin) self.assertIsNotNone(plugin) # Must have encode method self.assertIsNotNone(plugin.encode) plugins = Plugin.get_plugins('GeoHealthCheck.probe.Probe', filters=[('RESOURCE_TYPE', 'OGC:*'), ('RESOURCE_TYPE', 'OGC:WMS')]) for plugin in plugins: plugin_class = Factory.create_class(plugin) self.assertIsNotNone(plugin_class) plugin_obj = Factory.create_obj(plugin) self.assertIsNotNone( plugin_obj, 'Cannot create Plugin from string %s' + plugin) parameters = plugin_obj.PARAM_DEFS self.assertTrue( type(parameters) is dict, 'Plugin Parameters not a dict') checks = plugin_obj.CHECKS_AVAIL self.assertTrue(type(checks) is dict, 'Plugin checks not a dict') # Must have run_request method self.assertIsNotNone(plugin_obj.run_request) # Must have class var RESOURCE_TYPE='OGC:WMS' class_vars = Factory.get_class_vars(plugin) self.assertIn(class_vars['RESOURCE_TYPE'], ['OGC:WMS', 'OGC:*'])
def get_plugins(baseclass='GeoHealthCheck.plugin.Plugin', filters=None): """ Class method to get list of Plugins of particular baseclass (optional), default is all Plugins. filters is a list of tuples to filter out Plugins with class var values: (class var, value), e.g. `filters=[('RESOURCE_TYPE', 'OGC:*'), ('RESOURCE_TYPE', 'OGC:WMS')]`. """ from GeoHealthCheck.init import APP # Plugins (via Docker ENV) must be list, but may have been # specified as comma-separated string, or older set notation def to_list(obj): obj_type = type(obj) if obj_type is str: return obj.replace(' ', '').split(',') elif obj_type is list: return obj elif obj_type is set: return list(obj) else: raise TypeError('unknown type for Plugin: %s' + str(obj_type)) APP.config['GHC_PLUGINS'] = to_list(APP.config['GHC_PLUGINS']) APP.config['GHC_USER_PLUGINS'] = \ to_list(APP.config['GHC_USER_PLUGINS']) # Concatenate core and user Plugins plugins = APP.config['GHC_PLUGINS'] + APP.config['GHC_USER_PLUGINS'] result = [] baseclass = Factory.create_class(baseclass) def add_result(plugin_name, class_obj): if not filters: result.append(plugin_name) else: vars = Factory.get_class_vars(class_obj) for filter in filters: if vars[filter[0]] == filter[1]: result.append(plugin_name) break for plugin_name in plugins: try: # Assume module first module = Factory.create_module(plugin_name) for name in dir(module): class_obj = getattr(module, name) # Must be a class object inheriting from baseclass # but not the baseclass itself if inspect.isclass(class_obj) \ and baseclass in inspect.getmro(class_obj) and \ baseclass != class_obj: add_result('%s.%s' % (plugin_name, name), class_obj) except: # Try for full classname try: class_obj = Factory.create_class(plugin_name) if baseclass in inspect.getmro(class_obj) \ and baseclass != class_obj: add_result(plugin_name, class_obj) except: print('cannot create obj class=%s' % plugin_name) return result