Browse Source

Merge branch 'master' of github.com:patacrep/patacrep

pull/88/head
Louis 9 years ago
parent
commit
6b5b08701f
  1. 71
      patacrep/files.py

71
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,43 +90,37 @@ 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 else:
for (dirpath, __ignored, filenames) in os.walk(directory): try:
modules = ["patacrep"] + root_modules module = module_finder.find_spec(name).loader.load_module()
if os.path.relpath(dirpath, directory) != ".": except ImportError as error:
modules.extend(os.path.relpath(dirpath, directory).split("/")) LOGGER.debug("[plugins] Could not load module {}: {}".format(name, str(error)))
for name in filenames: continue
if name == "__init__.py": if hasattr(module, keyword):
modulename = [] for (key, value) in getattr(module, keyword).items():
elif name.endswith(".py"): if key in plugins:
modulename = [name[:-len('.py')]] LOGGER.warning(
else: "File %s: Keyword '%s' is already used. Ignored.",
module.__file__,
key,
)
continue continue
plugin = importlib.import_module(".".join(modules + modulename)) plugins[key] = value
if hasattr(plugin, keyword):
for (key, value) in getattr(plugin, keyword).items():
if key in plugins:
LOGGER.warning(
"File %s: Keyword '%s' is already used. Ignored.",
relpath(os.path.join(dirpath, name)),
key,
)
continue
plugins[key] = value
return plugins return plugins

Loading…
Cancel
Save