mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
DATASETTE_LOAD_PLUGINS environment variable for loading specific plugins
Closes #2164 * Load only specified plugins for DATASETTE_LOAD_PLUGINS=datasette-one,datasette-two * Load no plugins if DATASETTE_LOAD_PLUGINS='' * Automated tests in a Bash script for DATASETTE_LOAD_PLUGINS
This commit is contained in:
parent
30b28c8367
commit
6bfe104d47
4 changed files with 109 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import importlib
|
||||
import os
|
||||
import pluggy
|
||||
import pkg_resources
|
||||
import sys
|
||||
|
|
@ -22,10 +23,30 @@ DEFAULT_PLUGINS = (
|
|||
pm = pluggy.PluginManager("datasette")
|
||||
pm.add_hookspecs(hookspecs)
|
||||
|
||||
if not hasattr(sys, "_called_from_test"):
|
||||
DATASETTE_LOAD_PLUGINS = os.environ.get("DATASETTE_LOAD_PLUGINS", None)
|
||||
|
||||
if not hasattr(sys, "_called_from_test") and DATASETTE_LOAD_PLUGINS is None:
|
||||
# Only load plugins if not running tests
|
||||
pm.load_setuptools_entrypoints("datasette")
|
||||
|
||||
# Load any plugins specified in DATASETTE_LOAD_PLUGINS")
|
||||
if DATASETTE_LOAD_PLUGINS is not None:
|
||||
for package_name in [
|
||||
name for name in DATASETTE_LOAD_PLUGINS.split(",") if name.strip()
|
||||
]:
|
||||
try:
|
||||
distribution = pkg_resources.get_distribution(package_name)
|
||||
entry_map = distribution.get_entry_map()
|
||||
if "datasette" in entry_map:
|
||||
for plugin_name, entry_point in entry_map["datasette"].items():
|
||||
mod = entry_point.load()
|
||||
pm.register(mod, name=entry_point.name)
|
||||
# Ensure name can be found in plugin_to_distinfo later:
|
||||
pm._plugin_distinfo.append((mod, distribution))
|
||||
except pkg_resources.DistributionNotFound:
|
||||
sys.stderr.write("Plugin {} could not be found\n".format(package_name))
|
||||
|
||||
|
||||
# Load default plugins
|
||||
for plugin in DEFAULT_PLUGINS:
|
||||
mod = importlib.import_module(plugin)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue