Skip to content

Commit

Permalink
Further Work Toward Issue #386 - BLOCKED
Browse files Browse the repository at this point in the history
  • Loading branch information
derks committed Nov 23, 2016
1 parent 68c8955 commit 34c4496
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ development will happen as 2.11.x under the git master branch.

Bugs:

* :issue:`386` - Deprecate `imp` in favor of `importlib`
* :issue:`397` - Removes deprecated `warn` from ILog validator, in-favor
of `warning`
* :issue:`401` - Can't get user in daemon extension
Expand Down
66 changes: 52 additions & 14 deletions cement/ext/ext_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
import os
import sys
import glob
import imp
from ..core import plugin, exc
from ..utils.misc import is_true, minimal_logger
from ..utils.fs import abspath

# The `imp` module is deprecated in favor of `importlib` in 3.4, but it
# wasn't introduced until 3.1.
pyver = sys.version_info
if (pyver[0] < 3) or (pyver[0] == 3 and pyver[1] <= 4):
import imp # pragma: nocover # noqa
else:
import importlib # pragma: nocover # noqa
import importlib.util # pragma: nocover # noqa

LOG = minimal_logger(__name__)

# FIX ME: This is a redundant name... ?
Expand Down Expand Up @@ -62,6 +70,7 @@ def __init__(self):

def _setup(self, app_obj):
super(CementPluginHandler, self)._setup(app_obj)

self._enabled_plugins = []
self._disabled_plugins = []
self._plugin_configs = {}
Expand Down Expand Up @@ -151,6 +160,42 @@ def _setup(self, app_obj):
if plugin in self._enabled_plugins:
self._enabled_plugins.remove(plugin)

def _load_plugin_via_imp(self, plugin_name, plugin_dir):
# necessary for Python < 3.4
try:
f, path, desc = imp.find_module(plugin_name, [plugin_dir])
except ImportError:
LOG.debug("plugin '%s' does not exist in '%s'." %
(plugin_name, plugin_dir))
return False

# We don't catch this because it would make debugging a
# nightmare
mod = imp.load_module(plugin_name, f, path, desc)
if mod and hasattr(mod, 'load'):
mod.load(self.app)
return True

def _load_plugin_via_importlib(self, plugin_name, plugin_dir):
# Python > 3.4
spec = importlib.util.spec_from_file_location(plugin_name, plugin_dir)

paths = [
os.path.join(plugin_dir, '%s.py' % plugin_name),
os.path.join(plugin_dir, plugin_name, '__init__.py'),
]

for path in paths:
if not os.path.exists(path):
continue
spec = importlib.util.spec_from_file_location(plugin_name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
mod.load(self.app)
return True

return False

def _load_plugin_from_dir(self, plugin_name, plugin_dir):
"""
Load a plugin from a directory path rather than a python package
Expand All @@ -175,19 +220,12 @@ def _load_plugin_from_dir(self, plugin_name, plugin_dir):
LOG.debug("plugin directory '%s' does not exist." % plugin_dir)
return False

try:
f, path, desc = imp.find_module(plugin_name, [plugin_dir])
except ImportError:
LOG.debug("plugin '%s' does not exist in '%s'." %
(plugin_name, plugin_dir))
return False

# We don't catch this because it would make debugging a
# nightmare
mod = imp.load_module(plugin_name, f, path, desc)
if mod and hasattr(mod, 'load'):
mod.load(self.app)
return True
if (pyver[0] < 3) or (pyver[0] == 3 and pyver[1] <= 4):
import_func = self._load_plugin_via_imp
else:
import_func = self._load_plugin_via_importlib

return import_func(plugin_name, plugin_dir)

def _load_plugin_from_bootstrap(self, plugin_name, base_package):
"""
Expand Down

0 comments on commit 34c4496

Please sign in to comment.