Skip to content

Commit

Permalink
✨ choose a template engine based on its file extension. fix #47 (#63)
Browse files Browse the repository at this point in the history
* ✨ choose a template based on its file extension. fix #43

* 📚 update change log

* 🐛 fix the template names

* 📚 udpate readme

* ✨ default template engine will be jinja2 if template type(file extension) cannot be found

* 👕 address review feedback. moremoban/moban#61

* 🎨 keep cli --template_type option

* ✨ allow template_type in moban file to indicate default template engine. i.e. if unknown template type(file suffix) is found, the default template engine is used. So if mako engine is specified via template_type, an unknown template file such as strange.template will be rendered by mako engine

* ✨ allow jinja2 become template suffix too, meaning a.jj2 and b.jinja2 will be rendered by the same engine. however, for now, the engine instance will not be the same. later on, this can be consolidated

* 📚 update change log. #58

* ✨ associate jinja2 engine with more file types, j2, jinja and retore the default template engine name as jinja2

* 📚 documentation update
  • Loading branch information
chfw authored Jun 11, 2018
1 parent ff0db91 commit 70dd09f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
2 changes: 2 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@
JINJA_FILTER_EXTENSION = "jinja_filter"
JINJA_TEST_EXTENSION = "jinja_test"
JINJA_GLOBALS_EXTENSION = "jinja_globals"

TEMPLATE_ENGINE_EXTENSION = "template_engine"
37 changes: 23 additions & 14 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
from jinja2 import Environment, FileSystemLoader

from lml.plugin import PluginManager, PluginInfo
from lml.loader import scan_plugins

from moban.hashstore import HashStore
Expand All @@ -24,25 +25,33 @@
_FILTERS = JinjaFilterManager()
_TESTS = JinjaTestManager()
_GLOBALS = JinjaGlobalsManager()
scan_plugins("moban_", "moban", None, BUILTIN_EXENSIONS)


class EngineFactory(object):
class EngineFactory(PluginManager):
def __init__(self):
super(EngineFactory, self).__init__(
constants.TEMPLATE_ENGINE_EXTENSION
)

@staticmethod
def get_engine(template_type):
if template_type == constants.DEFAULT_TEMPLATE_TYPE:
return Engine
else:
try:
external_engine = utils.load_external_engine(template_type)
except ImportError:
raise exceptions.NoThirdPartyEngine(
constants.MESSAGE_NO_THIRD_PARTY_ENGINE
)
return external_engine.get_engine(template_type)
def get_engine(self, template_type):
return self.load_me_now(template_type)

def all_types(self):
return list(self.registry.keys())

def raise_exception(self, key):
raise exceptions.NoThirdPartyEngine(key)


ENGINES = EngineFactory()

scan_plugins("moban_", "moban", None, BUILTIN_EXENSIONS)


@PluginInfo(
constants.TEMPLATE_ENGINE_EXTENSION,
tags=['jinja2', 'jinja', 'jj2', 'j2']
)
class Engine(object):

def __init__(self, template_dirs, context_dirs):
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from moban.utils import merge, open_yaml
from moban.hashstore import HashStore
from moban.engine import EngineFactory
from moban.engine import ENGINES
import moban.constants as constants
import moban.mobanfile as mobanfile
import moban.exceptions as exceptions
Expand Down Expand Up @@ -131,7 +131,7 @@ def handle_command_line(options):
options = merge(options, constants.DEFAULT_OPTIONS)
if options[constants.LABEL_TEMPLATE] is None:
raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
engine_class = EngineFactory.get_engine(
engine_class = ENGINES.get_engine(
options[constants.LABEL_TEMPLATE_TYPE]
)
engine = engine_class(
Expand Down
34 changes: 23 additions & 11 deletions mobanfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import re
import sys
from collections import defaultdict

from lml.utils import do_import

import moban.constants as constants
import moban.reporter as reporter
from moban.engine import EngineFactory
from moban.engine import ENGINES
from moban.utils import merge, parse_targets
from moban.copier import Copier

Expand All @@ -29,16 +30,27 @@ def handle_copy(template_dirs, copy_config):

def handle_targets(merged_options, targets):
list_of_templating_parameters = parse_targets(merged_options, targets)
engine_class = EngineFactory.get_engine(
merged_options[constants.LABEL_TEMPLATE_TYPE]
)
engine = engine_class(
merged_options[constants.LABEL_TMPL_DIRS],
merged_options[constants.LABEL_CONFIG_DIR],
)
engine.render_to_files(list_of_templating_parameters)
engine.report()
return engine.number_of_templated_files()
jobs_for_each_engine = defaultdict(list)
for file_list in list_of_templating_parameters:
_, extension = os.path.splitext(file_list[0])
template_type = extension[1:]
if template_type not in ENGINES.all_types():
template_type = merged_options[constants.LABEL_TEMPLATE_TYPE]
jobs_for_each_engine[template_type].append(file_list)

count = 0
for template_type in jobs_for_each_engine.keys():
engine_class = ENGINES.get_engine(
template_type
)
engine = engine_class(
merged_options[constants.LABEL_TMPL_DIRS],
merged_options[constants.LABEL_CONFIG_DIR],
)
engine.render_to_files(jobs_for_each_engine[template_type])
engine.report()
count = count + engine.number_of_templated_files()
return count


def handle_plugin_dirs(plugin_dirs):
Expand Down
11 changes: 0 additions & 11 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import re
import sys
import stat

import yaml
Expand Down Expand Up @@ -80,16 +79,6 @@ def parse_targets(options, targets):
yield ((template_file, common_data_file, output))


def load_external_engine(template_type):
module_name = "%s_%s" % (constants.PROGRAM_NAME, template_type)
try:
__import__(module_name)
except ImportError:
raise
module = sys.modules[module_name]
return module


def file_permissions_copy(source, dest):
source_permissions = file_permissions(source)
dest_permissions = file_permissions(dest)
Expand Down

0 comments on commit 70dd09f

Please sign in to comment.