def get_exporter(name): """Given an exporter name or import path, return a class ready to be instantiated Raises ValueError if exporter is not found """ if name == 'ipynb': name = 'notebook' try: return entrypoints.get_single('nbconvert.exporters', name).load() except entrypoints.NoSuchEntryPoint: try: return entrypoints.get_single('nbconvert.exporters', name.lower()).load() except entrypoints.NoSuchEntryPoint: pass if '.' in name: try: return import_item(name) except ImportError: log = get_logger() log.error("Error importing %s" % name, exc_info=True) raise ExporterNameError('Unknown exporter "%s", did you mean one of: %s?' % (name, ', '.join(get_export_names())))
def get_exporter(name): """Given an exporter name or import path, return a class ready to be instantiated Raises ValueError if exporter is not found """ if name == 'ipynb': name = 'notebook' try: return entrypoints.get_single('nbconvert.exporters', name).load() except entrypoints.NoSuchEntryPoint: try: return entrypoints.get_single('nbconvert.exporters', name.lower()).load() except entrypoints.NoSuchEntryPoint: pass if '.' in name: try: return import_item(name) except ImportError: log = get_logger() log.error("Error importing %s" % name, exc_info=True) raise ValueError('Unknown exporter "%s", did you mean one of: %s?' % (name, ', '.join(get_export_names())))
def test_get_single(): ep = entrypoints.get_single('entrypoints.test1', 'abc', sample_path) assert ep.module_name == 'foo' assert ep.object_name == 'abc' ep2 = entrypoints.get_single('entrypoints.test1', 'njn', sample_path) assert ep.module_name == 'foo' assert ep.object_name == 'abc'
def test_dot_prefix(): ep = entrypoints.get_single('blogtool.parsers', '.rst', sample_path) assert ep.object_name == 'SomeClass.some_classmethod' assert ep.extras == ['reST'] group = entrypoints.get_group_named('blogtool.parsers', sample_path) assert set(group.keys()) == {'.rst'}
def get_exporter(name): """ given an exporter name, return a class ready to be instantiate Raises ValueError if exporter is not found """ if name.lower() in exporter_map: return exporter_map[name.lower()] if '.' in name: try: return import_item(name) except ImportError: log = logging.getLogger() log.error("Error importing %s" % name, exc_info=True) pass else: try: return entrypoints.get_single('nbconvert.exporter', name).load() except entrypoints.NoSuchEntryPoint: pass valid_names = sorted( get_export_names() + list(entrypoints.get_group_named('nbconvert.exporter'))) raise ValueError('Unknown exporter "%s", did you mean one of: %s?' % (name, ', '.join(valid_names)))
def _dispatch(file): # Ensure mimetypes is initialized. (This step pulls from the operating # system's MIME type registry.) mimetypes.init() if isinstance(file, str): # file is inferred to be a filename or filename glob. mimetype, _ = mimetypes.guess_type(file) else: # file is inferred to be a file buffer, which has a name attribute. # If this is a non-file-based buffer like a StringIO object it won't # have a name, in which case we can't infer the type this way. # In the future, we could employ libraries that peek at the first # couple bytes and infer the MIME type from the file signature, which # would work on any buffer. try: filename = file.name except AttributeError: raise DispatchError( "Expected a filename or file buffer with a 'name' attribute.") mimetype, _ = mimetypes.guess_type(filename) if mimetype is None: raise DispatchError(f"Could not detect MIME type of {file}") try: entrypoint = entrypoints.get_single('TBD.readers', mimetype) except entrypoints.NoSuchEntryPoint: raise DispatchError(f"No PIMS reader found for MIME type {mimetype}") reader = entrypoint.load() return reader
def _get_provisioner(self, name: str) -> EntryPoint: """Wrapper around entrypoints.get_single() - primarily to facilitate testing.""" try: ep = get_single(KernelProvisionerFactory.GROUP_NAME, name) except NoSuchEntryPoint: # Check if the entrypoint name is 'local-provisioner'. Although this should never # happen, we have seen cases where the previous distribution of jupyter_client has # remained which doesn't include kernel-provisioner entrypoints (so 'local-provisioner' # is deemed not found even though its definition is in THIS package). In such cass, # the entrypoints package uses what it first finds - which is the older distribution # resulting in a violation of a supposed invariant condition. To address this scenario, # we will log a warning message indicating this situation, then build the entrypoint # instance ourselves - since we have that information. if name == 'local-provisioner': distros = glob.glob( f"{path.dirname(path.dirname(__file__))}-*") self.log.warning( f"Kernel Provisioning: The 'local-provisioner' is not found. This is likely " f"due to the presence of multiple jupyter_client distributions and a previous " f"distribution is being used as the source for entrypoints - which does not " f"include 'local-provisioner'. That distribution should be removed such that " f"only the version-appropriate distribution remains (version >= 7). Until " f"then, a 'local-provisioner' entrypoint will be automatically constructed " f"and used.\nThe candidate distribution locations are: {distros}" ) ep = EntryPoint('local-provisioner', 'jupyter_client.provisioning', 'LocalProvisioner') else: raise return ep
def get_exporter(name): """ given an exporter name, return a class ready to be instantiate Raises ValueError if exporter is not found """ if name.lower() in exporter_map: return exporter_map[name.lower()] if '.' in name: try: return import_item(name) except ImportError: log = logging.getLogger() log.error("Error importing %s" % name, exc_info=True) pass else: try: return entrypoints.get_single('nbconvert.exporter', name).load() except entrypoints.NoSuchEntryPoint: pass valid_names = sorted(get_export_names() + list(entrypoints.get_group_named('nbconvert.exporter'))) raise ValueError('Unknown exporter "%s", did you mean one of: %s?' % (name, ', '.join(valid_names)))
def enable(self, name: str) -> None: """Enable a plugin by name.""" if name not in self._plugins: ep = entrypoints.get_single(self.entry_point_group, name) value = cast(PluginType, ep.load()) assert isinstance(value, self.plugin_type) self.register(name, value) self._active = self._plugins[name]
def test_load_zip(tmpdir): whl_file = str(tmpdir / 'parmesan-1.2.whl') with ZipFile(whl_file, 'w') as whl: whl.writestr('parmesan-1.2.dist-info/entry_points.txt', b'[entrypoints.test.inzip]\na = edam:gouda') whl.writestr('gruyere-2!1b4.dev0.egg-info/entry_points.txt', b'[entrypoints.test.inzip]\nb = wensleydale:gouda') ep = entrypoints.get_single('entrypoints.test.inzip', 'a', [str(whl_file)]) assert ep.module_name == 'edam' assert ep.object_name == 'gouda' assert ep.distro.name == 'parmesan' assert ep.distro.version == '1.2' ep2 = entrypoints.get_single('entrypoints.test.inzip', 'b', [str(whl_file)]) assert ep2.module_name == 'wensleydale' assert ep2.object_name == 'gouda' assert ep2.distro.name == 'gruyere' assert ep2.distro.version == '2!1b4.dev0'
def get_exporter(name, config=get_config()): """Given an exporter name or import path, return a class ready to be instantiated Raises ExporterName if exporter is not found or ExporterDisabledError if not enabled """ if name == 'ipynb': name = 'notebook' try: exporter = entrypoints.get_single('nbconvert.exporters', name).load() if getattr(exporter(config=config), 'enabled', True): return exporter else: raise ExporterDisabledError( 'Exporter "%s" disabled in configuration' % (name)) except entrypoints.NoSuchEntryPoint: try: exporter = entrypoints.get_single('nbconvert.exporters', name.lower()).load() if getattr(exporter(config=config), 'enabled', True): return exporter else: raise ExporterDisabledError( 'Exporter "%s" disabled in configuration' % (name)) except entrypoints.NoSuchEntryPoint: pass if '.' in name: try: exporter = import_item(name) if getattr(exporter(config=config), 'enabled', True): return exporter else: raise ExporterDisabledError( 'Exporter "%s" disabled in configuration' % (name)) except ImportError: log = get_logger() log.error("Error importing %s" % name, exc_info=True) raise ExporterNameError('Unknown exporter "%s", did you mean one of: %s?' % (name, ', '.join(get_export_names())))
def load_extensions(extensions): ret = [] for ext_name in extensions: try: ext = entrypoints.get_single("nginx2es.ext", ext_name) except entrypoints.NoSuchEntryPoint: raise ValueError("%s not found in \"nginx2es.ext\" " "entrypoints" % ext_name) ret.append(ext.load()) return ret
def test_bad(): bad_path = [osp.join(samples_dir, 'packages3')] with warnings.catch_warnings(record=True) as w: group = entrypoints.get_group_named('entrypoints.test1', bad_path) assert 'bad' not in group assert len(w) == 1 with warnings.catch_warnings(record=True) as w2, \ pytest.raises(entrypoints.NoSuchEntryPoint): ep = entrypoints.get_single('entrypoints.test1', 'bad') assert len(w) == 1
def _get_language_exporter(self, lang_name): """Find an exporter for the language name from notebook metadata. Uses the nbconvert.exporters.script group of entry points. Returns None if no exporter is found. """ if lang_name not in self._lang_exporters: try: Exporter = entrypoints.get_single('nbconvert.exporters.script', lang_name).load() except entrypoints.NoSuchEntryPoint: self._lang_exporters[lang_name] = None else: self._lang_exporters[lang_name] = Exporter(parent=self) return self._lang_exporters[lang_name]
def _get_language_exporter(self, lang_name): """Find an exporter for the language name from notebook metadata. Uses the nbconvert.exporters.script group of entry points. Returns None if no exporter is found. """ if lang_name not in self._lang_exporters: try: Exporter = entrypoints.get_single( 'nbconvert.exporters.script', lang_name).load() except entrypoints.NoSuchEntryPoint: self._lang_exporters[lang_name] = None else: self._lang_exporters[lang_name] = Exporter(parent=self) return self._lang_exporters[lang_name]
def enable(self, name: str) -> None: """Enable a plugin by name.""" if name not in self._plugins: try: ep = entrypoints.get_single(self.entry_point_group, name) except entrypoints.NoSuchEntryPoint as err: if name in self.entrypoint_err_messages: raise ValueError(self.entrypoint_err_messages[name]) else: raise value = cast(PluginType, ep.load()) assert isinstance(value, self.plugin_type) self.register(name, value) self._active_name = name self._active = self._plugins[name]
def export(filter_, extra_data, strategy, file_format): q = predict.db.Session.query(predict.models.Label) l = LtoList(q.all()) or [] if (extra_data != None): for data in extra_data: extra = entrypoints.get_single("predict.plugins", data).load() extra = extra(l, q) l = extra.add_data() if (filter_ != "none"): filter = entrypoints.get_single("predict.plugins", filter_).load() filter = filter(l) l = filter.filterl() if (strategy != "none"): strat = entrypoints.get_single("predict.plugins", strategy).load() strat = strat(l, q) l = strat.resolve() file_format = entrypoints.get_single("predict.plugins", file_format).load() file_format = file_format(l) return file_format.generate()
def test_edf_ingestor(tmp_path): from fabio.edfimage import EdfImage # test data data = np.random.random((1000, 1000)) # write data to test edf edf_path = os.path.join(tmp_path, "test.edf") print("edf_path:", edf_path) EdfImage(data).write(edf_path) # get edf ingestor edf_ingestor = entrypoints.get_single("databroker.ingestors", "application/x-edf").load() # load data into catalog document = list(edf_ingestor([edf_path])) uid = document[0][1]["uid"]
def _get_language_exporter(self, lang_name): """Find an exporter for the language name from notebook metadata. Uses the nbconvert.exporters.script group of entry points. Returns None if no exporter is found. """ if lang_name not in self._lang_exporters: try: Exporter = entrypoints.get_single( 'nbconvert.exporters.script', lang_name).load() except entrypoints.NoSuchEntryPoint: self._lang_exporters[lang_name] = None else: # TODO: passing config is wrong, but changing this revealed more complicated issues self._lang_exporters[lang_name] = Exporter(config=self.config, parent=self) return self._lang_exporters[lang_name]
def _enable(self, name, **options): # type: (str, **Any) -> None if name not in self._plugins: try: ep = entrypoints.get_single(self.entry_point_group, name) except entrypoints.NoSuchEntryPoint: if name in self.entrypoint_err_messages: raise ValueError(self.entrypoint_err_messages[name]) else: raise value = cast(PluginType, ep.load()) assert isinstance(value, self.plugin_type) self.register(name, value) self._active_name = name self._active = self._plugins[name] self._options = options
def _get_entrypoints_lib(group, name=None): import entrypoints # Monkey patch some attributes for better API compatibility entrypoints.EntryPoint.dist = property(lambda self: self.distro) if name: return entrypoints.get_single(group, name) else: from collections import OrderedDict # Copied from 'get_group_named()' except that it preserves order result = OrderedDict() for ep in entrypoints.get_group_all(group): if ep.name not in result: result[ep.name] = ep return result
def _enable(self, name, **options): # type: (str, **Any) -> None if name not in self._plugins: try: ep = entrypoints.get_single(self.entry_point_group, name) except entrypoints.NoSuchEntryPoint: if name in self.entrypoint_err_messages: raise ValueError(self.entrypoint_err_messages[name]) else: raise value = cast(PluginType, ep.load()) assert isinstance(value, self.plugin_type) self.register(name, value) self._active_name = name self._active = self._plugins[name] for key in set(options.keys()) & set(self._global_settings.keys()): self._global_settings[key] = options.pop(key) self._options = options
def load_backend(backend_name): # Static backends if backend_name in MLFLOW_BACKENDS: return MLFLOW_BACKENDS[backend_name]() # backends from plugin try: backend_builder = entrypoints.get_single(ENTRYPOINT_GROUP_NAME, backend_name).load() return backend_builder() except entrypoints.NoSuchEntryPoint: # TODO Should be a error when all backends are migrated here available_entrypoints = entrypoints.get_group_all( ENTRYPOINT_GROUP_NAME) available_plugins = [ entrypoint.name for entrypoint in available_entrypoints] __logger__.warning("Backend '%s' is not available. Available plugins are %s", backend_name, available_plugins + list(MLFLOW_BACKENDS.keys())) return None
def test_ingestor(tmp_path): # test data data = np.random.random((1000, 1000)) # write data to test edf edf_path = os.path.join(tmp_path, "test.edf") print("edf_path:", edf_path) EdfImage(data).write(edf_path) # get edf ingestor edf_ingestor = entrypoints.get_single("databroker.ingestors", "application/edf").load() # load data into catalog document = list(edf_ingestor([edf_path])) uid = document[0][1]["uid"] catalog = BlueskyInMemoryCatalog() # TODO -- change upsert signature to put start and stop as kwargs # TODO -- ask about more convenient way to get a BlueskyRun from a document generator catalog.upsert(document[0][1], document[-1][1], edf_ingestor, ([edf_path], ), {}) return catalog[uid]
def test_missing(): with pytest.raises(entrypoints.NoSuchEntryPoint) as ec: entrypoints.get_single('no.such.group', 'no_such_name', sample_path) assert ec.value.group == 'no.such.group' assert ec.value.name == 'no_such_name'
def _get_provisioner(name: str) -> EntryPoint: """Wrapper around entrypoints.get_single() - primarily to facilitate testing.""" return get_single(KernelProvisionerFactory.GROUP_NAME, name)
def import_iservice(name): if '.' in name: return import_class(name) else: return entrypoints.get_single('pylar_iservices', name).load()