diff --git a/.isort.cfg b/.isort.cfg index 53668475..659ea21f 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -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 diff --git a/moban/base_engine.py b/moban/base_engine.py index 58ff95c2..12b30ff2 100644 --- a/moban/base_engine.py +++ b/moban/base_engine.py @@ -1,7 +1,4 @@ -import os -import moban.utils as utils import moban.reporter as reporter -from moban.engine_factory import Strategy class BaseEngine(object): @@ -9,15 +6,6 @@ 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() @@ -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) diff --git a/moban/constants.py b/moban/constants.py index 2871ef76..8c2fa5fc 100644 --- a/moban/constants.py +++ b/moban/constants.py @@ -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) diff --git a/moban/engine.py b/moban/engine.py index 99e5c3b8..571b9e10 100644 --- a/moban/engine.py +++ b/moban/engine.py @@ -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 @@ -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( @@ -33,10 +23,11 @@ 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, @@ -44,13 +35,13 @@ def __init__(self, template_dirs, context_dirs): 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) @@ -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) @@ -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) diff --git a/moban/engine_factory.py b/moban/engine_factory.py index 986501f9..4f072da7 100644 --- a/moban/engine_factory.py +++ b/moban/engine_factory.py @@ -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): @@ -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) @@ -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] diff --git a/moban/engine_handlebars.py b/moban/engine_handlebars.py index d41d46c1..f7c32fd8 100644 --- a/moban/engine_handlebars.py +++ b/moban/engine_handlebars.py @@ -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 @@ -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 @@ -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) @@ -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)) diff --git a/moban/main.py b/moban/main.py index 2639ddfa..6d5b6dd6 100644 --- a/moban/main.py +++ b/moban/main.py @@ -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] @@ -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] ) diff --git a/moban/mobanfile.py b/moban/mobanfile.py index f78a9fdc..b52c040b 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -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 @@ -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) @@ -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 @@ -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], diff --git a/moban/plugins.py b/moban/plugins.py new file mode 100644 index 00000000..9de12bd4 --- /dev/null +++ b/moban/plugins.py @@ -0,0 +1,67 @@ +import os +from moban import utils +from lml.loader import scan_plugins_regex + +from moban.extensions import ( + JinjaTestManager, + JinjaFilterManager, + JinjaGlobalsManager +) +from moban.extensions import LibraryManager +from moban.engine_factory import EngineFactory +from moban.constants import MOBAN_ALL + +LIBRARIES = LibraryManager() +FILTERS = JinjaFilterManager() +TESTS = JinjaTestManager() +GLOBALS = JinjaGlobalsManager() +ENGINES = EngineFactory() + +BUILTIN_EXENSIONS = [ + "moban.filters.repr", + "moban.filters.github", + "moban.filters.text", + "moban.tests.files", +] + + +def refresh_plugins(): + scan_plugins_regex(MOBAN_ALL, "moban", None, BUILTIN_EXENSIONS) + + +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 diff --git a/tests/test_engine.py b/tests/test_engine.py index 25122eae..9d885945 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -7,7 +7,9 @@ from nose.tools import eq_, raises from moban.engine import Engine from moban.extensions import jinja_global -from moban.engine_factory import ENGINES, Context, expand_template_directories +from moban.engine_factory import Context +from moban.plugins import expand_template_directories +from moban.plugins import ENGINES from moban.engine_handlebars import EngineHandlebars