diff options
author | Waylan Limberg <waylan.limberg@icloud.com> | 2014-08-27 13:34:19 -0400 |
---|---|---|
committer | Waylan Limberg <waylan.limberg@icloud.com> | 2014-08-27 13:34:19 -0400 |
commit | 44e718ed82ed4c8e8e0f0fe1dbdb73d441747b19 (patch) | |
tree | 4006dad4eb9fa86169ededd817ede082bf720270 /markdown | |
parent | 8c29487fe973f9007510dd6c4f32083b8d4d0896 (diff) | |
download | markdown-44e718ed82ed4c8e8e0f0fe1dbdb73d441747b19.tar.gz markdown-44e718ed82ed4c8e8e0f0fe1dbdb73d441747b19.tar.bz2 markdown-44e718ed82ed4c8e8e0f0fe1dbdb73d441747b19.zip |
Refactored extension importing.
We now use importlib which means we no longer support Python 2.6.
Also, this refactor properly imports third party extensions which reside
at the root of PYTHONPATH. Previously, either `markdown.extensions.` or
`mdx_` would be appended to any extension name that did not contain a
dot, which required third party extensions to either be in submodules or
use the old `mdx_` naming convention.
This commit is also in preperation for #336. It will now be much easier to
deprecate (and later remove) support for the old ways of handling extension
names.
Diffstat (limited to 'markdown')
-rw-r--r-- | markdown/__init__.py | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 59dda4c..6190dd2 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -36,6 +36,7 @@ from .__version__ import version, version_info import codecs import sys import logging +import importlib from . import util from .preprocessors import build_preprocessors from .blockprocessors import build_block_parser @@ -163,6 +164,8 @@ class Markdown(object): ext = self.build_extension(ext, configs.get(ext, [])) if isinstance(ext, Extension): ext.extendMarkdown(self, globals()) + logger.info('Successfully loaded extension "%s.%s".' + % (ext.__class__.__module__, ext.__class__.__name__)) elif ext is not None: raise TypeError( 'Extension "%s.%s" must be of type: "markdown.Extension"' @@ -187,23 +190,28 @@ class Markdown(object): pairs = [x.split("=") for x in ext_args.split(",")] configs.update([(x.strip(), y.strip()) for (x, y) in pairs]) - # Setup the module name - module_name = ext_name - if '.' not in ext_name: - module_name = '.'.join(['markdown.extensions', ext_name]) - # Try loading the extension first from one place, then another - try: # New style (markdown.extensions.<extension>) - module = __import__(module_name, {}, {}, [str(module_name.rpartition('.')[0])]) + try: + # Assume string uses dot syntax (`path.to.some.module`) + module = importlib.import_module(ext_name) + logger.debug('Successfuly imported extension module "%s".' % ext_name) except ImportError: - module_name_old_style = '_'.join(['mdx', ext_name]) - try: # Old style (mdx_<extension>) - module = __import__(module_name_old_style) - except ImportError as e: - message = "Failed loading extension '%s' from '%s' or '%s'" \ - % (ext_name, module_name, module_name_old_style) - e.args = (message,) + e.args[1:] - raise + # Preppend `markdown.extensions.` to name + module_name = '.'.join(['markdown.extensions', ext_name]) + try: + module = importlib.import_module(module_name) + logger.debug('Successfuly imported extension module "%s".' % module_name) + except ImportError: + # Preppend `mdx_` to name + module_name_old_style = '_'.join(['mdx', ext_name]) + try: + module = importlib.import_module(module_name_old_style) + logger.debug('Successfuly imported extension module "%s".' % module_name_old_style) + except ImportError as e: + message = "Failed loading extension '%s' from '%s', '%s' or '%s'" \ + % (ext_name, ext_name, module_name, module_name_old_style) + e.args = (message,) + e.args[1:] + raise # If the module is loaded successfully, we expect it to define a # function called makeExtension() |