Skip to content
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
1 change: 0 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ known_first_party=lml, jinja2
indent=' '
multi_line_output=3
length_sort=1
forced_separate=django.contrib,django.utils
default_section=FIRSTPARTY
no_lines_before=LOCALFOLDER
sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER
20 changes: 0 additions & 20 deletions moban/base_engine.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import os
import moban.utils as utils
import moban.reporter as reporter
from moban.engine_factory import Strategy


class BaseEngine(object):
def __init__(self):
self.templated_count = 0
self.file_count = 0

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
sta.process()
choice = sta.what_to_do()
if choice == Strategy.DATA_FIRST:
self._render_with_finding_data_first(sta.data_file_index)
else:
self._render_with_finding_template_first(sta.template_file_index)

def report(self):
if self.templated_count == 0:
reporter.report_no_action()
Expand All @@ -30,11 +18,3 @@ def report(self):

def number_of_templated_files(self):
return self.templated_count

def _file_permissions_copy(self, template_file, output_file):
true_template_file = template_file
for a_template_dir in self.template_dirs:
true_template_file = os.path.join(a_template_dir, template_file)
if os.path.exists(true_template_file):
break
utils.file_permissions_copy(true_template_file, output_file)
4 changes: 4 additions & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@

TEMPLATE_ENGINE_EXTENSION = "template_engine"
LIBRARY_EXTENSION = "library"

MOBAN_EXTENSIONS = "^moban_.+$"
MOBAN_TEMPLATES = "^.+_mobans_pkg$"
MOBAN_ALL = "%s|%s" % (MOBAN_EXTENSIONS, MOBAN_TEMPLATES)
50 changes: 29 additions & 21 deletions moban/engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from jinja2 import Environment, FileSystemLoader
from lml.loader import scan_plugins_regex
from lml.plugin import PluginInfo

import moban.utils as utils
Expand All @@ -8,23 +9,12 @@
from moban.utils import get_template_path
from moban.hashstore import HASH_STORE
from moban.base_engine import BaseEngine
from moban.extensions import (
JinjaTestManager,
JinjaFilterManager,
JinjaGlobalsManager
)
from moban.engine_factory import (
MOBAN_ALL,
BUILTIN_EXENSIONS,
Context,
expand_template_directory,
expand_template_directories,
verify_the_existence_of_directories
verify_the_existence_of_directories,
Strategy
)

FILTERS = JinjaFilterManager()
TESTS = JinjaTestManager()
GLOBALS = JinjaGlobalsManager()
from moban import plugins


@PluginInfo(
Expand All @@ -33,24 +23,25 @@
class Engine(BaseEngine):
def __init__(self, template_dirs, context_dirs):
BaseEngine.__init__(self)
scan_plugins_regex(MOBAN_ALL, "moban", None, BUILTIN_EXENSIONS)
template_dirs = list(expand_template_directories(template_dirs))
plugins.refresh_plugins()
template_dirs = list(
plugins.expand_template_directories(template_dirs))
verify_the_existence_of_directories(template_dirs)
context_dirs = expand_template_directory(context_dirs)
context_dirs = plugins.expand_template_directory(context_dirs)
template_loader = FileSystemLoader(template_dirs)
self.jj2_environment = Environment(
loader=template_loader,
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
)
for filter_name, filter_function in FILTERS.get_all():
for filter_name, filter_function in plugins.FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function

for test_name, test_function in TESTS.get_all():
for test_name, test_function in plugins.TESTS.get_all():
self.jj2_environment.tests[test_name] = test_function

for global_name, dict_obj in GLOBALS.get_all():
for global_name, dict_obj in plugins.GLOBALS.get_all():
self.jj2_environment.globals[global_name] = dict_obj

self.context = Context(context_dirs)
Expand All @@ -65,6 +56,15 @@ def render_to_file(self, template_file, data_file, output_file):
utils.write_file_out(output_file, rendered_content)
self._file_permissions_copy(template_file, output_file)

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
sta.process()
choice = sta.what_to_do()
if choice == Strategy.DATA_FIRST:
self._render_with_finding_data_first(sta.data_file_index)
else:
self._render_with_finding_template_first(sta.template_file_index)

def _render_with_finding_template_first(self, template_file_index):
for (template_file, data_output_pairs) in template_file_index.items():
template = self.jj2_environment.get_template(template_file)
Expand All @@ -87,6 +87,14 @@ def _render_with_finding_data_first(self, data_file_index):
self.templated_count += 1
self.file_count += 1

def _file_permissions_copy(self, template_file, output_file):
true_template_file = template_file
for a_template_dir in self.template_dirs:
true_template_file = os.path.join(a_template_dir, template_file)
if os.path.exists(true_template_file):
break
utils.file_permissions_copy(true_template_file, output_file)

def _apply_template(self, template, data, output):
temp_file_path = get_template_path(self.template_dirs, template)
rendered_content = template.render(**data)
Expand Down
54 changes: 0 additions & 54 deletions moban/engine_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
import moban.constants as constants
import moban.exceptions as exceptions
from lml.plugin import PluginManager
from moban.extensions import LibraryManager

BUILTIN_EXENSIONS = [
"moban.filters.repr",
"moban.filters.github",
"moban.filters.text",
"moban.tests.files",
]

LIBRARIES = LibraryManager()


class EngineFactory(PluginManager):
Expand All @@ -33,12 +23,6 @@ def raise_exception(self, key):
raise exceptions.NoThirdPartyEngine(key)


ENGINES = EngineFactory()
MOBAN_EXTENSIONS = "^moban_.+$"
MOBAN_TEMPLATES = "^.+_mobans_pkg$"
MOBAN_ALL = "%s|%s" % (MOBAN_EXTENSIONS, MOBAN_TEMPLATES)


class Context(object):
def __init__(self, context_dirs):
verify_the_existence_of_directories(context_dirs)
Expand Down Expand Up @@ -100,44 +84,6 @@ def _append_to_array_item_to_dictionary_key(adict, key, array_item):
adict[key].append(array_item)


def expand_template_directories(dirs):
if not isinstance(dirs, list):
dirs = [dirs]

for directory in dirs:
yield expand_template_directory(directory)


def expand_template_directory(directory):
translated_directory = None
if ":" in directory:
library_or_repo_name, relative_path = directory.split(":")
potential_repo_path = os.path.join(
utils.get_moban_home(), library_or_repo_name
)
if os.path.exists(potential_repo_path):
# expand repo template path
if relative_path:
translated_directory = os.path.join(
potential_repo_path, relative_path
)
else:
translated_directory = potential_repo_path
else:
# expand pypi template path
library_path = LIBRARIES.resource_path_of(library_or_repo_name)
if relative_path:
translated_directory = os.path.join(
library_path, relative_path
)
else:
translated_directory = library_path
else:
# local template path
translated_directory = os.path.abspath(directory)
return translated_directory


def verify_the_existence_of_directories(dirs):
if not isinstance(dirs, list):
dirs = [dirs]
Expand Down
38 changes: 26 additions & 12 deletions moban/engine_handlebars.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
import moban.reporter as reporter
import moban.constants as constants
import moban.exceptions as exceptions
from lml.loader import scan_plugins_regex
from lml.plugin import PluginInfo
from moban.base_engine import BaseEngine
from moban.engine_factory import (
MOBAN_ALL,
BUILTIN_EXENSIONS,
Context,
expand_template_directory,
expand_template_directories,
verify_the_existence_of_directories
verify_the_existence_of_directories,
Strategy,
)

from moban import plugins
from pybars import Compiler


Expand All @@ -26,10 +22,11 @@
class EngineHandlebars(BaseEngine):
def __init__(self, template_dirs, context_dirs):
BaseEngine.__init__(self)
scan_plugins_regex(MOBAN_ALL, "moban", None, BUILTIN_EXENSIONS)
template_dirs = list(expand_template_directories(template_dirs))
plugins.refresh_plugins()
template_dirs = list(
plugins.expand_template_directories(template_dirs))
verify_the_existence_of_directories(template_dirs)
context_dirs = expand_template_directory(context_dirs)
context_dirs = plugins.expand_template_directory(context_dirs)
self.context = Context(context_dirs)
self.template_dirs = template_dirs

Expand All @@ -39,11 +36,28 @@ def find_template_file(self, template_file):
return os.path.abspath(os.path.join(directory, template_file))
raise exceptions.FileNotFound(template_file)

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
sta.process()
choice = sta.what_to_do()
if choice == Strategy.DATA_FIRST:
self._render_with_finding_data_first(sta.data_file_index)
else:
self._render_with_finding_template_first(sta.template_file_index)

def _file_permissions_copy(self, template_file, output_file):
true_template_file = template_file
for a_template_dir in self.template_dirs:
true_template_file = os.path.join(a_template_dir, template_file)
if os.path.exists(true_template_file):
break
utils.file_permissions_copy(true_template_file, output_file)

def render_to_file(self, template_file, data_file, output_file):
template_file = self.find_template_file(template_file)
with open(template_file, "r") as source:
if sys.version_info[0] < 3:
template = Compiler().compile(unicode(source.read())) # noqa
template = Compiler().compile(unicode(source.read())) # noqa
else:
template = Compiler().compile(source.read())
data = self.context.get_data(data_file)
Expand Down Expand Up @@ -77,7 +91,7 @@ def _apply_template(self, template, data, output):
template_file = self.find_template_file(template)
with open(template_file, "r") as source:
if sys.version_info[0] < 3:
template = Compiler().compile(unicode(source.read())) # noqa
template = Compiler().compile(unicode(source.read())) # noqa
else:
template = Compiler().compile(source.read())
rendered_content = ''.join(template(data))
Expand Down
6 changes: 4 additions & 2 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import moban.mobanfile as mobanfile
import moban.exceptions as exceptions
from moban.utils import merge, open_yaml
from moban.engine_factory import ENGINES
from moban.hashstore import HASH_STORE
from moban import plugins


def main():
"""
program entry point
"""
plugins.refresh_plugins()
parser = create_parser()
options = vars(parser.parse_args())
HASH_STORE.IGNORE_CACHE_FILE = options[constants.LABEL_FORCE]
Expand Down Expand Up @@ -132,7 +133,8 @@ 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 = ENGINES.get_engine(options[constants.LABEL_TEMPLATE_TYPE])
engine_class = plugins.ENGINES.get_engine(
options[constants.LABEL_TEMPLATE_TYPE])
engine = engine_class(
options[constants.LABEL_TMPL_DIRS], options[constants.LABEL_CONFIG_DIR]
)
Expand Down
8 changes: 4 additions & 4 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
expand_directories,
)
from moban.copier import Copier
from moban.engine_factory import ENGINES, expand_template_directories
from moban import plugins

try:
from urllib.parse import urlparse
Expand Down Expand Up @@ -79,7 +79,7 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):
def handle_copy(template_dirs, copy_config):
# expanding function is added so that
# copy function understands repo and pypi_pkg path, since 0.3.1
expanded_dirs = list(expand_template_directories(template_dirs))
expanded_dirs = list(plugins.expand_template_directories(template_dirs))

copier = Copier(expanded_dirs)
copier.copy_files(copy_config)
Expand All @@ -97,7 +97,7 @@ def handle_targets(merged_options, targets):
for file_list in list_of_templating_parameters:
_, extension = os.path.splitext(file_list[0])
template_type = extension[1:]
primary_template_type = ENGINES.get_primary_key(template_type)
primary_template_type = plugins.ENGINES.get_primary_key(template_type)
if primary_template_type is None:
primary_template_type = merged_options[
constants.LABEL_TEMPLATE_TYPE
Expand All @@ -106,7 +106,7 @@ def handle_targets(merged_options, targets):

count = 0
for template_type in jobs_for_each_engine.keys():
engine_class = ENGINES.get_engine(template_type)
engine_class = plugins.ENGINES.get_engine(template_type)
engine = engine_class(
merged_options[constants.LABEL_TMPL_DIRS],
merged_options[constants.LABEL_CONFIG_DIR],
Expand Down
Loading