Browse Source

Plugins: Using pkgutil.walk_packages instead of our own re-implementation

pull/77/head
Louis 10 years ago
parent
commit
69f8a5e831
  1. 53
      patacrep/files.py

53
patacrep/files.py

@ -1,11 +1,12 @@
"""File system utilities.""" """File system utilities."""
from contextlib import contextmanager from contextlib import contextmanager
import importlib
import logging import logging
import os import os
import pkgutil
import posixpath import posixpath
import re import re
import sys
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -89,41 +90,35 @@ def load_plugins(datadirs, root_modules, keyword):
- values are functions triggered when this keyword is met. - values are functions triggered when this keyword is met.
""" """
plugins = {} plugins = {}
directory_list = (
[ datadir_path = [
os.path.join(datadir, "python", *root_modules) os.path.join(datadir, "python", *root_modules)
for datadir in datadirs for datadir
in datadirs
] ]
+ [os.path.join( sys_path = [
os.path.dirname(__file__), os.path.join(path, "patacrep", *root_modules)
*root_modules for path
)] in sys.path
) ]
for directory in directory_list: for module_finder, name, __is_pkg in pkgutil.walk_packages(
if not os.path.exists(directory): datadir_path + sys_path,
LOGGER.debug( prefix="patacrep.{}.".format(".".join(root_modules))
"Ignoring non-existent directory '%s'.", ):
directory if name in sys.modules:
) module = sys.modules[name]
continue
for (dirpath, __ignored, filenames) in os.walk(directory):
modules = ["patacrep"] + root_modules
if os.path.relpath(dirpath, directory) != ".":
modules.extend(os.path.relpath(dirpath, directory).split("/"))
for name in filenames:
if name == "__init__.py":
modulename = []
elif name.endswith(".py"):
modulename = [name[:-len('.py')]]
else: else:
try:
module = module_finder.find_spec(name).loader.load_module()
except ImportError as error:
LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error)))
continue continue
plugin = importlib.import_module(".".join(modules + modulename)) if hasattr(module, keyword):
if hasattr(plugin, keyword): for (key, value) in getattr(module, keyword).items():
for (key, value) in getattr(plugin, keyword).items():
if key in plugins: if key in plugins:
LOGGER.warning( LOGGER.warning(
"File %s: Keyword '%s' is already used. Ignored.", "File %s: Keyword '%s' is already used. Ignored.",
relpath(os.path.join(dirpath, name)), module.__file__,
key, key,
) )
continue continue

Loading…
Cancel
Save