def event_entrypoints(): """Declare some events by mocking the invenio_stats.events entrypoint. It yields a list like [{event_type: <event_type_name>}, ...]. """ data = [] result = [] for idx in range(5): event_type_name = 'event_{}'.format(idx) from pkg_resources import EntryPoint entrypoint = EntryPoint(event_type_name, event_type_name) conf = dict(event_type=event_type_name, templates='/', processor_class=EventsIndexer) entrypoint.load = lambda conf=conf: (lambda: [conf]) data.append(entrypoint) result.append(conf) # including file_download event_type_name = 'file-download' from pkg_resources import EntryPoint entrypoint = EntryPoint('invenio_files_rest', 'test_dir') conf = dict(event_type=event_type_name, templates='contrib/file-download', processor_class=EventsIndexer) entrypoint.load = lambda conf=conf: (lambda: [conf]) data.append(entrypoint) entrypoints = mock_iter_entry_points_factory(data, 'invenio_stats.events') with patch('invenio_stats.ext.iter_entry_points', entrypoints): yield result
def testDeprecationWarnings(self): ep = EntryPoint( "foo", "pkg_resources.tests.test_resources", ["TestEntryPoints"], ["x"] ) with pytest.warns(pkg_resources.PkgResourcesDeprecationWarning): ep.load(require=False)
def container(self): container = PluginContainer('asphalt.core.test_plugin_container', BaseDummyPlugin) entrypoint = EntryPoint('dummy', 'test_util') entrypoint.load = Mock(return_value=DummyPlugin) container._entrypoints = {'dummy': entrypoint} return container
def setUp(self): self.mgr = cmds.CommandManager() self.mgr.load_namespace('contrail_api_cli.shell_command') ep = EntryPoint('cmd', 'contrail_api_cli.tests.test_command', attrs=('Cmd',)) cls = ep.load(require=False) ext = Extension('cmd', ep, cls, cls('cmd')) self.mgr.mgrs[0].extensions.append(ext)
def query_entrypoints(custom_permission_factory): """Same as event_entrypoints for queries.""" from pkg_resources import EntryPoint entrypoint = EntryPoint('invenio_stats', 'queries') data = [] result = [] conf = [ dict(query_name='test-query', query_class=CustomQuery, query_config=dict(index='stats-file-download', copy_fields=dict(bucket_id='bucket_id', ), required_filters=dict(bucket_id='bucket_id', )), permission_factory=custom_permission_factory), dict(query_name='test-query2', query_class=CustomQuery, query_config=dict(index='stats-file-download', copy_fields=dict(bucket_id='bucket_id', ), required_filters=dict(bucket_id='bucket_id', )), permission_factory=custom_permission_factory) ] result += conf result += register_queries() entrypoint.load = lambda conf=conf: (lambda: result) data.append(entrypoint) entrypoints = mock_iter_entry_points_factory(data, 'invenio_stats.queries') with patch('invenio_stats.ext.iter_entry_points', entrypoints): yield result
def processor_entrypoints(): """Entrypoint fixture.""" eps = [] event_type_name = DummyProcessor.id entrypoint = EntryPoint(event_type_name, event_type_name) entrypoint.load = lambda: lambda: DummyProcessor eps.append(entrypoint) return mock_iter_entry_points_factory(eps, 'invenio_files_processor')
def __init__(self, entry_point: pkg_resources.EntryPoint, with_prefix=False): self.name = self.entry_point_to_plugin_name(entry_point, with_prefix) self.plugin_cls: Type[interfaces.Plugin] = entry_point.load() self.entry_point = entry_point self.warning_message: Optional[str] = None self._initialized: Optional[interfaces.Plugin] = None self._prepared: Optional[Union[bool, Error]] = None self._hidden = False self._long_description: Optional[str] = None
def load_instrumentor( # pylint: disable=no-self-use self, entry_point: EntryPoint): """Takes a collection of instrumentation entry points and activates them by instantiating and calling instrument() on each one. Distros can override this method to customize the behavior by inspecting each entry point and configuring them in special ways, passing additional arguments, load a replacement/fork instead, skip loading entirely, etc. """ instrumentor: BaseInstrumentor = entry_point.load() instrumentor().instrument()
def _load_plugin(self, ep: pkg_resources.EntryPoint): try: self.logger.debug(" Loading plugin %s" % ep) plugin = ep.load(require=True) self.logger.debug(" Initializing plugin %s" % ep) plugin_context = copy.copy(self.app_context) plugin_context.logger = self.logger.getChild(ep.name) obj = plugin(plugin_context) return Plugin(ep.name, ep, obj) except ImportError as ie: self.logger.warn("Plugin %r import failed: %s" % (ep, ie)) except pkg_resources.UnknownExtra as ue: self.logger.warn("Plugin %r dependencies resolution failed: %s" % (ep, ue))
def _load_plugin(self, ep: pkg_resources.EntryPoint): try: self.logger.debug(" Loading plugin %s" % ep) plugin = ep.load(require=True) self.logger.debug(" Initializing plugin %s" % ep) plugin_context = copy.copy(self.app_context) plugin_context.logger = self.logger.getChild(ep.name) obj = plugin(plugin_context) return Plugin(ep.name, ep, obj) except ImportError as ie: self.logger.warning("Plugin %r import failed: %s" % (ep, ie)) except pkg_resources.UnknownExtra as ue: self.logger.warning("Plugin %r dependencies resolution failed: %s" % (ep, ue))
def template_entrypoints(): """Declare some events by mocking the invenio_stats.events entrypoint. It yields a list like [{event_type: <event_type_name>}, ...]. """ eps = [] for idx in range(5): event_type_name = 'mock_module' from pkg_resources import EntryPoint entrypoint = EntryPoint(event_type_name, event_type_name) entrypoint.load = lambda: lambda: ['mock_module.templates'] eps.append(entrypoint) entrypoints = mock_iter_entry_points_factory(eps, 'invenio_search.templates') return entrypoints
def test_duplicate_queue(app): """Check that duplicate queues raise an exception.""" with app.app_context(): data = [] for idx in range(2): queue_name = 'myqueue' entrypoint = EntryPoint(queue_name, queue_name) conf = dict(name=queue_name, exchange=MOCK_MQ_EXCHANGE) entrypoint.load = lambda conf=conf: (lambda: [conf]) data.append(entrypoint) entrypoints = mock_iter_entry_points_factory(data) with patch('pkg_resources.iter_entry_points', entrypoints): with pytest.raises(DuplicateQueueError): current_queues.queues()
def test_add_component(self, use_entrypoint): """ Tests that add_component works with an without an entry point and that external configuration overriddes local (hardcoded) configuration values. """ app = CustomApp({'shutdown': {'method': 'stop'}}) if not use_entrypoint: app.add_component('shutdown', ShutdownComponent, method='exception') else: entrypoint = EntryPoint('shutdown', __spec__.name) entrypoint.load = Mock(return_value=ShutdownComponent) app.component_types['shutdown'] = entrypoint app.add_component('shutdown', method='exception') assert len(app.components) == 1 assert isinstance(app.components[0], ShutdownComponent) assert app.components[0].method == 'stop'
def test_queues_entrypoints(app): """Declare some queues by mocking the invenio_queues.queues entrypoint. It yields a list like [{name: queue_name, exchange: conf}, ...]. """ data = [] result = [] for idx in range(5): queue_name = 'queue{}'.format(idx) from pkg_resources import EntryPoint entrypoint = EntryPoint(queue_name, queue_name) conf = dict(name=queue_name, exchange=MOCK_MQ_EXCHANGE) entrypoint.load = lambda conf=conf: (lambda: [conf]) data.append(entrypoint) result.append(conf) entrypoints = mock_iter_entry_points_factory(data) with patch('pkg_resources.iter_entry_points', entrypoints): try: yield result finally: remove_queues(app)
def _load(entry: EntryPoint) -> Any: try: return entry.load() except Exception as e: raise ImportError('Error on loading entry {}'.format(entry)) from e
def __init__(self, entrypoint: pkg_resources.EntryPoint) -> None: super().__init__(entrypoint.name, entrypoint.load()) self.entrypoint = entrypoint