Skip to content

Commit

Permalink
Experimental support for DATASETTE_LOAD_PLUGINS, refs #2164
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 30, 2023
1 parent 30b28c8 commit 943d60e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion datasette/plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
import os
import pluggy
import pkg_resources
import sys
Expand All @@ -22,10 +23,26 @@
pm = pluggy.PluginManager("datasette")
pm.add_hookspecs(hookspecs)

if not hasattr(sys, "_called_from_test"):
DATASETTE_LOAD_PLUGINS = os.environ.get("DATASETTE_LOAD_PLUGINS") or None

if not hasattr(sys, "_called_from_test") and not DATASETTE_LOAD_PLUGINS:
# Only load plugins if not running tests
pm.load_setuptools_entrypoints("datasette")

# Load any plugins specified in DATASETTE_LOAD_PLUGINS
if DATASETTE_LOAD_PLUGINS:
for package_name in DATASETTE_LOAD_PLUGINS.split(","):
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, plugin_name)
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)
Expand Down

0 comments on commit 943d60e

Please sign in to comment.