def test_template_target_output_suffix_updates_after_set(): require = TemplateTarget("template_file", "dat_file", "output.copy", template_type="copy") require.set_template_type("jinja2") eq_("template_file,dat_file,output.copy,jinja2", repr(require))
def _handle_implicit_target(options, template_file, output): common_data_file = options[constants.LABEL_CONFIG] default_template_type = options[constants.LABEL_TEMPLATE_TYPE] for src, dest, t_type in handle_template( template_file, output, options[constants.LABEL_TMPL_DIRS]): if t_type: yield TemplateTarget(src, common_data_file, dest, t_type) else: yield TemplateTarget(src, common_data_file, dest, default_template_type)
def test_single_command(self, fake_template_doer): test_args = ["moban", "-m", self.config_file] with patch.object(sys, "argv", test_args): from moban.main import main main() call_args = list(fake_template_doer.call_args[0][0]) eq_( call_args, [ TemplateTarget("README.rst.jj2", "data.yaml", "README.rst"), TemplateTarget("setup.py.jj2", "data.yaml", "setup.py"), ], )
def _render_with_finding_data_first(self, data_file_index): for (data_file, template_output_pairs) in data_file_index.items(): data = self.context.get_data(data_file) for (template_file, output) in template_output_pairs: try: template = self.engine.get_template(template_file) if isinstance(template, bool): if template: reporter.report_templating(self.engine_action, template_file, None) self.templated_count += 1 else: template_abs_path = self.template_fs.geturl( template_file, purpose="fs") flag = self.apply_template(template_abs_path, template, data, output) if flag: reporter.report_templating(self.engine_action, template_file, output) self.templated_count += 1 self.file_count += 1 except exceptions.PassOn: self.fall_out_targets.append( TemplateTarget( template_file, data_file, output, template_type=constants.TEMPLATE_COPY, )) reporter.report_info_message( f"{self.engine_action} is switched to copy:" + f" {template_file} to {output}") continue
def test_handle_targets(fake_renderer): from moban.core.mobanfile import handle_targets TEMPLATE = "copier-test01.csv" OUTPUT = "output.csv" CONFIGURATION = "child.yaml" TEMPLATE_DIRS = [fs.path.join("tests", "fixtures")] DEFAULT_TEMPLATE_TYPE = "jinja2" options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, configuration_dir=fs.path.join("tests", "fixtures"), ) short_hand_targets = [{OUTPUT: TEMPLATE}] handle_targets(options, short_hand_targets) call_args = list(fake_renderer.call_args[0][0]) eq_( call_args, [ TemplateTarget( "copier-test01.csv", "child.yaml", "output.csv", template_type="jinja2", ) ], )
def test_ad_hoc_type(self): target = dict(template=TEMPLATE, output=OUTPUT) template_type = [ { "base_type": "jinja2" }, { "options": [ { "block_end_string": "*))" }, { "block_start_string": "((*" }, ] }, ] options = dict( configuration=CONFIGURATION, template_type=template_type, template_dir=TEMPLATE_DIRS, ) actual = list(targets._handle_explicit_target(options, target)) file_extension = uuid.uuid4().hex expected = [ TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, file_extension) ] eq_(actual, expected)
def test_single_command(self, _): test_args = ["moban"] with patch.object(sys, "argv", test_args): from moban.main import main with patch("moban.core.moban_factory.MobanEngine.render_to_files" ) as fake: main() call_args = list(fake.call_args[0][0]) eq_( call_args, [ TemplateTarget("README.rst.jj2", "custom-data.yaml", "README.rst"), TemplateTarget("setup.py.jj2", "data.yml", "setup.py"), ], )
def _handle_group_target(options, a_list_short_hand_targets, group_template_type): # grouping by template type feature common_data_file = options[constants.LABEL_CONFIG] for _output, _template_file in _iterate_list_of_dicts( a_list_short_hand_targets): for src, dest, t_type in handle_template( _template_file, _output, options[constants.LABEL_TMPL_DIRS]): yield TemplateTarget(src, common_data_file, dest, group_template_type)
def test_handle_targets_sequence(fake_renderer): from moban.core.mobanfile import handle_targets from moban.core.mobanfile.store import STORE STORE.init() # required to reset the store TEMPLATE1 = "a.template.jj2" OUTPUT1 = "filterme.handlebars" # in the future, this could dynamic output OUTPUT2 = "filtered_output.txt" CONFIGURATION = "child.yaml" TEMPLATE_DIRS = [fs.path.join("tests", "fixtures", "mobanfile")] DEFAULT_TEMPLATE_TYPE = "jinja2" options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, configuration_dir=fs.path.join("tests", "fixtures"), ) short_hand_targets = [{OUTPUT1: TEMPLATE1}, {OUTPUT2: OUTPUT1}] handle_targets(options, short_hand_targets) call_args = list(fake_renderer.call_args_list) eq_( call_args[0][0][0][0], TemplateTarget( "a.template.jj2", "child.yaml", "filterme.handlebars", template_type="jj2", ), ) eq_( call_args[1][0][0][0], TemplateTarget( "filterme.handlebars", "child.yaml", "filtered_output.txt", template_type="handlebars", ), )
def test_single_command_with_a_few_options(self, fake_template_doer): test_args = ["moban", "-t", "README.rst.jj2", "-o", "xyz.output"] with patch.object(sys, "argv", test_args): from moban.main import main main() call_args = list(fake_template_doer.call_args[0][0]) eq_( call_args, [TemplateTarget("README.rst.jj2", "data.yaml", "xyz.output")], )
def test_derive_template_type_from_target_template_file(self): options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, ) actual = list( targets._handle_implicit_target(options, TEMPLATE, OUTPUT)) expected = [TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, "jj2")] eq_(expected, actual)
def test_use_target_template_type(self): target = dict(template_type="use-me", template=TEMPLATE, output=OUTPUT) options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, ) actual = list(targets._handle_explicit_target(options, target)) expected = [TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, "use-me")] eq_(expected, actual)
def _handle_explicit_target(options, target): common_data_file = options[constants.LABEL_CONFIG] default_template_type = options[constants.LABEL_TEMPLATE_TYPE] template_file = target.get(constants.LABEL_TEMPLATE, options.get(constants.LABEL_TEMPLATE, None)) data_file = target.get(constants.LABEL_CONFIG, common_data_file) output = target[constants.LABEL_OUTPUT] if output: template_type = target.get(constants.LABEL_TEMPLATE_TYPE) else: template_type = constants.TEMPLATE_DELETE if template_type and len(template_type) > 0: if constants.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type: reporter.report_file_extension_not_needed() if constants.TEMPLATE_TYPES_BASE_TYPE in template_type: adhoc_type = uuid.uuid4().hex file_extension = uuid.uuid4().hex base_type = template_type[constants.TEMPLATE_TYPES_BASE_TYPE] template_types_options = template_type[ constants.TEMPLATE_TYPES_OPTIONS] the_adhoc_type = { adhoc_type: { constants.TEMPLATE_TYPES_FILE_EXTENSIONS: [file_extension], constants.TEMPLATE_TYPES_BASE_TYPE: base_type, constants.TEMPLATE_TYPES_OPTIONS: template_types_options, } } core.ENGINES.register_options(the_adhoc_type) template_type = file_extension for src, dest, t_type in handle_template( template_file, output, options[constants.LABEL_TMPL_DIRS]): if template_type: yield TemplateTarget(src, data_file, dest, template_type) else: if t_type: yield TemplateTarget(src, data_file, dest, t_type) else: yield TemplateTarget(src, data_file, dest, default_template_type)
def test_handling_group_target(): group_template_type = "group-template-type" options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, ) short_hand_targets = [{OUTPUT: TEMPLATE}] actual = list( targets._handle_group_target(options, short_hand_targets, group_template_type)) expected = [ TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, group_template_type) ] eq_(expected, actual)
def test_use_moban_default_template_from_options(self): template_without_suffix = "template" target = dict(template=template_without_suffix, output=OUTPUT) options = dict( configuration=CONFIGURATION, template_type=DEFAULT_TEMPLATE_TYPE, template_dir=TEMPLATE_DIRS, ) actual = list(targets._handle_explicit_target(options, target)) expected = [ TemplateTarget( template_without_suffix, CONFIGURATION, OUTPUT, DEFAULT_TEMPLATE_TYPE, ) ] eq_(expected, actual)
def test_store(): store = Store() output = "output" target = TemplateTarget("template_file", "data_file", output) store.add(target) eq_(target, store.look_up_by_output.get(output))
def test_template_target_output_suffix_change(): require = TemplateTarget("template_file", "dat_file", "output.copy", template_type="copy") eq_("template_file,dat_file,output,copy", repr(require))
def test_template_target_repr(): require = TemplateTarget("template_file", "dat_file", "output") eq_("template_file,dat_file,output,jinja2", repr(require))