From 00595518db2a23e7896aaac0c0f14cdd3a79e1ce Mon Sep 17 00:00:00 2001 From: chfw Date: Tue, 13 Nov 2018 18:14:42 +0000 Subject: [PATCH 1/3] :fire: remove unrelated django --- .isort.cfg | 1 - 1 file changed, 1 deletion(-) 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 From 471645a93181d3c4ab865241081d61f3001f1dba Mon Sep 17 00:00:00 2001 From: PrajwalM2212 Date: Tue, 13 Nov 2018 21:18:21 +0530 Subject: [PATCH 2/3] fix moban.exceptions.NoThirdPartyEngine: jinja2 --- moban/base_engine.py | 20 ------------ moban/constants.py | 4 +++ moban/engine.py | 65 ++++++++++++++++++++++++++++++++------ moban/engine_factory.py | 30 ------------------ moban/engine_handlebars.py | 27 +++++++++++++--- moban/main.py | 2 +- moban/mobanfile.py | 3 +- tests/test_engine.py | 3 +- 8 files changed, 87 insertions(+), 67 deletions(-) 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..e4d61362 100644 --- a/moban/engine.py +++ b/moban/engine.py @@ -1,6 +1,9 @@ +import os + from jinja2 import Environment, FileSystemLoader from lml.loader import scan_plugins_regex from lml.plugin import PluginInfo +from lml.plugin import PluginManager import moban.utils as utils import moban.reporter as reporter @@ -14,17 +17,44 @@ 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 ) +import moban.exceptions as exceptions +from moban.constants import MOBAN_ALL + +_FILTERS = JinjaFilterManager() +_TESTS = JinjaTestManager() +_GLOBALS = JinjaGlobalsManager() + +BUILTIN_EXENSIONS = [ + "moban.filters.repr", + "moban.filters.github", + "moban.filters.text", + "moban.tests.files", +] + + +class EngineFactory(PluginManager): + def __init__(self): + super(EngineFactory, self).__init__( + constants.TEMPLATE_ENGINE_EXTENSION + ) -FILTERS = JinjaFilterManager() -TESTS = JinjaTestManager() -GLOBALS = JinjaGlobalsManager() + 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() @PluginInfo( @@ -44,13 +74,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 _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 _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 _GLOBALS.get_all(): self.jj2_environment.globals[global_name] = dict_obj self.context = Context(context_dirs) @@ -65,6 +95,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 +126,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..b8ce2ab4 100644 --- a/moban/engine_factory.py +++ b/moban/engine_factory.py @@ -4,41 +4,11 @@ import moban.utils as utils 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): - def __init__(self): - super(EngineFactory, self).__init__( - constants.TEMPLATE_ENGINE_EXTENSION - ) - - 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() -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) diff --git a/moban/engine_handlebars.py b/moban/engine_handlebars.py index d41d46c1..3461bce3 100644 --- a/moban/engine_handlebars.py +++ b/moban/engine_handlebars.py @@ -9,13 +9,13 @@ 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.engine import MOBAN_ALL, BUILTIN_EXENSIONS from pybars import Compiler @@ -39,11 +39,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 +94,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..3fb6cedf 100644 --- a/moban/main.py +++ b/moban/main.py @@ -17,7 +17,7 @@ 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.engine import ENGINES from moban.hashstore import HASH_STORE diff --git a/moban/mobanfile.py b/moban/mobanfile.py index f78a9fdc..46fc347d 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -15,7 +15,8 @@ expand_directories, ) from moban.copier import Copier -from moban.engine_factory import ENGINES, expand_template_directories +from moban.engine_factory import expand_template_directories +from moban.engine import ENGINES try: from urllib.parse import urlparse diff --git a/tests/test_engine.py b/tests/test_engine.py index 25122eae..2c6859fa 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -7,7 +7,8 @@ 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, expand_template_directories +from moban.engine import ENGINES from moban.engine_handlebars import EngineHandlebars From 190482c5ee60fd7cc662bc39fa08f56339c07839 Mon Sep 17 00:00:00 2001 From: chfw Date: Wed, 14 Nov 2018 07:31:33 +0000 Subject: [PATCH 3/3] :hammer: code refactoring --- moban/engine.py | 55 +++++-------------------------- moban/engine_factory.py | 56 +++++++++---------------------- moban/engine_handlebars.py | 13 +++----- moban/main.py | 6 ++-- moban/mobanfile.py | 9 +++-- moban/plugins.py | 67 ++++++++++++++++++++++++++++++++++++++ tests/test_engine.py | 5 +-- 7 files changed, 107 insertions(+), 104 deletions(-) create mode 100644 moban/plugins.py diff --git a/moban/engine.py b/moban/engine.py index e4d61362..571b9e10 100644 --- a/moban/engine.py +++ b/moban/engine.py @@ -1,9 +1,7 @@ import os from jinja2 import Environment, FileSystemLoader -from lml.loader import scan_plugins_regex from lml.plugin import PluginInfo -from lml.plugin import PluginManager import moban.utils as utils import moban.reporter as reporter @@ -11,50 +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 ( Context, - expand_template_directory, - expand_template_directories, verify_the_existence_of_directories, Strategy ) -import moban.exceptions as exceptions -from moban.constants import MOBAN_ALL - -_FILTERS = JinjaFilterManager() -_TESTS = JinjaTestManager() -_GLOBALS = JinjaGlobalsManager() - -BUILTIN_EXENSIONS = [ - "moban.filters.repr", - "moban.filters.github", - "moban.filters.text", - "moban.tests.files", -] - - -class EngineFactory(PluginManager): - def __init__(self): - super(EngineFactory, self).__init__( - constants.TEMPLATE_ENGINE_EXTENSION - ) - - 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() +from moban import plugins @PluginInfo( @@ -63,10 +23,11 @@ def raise_exception(self, key): 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, @@ -74,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) diff --git a/moban/engine_factory.py b/moban/engine_factory.py index b8ce2ab4..4f072da7 100644 --- a/moban/engine_factory.py +++ b/moban/engine_factory.py @@ -4,9 +4,23 @@ import moban.utils as utils import moban.constants as constants import moban.exceptions as exceptions -from moban.extensions import LibraryManager +from lml.plugin import PluginManager -LIBRARIES = LibraryManager() + +class EngineFactory(PluginManager): + def __init__(self): + super(EngineFactory, self).__init__( + constants.TEMPLATE_ENGINE_EXTENSION + ) + + 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) class Context(object): @@ -70,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 3461bce3..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 ( Context, - expand_template_directory, - expand_template_directories, verify_the_existence_of_directories, Strategy, ) -from moban.engine import MOBAN_ALL, BUILTIN_EXENSIONS - +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 diff --git a/moban/main.py b/moban/main.py index 3fb6cedf..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 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 46fc347d..b52c040b 100644 --- a/moban/mobanfile.py +++ b/moban/mobanfile.py @@ -15,8 +15,7 @@ expand_directories, ) from moban.copier import Copier -from moban.engine_factory import expand_template_directories -from moban.engine import ENGINES +from moban import plugins try: from urllib.parse import urlparse @@ -80,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) @@ -98,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 @@ -107,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 2c6859fa..9d885945 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -7,8 +7,9 @@ from nose.tools import eq_, raises from moban.engine import Engine from moban.extensions import jinja_global -from moban.engine_factory import Context, expand_template_directories -from moban.engine import ENGINES +from moban.engine_factory import Context +from moban.plugins import expand_template_directories +from moban.plugins import ENGINES from moban.engine_handlebars import EngineHandlebars