def import_modules_by_entry_point(_packages=None): """Import plugins by entry-point 'rally_plugins'.""" loaded_packages = _packages or find_packages_by_entry_point() for package in loaded_packages: if "plugins_path" in package: em = pkg_resources.get_entry_map(package["name"]) ep = em["rally_plugins"]["path"] try: m = ep.load() if hasattr(m, "__path__"): path = pkgutil.extend_path(m.__path__, m.__name__) else: path = [m.__file__] prefix = m.__name__ + "." for loader, name, _is_pkg in pkgutil.walk_packages( path, prefix=prefix): sys.modules[name] = importlib.import_module(name) except Exception as e: msg = ( "\t Failed to load plugins from module '%(module)s' " "(package: '%(package)s')" % { "module": ep.module_name, "package": "%s %s" % (package["name"], package["version"]) }) if logging.is_debug(): LOG.exception(msg) else: LOG.warning(msg + (": %s" % six.text_type(e))) return loaded_packages
def get_themes(): """Return a dict of theme names and their locations""" themes = {} builtins = pkg_resources.get_entry_map(dist='mkdocs', group='mkdocs.themes') for theme in pkg_resources.iter_entry_points(group='mkdocs.themes'): if theme.name in builtins and theme.dist.key != 'mkdocs': raise exceptions.ConfigurationError( "The theme {0} is a builtin theme but {1} provides a theme " "with the same name".format(theme.name, theme.dist.key)) elif theme.name in themes: multiple_packages = [themes[theme.name].dist.key, theme.dist.key] log.warning( "The theme %s is provided by the Python packages " "'%s'. The one in %s will be used.", theme.name, ','.join(multiple_packages), theme.dist.key) themes[theme.name] = theme themes = dict( (name, os.path.dirname(os.path.abspath(theme.load().__file__))) for name, theme in themes.items()) return themes
def __init__(self, config): """ Initialize a cache manager. Automatically initializes instances of all registered cache backends based on setuptools' support for entry points which makes it possible for external Python packages to register additional cache backends without any modifications to pip-accel. :param config: The pip-accel configuration (a :py:class:`.Config` object). """ self.config = config for entry_point in get_entry_map('pip-accel', 'pip_accel.cache_backends').values(): logger.debug("Importing cache backend: %s", entry_point.module_name) __import__(entry_point.module_name) # Initialize instances of all registered cache backends (sorted by # priority so that e.g. the local file system is checked before S3). self.backends = sorted( (b(self.config) for b in registered_backends if b != AbstractCacheBackend), key=lambda b: b.PRIORITY) logger.debug("Initialized %s: %s", pluralize(len(self.backends), "cache backend"), concatenate(map(repr, self.backends)))
def import_modules_by_entry_point(_packages=None): """Import plugins by entry-point 'rally_plugins'.""" loaded_packages = _packages or find_packages_by_entry_point() for package in loaded_packages: if "plugins_path" in package: em = pkg_resources.get_entry_map(package["name"]) ep = em["rally_plugins"]["path"] try: m = ep.load() if hasattr(m, "__path__"): path = pkgutil.extend_path(m.__path__, m.__name__) else: path = [m.__file__] prefix = m.__name__ + "." for loader, name, _is_pkg in pkgutil.walk_packages( path, prefix=prefix): sys.modules[name] = importlib.import_module(name) except Exception as e: msg = ("\t Failed to load plugins from module '%(module)s' " "(package: '%(package)s')" % {"module": ep.module_name, "package": "%s %s" % (package["name"], package["version"])}) if logging.is_debug(): LOG.exception(msg) else: LOG.warning(msg + (": %s" % six.text_type(e))) return loaded_packages
def list_plugins(): entry_point_names = [ entry_point_name for entry_point_name in pkg_resources.get_entry_map( 'cylc-flow').keys() if entry_point_name.startswith('cylc.') ] entry_point_groups = { entry_point_name: [ entry_point for entry_point in iter_entry_points(entry_point_name) if not entry_point.module_name.startswith('cylc.flow') ] for entry_point_name in entry_point_names } dists = { entry_point.dist for entry_points in entry_point_groups.values() for entry_point in entry_points } if dists: print('\nPlugins:') maxlen1 = max(len(dist.project_name) for dist in dists) + 2 maxlen2 = max(len(dist.version) for dist in dists) + 2 for dist in dists: print(f' {dist.project_name.ljust(maxlen1)}' f' {dist.version.ljust(maxlen2)}' f' {dist.module_path}') print('\nEntry Points:') for entry_point_name, entry_points in entry_point_groups.items(): if entry_points: print(f' {entry_point_name}:') for entry_point in entry_points: print(f' {entry_point}')
def test_register_backend_entrypoint(): # Code adapted from pandas backend entry point testing # https://github.com/pandas-dev/pandas/blob/2470690b9f0826a8feb426927694fa3500c3e8d2/pandas/tests/plotting/test_backend.py#L50-L76 dist = pkg_resources.get_distribution("distributed") if dist.module_path not in distributed.__file__: # We are running from a non-installed distributed, and this test is invalid pytest.skip("Testing a non-installed distributed") mod = types.ModuleType("dask_udp") mod.UDPBackend = lambda: 1 sys.modules[mod.__name__] = mod entry_point_name = "distributed.comm.backends" backends_entry_map = pkg_resources.get_entry_map("distributed") if entry_point_name not in backends_entry_map: backends_entry_map[entry_point_name] = dict() backends_entry_map[entry_point_name]["udp"] = pkg_resources.EntryPoint( "udp", mod.__name__, attrs=["UDPBackend"], dist=dist) # The require is disabled here since particularly unit tests may install # dirty or dev versions which are conflicting with backend entrypoints if # they are demanding for exact, stable versions. This should not fail the # test result = get_backend("udp", require=False) assert result == 1
def test_duplicate_entries(self): entry_map = pkg_resources.get_entry_map('dhcpkit') # Steal the distribution from an existing entry dist = entry_map['dhcpkit.ipv6.options']['1'].dist # Test one ID appearing multiple times (can happen if other packages overwrite our keys) entry_map['dhcpkit.tests.registry'] = OrderedDict() entry_map['dhcpkit.tests.registry'][ '1'] = pkg_resources.EntryPoint.parse( '1 = dhcpkit.ipv6.options:ClientIdOption', dist=dist) entry_map['dhcpkit.tests.registry'][ '1 '] = pkg_resources.EntryPoint.parse( '1 = dhcpkit.ipv6.options:ServerIdOption', dist=dist) with self.assertLogs('dhcpkit.registry', logging.WARNING) as cm: test_registry = TestRegistry() self.assertEqual(len(cm.output), 1) self.assertRegex( cm.output[0], '^WARNING:.*:Multiple entry points found for TestRegistry 1') self.assertEqual(len(test_registry), 1) self.assertEqual(test_registry[1], ClientIdOption)
def test_register_entrypoint(restore_backend): dist = pkg_resources.get_distribution("pandas") if dist.module_path not in pandas.__file__: # We are running from a non-installed pandas, and this test is invalid pytest.skip("Testing a non-installed pandas") mod = types.ModuleType("my_backend") mod.plot = lambda *args, **kwargs: 1 backends = pkg_resources.get_entry_map("pandas") my_entrypoint = pkg_resources.EntryPoint( "pandas_plotting_backend", mod.__name__, dist=dist ) backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint # TODO: the docs recommend importlib.util.module_from_spec. But this works for now. sys.modules["my_backend"] = mod result = pandas.plotting._core._get_plot_backend("my_backend") assert result is mod # TODO(GH#27517): https://github.com/pandas-dev/pandas/issues/27517 # Remove the td.skip_if_no_mpl with pandas.option_context("plotting.backend", "my_backend"): result = pandas.plotting._core._get_plot_backend() assert result is mod
def get_themes(): """Return a dict of theme names and their locations""" themes = {} builtins = pkg_resources.get_entry_map(dist='mkdocs', group='mkdocs.themes') for theme in pkg_resources.iter_entry_points(group='mkdocs.themes'): if theme.name in builtins and theme.dist.key != 'mkdocs': raise exceptions.ConfigurationError( "The theme {0} is a builtin theme but {1} provides a theme " "with the same name".format(theme.name, theme.dist.key)) elif theme.name in themes: multiple_packages = [themes[theme.name].dist.key, theme.dist.key] log.warning("The theme %s is provided by the Python packages " "'%s'. The one in %s will be used.", theme.name, ','.join(multiple_packages), theme.dist.key) themes[theme.name] = theme themes = dict((name, os.path.dirname(os.path.abspath(theme.load().__file__))) for name, theme in themes.items()) return themes
def help(args, parser): if args: # TODO: First check for static help text file before # generating it at run time. ep = pkg_resources.get_entry_info("rbtools", "rbtools_commands", args[0]) if ep: help_text = build_help_text(ep.load()) print help_text sys.exit(0) print "No help found for %s" % args[0] sys.exit(0) parser.print_help() # TODO: For now we'll print every command found with an entry # point. In the future this needs to be switched to printing # a hard-coded list, so that we can include commands we create # using shell scripts etc. commands = pkg_resources.get_entry_map('rbtools', 'rbtools_commands') print "\nThe most commonly used commands are:" for command in commands: print " %s" % command print ("See '%s help <command>' for more information " "on a specific command." % RB_MAIN) sys.exit(0)
def get_settings_from_entry_points(settings, contexts): context_settings = {} for context in contexts: entry_points = pkg_resources.get_entry_map(context, __package__).values() for entry_point in entry_points: setting_name = entry_point.name if settings.get(setting_name): # don't overwrite settings given from the CLI continue value = entry_point.load() if callable(value): value = value() old_value = context_settings.get(setting_name) if (old_value and old_value != value or isinstance(old_value, list) and value not in old_value): if not isinstance(old_value, list): context_settings[setting_name] = [old_value] context_settings[setting_name].append(value) else: context_settings[setting_name] = value for name, value in context_settings.items(): settings[name] = value
def help(args, parser): if args: # TODO: First check for static help text file before # generating it at run time. ep = pkg_resources.get_entry_info("rbtools", "rbtools_commands", args[0]) if ep: help_text = build_help_text(ep.load()) print help_text sys.exit(0) print "No help found for %s" % args[0] sys.exit(0) parser.print_help() commands = pkg_resources.get_entry_map('rbtools', 'rbtools_commands') common_commands = ['post', 'patch', 'close', 'diff'] print "\nThe most commonly used commands are:" for command in common_commands: print " %s" % command print "\nOther commands:" for command in sorted(commands): if command not in common_commands: print " %s" % command print ("See '%s help <command>' for more information " "on a specific command." % RB_MAIN) sys.exit(0)
def __init__(self, server, language='aml'): """ @type server: firefly.containers.Server @type language: C{str} @param language: The language engine to use for this instance. """ # Set up logging self._log = logging.getLogger('firefly') self.language = None """@type : firefly.languages.interface.LanguageInterface""" self._load_language_interface(language) # Set up our registry and server containers, then run setup self.registry = _Registry(self) self.server_info = ServerInfo() self.server = server self._setup() self.load_language_files() # Set up our authentication manager self.auth = Auth(self) # Finally, now that everything is set up, load our plugins self.plugins = pkg_resources.get_entry_map('firefly_irc', 'firefly.plugins') scanner = venusian.Scanner(firefly=self) scanner.scan(plugins)
def loadPackage(name, seen, first=True): packages = [] if name in seen: return packages seen.add(name) try: dist = pkg_resources.get_distribution(name) for req in dist.requires(): pkg = req.project_name if pkg in seen: continue packages.extend(loadPackage(pkg, seen, False)) distmap = pkg_resources.get_entry_map(dist, 'ptah') ep = distmap.get('package') if ep is not None: if dist.has_metadata('top_level.txt'): packages.extend( [p.strip() for p in dist.get_metadata('top_level.txt').split()]) except pkg_resources.DistributionNotFound: pass if first and name not in packages and '-' not in name: packages.append(name) return packages
def test_import_extensions(self): ''' Does a basic import test of extensions listed in 'opp.commands'. ''' opp_commands = pkg_resources.get_entry_map('opp')['opp.commands'] for command in opp_commands.values(): command.load()
def get_settings_from_entry_points(settings, contexts): context_settings = {} for context in contexts: entry_points = pkg_resources.get_entry_map( context, __package__).values() for entry_point in entry_points: setting_name = entry_point.name if settings.get(setting_name): # don't overwrite settings given from the CLI continue value = entry_point.load() if callable(value): value = value() old_value = context_settings.get(setting_name) if (old_value and old_value != value or isinstance(old_value, list) and value not in old_value): if not isinstance(old_value, list): context_settings[setting_name] = [old_value] context_settings[setting_name].append(value) else: context_settings[setting_name] = value for name, value in context_settings.items(): settings[name] = value
def loadPackage(name, seen, first=True): packages = [] if name in seen: return packages seen.add(name) try: dist = pkg_resources.get_distribution(name) for req in dist.requires(): pkg = req.project_name if pkg in seen: continue packages.extend(loadPackage(pkg, seen, False)) distmap = pkg_resources.get_entry_map(dist, 'memphis') ep = distmap.get('package') if ep is not None: packages.append(ep.module_name) except pkg_resources.DistributionNotFound: pass if first and name not in packages and '-' not in name: packages.append(name) return packages
def _load_exporters(self): self._exporters = {} # Check if exporters are enabled and load them. if "exporters" in self._config: exporters = pkg_resources.get_entry_map("murakami", group="murakami.exporters") for name, entry in self._config["exporters"].items(): logging.debug("Loading exporter %s", name) enabled = True if "enabled" in entry: enabled = utils.is_enabled(entry["enabled"]) if enabled: if "type" in entry: if entry["type"] in exporters: self._exporters[name] = exporters[ entry["type"]].load()( name=name, location=self._location, network_type=self._network_type, connection_type=self._connection_type, config=entry, ) else: logging.error( "No available exporter type %s, skipping.", entry["type"], ) else: logging.error( "No type defined for exporter %s, skipping.", name) else: logging.debug("Exporter %s disabled, skipping.", name)
def load(plugin_name, plugin_entry_point_name=None): # TODO centralize 'qiime.plugin', it is used here and in PluginManager plugin_group = 'qiime.plugin' if plugin_entry_point_name is None: plugin_entry_point_name = plugin_name try: plugin = pkg_resources.load_entry_point( plugin_name, plugin_group, plugin_entry_point_name) except ImportError: try: plugin_entry_map = pkg_resources.get_entry_map(plugin_name) except pkg_resources.DistributionNotFoundError: raise ImportError("Plugin %r is not installed." % plugin_name) if plugin_group not in plugin_entry_map: raise ImportError( "Plugin %r is not a valid QIIME plugin. A valid QIIME plugin " "must define its entry points under the %r entry point group." % (plugin_name, plugin_group)) else: plugin_entry_point_names = set(plugin_entry_map[plugin_group]) raise ImportError( "Could not find entry point name %r in plugin %r. Available " "entry point names: %r" % (plugin_entry_point_name, plugin_entry_point_names)) else: if not isinstance(plugin, Plugin): raise ImportError( "Plugin %r is not a valid QIIME plugin. Expected type %r, " "not %r" % (Plugin.__name__, type(plugin).__name__)) return plugin
def get_metadata(path): """Retrieve metadata from a file or directory specified by path, or from the name of a distribution that happens to be installed. `path` can be an installed egg, a zipped egg file, or a zipped or unzipped tar file of a Python distutils or setuptools source distribution. Returns a dict. """ dist = None if os.path.isdir(path): dists = [x for x in find_distributions(path, only=True)] if len(dists) == 0: raise RuntimeError( '%s is not a zipped egg or an installed distribution' % path) dist = dists[0] if os.path.abspath(path).lower() != os.path.abspath( dist.location).lower(): raise RuntimeError( '%s is not a valid distribution (dist.location=%s)' % (os.path.abspath(path), dist.location)) metadata = get_dist_metadata(dist) elif os.path.isfile(path): if path.lower().endswith('.tar') or '.tar.gz' in path.lower(): # it's a tar file or gzipped tar file return _meta_from_tarfile(path) # getting access to metadata in a zipped egg doesn't seem to work # right, so just use a ZipFile to access files under the EGG-INFO # directory to retrieve the metadata. if path.lower().endswith('.egg'): # it's a zip file metadata = _meta_from_zipped_egg(path) elif path.lower().endswith('.zip'): metadata = _meta_from_zipfile(path) else: raise RuntimeError('cannot process file %s: unknown file type' % path) else: # look for an installed dist with the given name for d in working_set: if path == d.egg_name().split('-')[0]: dist = d metadata = get_dist_metadata(dist) break else: raise RuntimeError("Could not locate distribution '%s'" % path) if dist is not None: metadata['py_version'] = dist.py_version if 'platform' not in metadata or metadata['platform'] == 'UNKNOWN': metadata['platform'] = dist.platform metadata['entry_points'] = {} for gname, group in get_entry_map(dist, group=None).items(): metadata['entry_points'][gname] = [ep for ep in group] return metadata
def get_command(self, ctx, name): ep_list: list = [ a for a in list( get_entry_map(self.build_plugin_name).get( PLUGIN_ENTRYPOINT_GROUP, {})) ] ep = None if not ep_list else ep_list[0].load() return ep
def gstat_help(): entry_map = pkg_resources.get_entry_map("groupmestats") console_scripts = entry_map["console_scripts"] print("Available commands:") for name in console_scripts.keys(): if name != 'gstat_help': print(" - " + name) print("Run `<command> --help` for details on each command")
def scripts(reqs, working_set, executable, dest, scripts=None, extra_paths=(), arguments='', interpreter=None, initialization='', relative_paths=False, ): path = [dist.location for dist in working_set] path.extend(extra_paths) path = map(realpath, path) generated = [] if isinstance(reqs, str): raise TypeError('Expected iterable of requirements or entry points,' ' got string.') if initialization: initialization = '\n'+initialization+'\n' entry_points = [] for req in reqs: if isinstance(req, str): req = pkg_resources.Requirement.parse(req) dist = working_set.find(req) for name in pkg_resources.get_entry_map(dist, 'console_scripts'): entry_point = dist.get_entry_info('console_scripts', name) entry_points.append( (name, entry_point.module_name, '.'.join(entry_point.attrs)) ) else: entry_points.append(req) for name, module_name, attrs in entry_points: if scripts is not None: sname = scripts.get(name) if sname is None: continue else: sname = name sname = os.path.join(dest, sname) spath, rpsetup = _relative_path_and_setup(sname, path, relative_paths) generated.extend( _script(module_name, attrs, spath, sname, executable, arguments, initialization, rpsetup) ) if interpreter: sname = os.path.join(dest, interpreter) spath, rpsetup = _relative_path_and_setup(sname, path, relative_paths) generated.extend(_pyscript(spath, sname, executable, rpsetup)) return generated
def test_call_entrypoint(capsys, cmd): entrypoint = pkg_resources.get_entry_map( 'brozzler')['console_scripts'][cmd] callable = entrypoint.resolve() with pytest.raises(SystemExit): callable(['/whatever/bin/%s' % cmd, '--version']) out, err = capsys.readouterr() assert out == 'brozzler %s - %s\n' % (brozzler.__version__, cmd) assert err == ''
def post(self, command, output_dir, vars): self.lastlogs.append( '* A project has been created in %s.\n' % vars['path'] ) minilay = os.path.join(self.output_dir, 'minilays', vars['project']) if vars['inside_minitage']: self.lastlogs.append( '* A minilay has been installed in %s.\n' '* It contains those minilbuilds:' '\n\t- %s \n' '\n' '* Think to finish the versionning stuff and put this minilay and' ' the projet under revision control.\n' '* The project must be archived here \'%s\' using \'%s\' or change the minibuild ' 'src_uri/scm_type.\n' '* Install your project running: \n\t\tminimerge -v %s' ''% ( minilay, '\n\t- '.join(os.listdir(minilay)), vars['uri'], vars['scm_type'], vars['project'] ) ) self.lastlogs.append( '* You can additionnaly create related databases or configuration or' ' other stuff using minitage instances ' ' (http://minitage.org/paster/instances/index.html)\n' '* Available instances are: \n' '\t- %s\n' '* Some extra instances are contained inside the' '\'minitage.paste.extras package\', install it as a classical egg.\n' '* Run an instance with: \n' ' \tpaster create -t minitage.instances.PROFIL project\n' '\n' % ( '\n\t- '.join( ["%s (%s)" % ( a, pkg_resources.load_entry_point('minitage.paste', 'paste.paster_create_template', a ).summary ) for a in pkg_resources.get_entry_map( 'minitage.paste' )['paste.paster_create_template'] if 'minitage.instances' in a] ) ) ) README = os.path.join(vars['path'],'README.%s.txt' % vars['project']) open(README, 'w').write('\n'.join(self.lastlogs)) self.lastlogs.append( 'Those informations have been writed to %s.' % README ) return common.Template.post(self, command, output_dir, vars)
def get_metadata(path): """Retrieve metadata from a file or directory specified by path, or from the name of a distribution that happens to be installed. path can be an installed egg, a zipped egg file, or a zipped or unzipped tar file of a python distutils or setuptools source distribution. Returns a dict. """ dist = None if os.path.isdir(path): dists = [x for x in find_distributions(path, only=True)] if len(dists) == 0: raise RuntimeError('%s is not a zipped egg or an installed distribution'%path) dist = dists[0] if os.path.abspath(path).lower() != os.path.abspath(dist.location).lower(): raise RuntimeError('%s is not a valid distribution (dist.location=%s)'% (os.path.abspath(path),dist.location)) metadata = get_dist_metadata(dist) elif os.path.isfile(path): if path.lower().endswith('.tar') or '.tar.gz' in path.lower(): # it's a tar file or gzipped tar file return _meta_from_tarfile(path) # getting access to metadata in a zipped egg doesn't seem to work # right, so just use a ZipFile to access files under the EGG-INFO # directory to retrieve the metadata. if path.lower().endswith('.egg'): # it's a zip file metadata = _meta_from_zipped_egg(path) elif path.lower().endswith('.zip'): metadata = _meta_from_zipfile(path) else: raise RuntimeError('cannot process file %s: unknown file type' % path) else: # look for an installed dist with the given name for d in working_set: if path == d.egg_name().split('-')[0]: dist = d metadata = get_dist_metadata(dist) break else: raise RuntimeError("Could not locate distribution '%s'" % path) if dist is not None: metadata['py_version'] = dist.py_version if 'platform' not in metadata or metadata['platform']=='UNKNOWN': metadata['platform'] = dist.platform metadata['entry_points'] = {} for gname,group in get_entry_map(dist, group=None).items(): metadata['entry_points'][gname] = [ep for ep in group] return metadata
def take_action(self, parsed_args): names = set() for dist in pkg_resources.working_set: LOG.debug('checking distribution "%s"', dist) entry_map = pkg_resources.get_entry_map(dist) names.update(set(entry_map.keys())) return ( ('Name', ), ((n, ) for n in sorted(names)), )
def take_action(self, parsed_args): names = set() for dist in pkg_resources.working_set: LOG.debug('checking distribution "%s"', dist) entry_map = pkg_resources.get_entry_map(dist) names.update(set(entry_map.keys())) return ( ('Name',), ((n,) for n in sorted(names)), )
def parse_args(): ps = pkg_resources.get_entry_map('swiftlm', 'swiftlm.plugins') p = construct_parser(ps) args = p.parse_args() # we make the common case easy, No selected flags indicate that we should # run all diagnostics. if args.selected is None: args.selected = [f.load() for f in ps.values()] return args
def get_installed_scripts(): """Get list of installed scripts via ``pkg-resources``. See http://peak.telecommunity.com/DevCenter/PkgResources#convenience-api TODO: not sure if this will be useful ... maybe to check if the list of installed packages matches the available scripts somehow? """ from pkg_resources import get_entry_map console_scripts = get_entry_map('gammapy')['console_scripts'] return console_scripts
def test_entrypoint_tolerance(self): # loosely based on Pandas test from: # https://github.com/pandas-dev/pandas/pull/27488 # FIXME: Python 2 workaround because nonlocal doesn't exist counters = {"init": 0} def init_function(): counters["init"] += 1 raise ValueError("broken") mod = types.ModuleType("_test_numba_bad_extension") mod.init_func = init_function try: # will remove this module at the end of the test sys.modules[mod.__name__] = mod # We are registering an entry point using the "numba" package # ("distribution" in pkg_resources-speak) itself, though these are # normally registered by other packages. dist = "numba" entrypoints = pkg_resources.get_entry_map(dist) my_entrypoint = pkg_resources.EntryPoint( "init", # name of entry point mod.__name__, # module with entry point object attrs=["init_func"], # name of entry point object dist=pkg_resources.get_distribution(dist), ) entrypoints.setdefault("numba_extensions", {})["init"] = my_entrypoint from numba.core import entrypoints # Allow reinitialization entrypoints._already_initialized = False with warnings.catch_warnings(record=True) as w: entrypoints.init_all() bad_str = "Numba extension module '_test_numba_bad_extension'" for x in w: if bad_str in str(x): break else: raise ValueError("Expected warning message not found") # was our init function called? self.assertEqual(counters["init"], 1) finally: # remove fake module if mod.__name__ in sys.modules: del sys.modules[mod.__name__]
def get_groups_by_pattern(self, pattern): env = pkg_resources.Environment() eps = {} for project_name in env: for dist in env[project_name]: for name in pkg_resources.get_entry_map(dist): if pattern and not pattern.search(name): continue if (not pattern and name.startswith('paste.description.')): continue eps[name] = None return sorted(eps.keys())
def get_template_services(): services = [] entry_points = pkg_resources.get_entry_map('nodeconductor').get( 'template_services', {}) for name, entry_point in entry_points.iteritems(): service_cls = entry_point.load() service_model = service_cls.get_model() setattr(service_model, 'service_type', name) setattr(service_model, '_serializer', service_cls.get_serializer()) setattr(service_model, '_admin_form', service_cls.get_admin_form()) services.append(service_model) return services
def initAddons(ev): dir = os.path.join(os.getcwd(), 'addons') if not os.path.isdir(dir): return for item in os.listdir(dir): path = os.path.join(dir, item) for dist in pkg_resources.find_distributions(path, True): distmap = pkg_resources.get_entry_map(dist, 'memphis') if 'package' in distmap: ADDONS.append(dist)
def getModule(cls, pluginId): """ Utility function that returns a module object given a plugin name. pluginId: a string such as "evo", "DummyPlugin", etc. """ pmodules = pkg_resources.get_entry_map('indico', group='indico.ext') entry = pmodules.get('Collaboration.%s' % pluginId, None) if entry: __import__(entry.module_name, globals(), locals(), ['collaboration', 'pages', 'actions', 'fossils', 'services']) return sys.modules[entry.module_name] else: return None
def cli_commands(): commands = set( pkg_resources.get_entry_map('brozzler')['console_scripts'].keys()) commands.remove('brozzler-wayback') try: import gunicorn except ImportError: commands.remove('brozzler-dashboard') try: import pywb except ImportError: commands.remove('brozzler-easy') return commands
def get_config_data(package): """Read the default configuration-data section from the given package""" data = None try: dist = pkg_resources.get_distribution(package) entries = pkg_resources.get_entry_map(dist, "resilient.circuits.configsection") if entries: entry = next(iter(entries)) func = entries[entry].load() data = func() except pkg_resources.DistributionNotFound: pass return data or ""
def cli_commands(): commands = set(pkg_resources.get_entry_map( 'brozzler')['console_scripts'].keys()) commands.remove('brozzler-wayback') try: import gunicorn except ImportError: commands.remove('brozzler-dashboard') try: import pywb except ImportError: commands.remove('brozzler-easy') return commands
def getModule(cls, pluginId): """ Utility function that returns a module object given a plugin name. pluginId: a string such as "evo", "DummyPlugin", etc. """ pmodules = pkg_resources.get_entry_map('indico', group='indico.ext') entry = pmodules.get('Collaboration.%s' % pluginId, None) if entry: __import__( entry.module_name, globals(), locals(), ['collaboration', 'pages', 'actions', 'fossils', 'services']) return sys.modules[entry.module_name] else: return None
def get_customization_definitions(package): """Read the default configuration-data section from the given package""" data = None try: dist = pkg_resources.get_distribution(package) entries = pkg_resources.get_entry_map(dist, "resilient.circuits.customize") if entries: for entry in iter(entries): func = entries[entry].load() data = func(client=None) except pkg_resources.DistributionNotFound: pass return data or []
def main(): args = set(sys.argv[1:]) days = pkg_resources.get_entry_map('aoc2020', 'aoc2020.days') for day, entry in sorted(days.items(), key=lambda item: int(item[0])): if args and day not in args: continue print(f'Day {day}') with io.TextIOWrapper( pkg_resources.resource_stream('aoc2020', f'day{day}.txt')) as fh: data = fh.readlines() for part in entry.load(): print(part(data)) print()
def __init__(self, **traits): super(CameraDialog, self).__init__(**traits) # Load the plugins self.plugins = pkg_resources.get_entry_map('beams', 'camera_plugins') if 'dummy' not in self.plugins.keys(): raise IOError("Plugin directory isn't configured properly") # Try to select the webcam try: self._select_plugin_by_name('webcam') except ValueError: self.select_fallback()
def _load_entry_point_factory(self, resource, entry_point_groups): pkg, name = resource.split('#') entry_point_map = pkg_resources.get_entry_map(pkg) factory = None for group in entry_point_groups: if group in entry_point_map: group_map = entry_point_map[group] if name in group_map: factory = group_map[name].load() factory = self._adapt_entry_point_factory(factory, group) break if factory is None: raise Exception('TODO') return factory
def test_entrypoint_tolerance(): # FIXME: Python 2 workaround because nonlocal doesn't exist counters = {"init": 0} def init_function(): counters["init"] += 1 raise ValueError("broken") mod = types.ModuleType("_test_mars_bad_extension") mod.init_func = init_function try: # will remove this module at the end of the test sys.modules[mod.__name__] = mod # We are registering an entry point using the "mars" package # ("distribution" in pkg_resources-speak) itself, though these are # normally registered by other packages. dist = "pymars" entrypoints = pkg_resources.get_entry_map(dist) my_entrypoint = pkg_resources.EntryPoint( "init", # name of entry point mod.__name__, # module with entry point object attrs=["init_func"], # name of entry point object dist=pkg_resources.get_distribution(dist), ) entrypoints.setdefault("mars_extensions", {})["init"] = my_entrypoint from .. import entrypoints # Allow reinitialization entrypoints.init_extension_entrypoints.cache_clear() with warnings.catch_warnings(record=True) as w: entrypoints.init_extension_entrypoints() bad_str = "Mars extension module '_test_mars_bad_extension'" for x in w: if bad_str in str(x): break else: raise ValueError("Expected warning message not found") # was our init function called? assert counters["init"] == 1 finally: # remove fake module if mod.__name__ in sys.modules: del sys.modules[mod.__name__]
def setup(self): dist = pkg_resources.get_distribution("pandas") spec = importlib.machinery.ModuleSpec("my_backend", None) mod = importlib.util.module_from_spec(spec) mod.plot = lambda *args, **kwargs: 1 backends = pkg_resources.get_entry_map("pandas") my_entrypoint = pkg_resources.EntryPoint("pandas_plotting_backend", mod.__name__, dist=dist) backends["pandas_plotting_backends"][mod.__name__] = my_entrypoint for i in range(10): backends["pandas_plotting_backends"][str(i)] = my_entrypoint sys.modules["my_backend"] = mod
def _listEntryPoints(group): """Returns a dictionary containing ``{name : type}`` entry points for the given entry point group. https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points """ items = collections.OrderedDict() for plugin in listPlugins(): for name, ep in pkg_resources.get_entry_map(plugin, group).items(): if name in items: log.debug( 'Overriding entry point %s [%s] with entry point ' 'of the same name from %s', name, group, plugin) items[name] = ep.load() return items
def list_sorted_templates(filter_group=False): """ Returns a dictionary of template lists by category. Key is the category name, value is a list of templates. If "filter_group" is True, then this explictly filters to things provided by the ZopeSkel package--thereby hiding any templates the user may have on their system that sit on top of zopeskel's base classes. This is required in places where we want to generate canonical documents, and don't want to accidentally include things from the machine it's being run on. """ cats = {} # grab a list of all paster create template entry points # # TODO: fix this filtering, post break-up this will not work as expected if filter_group: t_e_ps = pkg_resources.get_entry_map( 'zopeskel')['paste.paster_create_template'].values() else: t_e_ps = pkg_resources.iter_entry_points( 'paste.paster_create_template') templates = [] for entry in t_e_ps: try: # We only want our templates in this list template = entry.load() if issubclass(template, BaseTemplate): templates.append({ 'name': entry.name, 'summary': template.summary, 'class': template, 'category': getattr(template, 'category', 'Uncategorized'), 'help': getattr(template, 'help', "").strip(), 'entry': entry }) except Exception, e: # We will not be stopped! print 'Warning: could not load entry point %s (%s: %s)' % ( entry.name, e.__class__.__name__, e)
def show_entry_map(self): """ Show entry map for a package @param dist: package @param type: srting @returns: 0 for success or 1 if error """ pprinter = pprint.PrettyPrinter() try: pprinter.pprint(pkg_resources.get_entry_map(self.options.entry_map)) except pkg_resources.DistributionNotFound: self.logger.error("Distribution not found: %s" % self.options.entry_map) return 1 return 0
def loadRenderers(): """ Return a dictionary of all of the renderer objects installed. """ entry_map = pkg_resources.get_entry_map('autoprint', 'autoprint.renderers') output = {} for name, entry in entry_map.iteritems(): entry.require() class_ = entry.load() output[name] = class_() return output
def post(self, command, output_dir, vars): self.lastlogs.append("* A project has been created in %s.\n" % vars["path"]) minilay = os.path.join(self.output_dir, "minilays", vars["project"]) if vars["inside_minitage"]: self.lastlogs.append( "* A minilay has been installed in %s.\n" "* It contains those minilbuilds:" "\n\t- %s \n" "\n" "* Think to finish the versionning stuff and put this minilay and" " the projet under revision control.\n" "* The project must be archived here '%s' using '%s' or change the minibuild " "src_uri/scm_type.\n" "* Install your project running: \n\t\tminimerge -v %s" "" % (minilay, "\n\t- ".join(os.listdir(minilay)), vars["uri"], vars["scm_type"], vars["project"]) ) self.lastlogs.append( "* You can additionnaly create related databases or configuration or" " other stuff using minitage instances " " (http://minitage.org/paster/instances/index.html)\n" "* Available instances are: \n" "\t- %s\n" "* Some extra instances are contained inside the" "'minitage.paste.extras package', install it as a classical egg.\n" "* Run an instance with: \n" " \tpaster create -t minitage.instances.PROFIL project\n" "\n" % ( "\n\t- ".join( [ "%s (%s)" % ( a, pkg_resources.load_entry_point("minitage.paste", "paste.paster_create_template", a).summary, ) for a in pkg_resources.get_entry_map("minitage.paste")["paste.paster_create_template"] if "minitage.instances" in a ] ) ) ) README = os.path.join(vars["path"], "README.%s.txt" % vars["project"]) open(README, "w").write("\n".join(self.lastlogs)) self.lastlogs.append("Those informations have been writed to %s." % README) return common.Template.post(self, command, output_dir, vars)
def test_bad_entry(self): entry_map = pkg_resources.get_entry_map('dhcpkit') # Steal the distribution from an existing entry dist = entry_map['dhcpkit.ipv6.options']['1'].dist entry_map['dhcpkit.tests.registry'] = { # Test something that doesn't exist 'bad': pkg_resources.EntryPoint.parse('bad = dhcpkit.tests.does_not_exist:DummyOption', dist=dist), } with self.assertLogs('dhcpkit.registry', logging.WARNING) as cm: test_registry = TestRegistry() self.assertEqual(len(cm.output), 1) self.assertRegex(cm.output[0], '^ERROR:.*:Entry point bad .* could not be loaded') self.assertEqual(len(test_registry), 0)
def test_registry_loading(self): entry_map = pkg_resources.get_entry_map('dhcpkit') # Steal the distribution from an existing entry dist = entry_map['dhcpkit.ipv6.options']['1'].dist entry_map['dhcpkit.tests.registry'] = { # Test string ID 'one': pkg_resources.EntryPoint.parse('one = dhcpkit.ipv6.options:ClientIdOption', dist=dist), # Test numerical ID '1': pkg_resources.EntryPoint.parse('1 = dhcpkit.ipv6.options:ServerIdOption', dist=dist), } test_registry = TestRegistry() self.assertEqual(len(test_registry), 2) self.assertEqual(test_registry['one'], ClientIdOption) self.assertEqual(test_registry[1], ServerIdOption)
def register_plugin(plugin_group, name, klass, force=False): """Register a plugin on the fly. This works by adding *klass* as a pybtex entry point, under entry point group *plugin_group* and entry point name *name*. To register a suffix, append ".suffixes" to the plugin group, and *name* is then simply the suffix, which should start with a period. To register an alias plugin name, append ".aliases" to the plugin group. Aliases work just as real names, but are not returned by :function:`enumerate_plugin_names` and not advertised in the command line help. If *force* is ``False``, then existing entry points are not overwritten. If an entry point with the given group and name already exists, then returns ``False``, otherwise returns ``True``. If *force* is ``True``, then existing entry points are overwritten, and the function always returns ``True``. """ if plugin_group.endswith(".suffixes"): base_group, _ = plugin_group.rsplit(".", 1) if not name.startswith('.'): raise ValueError("a suffix must start with a period") elif plugin_group.endswith(".aliases"): base_group, _ = plugin_group.rsplit(".", 1) else: base_group = plugin_group if base_group not in _DEFAULT_PLUGINS: raise PluginGroupNotFound(base_group) dist = pkg_resources.get_distribution('pybtex') ep_map = pkg_resources.get_entry_map(dist) if plugin_group not in ep_map: ep_map[plugin_group] = {} if name in ep_map[plugin_group] and not force: return False else: ep_map[plugin_group][name] = _FakeEntryPoint(name, klass) return True