Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically import modules of extensions #229

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Change log
Added
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#90 <https://github.com/moremoban/moban/issues/90>`_: Dynamically import
modules of extensions in order to support third-party extensions

Added
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#165 <https://github.com/moremoban/moban/issues/165>`_: Copy as plugins

Added
Expand Down
12 changes: 12 additions & 0 deletions moban/jinja2/engine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from importlib import import_module

from jinja2 import Template, Environment, FileSystemLoader
from lml.loader import scan_plugins_regex
from lml.plugin import PluginInfo, PluginManager
Expand Down Expand Up @@ -74,6 +76,7 @@ def __init__(self, template_dirs, extensions=None):
if is_extension_list_valid(extensions):
# because it is modified here
env_params["extensions"] += extensions
import_module_of_extension(extensions)
self.jj2_environment = Environment(**env_params)
for filter_name, filter_function in FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function
Expand Down Expand Up @@ -136,3 +139,12 @@ def is_extension_list_valid(extensions):
and isinstance(extensions, list)
and len(extensions) > 0
)


def import_module_of_extension(extensions):
modules = set()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good use of set.

if extensions:
for extension in extensions:
modules.add(extension.split('.')[0])
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
for module in modules:
import_module(module)
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ black;python_version>="3.6"
isort;python_version>="3.6"
moban-handlebars
pypi-mobans-pkg

jinja2_time
19 changes: 18 additions & 1 deletion tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import os

from mock import patch
Expand All @@ -6,7 +7,11 @@

import moban.exceptions as exceptions
from moban.plugins import ENGINES
from moban.jinja2.engine import Engine, is_extension_list_valid
from moban.jinja2.engine import (
Engine,
is_extension_list_valid,
import_module_of_extension,
)
from moban.plugins.context import Context
from moban.plugins.template import TemplateEngine, expand_template_directories

Expand Down Expand Up @@ -137,3 +142,15 @@ def test_extensions_validator():
actual.append(is_extension_list_valid(fixture))

eq_(expected, actual)


def test_import():
extensions = [
"jinja2.ext.do",
"jinja2_time.TimeExtension",
"jinja2.ext.loopcontrols",
]
import_module_of_extension(extensions)
modules = ["jinja2", "jinja2_time"]
for module in modules:
assert module in sys.modules