Skip to content

Commit

Permalink
Merge pull request #1284 from HaoZeke/cleanUpPlugins
Browse files Browse the repository at this point in the history
MAINT: Use importlib, pkgutil over __import__
  • Loading branch information
mattip committed Apr 30, 2023
2 parents 5da0dd3 + 29b6c30 commit 9c2f028
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions asv/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import pkgutil
import importlib

from . import commands, plugins
from .console import log
Expand All @@ -23,28 +24,37 @@ def __init__(self):

def load_plugins(self, package):
prefix = package.__name__ + "."
for module_finder, name, ispkg in pkgutil.iter_modules(
package.__path__, prefix
):
for module_finder, name, ispkg in pkgutil.iter_modules(package.__path__, prefix):
try:
__import__(name)
mod = sys.modules[name]
mod = importlib.import_module(name)
self.init_plugin(mod)
self._plugins.append(mod)
except ModuleNotFoundError as err:
log.error(f"Couldn't load {name} because\n{err}")
continue # Couldn't find mamba

def _load_plugin_by_name(self, name):
prefix = plugins.__name__ + "."
for module_finder, module_name, ispkg in pkgutil.iter_modules(plugins.__path__, prefix):
if name in module_name:
mod = importlib.import_module(module_name)
return mod
return None

def import_plugin(self, name):
extended = False
if name.startswith("."):
extended = True
sys.path.insert(0, ".")
name = name[1:]
try:
mod = __import__(name, {}, {}, [], level=0)
self.init_plugin(mod)
self._plugins.append(mod)
if extended:
mod = importlib.import_module(name)
else:
mod = self._load_plugin_by_name(name)
if mod:
self.init_plugin(mod)
self._plugins.append(mod)
finally:
if extended:
del sys.path[0]
Expand Down
2 changes: 1 addition & 1 deletion test/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

"version": 1,

"plugins": [".example_plugin"],
"plugins": [".example_plugin", "github"],

"benchmark_dir": "benchmark",

Expand Down

0 comments on commit 9c2f028

Please sign in to comment.